I need help with this Java program. Given partial program public class coffee {
ID: 3572873 • Letter: I
Question
I need help with this Java program.
Given partial program
public class coffee
{
private String name;
private String country;
private int body;
private double caffine_index;
private char grade;
public coffee(String n, String c, int b, double ci, char grade)
{
}
Remaining mutators and accessors
} end class coffee
1. Write a Boolean method to be included in the coffee class to compare two instances of coffee for equality. Assume two coffees can be equal if all fields with the exception of name are the same.
2. Write a method of coffee type which will copy the current instance of the coffee class with a new name which will be passed to the method.
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class coffee
{
private String name;
private String country;
private int body;
private double caffine_index;
private char grade;
public coffee(String name, String country, int body, double caffine_index, char grade) //constructor
{
this.name = name;
this.country = country;
this.body = body;
this.caffine_index = caffine_index;
this.grade = grade;
}
public void setName(String name) //accessors and mutators for all variables
{
this.name = name;
}
public String getName()
{
return name;
}
public void setCountry(String country)
{
this.country = country;
}
public String getCountry()
{
return country;
}
public void setBody(int body)
{
this.body = body;
}
public int getBody()
{
return body;
}
public void setCaffine_Index(double caffine_index)
{
this.caffine_index = caffine_index;
}
public double getCaFfine_Index()
{
return caffine_index;
}
public void setGrade(char grade)
{
this.grade = grade;
}
public char getGrade()
{
return grade;
}
public boolean compare(coffee c2) //function to compare two coffee
{
if(this.country==c2.country && this.body == c2.body && this.caffine_index == c2.caffine_index && this.grade == c2.grade)
return true;
else
return false;
}
public coffee copy(coffee c) //function to copy coffee c to object calling it
{
this.name = c.name;
this.country = c.country;
this.body = c.body;
this.caffine_index = c.caffine_index;
this.grade = c.grade;
return this;
}
}
class CoffeeTest
{
public static void main (String[] args)
{
coffee c1 = new coffee("Espresso","USA",3,2.34,'a');
coffee c2 = new coffee("cappuccino","USA",4,3.24,'b');
if(c1.compare(c2))
System.out.println("Both coffee are same");
else
System.out.println("The coffee are different");
c1.copy(c2);
if(c1.compare(c2))
System.out.println("Both coffee are same");
else
System.out.println("The coffee are different");
}
}
output: