338,151 views
34 votes
34 votes
Write a program that uses the Purchase class in 5.13. Set the prices to the following: Oranges: 10 for $2.99 Eggs: 12 for $1.69 Apples: 3 for $1.00 Watermelons: $4.39 each Bagels: 6 for $3.50 Set the purchased quantity to the following: 2 dozen oranges, 2 dozen eggs, 20 apples, 2 watermelons, 1 dozen bagels Display the total cost of the bill

User Khwaja
by
2.9k points

2 Answers

13 votes
13 votes

Final answer:

To write a program that uses the Purchase class, you can define a Purchase object for each item and set their prices accordingly. Then, you can calculate the total cost of the bill by multiplying the quantity and price of each item and adding them together.

Step-by-step explanation:

To write a program that uses the Purchase class, you can define a Purchase object for each item and set their prices accordingly. Here's an example:



Purchase oranges = new Purchase(10, 2.99);
Purchase eggs = new Purchase(12, 1.69);
Purchase apples = new Purchase(3, 1.00);
Purchase watermelons = new Purchase(1, 4.39);
Purchase bagels = new Purchase(6, 3.50);


Then, you can calculate the total cost of the bill by multiplying the quantity and price of each item and adding them together:

double totalCost = oranges.getTotalCost() + eggs.getTotalCost() + apples.getTotalCost() + watermelons.getTotalCost() + bagels.getTotalCost();


The totalCost variable will hold the calculated value which you can then display to the user.

User Snakebyte
by
3.0k points
9 votes
9 votes

Answer:

Step-by-step explanation:

The following program is written in Java. Using the program code from Purchase class in 5.13 I created each one of the fruit objects. Then I set the price for each object using the setPrice method. Then I set the number of each fruit that I intended on buying with the setNumberBought method. Finally, I called each objects getTotalCost method to get the final price of each object which was all added to the totalCost instance variable. This instance variable was printed as the total cost of the bill at the end of the program. My code HIGHLIGHTED BELOW

//Entire code is in text file attached below.

//MY CODE HERE

DecimalFormat df = new DecimalFormat("0.00");

oranges.setPrice(10, 2.99);

oranges.setNumberBought(2*12);

eggs.setPrice(12, 1.69);

eggs.setNumberBought(2*12);

apples.setPrice(3, 1);

apples.setNumberBought(20);

watermelons.setPrice(1, 4.39);

watermelons.setNumberBought(2);

bagels.setPrice(6, 3.50);

bagels.setNumberBought(12);

totalCost = oranges.getTotalCost() + eggs.getTotalCost() + apples.getTotalCost() + watermelons.getTotalCost() + bagels.getTotalCost();

System.out.println("Total Cost: $" + df.format(totalCost));

}

}

Write a program that uses the Purchase class in 5.13. Set the prices to the following-example-1
User Muhammad Ahsan
by
2.9k points