write a class called party.... A catering program uses a class named \"Party\" t
ID: 3687410 • Letter: W
Question
write a class called party....
A catering program uses a class named "Party" to keep track of information about a party. The class has the following two private instance variables: private String name;//Represents the name of the party private double cost;//Represents the cost of the party in dollars Define the following Party class methods (you don't to have to define the class). All the methods are non- static unless otherwise specified. Constructor - Takes a String reference and a double as parameters. The current object is initialized with a copy of the parameters' values. Copy Constructor - Define the appropriate copy constructor for this class. This method produces a new object containing a copy of the original object's instance variables. different - Takes a Party object reference as a parameter. It returns true if the current object and the parameter represent different parties and false otherwise. Two parties are considered different if they have different names. This method is case sensitive, i.e., "a" and "A" are considered different. merge - Static method that takes two Party object references as parameters. The method returns a new object that represents the merging of two parties. The new object will have as its name the concatenation of the parameters' names, the first parameter followed by the second parameter, separated by a The cost of the new object will be the sum of the parties' costs. merge - Non-static method that takes as parameter a Party object reference. The method returns a new object that represents the merging of the current object and the parameter, in that order. You must implement this method using the static merge method previously described. The current object may not be modified. incrementCost - This method takes a double as a parameter. The method increases the cost of the current object by the parameter value and returns a reference to the current object. The current object is modified. No new object is created.Explanation / Answer
/**
* @author Srinivas Palli
*
*/
public class Party {
private String name;
private double cost;
/**
* @param name
* @param cost
*/
public Party(String name, double cost) {
this.name = name;
this.cost = cost;
}
/**
* copy constructor
*
* @param p
*/
public Party(Party p) {
this.name = p.name;
this.cost = p.cost;
}
/**
* check both are different or not
*
* @param p
* @return
*/
public boolean different(Party p) {
if (this.name.equals(p.name))
return false;
else
return true;
}
/**
* merge two party objects
*
* @param p1
* @param p2
* @return
*/
public static Party merge(Party p1, Party p2) {
Party p = new Party(p1.name + p2.name, p1.cost + p2.cost);
return p;
}
/**
* returns new party object by adding p1
*
* @param p1
* @return
*/
public Party merge(Party p1) {
return merge(this, p1);
}
/**
* increment cost
*
* @param cost
*/
public void incrementCost(double cost) {
this.cost += cost;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.