Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // Creature & Herd
- // Spec: In this lab you will complete missing methods. A Creature has been almost fully implemented and a Herd is an ArrayList
- // of Creatures. HerdRunner is your client program.
- // ArrayList of User-defined Classes
- public class Creature implements Comparable
- {
- private int size;
- public Creature(int girth)
- {
- size = girth;
- }
- public void setSize(int girth)
- {
- size = girth;
- }
- /**
- * method isBig should return true if size > 75
- * method isBig should return false if size <= 75
- */
- public boolean isBig()
- {
- if (size > 75)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public boolean equals(Object obj)
- {
- Creature other = (Creature)obj;
- if(size == other.size)
- {
- return true;
- }
- return false;
- }
- public int compareTo(Object obj)
- {
- Creature other = (Creature)obj;
- if(size > other.size)
- {
- return 1;
- }
- else if(size < other.size)
- {
- return -1;
- }
- return 0;
- }
- public String toString()
- {
- return "" + size;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Creature & Herd
- // Spec: In this lab you will complete missing methods. A Creature has been almost fully implemented and a Herd is an ArrayList
- // of Creatures. HerdRunner is your client program.
- // ArrayList of User-defined Classes
- import java.util.Collections;
- import java.util.List;
- import java.util.ArrayList;
- public class Herd
- {
- private List<Creature> list;
- public Herd()
- {
- // point list to a new ArrayList (instantiate list)
- list = new ArrayList<Creature>();
- }
- public void add(int creatureSize)
- {
- // add a new Creature to the list with the size given
- list.add(new Creature(creatureSize));
- }
- /* method countBigOnes should return the count of
- big creatures - use the isBig() Creature method
- */
- public int countBigOnes()
- {
- int x = 0;
- for (int i = 0; i < list.size(); i++)
- {
- if (list.get(i).isBig())
- {
- x++;
- }
- }
- return x;
- }
- public String toString()
- {
- return "" + list;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Creature & Herd
- // Spec: In this lab you will complete missing methods. A Creature has been almost fully implemented and a Herd is an ArrayList
- // of Creatures. HerdRunner is your client program.
- // ArrayList of User-defined Classes runner
- import java.util.ArrayList;
- import java.util.Collections;
- public class HerdRunner
- {
- public static void main (String args[])
- {
- Herd bunch = new Herd();
- bunch.add(33);
- bunch.add(115);
- bunch.add(16);
- bunch.add(83);
- bunch.add(45);
- bunch.add(96);
- System.out.println(bunch);
- System.out.println("Big One Count = " + bunch.countBigOnes());
- }
- }
- /*
- [33, 115, 16, 83, 45, 96]
- Big One Count = 3
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement