Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Name: Peter Aydin
- //Prog: Toy.java
- //Spec: Simulates a toy and potential accessor and mutator methods for a "toy"
- public class Toy
- {
- private String name;
- private int count;
- /** creates a generic toy named 'toy' with a count of 1 */
- public Toy()
- {
- name = "";
- 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;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- //Name: Peter Aydin
- //Prog: ToyStore.java
- //Spec: Simulates an ArrayList which represents a Ty 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 names: toys.split(" "))
- {
- Toy t = getThatToy(names);
- if(t==null) //uses the getThatToy method to see if the toy exists, if not then a new toy is made for it
- {
- toyList.add(new Toy(names));
- }
- else //if we find that the toy already existed in the ArrayList, we add 1 to the count and avoid duplication
- {
- 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 )
- {
- for(int i = 0; i<toyList.size(); i++) //I run through a for loop to see if the arrayList has a toy that has the same name as "nm"
- {
- if(toyList.get(i).getName().equals(nm))
- return toyList.get(i);
- }
- return null; //if there are no matches, null is returned instead of an actual Toy
- }
- /** Returns the name of the toy that occurs the most frequently in toyList */
- public String getMostFrequentToy()
- {
- int max = toyList.get(0).getCount(); //by default makes the first index the maximum
- String name = toyList.get(0).getName(); //gets the name of the first index
- for(int i = 0; i<toyList.size();i++)
- {
- if(toyList.get(i).getCount()>max) //checks to see if the current index ("i") is larger than the current max
- {
- max = toyList.get(i).getCount(); //gives a new value to the max variable
- name = toyList.get(i).getName(); //changes the name of the maximum value
- }
- }
- return name;
- }
- /* My approach***
- **I've decided to create a temporary ArrayList called temp
- **I did this because I wanted to use the getMostFrequent method
- **to constantly get the most common toy and add it to the temp arraylist.
- **Then I got rid of the Toy I just recently added to the new list
- **After the loop ends the toyList arrayList should be empty I set toyList
- **I don't want to so I set toyList equal to "temp" to make sure that toyList doesn't
- **stay empty.
- */
- public void sortToysByCount()
- {
- int count = 0; //keeps the current index of the new arrayList for temp
- ArrayList<Toy> temp = new ArrayList<Toy>();
- for(int i = toyList.size(); i>0; i--)
- {
- temp.add(new Toy(getMostFrequentToy()));
- temp.get(count).setCount(getThatToy(getMostFrequentToy()).getCount());
- toyList.remove(getThatToy(getMostFrequentToy()));
- count++; //increments the variable to make sure that we don't get an out of bounds error with the temp arrayList
- }
- toyList = temp; //makes sure that toyList(which is used everywhere else in the class) doesn't actually stay cleared
- }
- public String toString()
- {
- return "" + toyList;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- //Name: Peter Aydin
- //Prog: ToyStoreRunner.java
- //Spec: Tests the ToyStore class
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.Collections;
- public class ToyStoreRunner
- {
- public static void main( String args[] )
- {
- ToyStore store = new ToyStore();
- store.loadToys("sorry bat sorry sorry sorry train train teddy teddy ball ball");
- System.out.println(store);
- System.out.println("\nMost Frequent: " + store.getMostFrequentToy());
- System.out.println("\nAfter Sorting....\n");
- store.sortToysByCount();
- System.out.println(store);
- System.out.println("\n------------------------After adding more toys----------------------------------");
- store.loadToys("yahtzee monopoly candyland monopoly yahtzee candyland");
- System.out.println(store);
- System.out.println("\nMost Frequent: " + store.getMostFrequentToy());
- System.out.println("\nAfter Sorting....\n");
- store.sortToysByCount();
- System.out.println(store);
- System.out.println("\n--------------------------After adding more toys--------------------------------");
- store.loadToys("operation operation boggle scrabble clue life clue clue");
- System.out.println(store);
- System.out.println("\nMost Frequent: " + store.getMostFrequentToy());
- System.out.println("\nAfter Sorting....\n");
- store.sortToysByCount();
- System.out.println(store);
- System.out.println("--------------------------------------------------------------------------------");
- }
- }
- /*
- [sorry 4, bat 1, train 2, teddy 2, ball 2]
- Most Frequent: sorry
- After Sorting....
- [sorry 4, train 2, teddy 2, ball 2, bat 1]
- ------------------------After adding more toys----------------------------------
- [sorry 4, train 2, teddy 2, ball 2, bat 1, yahtzee 2, monopoly 2, candyland 2]
- Most Frequent: sorry
- After Sorting....
- [sorry 4, train 2, teddy 2, ball 2, yahtzee 2, monopoly 2, candyland 2, bat 1]
- --------------------------After adding more toys--------------------------------
- [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: sorry
- After Sorting....
- [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