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 main directly 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.

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

Solution:

public class Purchase
{

    public void setName(String theName)

    {

        name = theName;

    }

    public void setPrice(int count, double costForCount)

    {

        if (count <= 0)

        {

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

            System.exit(0);

        }

        else

        {

            groupCount = count;

            groupPrice = costForCount;

        }

    }

    public void setNumber(int number)

    {

        numberBought = number;

    }

    public String nameOfItem()

    {

        return name;

    }

    public double totalCost()

    {

        return ((groupPrice/groupCount)*numberBought);

    }

    public double unitCost()

    {

        return (groupPrice/groupCount);

    }

    public int number()

    {                  

        return numberBought;

    }

    public void input() throws IOException

    {

        SavitchIn keyboard = new SavitchIn();

        String itemName;

        int count;

        double cost;

        int totalNumber;

        System.out.println("Enter name of what is being purchased:");

        itemName = keyboard.readLine();

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

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

        System.out.println("3");

        System.out.println("2.99");

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

        count = keyboard.readLineInt();

        cost = keyboard.readLineDouble();

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

        totalNumber = keyboard.readLineInt();

        System.out.println(totalNumber + " " + itemName);

        System.out.println("at " + count + " for $" + cost);

        System.out.println("Is that correct?(y/n)");

        char ans = keyboard.readLineNonwhiteChar();

        while ((ans != 'y') && (ans != 'Y'))

        {

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

            itemName = keyboard.readLine();

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

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

            System.out.println("3");

            System.out.println("2.99");

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

            count = keyboard.readLineInt();

            cost = keyboard.readLineDouble();

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

            totalNumber = keyboard.readLineInt();

            System.out.println(totalNumber + " " + itemName);

            System.out.println("at " + count + " for $" + cost);

            System.out.println("Is that correct?(y/n)");

            ans = keyboard.readLineNonwhiteChar();

        }

        name = itemName;

        groupCount = count;

        groupPrice = cost;

        numberBought = totalNumber;

    }

    public void output()

    {

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

        System.out.println("at " + groupCount + " for $" + groupPrice);

    }

     private String name;

    private int groupCount;

    private double groupPrice;

    private int numberBought;

}


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.99Eggs: 12 for $1.69Apples: 3 for $1.00Watermelons: $4.39 eachBagels: 6 for $3.50Also 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 oranges3 dozen eggs20 apples2 watermelons1 dozen bagelsYou...
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 program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
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 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 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
Design and implement the class Day that implements the day of the week in a program....
Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day: Set the day. Print the day. Return the day. Return the next day. Return the previous day. Calculate and return the day by adding certain days to the current day. For example, if the current day...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT