Question

JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...

JAVA

public class Purchase

{ private String name;

private int groupCount; //Part of price, like the 2 in 2 for $1.99.

private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99.

private int numberBought; //Total number being purchased.

public Purchase ()

{ name = "no name";

groupCount = 0;

groupPrice = 0;

numberBought = 0;

}

public Purchase (String name, int groupCount,

double groupPrice, int numberBought)

{

this.name = name;

this.groupCount = groupCount;

this.groupPrice = groupPrice;

this.numberBought = numberBought;

}

public void setName(String name)

{

this.name = name;

}

/** Sets price to count pieces for $costForCount.

For example, 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;

}

public String getName( )

{

return name;

}

public double getTotalCost( )

{

return ((groupPrice/groupCount)*numberBought);

}

public double getUnitCost( )

{ return (groupPrice/groupCount);

}

public int getNumberBought( )

{ return numberBought;

}

/** Gets price and number being purchased from keyboard. */

public void readInput( )

{

Scanner scan = new Scanner(System.in);

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

name = scan.nextLine( );

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:");

groupCount = scan.nextInt( );

groupPrice = scan.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 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:");

groupCount = scan.nextInt( );

groupPrice = scan.nextDouble( );

}

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

numberBought = Scan.nextInt( );

while (numberBought <= 0)

{//Try again:

System.out.println(

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

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

numberBought = scan.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 Purchase testQuestion(Purchase p1, int newX)

{

newX = 20;

this.setName("pears");

p1.setName("oranges");

p1 = new Purchase("kiwi", 6, 3.00, 3);

Purchase p2 = new Purchase("banana", 5, 4.50, 10);

return p2;

}

}


public class PurchaseDemo

{

public static void main(String[] args)

{

Purchase oneSale = new Purchase( );

oneSale.readInput( );

oneSale.writeOutput( );

System.out.println("Cost each $" + oneSale.getUnitCost( ));

System.out.println("Total cost $" + oneSale.getTotalCost( ));

}

}

4) (8 points) Assume that the class Purchase has get() and set() methods for all the instance variables. Using the class Purchase and the class PurchaseDemo on the top of page 5 as a guide, write a program that uses the Purchase class and the following data as input. The program is to determine which input value generates the highest total cost to the consumer. The output(you may ignore the output associated with the readInput() method) will consist of listing the names of the input items and their total cost and then a line that states the item which cost the most and its cost. You may use integer, double, and String variables as needed but you must use only one object of the Purchase class and a for loop. This question does not use arrays.

Oranges: 10 for 2.99 buy 2 dozen oranges

Eggs; 12 for 1.69 buy 3 dozen eggs

Apples: 3 for 1.00 buy 20 apples

Watermelons: 4.39 each buy 2 watermelons

Bagels; 6 for 3.50 buy 1 dozen bagels

SAMPLE OUTPUT:( this ignores the lines of output generated by readInput())

You bought these five items:

AAAAA $$$$$

BBBBB $$$$$

CCCCC $$$$$

DDDDD $$$$$

EEEEE $$$$$

The most expensive item you bought was XXXXX and it cost $$$$$.

Homework Answers

Answer #1

Here is the solution to the given problem. Solution.java is the main file containing the answer to your question

Purchase.java (given by you)

import java.io.*;
import java.util.Scanner;
class Purchase {
private String name;
private int groupCount; //Part of price, like the 2 in 2 for $1.99.
private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99.
private int numberBought; //Total number being purchased.
public Purchase() {
name = "no name";
groupCount = 0;
groupPrice = 0;
numberBought = 0;
}
public Purchase(String name, int groupCount, double groupPrice, int numberBought) {
this.name = name;
this.groupCount = groupCount;
this.groupPrice = groupPrice;
this.numberBought = numberBought;
}
public void setName(String name) {
this.name = name;
}

/** Sets price to count pieces for $costForCount.
For example, 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;
}

public String getName() {
return name;
}

public double getTotalCost() {
return ((groupPrice / groupCount) * numberBought);
}

public double getUnitCost() {
return (groupPrice / groupCount);
}

public int getNumberBought() {
return numberBought;
}

/** Gets price and number being purchased from keyboard. */

public void readInput() {
Scanner scan = new Scanner(System.in);
System.out.println("Enter name of item you are purchasing:");
name = scan.nextLine();
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:");
groupCount = scan.nextInt();
groupPrice = scan.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 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:");
groupCount = scan.nextInt();
groupPrice = scan.nextDouble();
}
System.out.println("Enter number of items purchased:");
numberBought = scan.nextInt();
while (numberBought <= 0) { //Try again:
System.out.println("Number must be positive. Try again.");
System.out.println("Enter number of items purchased:");
numberBought = scan.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 Purchase testQuestion(Purchase p1, int newX) {
newX = 20;
this.setName("pears");
p1.setName("oranges");
p1 = new Purchase("kiwi", 6, 3.00, 3);
Purchase p2 = new Purchase("banana", 5, 4.50, 10);
return p2;
}
}

Solution.java

public class Solution {
   public static void writeOutput(Purchase oneSale) {
       System.out.println(oneSale.getName() + "\t" + oneSale.getTotalCost());
   }
   public static void main(String[] args) {
       String mostExpensiveName = "";
       double highestTotalCost = 0;
       System.out.println("You bought these five items:");
       // One Purchase object to read input for each iteration of the loop
       Purchase oneSale = new Purchase( );
       for(int i=1; i<=5; i++) {
           oneSale.readInput( );
           writeOutput(oneSale);
           if(oneSale.getTotalCost() > highestTotalCost) {
               highestTotalCost = oneSale.getTotalCost();
               mostExpensiveName = oneSale.getName();
           }
       }
       System.out.println("The most expensive item you bought was "
               + mostExpensiveName + " and it costs "
               + highestTotalCost);
   }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Which method is correct to access the value of count? public class Person { private String...
Which method is correct to access the value of count? public class Person { private String name; private int age; private static int count = 0; } A. private int getCount() {return (static)count;} B. public static int getCount() {return count;} C. public int getCount() {return static count;} D. private static int getCount() {return count;} How can you print the value of count? public class Person { private String name; private int age; private static int count=0; public Person(String a, int...
How do I make this: public class Country {     private String name;     private double area;     private...
How do I make this: public class Country {     private String name;     private double area;     private int population;     public Country(String name, double area, int population) {         this.name = name;         this.area = area;         this.population = population;     }     public double getPopulationDensity() {         return population / area;     }     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     }     public double getArea() {         return area;     }     public void setArea(double area) {         this.area = area;     }     public int getPopulation()...
Write a pseudocode for the following java programs: public class Item {    private String name;...
Write a pseudocode for the following java programs: public class Item {    private String name;    private double cost;    public Item(String name, double cost) {        this.name = name;        this.cost = cost;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public double getCost() {        return cost;    }    public void setCost(double cost) {...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
2. String is an example of an immutable class. The Person class that is started below,...
2. String is an example of an immutable class. The Person class that is started below, is not. Add the method haveBirthday that will increase the person’s age and the getter and setter method for the name variable. public class Person{ private String myName; private int myAge; public Person(String name, int age) { myName = name; myAge = age; } public void printMe() { System.out.println(“Name: “ + myName + “\tAge: “ + myAge); } }
This assignment is an individual assignment. For Questions 1-3: consider the following code: public class A...
This assignment is an individual assignment. For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B()...