import static java.lang.System.*;
import java.util.Scanner;

public class Sales{

//you haven't learned about "static" yet but take it on faith that it is needed
//we'll discuss in future classes what it actually means and why you need it
//create a private static variable for price
//create a private static variable for discountRate - it's an integer
//create a private static variable for tax


// the sale method takes in no parameters
// there are 3 print satements and the method returns the final sale price
public static double sale(){

//write your code here. Use the Word doc example as a guide to what the print statements
//should look like. Don't forget the "return" statement.

}

/*
*The main method is what is executed when the "run" button is clicked.
*It is necessary, in this case, for the program to be tested.
*Actually study this code and trace through it to see how the Scanner is used to assign
* values to the variables. Also pay attention to the use of print, println and \n to cause the
* output to be formatted the way it is in the example.
*/

public static void main(String[] args){
Scanner kb = new Scanner(System.in);
System.out.print("Please enter a price: ");
price = kb.nextDouble();
System.out.println(price + "\n");
System.out.print("What is the tax rate? ");
tax = kb.nextDouble();
System.out.println(tax);
discountRate = (int)(Math.random() * 40);
System.out.println("Result returned is " + sale());
}
}




Can anyone help with this

Respuesta :

tonb
First you add the three static variables as instructed:

    private static double price;
    private static int discountRate;
    private static double tax;

Then you implement the sale() method:

public static double sale(){
    System.out.printf("Price before discount: %.2f\n", price);

    double sale = price * (1-(double)discountRate/100.0);
    System.out.printf("Price after discounting %d%% but before tax: %.2f\n", discountRate, sale);

    sale *= (1+tax/100.0);
    System.out.printf("Discounted price with %.1f%% tax added: %.2f\n", tax, sale);

    return sale;
}

I kind of took a stab at how to calculate the sale. Not sure if the tax has to be added before or after the discount. But I'm sure you can fix that if needed.

ACCESS MORE