-
-
Save udacityandroid/45f2a426a2d66e57d672 to your computer and use it in GitHub Desktop.
| int day1 = 15; | |
| int day2 = 22; | |
| int day3 = 18; | |
| display(day1 + day2 + day3 / 3); |
/**
*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);
}
}
Workaround:
int day1 = 15; int day2 = 22; int day3 = 18; display((day1 + day2 + day3) / 3);
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);
}
}
`
I still do not understand some parts of the code, but it seems like videos are about mathematical facts from elementary school only :D
why am i always getting this error. "cannot resolve symbol R".
I have use this package --> package com.example.android.practiceset2;
ok how about the MainActivity.java code, where is it?
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);
}
}
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);
}
}
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);
}
}
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));
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 + "");
}
Awesome and fun. needed to add brackets around days to be calculated/added first before the division on the 3 days
not correct
int day1 = 15;
int day2 = 22;
int day3 = 18;
display(day1 + day2 + day3 / 3);........................................(18/3)+15+22=43
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 + "");
}
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
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
`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
@shoooka can you share a screenshot of your console let's help
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);
}
}
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);
}
}
`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
int day1 = 15; int day2 = 22; int day3 = 18; float sumOfDays = day1 + day2 + day3; display(sumOfDays / 3);
I got this instead of 43
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. )
Hi @Mostafa1A
Your result will be always the same as above I wrote.
Here are some simple example codes:
-
version:
int day1 = 15;
int day2 = 22;
int day3 = 18;
display((day1 + day2 + day3) / 3);
Result is 18. -
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. -
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
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 */
*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"
/>






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