Question

This is my code so far for the assignment. I am stuck on the last 3...

This is my code so far for the assignment. I am stuck on the last 3 tasks which are task#3, task#4 and task#5.

Task #3 String Comparisons

In

checkForDiscount()

method of the driver:

1. Figure out if there is a discount of $2 on this order, due to the customer's name being

either Mike or Diane

2. Return either 2 or 0 as the discount to the cost of the pizza

3. Compile, debug, and run. You should now be able to test all the crust types.

Run your program multiple times with different user names, including Mike and Diane.

Task #4 Arithmetic Calculations

In the calculateTax(double cost) method of the driver:

1. Compute the tax by multiplying the constant TAX_Rate by the cost (passed as a

parameter to the method).

2. Return the tax (back to main)

Task #5 Formatting Numbers

In the displayFinalPrice(double cost, double tax) method of the driver:

1. Instantiate the DecimalFormat class (make sure you have imported java.text.DecimalFormat).

Then, using JOptionPane, display the cost, tax, and grand total of the order, applying the

DecimalFormat object.

Hint:

To use DecimalFormat, you must first instantiate it like this:

DecimalFormat aFormat = new DecimalFormat("$##0.00");

Then, to format a variable named cost, you would use the following statement:

aFormat.format(cost)

Note that the DecimalFormat object that always shows 2 decimal places.

2. Compile, debug, and run. Your output should be completely correct at this time, and numeric

output should look like money.


public static double checkForDiscount()
{
double discount = 0;
//return a discount if user is eligible
//Task #3: Figure out whether to return 0 or $2, depending on whether the customer's name is either Mike or Diana
  
  
  
  
return discount;
}
  
  
/**
* add comments about what this method does, what it receives as a parameter, and what value it returns.
* @param cost
* @return
*/
public static double calculateTax(double cost)
{
final double TAX_RATE = .08; //sales tax rate   
double tax = 0;
  
//Task #4: Calculate tax by multiplying tax_rate by parameter
  
  
return tax;
}
  
  
/**
* add comments about what this method does, what it receives as parameters.
* @param cost
* @param tax
*/
public static void displayFinalPrice(double cost, double tax)
{
//ALL MONEY OUTPUT APPEARS WITH 2 DECIMAL PLACES, using the DecimalFormat class defined at the beginning of this method.
//calculate and display tax and total cost
  
DecimalFormat aFormat = new DecimalFormat("$##0.00");
  
//Task #5: Using JOptionPane, display "The cost of your order is: " (use the DecimalFormat object)
// "The tax is: " (use the DecimalFormat object)
// "The total due is: " (use the DecimalFormat object)
// "Your order will be ready for pick-up in 30 minutes."

}
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In case of any query do comment. Please rate answer as well.

Code:

import javax.swing.*;
import java.util.*;
import java.text.DecimalFormat;

public class Test
{

    public static void main(String[] args) {
  
//Let us assume the cost is $25.
double cost = 25.00;
double tax =0.0;
cost = cost - checkForDiscount();
tax = calculateTax(cost);
  
displayFinalPrice(cost,tax);
  
   }

public static double checkForDiscount()
{
double discount = 0;
//return a discount if user is eligible
//Task #3: Figure out whether to return 0 or $2, depending on whether the customer's name is either Mike or Diana

/*
you did not mention the full structure of your class and other things so for testing purpose I am taking input from the user for name.
It is assumed that customer name is already available for you in checkFOrDiscount Method, you can remove these lines to get customer name
*/
String customerName;
Scanner scnr = new Scanner(System.in);
System.out.println("Enter the customer name");
customerName = scnr.next();

if(customerName.equals("Mike") || customerName.equals("Diane"))
discount =2.0;


return discount;
}

/**
* Compute the tax by multiplying the constant TAX_Rate by the cost (passed as aparameter to the method).
* @param cost
* @return cost mulitplied by constant tax rate
*/
public static double calculateTax(double cost)
{
final double TAX_RATE = .08; //sales tax rate   
double tax = 0;
  
//Task #4: Calculate tax by multiplying tax_rate by parameter
tax = cost * TAX_RATE;
  
return tax;
}
  
  
/**
* using JOptionPane, display the cost, tax, and grand total of the order, applying the DecimalFormat object.
* @param cost
* @param tax
*/
public static void displayFinalPrice(double cost, double tax)
{
//ALL MONEY OUTPUT APPEARS WITH 2 DECIMAL PLACES, using the DecimalFormat class defined at the beginning of this method.
//calculate and display tax and total cost
  
DecimalFormat aFormat = new DecimalFormat("$##0.00");
String output;
  
//Task #5: Using JOptionPane, display "The cost of your order is: " (use the DecimalFormat object)
// "The tax is: " (use the DecimalFormat object)
// "The total due is: " (use the DecimalFormat object)
// "Your order will be ready for pick-up in 30 minutes."
  
output = "The cost of your order is: " + aFormat.format(cost) + "\n";
output = output + "The tax is: " + aFormat.format(tax) + "\n";
output = output + "The total due is: " + aFormat.format(cost + tax) + "\n";
output = output + "Your order will be ready for pick-up in 30 minutes.";
  
JFrame f = new JFrame();      
JOptionPane.showMessageDialog(f,output);

  
}
}

====================Screen shot of the code========

Output:

Add a comment
Know the answer?
Add Answer to:
This is my code so far for the assignment. I am stuck on the last 3...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello can someone help me in my code I left it as comment  task #0, task#1, task#2a...

    Hello can someone help me in my code I left it as comment  task #0, task#1, task#2a and task#2b the things that I am missing I'm using java domain class public class Pizza { private String pizzaCustomerName; private int pizzaSize; // 10, 12, 14, or 16 inches in diameter private char handThinDeep; // 'H' or 'T' or 'D' for hand tossed, thin crust, or deep dish, respecitively private boolean cheeseTopping; private boolean pepperoniTopping; private boolean sausageTopping; private boolean onionTopping; private boolean...

  • Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public...

    Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public class Matrix3 { private double[][] matrix; /** * Creates a 3x3 matrix from an 2D array * @param v array containing 3 components of the desired vector */ public Matrix3(double[][] array) { this.matrix = array; } /** * Clones an existing matrix * @param old an existing Matrix3 object */ public Matrix3(Matrix3 old) { matrix = new double[old.matrix.length][]; for(int i = 0; i <...

  • Java Assignment Calculator with methods. My code appears below what I need to correct. What I...

    Java Assignment Calculator with methods. My code appears below what I need to correct. What I need to correct is if the user attempts to divide a number by 0, the divide() method is supposed to return Double.NaN, but your divide() method doesn't do this. Instead it does this:     public static double divide(double operand1, double operand2) {         return operand1 / operand2;     } The random method is supposed to return a double within a lower limit and...

  • Java. Java is a new programming language I am learning, and so far I am a...

    Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...

  • I am currently stuck with this portion of my assignment: Pointers review: to help determine how...

    I am currently stuck with this portion of my assignment: Pointers review: to help determine how many fields are presented from the form at run time, the program will count the '=' signs from the QUERY_STRING and dynamically create a name_value_pairs array of cnt elements to be used by parse() and param(). Here is what to do, in order: As directed above, make a back up of the work so far and work with the new version named retrieve_form_OOP_2.cpp. These...

  • In java language here is my code so far! i only need help with the extra...

    In java language here is my code so far! i only need help with the extra credit part For this project, you will create a Rock, Paper, Scissors game. Write a GUI program that allows a user to play Rock, Paper, Scissors against the computer. If you’re not familiar with Rock, Paper, Scissors, check out the Wikipedia page: http://en.wikipedia.org/wiki/Rock-paper-scissors This is how the program works: The user clicks a button to make their move (rock, paper, or scissors). The program...

  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

  • PLEASE HELP this is the last part of my lab and I need help ): add comments please (: if it is to...

    PLEASE HELP this is the last part of my lab and I need help ): add comments please (: if it is too difficult you do not have to do part 3 but it would be greatly appreciated if you do ! Obtain example code files Circle.java, Shape.java, CircleShape2.java, Sphere.java, and CircleShapeApp.java from the downloaded files in Ch8. Compile and execute the example and understand the polymorphism it performs. (I have copied and pasted all of these files below) Modify...

  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

  • This is my code that i need to finish. In BoxRegion.java I have no idea how...

    This is my code that i need to finish. In BoxRegion.java I have no idea how to create the constructor. I tried to use super(x,y) but It is hard to apply. And Also In BoxRegionHashTable, I don't know how to create displayAnnotation BoxRegion.java ------------------------------------------------ public final class BoxRegion { final Point2D p1; final Point2D p2; /** * Create a new 3D point with given x, y and z values * * @param x1, y1 are the x,y coordinates for point...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT