Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Name: Sammy Samkough
- //Prog: Toy & ToyStore
- //Spec: Completing methods for a Toy & ToyStore class that consists of an ArrayList of Toy objects.
- // The Toy class stores a Toy name and a count of how many exist in the store.
- public class Toy
- {
- private String name;
- private int count; // how many toys exist
- /** creates a generic toy named 'toy' with a count of 1 */
- public Toy()
- {
- name = "toy";
- count = 1;
- }
- /** assigns the name given with a count of 1*/
- public Toy(String nm)
- {
- name = nm;
- count = 1;
- }
- public int getCount()
- {
- return count;
- }
- public void setCount(int cnt)
- {
- count = cnt;
- }
- public String getName()
- {
- return name;
- }
- public void setName(String nm)
- {
- name = nm;
- }
- public String toString()
- {
- return name + " " + count;
- // return "Name: " + name + "\n" + "Count: " + String.valueOf(count) + "\n";
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- //Name: Sammy Samkough
- //Prog: Toy & ToyStore
- //Spec: Completing methods for a Toy & ToyStore class that consists of an ArrayList of Toy objects.
- // The Toy class stores a Toy name and a count of how many exist in the store.
- public class ToyRunner
- {
- public static void main(String[] args)
- {
- Toy t = new Toy("sorry");
- System.out.println(t);
- t = new Toy("ji goe");
- t.setCount(5);
- System.out.println(t);
- }
- }
- /*
- sorry 1
- ji goe 5
- Press any key to continue . . .
- */
- -------------------------------------------------------------------------------------------------------------------------------
- //Name: Sammy Samkough
- //Prog: Toy & ToyStore
- //Spec: Completing methods for a Toy & ToyStore class that consists of an ArrayList of Toy objects.
- // The Toy class stores a Toy name and a count of how many exist in the store.
- import java.util.Scanner;
- import java.util.List;
- import java.util.ArrayList;
- import java.util.Collections;
- public class ToyStore
- {
- private ArrayList<Toy> toyList;
- public ToyStore()
- {
- toyList = new ArrayList<Toy>();
- }
- /** Load toys takes a String such as "Clue Clue Clue Sorry Sorry"
- * It will parse this String into an array of Strings and then either:
- * a) Add that toy if it does not yet exist in the toyList or
- * b) Increment the count for that toy by one
- */
- public void loadToys(String toys)
- {
- for (String name : toys.split(" "))
- {
- Toy t = getThatToy(name);
- if(t == null)
- {
- toyList.add(new Toy(name));
- }
- else
- {
- t.setCount(t.getCount() + 1);
- }
- }
- }
- /** Searches toyList for a toy with the same name as the parameter nm
- * If found - returns that toy, otherwise returns null
- */
- public Toy getThatToy(String nm)
- {
- int size = toyList.size();
- for(int i = 0; i < size; i++)
- {
- if(toyList.get(i).getName().equals(nm))
- {
- return toyList.get(i);
- }
- }
- return null;
- }
- /** Returns the name of the toy that occurs the most frequently in toyList */
- public String getMostFrequentToy()
- {
- int max = toyList.get(0).getCount();
- String name = toyList.get(0).getName();
- int size = toyList.size();
- for(int i = 0; i < size; i++)
- {
- if(toyList.get(i).getCount() > max)
- {
- max = toyList.get(i).getCount();
- name = toyList.get(i).getName();
- }
- }
- return name;
- }
- public void sortToysByCount()
- {
- ArrayList<Toy> t = new ArrayList<Toy>();
- int count = 0;
- int size = toyList.size();
- for(int i = size; i > 0; i--)
- {
- t.add(new Toy(getMostFrequentToy()));
- t.get(count).setCount(getThatToy(getMostFrequentToy()).getCount());
- toyList.remove(getThatToy(getMostFrequentToy()));
- count++;
- }
- toyList = t;
- }
- public String toString()
- {
- return "" + toyList;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- //Name: Sammy Samkough
- //Prog: Toy & ToyStore
- //Spec: Completing methods for a Toy & ToyStore class that consists of an ArrayList of Toy objects.
- // The Toy class stores a Toy name and a count of how many exist in the store.
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.Collections;
- public class ToyStoreRunner
- {
- public static void main(String args[])
- {
- ToyStore s = new ToyStore();
- // first load
- s.loadToys("sorry bat sorry sorry sorry train train teddy teddy ball ball"); // loads the toys
- System.out.println(s); // prints out load
- System.out.println("\nMost Frequent Toy: " + s.getMostFrequentToy()); // prints out most frequent toy
- System.out.println("\nSorted: "); // prints out the "Sorted: "
- s.sortToysByCount(); // sorts the toys
- System.out.println(s); // prints out the sorted toys
- System.out.println("\n--------------------------------------------------------------------------------");
- // second load
- s.loadToys("yahtzee monopoly candyland monopoly yahtzee candyland");
- System.out.println(s);
- System.out.println("\nMost Frequent Toy: " + s.getMostFrequentToy());
- System.out.println("\nSorted: ");
- s.sortToysByCount();
- System.out.println(s);
- System.out.println("\n--------------------------------------------------------------------------------");
- // third load
- s.loadToys("operation operation boggle scrabble clue life clue clue");
- System.out.println(s);
- System.out.println("\nMost Frequent Toy: " + s.getMostFrequentToy());
- System.out.println("\nSorted: ");
- s.sortToysByCount();
- System.out.println(s);
- System.out.println("\n--------------------------------------------------------------------------------");
- }
- }
- /*
- [sorry 4, bat 1, train 2, teddy 2, ball 2]
- Most Frequent Toy: sorry
- Sorted:
- [sorry 4, train 2, teddy 2, ball 2, bat 1]
- --------------------------------------------------------------------------------
- [sorry 4, train 2, teddy 2, ball 2, bat 1, yahtzee 2, monopoly 2, candyland 2]
- Most Frequent Toy: sorry
- Sorted:
- [sorry 4, train 2, teddy 2, ball 2, yahtzee 2, monopoly 2, candyland 2, bat 1]
- --------------------------------------------------------------------------------
- [sorry 4, train 2, teddy 2, ball 2, yahtzee 2, monopoly 2, candyland 2, bat 1, o
- peration 2, boggle 1, scrabble 1, clue 3, life 1]
- Most Frequent Toy: sorry
- Sorted:
- [sorry 4, clue 3, train 2, teddy 2, ball 2, yahtzee 2, monopoly 2, candyland 2,
- operation 2, bat 1, boggle 1, scrabble 1, life 1]
- --------------------------------------------------------------------------------
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement