Skip to content

Instantly share code, notes, and snippets.

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

This comment has been minimized.

Copy link

commented Apr 9, 2016

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

@luiggyneyra

This comment has been minimized.

Copy link

commented Apr 27, 2016

It's following a pattern, your thinking that it should multiply the variable raspberryPrice by 2 but the variable is giving a new value in line 3, which is 10.
The patter is correct, just different approach for educational purposes, my guess

@srafay

This comment has been minimized.

Copy link

commented May 31, 2016

@webgrrrl the code is correct, since they changed raspberryPrice from 5 to 10 in line 3 :) However, the price in line #05 could be wrong because now it will display 30 instead of 15 :/

@asadriz

This comment has been minimized.

Copy link

commented Jul 7, 2016

in line 4 it should display the price of two boxes where each box price set to 10 in line 3, so it should display a multiplied value by 2 which is not happening here

@NAnushree

This comment has been minimized.

Copy link

commented Aug 19, 2016

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

@rolfing

This comment has been minimized.

Copy link

commented Sep 4, 2016

on line 4 :I agree with @webgrrrl this should be multiply * 2

@EckoZ04

This comment has been minimized.

Copy link

commented Sep 11, 2016

The error is actually on line 5. The line 4 is correct because the value of the variable raspberry was change from 5 to 10 on line 3. However, line 5 does not take that into consideration and multiply raspberryPrice by 3 which is actually false (If 1 box is 5$ and 2 boxes are 10$, 3 boxes should be 15$ while the code returns 30$).

@manmeet-22

This comment has been minimized.

Copy link

commented Sep 22, 2016

If
`

int raspberryPrice=5;

then price of 3 should be 15.
The correction can be-

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

`or

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

@DaWanderer7

This comment has been minimized.

Copy link

commented Nov 20, 2016

All of you are correct and wrong in my opinion.
It depends on line 3, the meaning/purpose of initialize the price once again
Scenario 1: if the developer just wants to show us the effect of code/value change of final price after raspberryPrice is updated in the middle of the code, then result from display3 should be wrong while correct final price should be $15
Scenario 2: if the developer updates the price due to inflation or price change (it could be a price change due to volume discount in a different scenario), in this case, the result from display2 should be wrong while correct answer should be $20
I think this error is quite inspiring as I just learned from this example one should not just simply read the code without thinking further but should try to understand the meaning or the purpose behind the code on each line or specific block of codes
Once again, this also reminds us how important to leave a comment right below the code/area that easily raises confusion

@varsha917

This comment has been minimized.

Copy link

commented Jan 10, 2017

Hi
Im not able to get anything on this code what will be the code for activity_main xml and MainActivity.Java ?

@madmat27

This comment has been minimized.

Copy link

commented Jan 31, 2017

Should remain as was, in order to comply with exercise:

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

@n21cosmin

This comment has been minimized.

Copy link

commented Feb 6, 2017

    int raspberryPrice = 5; /*declares an int raspberryPrice with the value of 5 that represents the price of 1 box*/
    display1("1 box: $" + raspberryPrice); /*it displays the price of 1 box by concatenation*/
    raspberryPrice = 10; /*it then changes the value of raspberryPrice to 10 like a mental calculus, just another way of demonstrating*/
    display2("2 boxes: $" + (raspberryPrice));/*prints the value of two boxes already calculated*/
    display3("3 boxes: $" + (raspberryPrice * 3));/*displays the wrong value of three boxes if that was intended*/
    solution: you need to reinitialize the value of raspberryPrice = 5; before running display3 or 
    remove line from code raspberryPrice = 10 and update display2("2 boxes: $" + (raspberryPrice*2));
@eveofdante

This comment has been minimized.

Copy link

commented Feb 10, 2017

Worth noting for anyone coming here for a code example during Practice Set 2-2 that you will revisit this in Practice Set 2-10: Shopping List, Once More..., so don't worry about it too much until then.

@jcunanan05

This comment has been minimized.

Copy link

commented Mar 4, 2017

thanks

@waschbaerchen

This comment has been minimized.

Copy link

commented Mar 26, 2017

Just as NAnushree said, the code is correct. There is no syntax error and the output is:
1 box: $5
2 boxes: $10
3 boxes: $30

But if I want to get to following output:
1 box: $5
2 boxes: $10
3 boxes: $15

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

Update: you may find the gist to the code here: https://gist.github.com/udacityandroid/6dfcdd2d7bc9b9656e73

@razajaveid

This comment has been minimized.

Copy link

commented Apr 3, 2017

can please someone explain this because i didn't get this.

@MikaVeg

This comment has been minimized.

Copy link

commented Apr 11, 2017

Hi all,
I agree, there is no syntax error and the output is:
1 box: $5
2 boxes: $10
3 boxes: $30

Clearly, the pointis that we need to change the code lines to have to proper mathematics behind: if the ox price is set to 10 usd and then display3 displays 3 time this price, we're spending a little to much to buy these boxes...

@karan357951

This comment has been minimized.

Copy link

commented Jun 28, 2017

there is no syntax error
the code works fine
output:
1 box: $5
2 boxes: $10
3 boxes: $30

@ginamerla

This comment has been minimized.

Copy link

commented Jul 12, 2017

Why if i answer with
1 box: $5
2 boxes: $10
3 boxes: $30

keep saying try again??

@Mahmoudtarek59

This comment has been minimized.

Copy link

commented Aug 17, 2017

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

@TIPCAPPS

This comment has been minimized.

Copy link

commented Sep 4, 2017

The code is correct.
Output will be displayed.
and the only thing different here is business strategy. May be they should change their marketing team who gave these requirements to BAs. ;D

@kkchain

This comment has been minimized.

Copy link

commented Oct 29, 2017

it was fun to code. i misspelled the rasberryPrice, right click on it and it can check the spelling "raspberryPrice" where option provided, automatically corrected all variable that use rasberryPrice

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

@US18

This comment has been minimized.

Copy link

commented Nov 4, 2017

What will be the code for display method ??

@robertl95

This comment has been minimized.

Copy link

commented Nov 14, 2017

Use this

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

Works properly.

@caioguedesbra

This comment has been minimized.

Copy link

commented Dec 20, 2017

Thank you guys for the help, really useful.

@oaajagbe

This comment has been minimized.

Copy link

commented Dec 27, 2017

I agree with DaWanderer7. It depends on the coders mind. What does (s)/he wants us to see, errors in calculation of Errors in compilation. This reminds me of Kunaal's Professor's words he mentioned earlier..."No Magic numbers"

@DeusXmac

This comment has been minimized.

Copy link

commented Jan 6, 2018

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

private void display(1 or 2 or 3)(String s) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText("" + s);
}

Results

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

@akshayrajkumar

This comment has been minimized.

Copy link

commented Jan 19, 2018

@Janek666

This comment has been minimized.

Copy link

commented Jan 21, 2018

Haha. raspberryPrice = 10; That's clearly on of the Magic numbers Kunal mentioned. No one know where did it come from, but it's there. Magic

Code is working fine. But the numbers don't make too much sense. Debugging will give you an answer where are the numbers coming from.
Display 1 > just simply displays 5 as we set int raspberryPrice to 5
Display 2 > there is no calculation, we just set a new int for raspberryPrice - 10
Displays 3 > takes the last value of int raspberryPrice (which is 10 in this case and its multiplied by 3) - 30.

There would be more then one approach to fix the calculation, but probably the easiest way is the following:

  • remove int raspberryPrice = 10;
  • add calculation to Display 2 (raspberryPrice * 2)

So, the code could be:
int raspberryPrice = 5;
display1("1 box: $" + raspberryPrice);
display2("2 boxes: $" + (raspberryPrice * 2));
display3("3 boxes: $" + (raspberryPrice * 3));

This will give you
1 box: $5
2 boxes: $10
3 boxes: $15

Btw. I won't never ever misspell raspberry again after this exercise 😃

@Maxwe1

This comment has been minimized.

Copy link

commented Jan 31, 2018

The code is 100% correct and no any Syntax errors.
On screen output is:
1 box: $5
2 boxes: $10
3 boxes: $30
The code should be:
int raspberryPrice = 5;
display1("1 box: $" + raspberryPrice);
display2("2 boxes: $" + (raspberryPrice * 2));
display3("3 boxes: $" + (raspberryPrice * 3));

@hosham9

This comment has been minimized.

Copy link

commented Jan 31, 2018

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

@Nogaeman

This comment has been minimized.

Copy link

commented Feb 3, 2018

The Output Of Code
untitled

@Deerty

This comment has been minimized.

Copy link

commented Feb 4, 2018

I've had the same problem as @ginamerla had, but after several attempts it went through. Don't know why...

@abdullahelsheikh

This comment has been minimized.

Copy link

commented Feb 4, 2018

لم يحقق المطلوب لان في
display3 = 3 boxes: $30
و الاجابه الصحيحه هي
3 boxes: $15

@tahermahmoud

This comment has been minimized.

Copy link

commented Feb 5, 2018

@abdullahelsheikh
هوه كدا صح لا الكود بيقول القيمه ضرب 3
و هوه حاطتها ب 10 يعني 10 ضرب 3
=30

@BENLEKKAM

This comment has been minimized.

Copy link

commented Feb 9, 2018

Delete raspberryPrice = 10;
and this code will be correct

@psondagar10

This comment has been minimized.

Copy link

commented Feb 11, 2018

Once we remove line 4 the code turns out correct.
Well, code itself has no syntax error but it has a logical error.

@abdahma01

This comment has been minimized.

Copy link

commented Feb 13, 2018

untitled

@yasserelwany

This comment has been minimized.

Copy link

commented Feb 19, 2018

screenshot_1519061980
thank you

@badrddinb

This comment has been minimized.

Copy link

commented Feb 21, 2018

DONE

@first-hero

This comment has been minimized.

Copy link

commented Feb 23, 2018

screenshot_20180223-222512 1

@first-hero

This comment has been minimized.

Copy link

commented Feb 24, 2018

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

screenshot_20180224-094701 1

@ahmad23805

This comment has been minimized.

Copy link

commented Mar 7, 2018

thank for you, this is nice

@theiceman1010

This comment has been minimized.

Copy link

commented Mar 11, 2018

1.give id to textview:
android:id="@+id/display_text_view"
2.copy code in onCreate method
int raspberryPrice = 5;
display1("1 box: $" + raspberryPrice);
raspberryPrice = 10;
//display2("2 boxes: $" + (raspberryPrice));
//display3("3 boxes: $" + (raspberryPrice * 3));
3.have your display method match what you want
private void display1(String s) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText("" + s);
}
or
private void display2(String s) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText("" + s);
}
or
private void display3(String s) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText("" + s);
}

@louayeldin

This comment has been minimized.

Copy link

commented Mar 15, 2018

running result android

@MostafaMahmoudAhmed

This comment has been minimized.

Copy link

commented Mar 15, 2018

the Code is Ok
1 box: $5
2 boxes: $10
3 boxes: $30

@louayeldin

This comment has been minimized.

Copy link

commented Mar 16, 2018

The Syntax is correct, the result will be:

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

@piyushkantmaav

This comment has been minimized.

Copy link

commented Mar 27, 2018

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

@piyushkantmaav

This comment has been minimized.

Copy link

commented Mar 27, 2018

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

@conbradst

This comment has been minimized.

Copy link

commented Apr 6, 2018

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

This comment has been minimized.

Copy link

commented Apr 11, 2018

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

@megaunknown

This comment has been minimized.

Copy link

commented May 17, 2018

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

This comment has been minimized.

Copy link

commented May 17, 2018

It's awesomely running fine

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

@trustidkid

This comment has been minimized.

Copy link

commented May 21, 2018

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

This comment has been minimized.

Copy link

commented May 21, 2018

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

@albaraa2015

This comment has been minimized.

Copy link

commented May 25, 2018

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

@hero82

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Jun 2, 2018

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

@mohammeddbadawy

This comment has been minimized.

Copy link

commented Jun 6, 2018

Worked

@bimbolanko

This comment has been minimized.

Copy link

commented Jun 6, 2018

Works but illogical.

@ahmedmtaher

This comment has been minimized.

Copy link

commented Jun 7, 2018

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

@ZahraaElhaj

This comment has been minimized.

Copy link

commented Jun 11, 2018

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

This comment has been minimized.

Copy link

commented Jun 18, 2018

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

This comment has been minimized.

Copy link

commented Jun 18, 2018

    <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

This comment has been minimized.

Copy link

commented Jun 25, 2018

image

@KobiPeeri

This comment has been minimized.

Copy link

commented Jul 2, 2018

find1010 is just great help. thank you !!!

@moiyd

This comment has been minimized.

Copy link

commented Sep 29, 2018

capture

@Yogesh-Bhati

This comment has been minimized.

Copy link

commented Oct 2, 2018

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

@normanmos

This comment has been minimized.

Copy link

commented Nov 1, 2018

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

@shankar-shiv

This comment has been minimized.

Copy link

commented Dec 23, 2018

Done!

photo_2018-12-23_15-46-39

@sslaia

This comment has been minimized.

Copy link

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

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.