Answer to Question #262381 in Java | JSP | JSF for laiba

Question #262381

Provide a method calculatePrice() that calculates and returns the price of this sandwich

according to the rates given in the above table. E.g. the price of a sandwich with 3 bread slices, 2

cheese slices, 2 tomato slices, 3 patties, mustard, no ketchup, no ice berg with grilling will be

calculated as

3 * 20 + 2 * 30 + 2 * 5 + 3 * 70 + 5+ 0 + 0 + 10

 Provide a method applyDiscount(double disc) that calculates the price, applies the discount %

given in the argument and returns the discounted price of the sandwich.E.g. if the method is

called on a sandwich whose real price returned by the calculatePrice function is 550 and

applyDiscount(10) is called, then a ten percent discount will be calculated as 550 * 10/100 = 55.

Now this function will return the discounted price as 550 – 55 = 495. (Please note that this is just

an example, do not hard code these values in your function).


1
Expert's answer
2021-11-07T14:52:14-0500


package sandwich;


import java.util.Scanner;




public class Sandwich {
    static Scanner scan = new Scanner(System.in);
  static double calculatePrice(){
      System.out.println("Enter the number of bread slices");
      double slices = scan.nextDouble();
      System.out.println("Enter the number of cheese slices");
      double cheese_lices = scan.nextDouble();
      System.out.println("Enter the number of tomato slices");
      double tomato_lices = scan.nextDouble();
      System.out.println("Enter the number of patties");
      double patties = scan.nextDouble();
      System.out.println("Enter the number of ketchup");
      double ketchup = scan.nextDouble();
      System.out.println("Enter the number of ice_berg");
      double ice_berg = scan.nextDouble();
      
      System.out.println("Do you want to mustard. Enter no or yes");
      String mustard = scan.next();
      System.out.println("Do you want to mustard. Enter no or yes");
      String grilling = scan.next();
      
      if(mustard=="yes" || grilling=="yes" ){
      return slices * 20 + cheese_lices * 30 + tomato_lices * 5 + patties * 70 + 5+ ketchup* 4 + ice_berg * 4  + 10;
  }
      else{
          return slices * 20 + cheese_lices * 30 + tomato_lices * 5 + patties * 70 +  ketchup* 4 + ice_berg * 4;
      }
  }
  static double applyDiscount(double disc){
      return calculatePrice()  -  (calculatePrice() * disc /100);
  }
    public static void main(String[] args) {
        
    }
    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS