write two definitions of a boolean method called equals() . The method compares
ID: 3862476 • Letter: W
Question
write two definitions of a boolean method called equals(). The method compares the instance variables of the class for equality. One is in the Purchase class and the other is a static method of the main. Give sample calls for each.
______________
the purchase class is:
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; //Number of items bought.
public void setName(String newName)
{
name = newName;
}
/**
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;
}
/**
Reads from keyboard the price and number of a purchase.
*/
public void readInput( )
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter name of item you are purchasing:");
name = keyboard.nextLine( );
System.out.println("Enter price of item as two numbers.");
System.out.println("For example, 3 for $2.99 is entered as");
System.out.println("3 2.99");
System.out.println("Enter price of item as two numbers, 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 as two numbers.");
System.out.println("For example, 3 for $2.99 is entered as");
System.out.println("3 2.99");
System.out.println("Enter price of item as two numbers, now:");
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( );
}
}
/**
Displays price and number being purchased.
*/
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;
}
}
Explanation / Answer
import java.util.Scanner;
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; //Number of items bought.
public void setName(String newName)
{
name = newName;
}
public boolean equals(Purchase p){
if(this.name.equals(p.name) && this.groupCount == p.groupCount && this.groupPrice==p.groupPrice){
return true;
}
else{
return false;
}
}
public static void main(String a[]){
Purchase p = new Purchase();
Purchase p1 = new Purchase();
p.readInput();
p.writeOutput();
p1.readInput();
p1.writeOutput();
System.out.println("Get unit price: "+p.getUnitCost());
System.out.println("Get total price: "+p.getTotalCost());
System.out.println("Both purchase objects are Equals: "+p.equals(p1));
}
/**
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;
}
/**
Reads from keyboard the price and number of a purchase.
*/
public void readInput( )
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter name of item you are purchasing:");
name = keyboard.nextLine( );
System.out.println("Enter price of item as two numbers.");
System.out.println("For example, 3 for $2.99 is entered as");
System.out.println("3 2.99");
System.out.println("Enter price of item as two numbers, 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 as two numbers.");
System.out.println("For example, 3 for $2.99 is entered as");
System.out.println("3 2.99");
System.out.println("Enter price of item as two numbers, now:");
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( );
}
}
/**
Displays price and number being purchased.
*/
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;
}
}
Output:
Enter name of item you are purchasing:
Apple
Enter price of item as two numbers.
For example, 3 for $2.99 is entered as
3 2.99
Enter price of item as two numbers, now:
3 2.99
Enter number of items purchased:
2
2 Apple
at 3 for $2.99
Enter name of item you are purchasing:
Apple
Enter price of item as two numbers.
For example, 3 for $2.99 is entered as
3 2.99
Enter price of item as two numbers, now:
3 2.99
Enter number of items purchased:
2
2 Apple
at 3 for $2.99
Get unit price: 0.9966666666666667
Get total price: 1.9933333333333334
Apple Apple
3 3
2.99 2.99
Both purchase objects are Equals: true
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.