In: Computer Science
JAVA CODE
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.
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());
}
}
import java.util.Scanner;
/**
Class for the purchase of one kind of item, such as 3
oranges.
Prices are set supermarket style, such as 5 for $1.25.
*/
public class Purchase
{
private String name;
private int groupCount; //Part of a price, like the 2 in //2 for
$1.99.
private double groupPrice; //Part of a 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;
}
}
Code
PurchaseDemo.java
public class PurchaseDemo
{
public static void main(String[] args)
{
Purchase oneSale1 = new Purchase();
Purchase oneSale2 = new Purchase();
oneSale1.readInput();
oneSale1.writeOutput();
oneSale2.readInput();
oneSale2.writeOutput();
if(oneSale1.equals(oneSale2))
System.out.println("Using Class level method:\nBoth are
same");
else
System.out.println("Using Class level method:\nBoth are not
same");
if(equals(oneSale1,oneSale2))
System.out.println("Using static method:\nBoth are same");
else
System.out.println("Using static method:\nBoth are not
same");
//System.out.println("Cost each $" + oneSale.getUnitCost());
//System.out.println("Total cost $" +
oneSale.getTotalCost());
}
public static boolean equals(Purchase obj1,Purchase obj2)
{
if (obj1 == null || obj1==null) {
return false;
}
if (obj1.getClass() != obj2.getClass()) {
return false;
}
if (!obj1.getName().equals(obj2.getName())) {
return false;
}
if (obj1.getNumberBought() != obj2.getNumberBought()) {
return false;
}
if (Double.doubleToLongBits(obj1.getUnitCost()) !=
Double.doubleToLongBits(obj2.getUnitCost())) {
return false;
}
return true;
}
}
Purchase .java class
import java.util.Objects;
import java.util.Scanner;
/**
Class for the purchase of one kind of item, such as 3
oranges.
Prices are set supermarket style, such as 5 for $1.25.
*/
public class Purchase
{
private String name;
private int groupCount; //Part of a price, like the 2 in //2 for
$1.99.
private double groupPrice; //Part of a 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;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Purchase other = (Purchase) obj;
if (!Objects.equals(this.name, other.name)) {
return false;
}
if (this.groupCount != other.groupCount) {
return false;
}
if (Double.doubleToLongBits(this.groupPrice) !=
Double.doubleToLongBits(other.groupPrice)) {
return false;
}
return true;
}
}
output
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.