408,548 views
43 votes
43 votes
Hi ! How can you return the value of the class computeDiscountInfo to the variable savings in the main class ?

Here is my code :

import java.util.Scanner;
public class RoomCost{
public static void main (String [] args){
double price;
double discount;
double savings;

Scanner keyboard = new Scanner(System.in);
System.out.print("Enter cutoff price for discount $>> ");
price = keyboard.nextDouble();
System.out.print("Enter discount rate as a whole number >> ");
discount = keyboard.nextDouble();
displayinfo();
//insert code to set the value returned from computeDiscountInfo method to savings);
// savings =
System.out.println("Special this week on any room over " + price);

System.out.println("Discount of " + discount + " percent");

System.out.println("That's a savings of at least $" + savings);
}
public static void displayinfo(){
System.out.println("We want your stay to be memorable.");

System.out.println("We promise to make you as comfortable as possible.");

}
public static double computeDiscountInfo(double price, double discountRate)

{

double savings;

//(insert code to calculate savings as price multiplied by discount divided by 100);
savings = (price * discountRate)/100;
return savings;
}


}

Hi ! How can you return the value of the class computeDiscountInfo to the variable-example-1
User HeDinges
by
3.0k points

1 Answer

8 votes
8 votes

Answer:

savings = computeDiscountInfo(price, discount);

Step-by-step explanation:

First of all computeDiscountInfo is a function, not a class.

You can call this function and assign its return value to a variable, savings in this example.

User Alvis
by
2.8k points