Need to create a program for java. How do you go about writing this code?
1. Write an if statement that uses the flag as the condition. Remember that the flag is a Boolean variable, therefore is true or false. It does not have to be compared to anything.
2. The body of the if statemetn should contain two statements:
a) A statement that prints a message indicating that the user is eligible for a $2.00 discount
b) A statement that reduces the variable cost by 2.
public class Main {
public static void main(String[] args) {
double price = 98.22;
double discount = 2;
boolean eligibleForDiscount = true;
//Price before discount
System.out.println("Original Price: " + price);
//Checks if there is a discount
price = CheckDiscount(eligibleForDiscount, price, discount);
//Prints final result
System.out.println("Price is: " + price );
}
private static double CheckDiscount
(boolean eligibleForDiscount, double price, double discount){
//Checks to see if egligble for discount,
//is true, computes and returns discount,
//if false, returns origial discount.
System.out.print("!!2 Dollars off!!");
if (eligibleForDiscount == true) {
price = (price - discount);
}
return price;
}
}