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
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];            ...
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...
public class Auto { private String make; private String model; private int year; } a) write...
public class Auto { private String make; private String model; private int year; } a) write a default constructor for the Auto Class n) Write a constructor to initialize all instance variables c) write accessor and mutator for make variable d) create an onject name it myAuto with values of Toyota, Camry, and 2016
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT