Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Nov 2, 2016

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

This comment has been minimized.

Copy link

commented Nov 11, 2016

/**
*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

This comment has been minimized.

Copy link

commented Jan 31, 2017

Workaround:

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

@JorgeRibeiroGomes

This comment has been minimized.

Copy link

commented Feb 7, 2017

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

This comment has been minimized.

Copy link

commented Mar 4, 2017

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

This comment has been minimized.

Copy link

commented Mar 19, 2017

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

@AiMS38

This comment has been minimized.

Copy link

commented Apr 11, 2017

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

@Pharouk90

This comment has been minimized.

Copy link

commented Sep 19, 2017

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);
}

}

@DeusXmac

This comment has been minimized.

Copy link

commented Dec 31, 2017

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

This comment has been minimized.

Copy link

commented Jan 14, 2018

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

This comment has been minimized.

Copy link

commented Jan 14, 2018

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

This comment has been minimized.

Copy link

commented Jan 25, 2018

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

This comment has been minimized.

Copy link

commented Jan 31, 2018

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

@Nogaeman

This comment has been minimized.

Copy link

commented Feb 3, 2018

Yes , This Is Great
untitled

@abdahma01

This comment has been minimized.

Copy link

commented Feb 13, 2018

untitled

@first-hero

This comment has been minimized.

Copy link

commented Feb 24, 2018

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

This comment has been minimized.

Copy link

commented Apr 6, 2018

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

This comment has been minimized.

Copy link

commented May 5, 2018

    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

This comment has been minimized.

Copy link

commented May 16, 2018

`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

This comment has been minimized.

Copy link

commented May 16, 2018

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

@Tapendrakmr

This comment has been minimized.

Copy link

commented May 20, 2018

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

This comment has been minimized.

Copy link

commented Jun 2, 2018

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

This comment has been minimized.

Copy link

commented Jun 26, 2018

image

@Mostafa1A

This comment has been minimized.

Copy link

commented Jul 4, 2018

`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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.