简体   繁体   中英

int definition - not sure what the purpose is in the code

public void submitOrder(View view) {
    CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whippedCreamCheckBox);
    boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
    Log.v("MainActivity", "Has whipped Cream: " + hasWhippedCream);

    int price = calculatePrice();
    String priceMessage = createOrderSummary(price, hasWhippedCream);
    displayMessage(priceMessage);
}
/**
 * Calculates the price of the order.
 *
 * @param 'quantity' is the number of cups of coffee ordered
 * "5" is used for the price of each coffee
 * @return total price
 */
private int calculatePrice() {
    int price = quantity *5;
    return price;
}

I am very new to coding, so please bear in mind this is something new, and I'm trying to understand how this works.. I'm taking this course, and I ended up getting the code above.

I am questioning why I have "calculatePrice" int because all I need is int "price" that is defined by quantity and stores the value of price. This is what I am understanding based on "private int calculatePrice" 3 lines. What is the purpose of having "int price = calculatePrice();" in the public void? I feel like I defined "price" within private "calculatePrice," and now I am RE-DEFINING "price" by writing "int price = calculatePrice();". It's confusing. Could someone explain why I "int price" is defined twice, meaning defined within "calculatePrice" and re-defined by "calculatePrice" again in the public void?

I'm having hard time getting the concept... thanks for helping out!

In ref to the above code, int price defined two times in two methods whose scope very much ends in the methods they are defined. Though calculatePrice method in your code does not do much, but in real projects, you can assign a complex method which executes lots of logic and finally returns evaluated value in the form of int.

You are not re-defining the variable price. Both "Price" variables are in different scopes, so actually they are not the same variable. In the first case, price is only visible and accessible from the submitOrder method since it's declared inside that method. In the second case, price is only visible and accesible from calculatePrice method, so even having the same name they are totally different variables.

You can read more about scope and lifetime of variables in this link: http://www.javawithus.com/tutorial/scope-and-lifetime-of-variables

By the way, I think the method calculatePrice is not properly defined, it needs a int parameter to setthe quantity:

private int calculatePrice(int quantity) {
    int price = quantity *5;
    return price;
}

Once you have modified this method, you should call it from the method submitOrder in this way:

 int price = calculatePrice("a number");

The second method :

private int calculatePrice() { int price = quantity *5; return price; } private int calculatePrice() { int price = quantity *5; return price; } this is a method with returning type of integer, it is responsible for calculate the price and return the result which is price. in your first method you call this function and get the result of your price and store it in your actual variable which is also named price. to not be confused you can write the second method as :

private int calculatePrice() { return quantity *5; } private int calculatePrice() { return quantity *5; } and then call it in the first method as int price=calculatePrice()

The calculatePrice() is a function which returns an integer value return price . So when you call that function it will execute that part of code inside calculatePrice() and will return an answer which is integer.

The scope of the variable int price which is defined inside the function is only limited to that function (you can name your variable as you like, it is not necessary to name only "price").

The int price = calculatePrice(); is another variable which you are assigning value which is returned from your function.

The methods after modifying the variable name...

/**
 * Calculates the TOTAL price of the order.
 */
private int calculatePrice(int qty) {
    int totalPrice = quantity * qty;
    return totalPrice;
}

// You are calling by passing the quantity (qty)
int qty = 5;
int invoicePrice = calculatePrice(qty);
    String priceMessage = createOrderSummary(inoicePrice, hasWhippedCream);

Hope this will help to understand the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM