Question

In: Computer Science

Write a program that uses the Purchase class in Listing 5.13(found in the Sakai Week...

Write a program that uses the Purchase class in Listing 5.13 (found in the Sakai Week 6 Source Code folder) to create 5 Purchase objects and set the following prices for them:

Oranges: 10 for $2.99

Eggs: 12 for $1.69

Apples: 3 for $1.00

Watermelons: $4.39 each

Bagels: 6 for $3.50

Also use the 5 Purchase objects to set the number bought and then calculate the cost of each of the following five items and the total bill:

2 dozen oranges

3 dozen eggs

20 apples

2 watermelons

1 dozen bagels

You may use the readInput method to read these values in from the user, including the number of items bought (shown above), or you may just call each of the objects'setName and setPrice methods in maindirectly to set their names, group prices, and number bought by using literal values like "oranges", 10, 2.99, and 24 (the number of oranges bought, 2 dozen = 2 * 12 == 24).

The program should output exactly the following lines with the letters a through o and t replaced by the correct values - use the appropriate Purchase object setters and getters to generate these lines and fill in the correct values based on the purchases given above:

a *items* at $b each cost $c
d *items* at $e each cost $f
g *items* at $g each cost $i
j *items* at $k each cost $l
m *items* at $n each cost $o
total cost: $t

In the first line above, items is oranges, and a is 24, b is the unit cost of an orange, and c is the total cost of buying 24 of them. Similarly, items in the next 4 lines will be eggs, apples, watermelons, and bagels with appropriate values in letters d through o. t in the last line is the sum c+f+i+l+o from the 5 lines above. Use the 5 Purchase objects to fill in all of these values.

IN JAVA

public class Purchase

{

private String name;

private int groupCount; // Number of items in a group, like the 2 in 2 for $1.99.

private double groupPrice;

// Price per item in a group, like the 1.99 in 2 for $1.99.

private int numberBought; // Total number being purchased by customer.

public void setName(String newName)

{

name = newName;

}

/**

Sets price to groupCount pieces for $costForCount. E.g., 2 for $1.99.

*/

public void setPrice(int count, double costForCount)

{

if ((count <= 0) || (costForCount <= 0))

{

System.out.println("Error: Bad parameter in setPrice.");

System.exit(0);

}

else

{

groupCount = count;

groupPrice = costForCount;

}

}

public void setNumberBought(int number)

{

if (number <= 0)

{

System.out.println("Error: Bad parameter in setNumberBought.");

System.exit(0);

}

else

numberBought = number;

}

/**

Gets price and number being purchased from keyboard.

*/

public void readInput()

{   

System.out.println("Enter name of item you are purchasing on a separate line:");

name = Keyboard.nextLine(); // Keyboard is a special class that only reads from the keyboard

System.out.println("Enter price of item on one or two lines.");

System.out.println("For example, 3 for $2.99 is entered as either");

System.out.println("3");

System.out.println("2.99");

System.out.println("or: 3 2.99");

System.out.println("Enter price of item on one or two lines, now:");

groupCount = Keyboard.nextInt();

groupPrice = Keyboard.nextDouble();

while ((groupCount <= 0) || (groupPrice <= 0))

{

//Try again:

System.out.println(

"Both numbers must be positive. Try again.");

System.out.println("Enter price of item on one or two lines as shown above:");

groupCount = Keyboard.nextInt();

groupPrice = Keyboard.nextDouble();

}

System.out.println("Enter number of items purchased:");

numberBought = keyboard.nextInt();

while (numberBought <= 0)

{

//Try again:

System.out.println(

"Number must be positive. Try again.");

System.out.println("Enter number of items purchased:");

numberBought = keyboard.nextInt();

}

}

/**

Outputs price and number being purchased to screen.

*/

public void writeOutput()

{

System.out.println(numberBought + " " + name);

System.out.println("at " + groupCount

+ " for $" + groupPrice);

}

public String getName()

{

return name;

}

public double getTotalCost()

{

return ((groupPrice/groupCount)*numberBought);

}

public double getUnitCost()

{

return (groupPrice/groupCount);

}

public int getNumberBought()

{

return numberBought;

}

public static void main(String[] args)

{

Purchase oranges = new Purchase();

Purchase eggs = new Purchase();

Purchase apples = new Purchase();

Purchase watermelons = new Purchase();

Purchase bagels = new Purchase();

double totalCost = 0;

// write code to fill in the appropriate values and

// calculate the costs and total using the methods

// from the Purchase class; do not calculate the

// values on your own, use the methods to do that!

// you may read values from the user using readInput

// or you can just fill in the values given above as

// literal values, like "oranges", 10, and 2.99, and,

// later, the number of oranges bought, 2 * 12 == 24

// (2 dozen)

/* your code goes here */

}

}

Solutions

Expert Solution

// Purchase.java

import java.util.Scanner;

public class Purchase

{

      private String name;

      private int groupCount; // Number of items in a group, like the 2 in 2 for

                                           // $1.99.

      private double groupPrice;

      // Price per item in a group, like the 1.99 in 2 for $1.99.

      private int numberBought; // Total number being purchased by customer.

      // for the sake of compilation, I have declared Keyboard as a Scanner object

      // to read inputs. If you have a Keyboard class, then please remove below

      // line, the program will work fine.

      private static Scanner Keyboard = new Scanner(System.in);

      public void setName(String newName)

      {

            name = newName;

      }

      /**

      * Sets price to groupCount pieces for $costForCount. E.g., 2 for $1.99.

      */

      public void setPrice(int count, double costForCount)

      {

            if ((count <= 0) || (costForCount <= 0))

            {

                  System.out.println("Error: Bad parameter in setPrice.");

                  System.exit(0);

            }

            else

            {

                  groupCount = count;

                  groupPrice = costForCount;

            }

      }

      public void setNumberBought(int number)

      {

            if (number <= 0)

            {

                  System.out.println("Error: Bad parameter in setNumberBought.");

                  System.exit(0);

            }

            else

                  numberBought = number;

      }

      /**

      * Gets price and number being purchased from keyboard.

      */

      public void readInput()

      {

            System.out

                        .println("Enter name of item you are purchasing on a separate line:");

            name = Keyboard.nextLine(); // Keyboard is a special class that only

                                                       // reads from the keyboard

            System.out.println("Enter price of item on one or two lines.");

            System.out.println("For example, 3 for $2.99 is entered as either");

            System.out.println("3");

            System.out.println("2.99");

            System.out.println("or: 3 2.99");

            System.out.println("Enter price of item on one or two lines, now:");

            groupCount = Keyboard.nextInt();

            groupPrice = Keyboard.nextDouble();

            while ((groupCount <= 0) || (groupPrice <= 0))

            {

                  // Try again:

                  System.out.println(

                  "Both numbers must be positive. Try again.");

                  System.out

                              .println("Enter price of item on one or two lines as shown above:");

                  groupCount = Keyboard.nextInt();

                  groupPrice = Keyboard.nextDouble();

            }

            System.out.println("Enter number of items purchased:");

            numberBought = Keyboard.nextInt();

            while (numberBought <= 0)

            {

                  // Try again:

                  System.out.println(

                  "Number must be positive. Try again.");

                  System.out.println("Enter number of items purchased:");

                  numberBought = Keyboard.nextInt();

            }

      }

      /**

      * Outputs price and number being purchased to screen.

      */

      public void writeOutput()

      {

            // updated to print the details in proper format as shown in sample

            // output. Note that values are rounded to 2 digits after decimal point,

            // so values like 0.299 may appear as 0.30

            System.out.printf("%d %s at $%.2f each cost $%.2f\n", numberBought,

                        name, getUnitCost(), getTotalCost());

      }

      public String getName()

      {

            return name;

      }

      public double getTotalCost()

      {

            return ((groupPrice / groupCount) * numberBought);

      }

      public double getUnitCost()

      {

            return (groupPrice / groupCount);

      }

      public int getNumberBought()

      {

            return numberBought;

      }

      public static void main(String[] args)

      {

            Purchase oranges = new Purchase();

            Purchase eggs = new Purchase();

            Purchase apples = new Purchase();

            Purchase watermelons = new Purchase();

            Purchase bagels = new Purchase();

            double totalCost = 0;

            // write code to fill in the appropriate values and

            // calculate the costs and total using the methods

            // from the Purchase class; do not calculate the

            // values on your own, use the methods to do that!

            // you may read values from the user using readInput

            // or you can just fill in the values given above as

            // literal values, like "oranges", 10, and 2.99, and,

            // later, the number of oranges bought, 2 * 12 == 24

            // (2 dozen)

            // updating name, number bought and price of each item using hardcoding

            // as it is allowed

            oranges.setName("oranges");

            oranges.setNumberBought(2 * 12);

            oranges.setPrice(10, 2.99);

            eggs.setName("eggs");

            eggs.setNumberBought(3 * 12);

            eggs.setPrice(12, 1.69);

            apples.setName("apples");

            apples.setNumberBought(20);

            apples.setPrice(3, 1.00);

            watermelons.setName("watermelons");

            watermelons.setNumberBought(2);

            watermelons.setPrice(1, 4.39);

            bagels.setName("bagels");

            bagels.setNumberBought(12);

            bagels.setPrice(6, 3.50);

            // printing output for oranges

            oranges.writeOutput();

            // adding oranges cost to total

            totalCost += oranges.getTotalCost();

            // doing the same for other purchases as well

            eggs.writeOutput();

            totalCost += eggs.getTotalCost();

            apples.writeOutput();

            totalCost += apples.getTotalCost();

            watermelons.writeOutput();

            totalCost += watermelons.getTotalCost();

            bagels.writeOutput();

            totalCost += bagels.getTotalCost();

            // displaying total cost formatted to 2 digits after decimal point

            System.out.printf("total cost: $%.2f\n", totalCost);

      }

}

/*OUTPUT*/

24 oranges at $0.30 each cost $7.18

36 eggs at $0.14 each cost $5.07

20 apples at $0.33 each cost $6.67

2 watermelons at $4.39 each cost $8.78

12 bagels at $0.58 each cost $7.00

total cost: $34.69


Related Solutions

Write a program that uses the Purchase class in Listing 5.13 (found in the Sakai Week...
Write a program that uses the Purchase class in Listing 5.13 (found in the Sakai Week 6 Source Code folder) to create 5 Purchase objects and set the following prices for them: Oranges: 10 for $2.99 Eggs: 12 for $1.69 Apples: 3 for $1.00 Watermelons: $4.39 each Bagels: 6 for $3.50 Also use the 5 Purchase objects to set the number bought and then calculate the cost of each of the following five items and the total bill: 2 dozen...
3. write a program that uses a class called "garment" that is derived from the class...
3. write a program that uses a class called "garment" that is derived from the class "fabric" and display some values of measurement. 20 pts. Based on write a program that uses the class "fabric" to display the square footage of a piece of large fabric.           class fabric            {                private:                    int length;                    int width;                    int...
Write and test a user-defined class (requiring conditions). Write an application (client) program that uses an...
Write and test a user-defined class (requiring conditions). Write an application (client) program that uses an instance(s) of a user-defined class. The federal income tax that a person pays is a function of the person's taxable income. The following table contains formulas for computing a single person's tax. Bracket Taxable Income Tax Paid 1 $22,100 or less 15% 2 More than $22,100 but $53,500 or less $3,315 plus 28% of the taxable income over $22,100 3 More than $53,500 but...
Write a program that uses an array of high temperatures for your hometown from last week...
Write a program that uses an array of high temperatures for your hometown from last week (Sunday – Saturday). Write methods to calculate and return the lowest high temperature (minimum) and a method to calculate and return the highest high temperature (maximum) in the array. You must write YOUR ORIGINAL methods for minimum and maximum. You MAY NOT use Math class methods or other library methods. Write an additional method that accepts the array as a parameter and then creates...
Write a program that uses an array of high temperatures for your hometown from last week...
Write a program that uses an array of high temperatures for your hometown from last week (Sunday – Saturday). Write methods to calculate and return the lowest high temperature (minimum) and a method to calculate and return the highest high temperature (maximum) in the array. You must write YOUR ORIGINAL methods for minimum and maximum. You MAY NOT use Math class methods or other library methods. Write an additional method that accepts the array as a parameter and then creates...
Write a script named numberlines.py. This script creates a program listing from a source program. This...
Write a script named numberlines.py. This script creates a program listing from a source program. This script should: Prompt the user for the names of two files. The input filename could be the name of the script itself, but be careful to use a different output filename! The script copies the lines of text from the input file to the output file, numbering each line as it goes. The line numbers should be right-justified in 4 columns, so that the...
Write a program with class name ForLoops that uses for loops to perform the following steps:1....
Write a program with class name ForLoops that uses for loops to perform the following steps:1. Prompt the user to input two positive integers: firstNum and secondNum (firstNum must be smallerthan secondNum).2. Output all the even numbers between firstNum and secondNum inclusive.3. Output the sum of all the even numbers between firstNum and secondNum inclusive.4. Output all the odd numbers between firstNum and secondNum inclusive.5. Output the sum of all the odd numbers between firstNum and secondNum inclusive. Java programming
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
Using class, write a program that: Body Mass Index (BMI) calculation - write a program that...
Using class, write a program that: Body Mass Index (BMI) calculation - write a program that takes users' input (weight and Height) and calculates the BMI. If the result is less than 18.5 display "you are underweight", if the result is greater than 18.5 display "you have a normal weight", if the result is greater than 24.9 display "your weight is considered overweight", and the result is greater than 30 display "your weight is considered as obese" (BMI = 703...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT