Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Created May 25, 2015 21:24
Show Gist options
  • Select an option

  • Save udacityandroid/45f2a426a2d66e57d672 to your computer and use it in GitHub Desktop.

Select an option

Save udacityandroid/45f2a426a2d66e57d672 to your computer and use it in GitHub Desktop.
Android Development for Beginners : Practice Set 2 Travel Times Example
int day1 = 15;
int day2 = 22;
int day3 = 18;
display(day1 + day2 + day3 / 3);
@MuhammadSugiharto

Copy link
Copy Markdown

if you want us to take this to java (main activity) please give us all the program.
I do not understand how to write and the function of
android. import.xxxx.xxx
and override, public boolean private void and what it is R.xxx.xxx.xxx
I just know how it solve with my brain not in Android Studio

@100rabhkr

100rabhkr commented Nov 11, 2016

Copy link
Copy Markdown

/**
*Copy and Paste this
*/
package com.example.android.practiceset2;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // PASTE CODE YOU WANT TO TEST HERE
    int day1 = 15;
    int day2 = 22;
    int day3 = 18;
    display(day1 + day2 + day3 / 3);
}

/**
 * Display methods that allow the text to appear on the screen. Don't worry if you don't know
 * how these work yet. We'll be covering them in lesson 3.
 */

public void display(String text) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(text);
}

public void display(int text) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(text + "");
}

public void display1(String text) {
    display(text);
}

public void display2(String text) {
    TextView t = (TextView) findViewById(R.id.display_text_view_2);
    t.setText(text);
}

public void display3(String text) {
    TextView t = (TextView) findViewById(R.id.display_text_view_3);
    t.setText(text);
}

}

@madmat27

madmat27 commented Jan 31, 2017

Copy link
Copy Markdown

Workaround:

int day1 = 15; int day2 = 22; int day3 = 18; display((day1 + day2 + day3) / 3);

@JorgeRibeiroGomes

JorgeRibeiroGomes commented Feb 7, 2017

Copy link
Copy Markdown

madmat27
in fact you should change the data type:

`package com.example.android.practiceset2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    float day1 = 15;
    float day2 = 22;
    float day3 = 18;
    display((day1 + day2 + day3) / 3);
}

public void display (float i) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(""+i);
}

}
`

@kristinababikova

Copy link
Copy Markdown

I still do not understand some parts of the code, but it seems like videos are about mathematical facts from elementary school only :D

@numan619

Copy link
Copy Markdown

why am i always getting this error. "cannot resolve symbol R".
I have use this package --> package com.example.android.practiceset2;

@AiMS38

AiMS38 commented Apr 11, 2017

Copy link
Copy Markdown

ok how about the MainActivity.java code, where is it?

@uab234

uab234 commented Sep 19, 2017

Copy link
Copy Markdown

HERE IT'S @AiMS38

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int day1 = 15;
    int day2 = 22;
    int day3 = 18;
    display((day1 + day2 + day3 / 3));
}

/**
 * Display methods that allow the text to appear on the screen. Don't worry if you don't know
 * how these work yet. We'll be covering them in lesson 3.
 */

public void display(String text) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(text);
}

public void display(int text) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(text + "");
}

public void display1(String text) {
    display(text);
}

}

@freryksl

freryksl commented Dec 31, 2017

Copy link
Copy Markdown

import android.widget.TextView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int day1 = 15;
    int day2 = 22;
    int day3 = 18;
    display((day1 + day2 + day3) / 3);
}

private void display(float i) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText("" + i);
}

}

@drszabodavid

Copy link
Copy Markdown

This worked for me:

package com.example.android.practiceset2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int day1 = 15;
    int day2 = 22;
    int day3 = 18;
    float sumOfDays = day1 + day2 + day3;
    display(sumOfDays / 3);
}



public void display (float i) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(""+i);
}

}

@pmheintz

Copy link
Copy Markdown

An alternative to @drszabodavid

The Java compiler will implicitly convert the result to a double if any of the numbers in the equation have a decimal value. So, if you change the statement to:

display((day1 + day2 + day3) / 3.0);

you'll pass a double value to the display method. Then you can overload the display method like @drszabodavid did using the double datatype instead of float:

public void display (double i) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(""+i);
}

The starter code provided has an overloaded method for display() that accepts either a String or int. If you don't want to change the display() method, you can use the toString() method from Java's Double class which takes a Double number as an argument and converts it to a String. This way you only have to change one line of code:

display(Double.toString((day1 + day2 + day3) / 3.0));

@Forky1227

Copy link
Copy Markdown

This worked for me to display the Float:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    float day1 = 15;
    float day2 = 22;
    float day3 = 18;
    display((day1 + day2 + day3 )/ 3);

public void display(float text) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText(text + "");
}

@Maxwe1

Maxwe1 commented Jan 31, 2018

Copy link
Copy Markdown

Awesome and fun. needed to add brackets around days to be calculated/added first before the division on the 3 days

@Nogaeman

Nogaeman commented Feb 3, 2018

Copy link
Copy Markdown

Yes , This Is Great
untitled

@abdahma01

Copy link
Copy Markdown

untitled

@first-hero

Copy link
Copy Markdown

not correct
int day1 = 15;
int day2 = 22;
int day3 = 18;
display(day1 + day2 + day3 / 3);........................................(18/3)+15+22=43

screenshot_20180224-102737 1

the correct answer
int day1 = 15;
int day2 = 22;
int day3 = 18;
double sumOfDays = day1 +day2 + day3
display(sumOfDays / 3);

add this method to the code
public void display(double text) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText(text + "");
}

screenshot_20180224-103322 1

@conbradst

Copy link
Copy Markdown

int day1 = 15;

    int day2 = 22;

    int day3 = 18;

    display((day1 + day2 + day3) / 3);

answer displayed (rounded off since we're using int) = 18

  • Math rule - PEMDAS

@shoooka

shoooka commented May 5, 2018

Copy link
Copy Markdown
    display(day1 + day2 + day3 / 3);

i have problem with "display " not work with me
give me "Compilation failed; see the compiler error output for details."
and word "display "was red " why

@OluwatobilobaLight

OluwatobilobaLight commented May 16, 2018

Copy link
Copy Markdown

`package com.example.android.practiceset2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int day1 = 15;
    int day2 = 22;
    int day3 = 18;
    float sumOfDays = day1 + day2 + day3;
    display(sumOfDays / 3);
}



public void display (float i) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(""+i);
}

}`

I got this instead of 43

screenshot_1526472228

@OluwatobilobaLight

OluwatobilobaLight commented May 16, 2018

Copy link
Copy Markdown

@shoooka can you share a screenshot of your console let's help

@Tapendrakmr

Copy link
Copy Markdown

please help me
it doesn't show any error on android studio
but show message unfortunately ,stopped at the time of installation in device

package com.example.android.practiseset2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.example.android.practiseset2.R;

public abstract class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // PASTE CODE YOU WANT TO TEST HERE

    int day1 = 15;
    int day2 = 22;
    int day3 = 18;

    display(day1 + day2 + day3 /3 );

}

public void display(int i) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText("" + i);
}

}

@ZahraaElhaj

Copy link
Copy Markdown

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@OverRide
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
float day1 = 15;
float day2 = 22;
float day3 = 18;
display((day1 + day2 + day3) / 3);
}

public void display (float i) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText(""+i);
}
}

@jerylee

jerylee commented Jun 26, 2018

Copy link
Copy Markdown

image

@Mostafa1A

Copy link
Copy Markdown

`package com.example.android.practiceset2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@OverRide
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int day1 = 15;
int day2 = 22;
int day3 = 18;
float sumOfDays = day1 + day2 + day3;
display(sumOfDays / 3);
}

public void display (float i) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText(""+i);
}
}`

I got this instead of 43

@LaszloLajosT

LaszloLajosT commented Nov 10, 2019

Copy link
Copy Markdown
int day1 = 15;
int day2 = 22;
int day3 = 18;
float sumOfDays = day1 + day2 + day3;
display(sumOfDays / 3);

I got this instead of 43

Hi @OluwatobilobaLight

You got 18.333 as result because you changed the data type.
Originally, the day1,day2,day3 datatype is "int".
You declared a "float" type and initialised the day1,day2,day3 summary.

float sumOfDays = day1 + day2 + day3;

"int" type is always a rounded number like 1, 2, 3, 10, whatever number.
"float" type can be anything like 1, 1.2, or 1.112
If you declare sumOfDays like this:

int sumOfDays = day1 + day2 + day3;

then your result for this :

display(sumOfDays / 3);

will be 18. (Because "/" (Division) Divides 45 by 3. And leaves behind everything after the rounded number. )

@LaszloLajosT

Copy link
Copy Markdown

Hi @Mostafa1A
Your result will be always the same as above I wrote.

Here are some simple example codes:

  1. version:
    int day1 = 15;
    int day2 = 22;
    int day3 = 18;
    display((day1 + day2 + day3) / 3);
    Result is 18.

  2. version:
    int day1 = 15;
    int day2 = 22;
    int day3 = 18;
    int sumOfDays = day1 + day2 + day3;
    display( sumOfDays / 3);
    Result is 18. Same as 1. version.

  3. version:
    int day1 = 15;
    int day2 = 22;
    int day3 = 18;
    display(day1 + day2 + day3 / 3);
    Result is 43.

Here is a link for basic java assignment operators with examples:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html

@rajan3300

Copy link
Copy Markdown

package com.example.android.practiceset2;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // PASTE CODE YOU WANT TO TEST HERE
    int day1 = 15;
    int day2 = 22;
    int day3 = 18;
    display(day1 + day2 + day3 / 3);

 

}

/**
 * Display methods that allow the text to appear on the screen. Don't worry if you don't know
 * how these work yet. We'll be covering them in lesson 3.
 */

public void display(String text) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(text);
}

public void display(int text) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(text + "");
}

}
/* try this you'll sure get it */

@YassineOuardini

Copy link
Copy Markdown

*Java CODE
package com.example.com;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    int raspberryPrice = 5;
    display1("1 box: $" + raspberryPrice);
    display2("2 boxes: $" + (raspberryPrice * 2));
    display3("3 boxes: $" + (raspberryPrice * 3));
}


public void display1(String text) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(text + "");
}

public void display2(String text) {
    TextView t = (TextView) findViewById(R.id.display2_text_view);
    t.setText(text + "");
}


public void display3(String text) {
    TextView t = (TextView) findViewById(R.id.display3_text_view);
    t.setText(text + "");
}

}

XML

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/display_text_view"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:id="@+id/display2_text_view"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
   />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:id="@+id/display3_text_view"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
   />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment