Question

In: Computer Science

Preliminaries The owner of the candle shop must begin collecting a tax on each candle sold.  The...

Preliminaries

The owner of the candle shop must begin collecting a tax on each candle sold.  The owner already has a program to perform certain calculations when customers buy different numbers of the candles but this program needs to be modified to include the new tax.  The taxes on each of the three types of candles are shown in the table below:

Type

Tax

1

1%

2

3%

3

5%

Code Provided

Candle.java

public class Candle {
  
// ATTRIBUTES (also called FIELDS)
private int type; // This candle object's Type: 1, 2, or 3
private double cost; // This candle object's cost
private int burnTime; // This candle object's burn time in minutes
  
// CONSTRUCTOR
// User must supply three arguments: int, double, int (representing Type, cost, and burn time in that order) when creating this Candle object
// See lines 11-13 in the CandleShop class to see how this constructor is used
public Candle(int typ, double cst, int brn) {
this.type = typ; // Store the first user-suplied argument (int) in the Type attribute
this.cost = cst; // Store the second user-supplied argument (double) in the cost attribute
this.burnTime = brn; // Store the third user-supplied argument (int) in the burnTime attribute
}
  
// GETTER FOR COST
public double getCost() {
return this.cost; // Return the cost of the current Candle object
}
  
// GETTER FOR BURN TIME
public int getTime() {
return this.burnTime; // Return the burn time of the current Candle object
}
  
// Returns a String object with all the data from the current Candle object
public String toString() {
return "Each type " + this.type + " candle costs $" + this.cost +
" and burns for " + this.burnTime + " minutes";
}
  
}

Candleshop.java

public class CandleShop {

public static void main(String[] args) {
Candle type1 = new Candle(1, 2.5, (5*60)); // Create a Candle object for a Type 1 candle
Candle type2 = new Candle(2, 3.75, (7*60)); // Create a Candle object for a Type 2 candle
Candle type3 = new Candle(3, 5.99, (12*60)); // Create a Candle object for a Type 3 candle

// Each print statement below:
// Calls the Candle class toString method to return a String with data about the object being referenced
// Prints this String to the console
System.out.println(type1.toString()); // Print the type1 object's data
System.out.println(type2.toString()); // Print the type2 object's data
System.out.println(type3.toString()); // Print the type3 object's data

/*
* In the assignment statements below, work from right to left:
* 1. Determine the value of the expression on the right side of the assignment operator (=)
* 2. Assign that value to the variable on the left side of the assignment operator
*/

// HOW MANY CANDLES OF EACH TYPE SHOULD BE BOUGHT?
// Right side:
// 1. "Math.random()" generates a random double value from 0.0 to as much as 0.99999999
// 2. Multiplying that by 11 gives you a random double value from 0.0 to as much as 10.99999999
// 3. Casting that product to an integer truncates the fractional part and leaves you with a random integer from 0 to 10
// Left side:
// 1. Declare one integer variable to hold the number of candles of the type being bought
// Result of statement: Assign a random integer from 0 to 10 to an integer variable for each type of candle
int numType1 = 8;//(int)(Math.random()*11); // Number of Type 1 candles being bought
int numType2 = 9;//(int)(Math.random()*11); // Number of Type 2 candles being bought
int numType3 = 2;//(int)(Math.random()*11); // Number of Type 3 candles being bought
int totalNum = numType1 + numType2 + numType3; // Compute total number of candles being bought

// COMPUTE THE COST OF THAT MANY CANDLES OF EACH TYPE
// Right side:
// 1. Call that Candle object's getCost method to return the cost for that type of candle
// 2. Multiply that cost by the number of that type of candle being bought
// Left side:
// 1. Declare a double variable to hold the cost of the number of candles of that type being bought
// Result of statement: Assign the cost of the number of candles being bought to a double variable for each type of candle
double costType1 = numType1 * type1.getCost();
double costType2 = numType2 * type2.getCost();
double costType3 = numType3 * type3.getCost();
double totalCost = costType1 + costType2 + costType3; // Compute total cost of all types of candles being bought

// COMPUTE THE BURN TIME FOR THAT MANY CANDLES OF EACH TYPE
// Same process as above but with burn time instead of cost
int burnType1 = numType1 * type1.getTime();
int burnType2 = numType2 * type2.getTime();
int burnType3 = numType3 * type3.getTime();
int totalBurn = burnType1 + burnType2 + burnType3; // Total burn time of all types of candles being bought

// DISPLAY THE RESULTS OF THIS SIMUATION
// Print the results for each type of candle being bought and for the overall totals as well
System.out.println("This order is for:");
System.out.println(" " + numType1 + " Type 1 candles that costs $" + costType1 + " and will burn for " + burnType1 + " minutes");
System.out.println(" " + numType2 + " Type 2 candles that costs $" + costType2 + " and will burn for " + burnType2 + " minutes");
System.out.println(" " + numType3 + " Type 3 candles that costs $" + costType3 + " and will burn for " + burnType3 + " minutes");
System.out.println(" The total cost of these " + totalNum + " candles is $" + totalCost + " and the total burn time is " + totalBurn + " minutes");

}

}

Download and save them on your device then open both in DrJava and compile them.

  • Candle.java is the type of class that is used as a template for creating Candle objects. Review the code and note the following:
    • There are three private attributes,
    • There is a public constructor that includes three parameters,
    • There are two getters,
    • There is a toString method that returns a String describing that object.
  • CandleShop.java is a class with a main method that acts as a test harness. Its purpose is to exercise the Candle class to make sure the methods work. It does this by simulating the purchase of a random number of each Type of Candle. The main method performs the following actions:
    • Instantiates three Candle objects, one for each Type of candle (refer to your code for Programming Checkpoint #3 for the values associated with each Type),
    • Prints each object's information using the Candle class toString method,
    • Generates three random integers from 0 to 10. Each represents the number of each Type of candle being purchased.
    • Computes the total cost for each Type of candle and the total cost for all candles,
    • Computes the burn time for each Type of candle and the total burn time for all candles,
    • Prints the details of the order.

Compile and run the CandleShop class multiple times to see how it functions.

Warning

Sometimes your double amounts will be printed with excessive decimal places like 95.52000000000001. You're not doing anything wrong; this is just an artifact of using the double data type to represent currency. There is no need for you to change this output.

Part A – Modifications to the Candle Class - (8 points)

Modify the Candle class as follows:

  1. Add a new double attribute to store the tax in.
  2. Update the constructor to accept an argument representing the tax and store it in the appropriate attribute. The data type of the argument is up to you. Keep in mind that a tax of 20% could be represented as 0.2 or 20 but the representation you pick will determine how the tax is computed so keep this in mind.
  3. Add a getter to return the tax value for the current object.
  4. Update the toString method to include the tax amount in the output.

Part B – Modifications to the CandleShop Class - (17 points)

Modify the CandleShop class as follows:

  1. Update the object instantiation statements to pass in the appropriate tax amount for each Type of candle.
  2. Compute the tax for each Type of candle and the total tax for all candles. How you do this is up to you.
  3. Update the simulation result print statements to include:
    1. The tax on that Type of candle with an appropriate description,
    2. The total cost of the entire order including tax, and
    3. The total tax on the entire order.

Once you've made the changes, don’t forget to test your program multiple times (to generate a range of random numbers) and manually verify that your program's calculations are correct.

Solutions

Expert Solution

Code for your program is provided below. All the modification done is explained thoroughly in code comments. You can also refer to the screenshot of properly indented code in IDE that i have attached in the last. Output screenshot is also provided.

If you need any further clarification , please feel free to ask in comments. I would really appreciate if you would let me know if you are satisfied with the explanation provided.

###########################################################################

Candle.java

public class Candle {

        // ATTRIBUTES (also called FIELDS)
        private int type; // This candle object's Type: 1, 2, or 3
        private double cost; // This candle object's cost
        private int burnTime; // This candle object's burn time in minutes
        private double tax;   //This candle objects's tax rate, it is represented as 1 for  1%, 3 for 3% or 5 for 5% 

        // CONSTRUCTOR
        // User must supply four arguments: int, double, int,double (representing Type, cost,burn time and tax in that order) when creating this Candle object
        // See lines 11-13 in the CandleShop class to see how this constructor is used
        public Candle(int typ, double cst, int brn,double tax) {
                this.type = typ; // Store the first user-suplied argument (int) in the Type attribute
                this.cost = cst; // Store the second user-supplied argument (double) in the cost attribute
                this.burnTime = brn; // Store the third user-supplied argument (int) in the burnTime attribute
                this.tax=tax;  //Store the fourth user supplied argument (double) in tax attribute
        }

        // GETTER FOR COST
        public double getCost() {
                return this.cost; // Return the cost of the current Candle object
        }

        // GETTER FOR BURN TIME
        public int getTime() {
                return this.burnTime; // Return the burn time of the current Candle object
        }
        
        //GETTER FOR TAX
        public double getTax()
        {
                return this.tax;  //Return the tax of the current candle object
        }

        // Returns a String object with all the data from the current Candle object
        public String toString() {
                return "Each type " + this.type + " candle costs $" + this.cost + "with tax "+this.tax+"%"+
                                " and burns for " + this.burnTime + " minutes";
        }

}

CandleShop.java


public class CandleShop {

        public static void main(String[] args) {
                Candle type1 = new Candle(1, 2.5, (5*60),1); // Create a Candle object for a Type 1 candle
                Candle type2 = new Candle(2, 3.75, (7*60),3); // Create a Candle object for a Type 2 candle
                Candle type3 = new Candle(3, 5.99, (12*60),5); // Create a Candle object for a Type 3 candle

                // Each print statement below:
                // Calls the Candle class toString method to return a String with data about the object being referenced
                // Prints this String to the console
                System.out.println(type1.toString()); // Print the type1 object's data
                System.out.println(type2.toString()); // Print the type2 object's data
                System.out.println(type3.toString()); // Print the type3 object's data

                /*
                 * In the assignment statements below, work from right to left:
                 * 1. Determine the value of the expression on the right side of the assignment operator (=)
                 * 2. Assign that value to the variable on the left side of the assignment operator
                 */

                // HOW MANY CANDLES OF EACH TYPE SHOULD BE BOUGHT?
                // Right side:
                // 1. "Math.random()" generates a random double value from 0.0 to as much as 0.99999999
                // 2. Multiplying that by 11 gives you a random double value from 0.0 to as much as 10.99999999
                // 3. Casting that product to an integer truncates the fractional part and leaves you with a random integer from 0 to 10
                // Left side:
                // 1. Declare one integer variable to hold the number of candles of the type being bought
                // Result of statement: Assign a random integer from 0 to 10 to an integer variable for each type of candle
                int numType1 = (int)(Math.random()*11); // Number of Type 1 candles being bought
                int numType2 = (int)(Math.random()*11); // Number of Type 2 candles being bought
                int numType3 = (int)(Math.random()*11); // Number of Type 3 candles being bought
                int totalNum = numType1 + numType2 + numType3; // Compute total number of candles being bought

                // COMPUTE THE COST OF THAT MANY CANDLES OF EACH TYPE
                // Right side:
                // 1. Call that Candle object's getCost method to return the cost for that type of candle
                // 2. Multiply that cost by the number of that type of candle being bought
                // Left side:
                // 1. Declare a double variable to hold the cost of the number of candles of that type being bought
                // Result of statement: Assign the cost of the number of candles being bought to a double variable for each type of candle
                double costType1 = numType1 * type1.getCost();  //cost for type1 candle
                double tax1=costType1*(type1.getTax()/100.0);   //tax for type1 candles
                double costType2 = numType2 * type2.getCost();  //cost for type2 candle
                double tax2=costType2*(type2.getTax()/100.0);   //tax for type2 candles
                double costType3 = numType3 * type3.getCost();  //cost for type3 candle
                double tax3=costType3*(type3.getTax()/100.0);   //tax for type3 candles
                double totalTax=tax1+tax2+tax3;   //total tax on whole order
                double totalCost = costType1 + costType2 + costType3+totalTax; // Compute total cost of all types of candles being bought including total tax

                // COMPUTE THE BURN TIME FOR THAT MANY CANDLES OF EACH TYPE
                // Same process as above but with burn time instead of cost
                int burnType1 = numType1 * type1.getTime();
                int burnType2 = numType2 * type2.getTime();
                int burnType3 = numType3 * type3.getTime();
                int totalBurn = burnType1 + burnType2 + burnType3; // Total burn time of all types of candles being bought

                // DISPLAY THE RESULTS OF THIS SIMUATION
                // Print the results for each type of candle being bought and for the overall totals as well
                System.out.println("This order is for:");
                //type1 cost, tax and burn time
                System.out.println(" " + numType1 + " Type 1 candles that costs $" + costType1 + " with tax $"+tax1+" and will burn for " + burnType1 + " minutes");  
                //type2 cost, tax and burn time
                System.out.println(" " + numType2 + " Type 2 candles that costs $" + costType2 + " with tax $"+tax2+" and will burn for " + burnType2 + " minutes");
                //type3 cost, tax and burn time
                System.out.println(" " + numType3 + " Type 3 candles that costs $" + costType3 + " with tax $"+tax3+" and will burn for " + burnType3 + " minutes");
                //total cost, total tax and total burntime of whole order
                System.out.println(" The total cost of these " + totalNum + " candles is $" +totalCost +  " with total tax $"+totalTax+" and the total burn time is " + totalBurn + " minutes");

        }

}

###################################################################

OUTPUT

######################################################################

CODE SCREENSHOT

Candle.java

CandleShop.java


Related Solutions

A small candy shop is preparing for the holiday season. The owner must decide how many...
A small candy shop is preparing for the holiday season. The owner must decide how many bags of deluxe mix and how many bags of standard mix of Peanut/Raisin Delite to put up. The deluxe mix has .68 pound raisins and .32 pound peanuts, and the standard mix has .56 pound raisins and .44 pound peanuts per bag. The shop has 81 pounds of raisins and 66 pounds of peanuts to work with. Peanuts cost $.66 per pound and raisins...
A small candy shop is preparing for the holiday season. The owner must decide how many...
A small candy shop is preparing for the holiday season. The owner must decide how many bags of deluxe mix and how many bags of standard mix of Peanut/Raisin Delite to put up. The deluxe mix has .67 pound raisins and .33 pound peanuts, and the standard mix has .55 pound raisins and .45 pound peanuts per bag. The shop has 80 pounds of raisins and 65 pounds of peanuts to work with. Peanuts cost $.65 per pound and raisins...
A small candy shop is preparing for the holiday season. The owner must decide how many...
A small candy shop is preparing for the holiday season. The owner must decide how many bags of deluxe mix and how many bags of standard mix of Peanut/Raisin Delite to put up. The deluxe mix has .70 pound raisins and .30 pound peanuts, and the standard mix has .50 pound raisins and .50 pound peanuts per bag. The shop has 75 pounds of raisins and 60 pounds of peanuts to work with. Peanuts cost $.60 per pound and raisins...
An owner of an automobile body shop comes into your office for tax advice. He has...
An owner of an automobile body shop comes into your office for tax advice. He has been thinking about updating his antiquated computer system so he purchased a consumer handbook then he flew to San Francisco to visit the Apple Store to shop for computers for his personal and business use. He wants to deduct the trip, his food, the book, and the computer he purchased in San Francisco. How would you advise him regarding the deductions and expenses he...
Bicycles arrive at a bike shop in boxes. Before they can be​sold, they must be​ unpacked,...
Bicycles arrive at a bike shop in boxes. Before they can be​sold, they must be​ unpacked, assembled, and tuned​(lubricated, adjusted,​ etc.). Based on past​ experience, the shop manager creates a model of how long this may take based on the assumptions that the times for each setup phase are​ independent, each phase follows a Normal​model, and the means and standard deviations of the times in minutes are as shown in the table. Complete parts​ a) and​ b). Phase Mean SD...
Pasadena Candle Inc. budgeted production of 785,000 candles for January. Each candle requires molding. Assume that...
Pasadena Candle Inc. budgeted production of 785,000 candles for January. Each candle requires molding. Assume that six minutes are required to mold each candle. If molding labor costs $18 per hour, determine the direct labor cost budget for January. Wax is required to produce a candle. Assume 487,125 pounds of material will be purchased during January. The candle wax costs $1.24 per pound. Prepare a cost of goods sold budget for Pasadena Candle Inc. using the information above. Assume the...
1. Pasadena Candle Inc. budgeted production of 45,000 candles for January. Each candle requires molding. Assume...
1. Pasadena Candle Inc. budgeted production of 45,000 candles for January. Each candle requires molding. Assume that two minutes are required to mold each candle. If molding labor costs $13 per hour, determine the direct labor cost budget for January. Round total direct labor cost to the nearest dollar, if required. Pasadena Candle Inc. Direct Labor Cost Budget For the Month Ending January 31 Hours required for assembly: Candles min. Convert minutes to hours ÷ min. Molding hours hrs. Hourly...
EXERCISE 1. A shop sells home computers. The numbers of computers sold in each of five...
EXERCISE 1. A shop sells home computers. The numbers of computers sold in each of five successive years were as follows: Year (x) 1 2 3 4 5 Sales (y) 10 30 70 140 170 Draw a scatter diagram for the data Find the least squares regression line of y on x and fit it on you scatter plot The shop manager uses this regression line to predict the sales in the following (i.e. the 6th) year. Find the predicted...
What are the consequences of not following Wayfair and not collecting and remitting sales tax in...
What are the consequences of not following Wayfair and not collecting and remitting sales tax in the identified jurisdictions?
Karen Battle​, owner of Flower Hour​, operates a local chain of floral shops. Each shop has...
Karen Battle​, owner of Flower Hour​, operates a local chain of floral shops. Each shop has its own delivery van. Instead of charging a flat delivery​ fee, Battle wants to set the delivery fee based on the distance driven to deliver the flowers. Battle wants to separate the fixed and variable portions of her van operating costs so that she has a better idea how delivery distance affects these costs. She has the following data from the past seven​ months:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT