Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Name: Sammy Samkough
- // Prog: Monster
- // Spec: In this project, you will be completing methods for a Monster class. Because the Monster class implements Monsterable,
- // you will need to implement each of the methods as shown in the Monsterable interface. Monsterable contains accessor
- // and modifier methods, along with equals(), compareTo, clone() and toString() methods.
- public class Monster implements Monsterable
- {
- private int myHeight;
- private int myWeight;
- private int myAge;
- // write a default Constructor
- public Monster()
- {
- myHeight = 0;
- myWeight = 0;
- myAge = 0;
- }
- // write an initialization constructor with an int parameter ht
- public Monster(int h)
- {
- myHeight = h;
- }
- // write an initialization constructor with int parameters ht and wt
- public Monster(int h, int w)
- {
- myHeight = h;
- myWeight = w;
- }
- // write an initialization constructor with int parameters ht, wt, and age
- public Monster(int h, int w, int a)
- {
- myHeight = h;
- myWeight = w;
- myAge = a;
- }
- public void setMonster(int h, int w, int a)
- {
- myHeight = h;
- myWeight = w;
- myAge = a;
- }
- // modifiers - write set methods for height, weight, and age
- public void setHeight(int h)
- {
- myHeight = h;
- }
- public void setWeight(int w)
- {
- myWeight = w;
- }
- public void setAge(int a)
- {
- myAge = a;
- }
- // accessors - write get methods for height, weight, and age
- public int getHeight()
- {
- return myHeight;
- }
- public int getWeight()
- {
- return myWeight;
- }
- public int getAge()
- {
- return myAge;
- }
- // creates a new copy of this Object
- public Object clone()
- {
- return new Monster(myHeight, myWeight, myAge);
- }
- // returns true if height, weight and age are equal, otherwise false
- public boolean equals(Object obj)
- {
- Monster m = (Monster)obj;
- if (myHeight == m.myHeight && myWeight == m.myWeight && myAge == m.myAge)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public int compareTo(Object obj)
- {
- Monster rhs = (Monster)obj;
- // height
- if (myHeight > rhs.myHeight)
- {
- return 1;
- }
- else if (myHeight < rhs.myHeight)
- {
- return -1;
- }
- // weight
- else if (myWeight > rhs.myWeight)
- {
- return 1;
- }
- else if (myWeight < rhs.myWeight)
- {
- return -1;
- }
- // age
- else if (myAge > rhs.myAge)
- {
- return 1;
- }
- else if (myAge < rhs.myAge)
- {
- return -1;
- }
- else
- {
- return 0;
- }
- }
- // write a toString() method
- public String toString()
- {
- String result = "";
- result += "The height of the monster is " + myHeight + ".\n";
- result += "The weight of the monster is " + myWeight + ".\n";
- result += "The age of the monster is " + myAge + ".\n";
- return result;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Name: Sammy Samkough
- // Prog: Monster
- // Spec: In this project, you will be completing methods for a Monster class. Because the Monster class implements Monsterable,
- // you will need to implement each of the methods as shown in the Monsterable interface. Monsterable contains accessor
- // and modifier methods, along with equals(), compareTo, clone() and toString() methods.
- public interface Monsterable
- {
- /** sets the height, weight and age for this Monster*/
- public void setMonster(int ht, int wt, int age);
- /** sets the height for this Monster */
- public void setHeight(int ht);
- /** sets the weight for this Monster */
- public void setWeight(int wt);
- /** sets the age for this Monster */
- public void setAge(int age);
- /** gets the height for this Monster */
- public int getHeight();
- /** gets the weight for this Monster */
- public int getWeight();
- /** gets the age for this Monster */
- public int getAge();
- /** creates a new copy of this Object */
- public Object clone();
- /** returns true if height, weight and age are equal, otherwise false */
- public boolean equals(Object o);
- /** returns a value based on the criteria of height, weight and age */
- public int compareTo(Object o);
- /** displays height, weight and age for this Monster */
- public String toString();
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Name: Sammy Samkough
- // Prog: Monster
- // Spec: In this project, you will be completing methods for a Monster class. Because the Monster class implements Monsterable,
- // you will need to implement each of the methods as shown in the Monsterable interface. Monsterable contains accessor
- // and modifier methods, along with equals(), compareTo, clone() and toString() methods.
- public class MonsterRunner
- {
- public static void main(String[] args)
- {
- Monster zero = new Monster();
- Monster one = new Monster(8);
- Monster sam = new Monster(9, 4);
- Monster harry = new Monster(1, 2, 3);
- System.out.println("\nMonster zero: \n" + zero);
- System.out.println("\nMonster one: \n" + one);
- System.out.println("\nMonster sam: \n" + sam);
- System.out.println("\nMonster harry: \n" + harry);
- System.out.println("\nChanging harry's properties... ");
- harry.setHeight(7);
- harry.setWeight(6);
- harry.setAge(5);
- System.out.println("\nMonster harry: \n" + harry);
- System.out.println("\nCloning sam into harry...");
- sam = (Monster)harry.clone();
- System.out.println("\nMonster sam: \n" + sam);
- Monster mOne = new Monster(33,33,11);
- Monster mTwo = new Monster(55,33,11);
- System.out.println("\nMonster 1: \n" + mOne);
- System.out.println("\nMonster 2: \n" + mTwo);
- System.out.print("\nmOne.equals(mTwo) = ");
- System.out.println(mOne.equals(mTwo));
- System.out.print("\nmOne.compareTo(mTwo) = ");
- System.out.println(mOne.compareTo(mTwo));
- System.out.print("\nmTwo.compareTo(mOne) = ");
- System.out.println(mTwo.compareTo(mOne));
- }
- }
- /*
- Monster zero:
- The height of the monster is 0.
- The weight of the monster is 0.
- The age of the monster is 0.
- Monster one:
- The height of the monster is 8.
- The weight of the monster is 0.
- The age of the monster is 0.
- Monster sam:
- The height of the monster is 9.
- The weight of the monster is 4.
- The age of the monster is 0.
- Monster harry:
- The height of the monster is 1.
- The weight of the monster is 2.
- The age of the monster is 3.
- Changing harry's properties...
- Monster harry:
- The height of the monster is 7.
- The weight of the monster is 6.
- The age of the monster is 5.
- Cloning sam into harry...
- Monster sam:
- The height of the monster is 7.
- The weight of the monster is 6.
- The age of the monster is 5.
- Monster 1:
- The height of the monster is 33.
- The weight of the monster is 33.
- The age of the monster is 11.
- Monster 2:
- The height of the monster is 55.
- The weight of the monster is 33.
- The age of the monster is 11.
- mOne.equals(mTwo) = false
- mOne.compareTo(mTwo) = -1
- mTwo.compareTo(mOne) = 1
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement