Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Last active October 31, 2022 07:15
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 26 You must be signed in to fork a gist
  • Save udacityandroid/a00cf27969aea4ba7848 to your computer and use it in GitHub Desktop.
Save udacityandroid/a00cf27969aea4ba7848 to your computer and use it in GitHub Desktop.
Android Development for Beginners : Practice Set 2 Shopping List 2 Example
int raspberryPrice = 5;
display1("1 box: $" + raspberryPrice);
raspberryPrice = 10;
display2("2 boxes: $" + (raspberryPrice));
display3("3 boxes: $" + (raspberryPrice * 3));
@piyushkantmaav
Copy link

The code is correct. There is no syntax error.
Output is:
1 box: $5
2 boxes: $10
3 boxes: $30

@conbradst
Copy link

int raspberryPrice = 5;

    display1("1 box: $" + raspberryPrice);

    raspberryPrice = 10;

    display2("2 boxes: $" + (raspberryPrice));

    display3("3 boxes: $" + (raspberryPrice * 3));

You will get $5, 10, 30. Last one is incorrect. It should be $5 x 3 = $15 (not $10 x 3 = 30)

@kapeller77
Copy link

it seems there is an up-charge for buying 3 boxes...

@mohamedsaleh1984
Copy link

I'm getting an error when I submit the answers.. !! Anyone else
Quiz Link
https://classroom.udacity.com/courses/ud803-track-1mac/lessons/ed4dafe5-c2dc-4fe8-8705-11b408c8b01e/concepts/42721187320923

Output is:
1 box: $5
2 boxes: $10
3 boxes: $30

@Eduese
Copy link

Eduese commented May 17, 2018

It's awesomely running fine

Output
1 box: $5
2 boxes: $10
3 boxes: $30

@trustidkid
Copy link

The code is correct and the intention of the developer has been communicated but it does not follow good programming practice. We need not assign value 10 to already initiated variable (raspberryPrice) since it a multiple of the initial value. We just use the java operator (*) to multiply the value by 2 like we did in the last line in the code

@trustidkid
Copy link

The code did not display what the user intend
it displays
1 box: $5
2 boxes: $10
3 boxes: $30

@albaraa2015
Copy link

output in my phone
screenshot_ - - - - -
](url)

@hero82
Copy link

hero82 commented May 31, 2018

Ok This Is
-----MainActivity.java-----
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// PASTE CODE YOU WANT TO TEST HERE

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

}
-----Output-----
1 box: $5
2 boxes: $10
3 boxes:$30
screenshot_2018-05-31-18-53-38

@khldonemad
Copy link

Output----
1 box: $5
2 boxes: $10
3 boxes:$30

@mohammeddbadawy
Copy link

Worked

@bimbolanko
Copy link

bimbolanko commented Jun 6, 2018

Works but illogical.

@ahmedmtaher
Copy link

Output is:
1 box: $5
2 boxes: $10
3 boxes: $30

@ZahraaElhaj
Copy link

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

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

}
1 box: $5
2 boxes: $10
3 boxes:$30

@find1010
Copy link

package com.example.android.practiceset2;

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

import com.example.android.practiceset2.R;

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 raspberryPrice = 5;
    display1("1 box: $" + raspberryPrice);
    display2("2 boxes: $" + (raspberryPrice*2));
    display3("3 boxes: $" + (raspberryPrice * 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);
}

}

@find1010
Copy link

    <TextView
        android:id="@+id/display_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="45sp" />
<TextView
    android:id="@+id/display_text_view_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="45sp" />
<TextView
    android:id="@+id/display_text_view_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="45sp" />

</LinearLayout>

@jerylee
Copy link

jerylee commented Jun 25, 2018

image

@KobiPeeri
Copy link

find1010 is just great help. thank you !!!

@moiyd
Copy link

moiyd commented Sep 29, 2018

capture

@Yogesh-Bhati
Copy link

This is the output:
1 box: $5
2 boxes: $10
3 boxes:$30

@normanmos
Copy link

Line 4 has an error.
Original:
display2("2 boxes: $" + (raspberryPrice));
Correction:
display2("2 boxes: $" + (raspberryPrice * 2));

It's not, the code it's correct because, you're changing the value before display on screen to 10

@shiva-karthick
Copy link

Done!

photo_2018-12-23_15-46-39

@sslaia
Copy link

sslaia commented May 19, 2019

I'm still wondering how you guys can run the code in Android Studio without issues. Here it exits with an error "incompatible type: String cannot be converted into int." So my guess is because the method display in "display("1 box: $" + raspberryPrice);" is defined as integer as instructed in:
public void display (int i) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText(""+i);
}

@LaszloLajosT
Copy link

I'm still wondering how you guys can run the code in Android Studio without issues. Here it exits with an error "incompatible type: String cannot be converted into int." So my guess is because the method display in "display("1 box: $" + raspberryPrice);" is defined as integer as instructed in:
public void display (int i) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText(""+i);
}

Hi @sslaia

You maybe not interested anymore of this answer. But just in case I try to do my best :)
So, first in first. I call this method:

display1("1 box: $" + raspberryPrice);

So we are jumping to this declaration:
public void display1(String text) {
display(text);
}
->display1 method calls display method with String.
That means we jump to this method:

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

Basically, we change the TextView's value to

1 box: $ 5.

What you mention "display" method with "int", we never called.

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

@YassineOuardini
Copy link

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

@Oshomo
Copy link

Oshomo commented May 29, 2020

If you struggle in latest version of Android Studio check below codes
JAVA CODE:

package com.example.android.practiceset2;

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.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 + "");
}

}

XML CODE:

<TextView
    android:id="@+id/display_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="45sp" />

<TextView
    android:id="@+id/display_text_view_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="45sp" />

<TextView
    android:id="@+id/display_text_view_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="45sp" />

@surajsahani
Copy link

Correct Answer
Click On No than
1 box: $5
2 boxes: $10
3 boxes: $30

@Ashish538
Copy link

katherine is best

@MarvUkazu
Copy link

the goal wasn't accomplished and it displayed

1 box: $5
2 boxes: $10
3 boxes: $30

I wrote just the price without adding the number of boxes

1 box: $5
2 boxes: $10
3 boxes: $30

This is the answer an not :

$5
$10
$30

or

5
10
30

@AjayThenuan-CodingQuiz
Copy link

Great it Works Totally guys

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