In: Computer Science
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.
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:
Part B – Modifications to the CandleShop Class - (17 points)
Modify the CandleShop class as follows:
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.
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