The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Need (a lot of) help with a Java assignment.

SpeedySwafSpeedySwaf Registered User regular
edited April 2007 in Help / Advice Forum
I'm sorry if this ends up being a lot to ask, but I've been working on this java assignment I need help with. I got most of the essentials down, but I'm still having some problems with it. For the sake of completness, I'm going to paste the intsructions we were given, plus everything I've written so far.

First, the directions:
You are a keen collector of small animal figures, manufactured by FurryFigure pottery and you wish to keep track of your collection. Define an abstract class, FurryFigure, that has attributes of description (“Green, with tail curled around it”, etc.) and the weight of the figure in ounces. You should write a default constructor and a constructor that will take values for all of the above attributes. Checks should be made to insure that the weight is > 0. If the weight is <= 0 an Exception should be thrown. This class should also have an abstract method named getType. Put as much code in this class as possible to take advantage of inheritance.

Write classes named Cat, Dog and Pig that are subclasses of the FurryFigure class.

Develop a class, FurryFigureCollection, that uses an ArrayList to keep track of your FurryFigure objects. Its public interface should include the following methods:
public void addToCollection(FurryFigure figure)
public void printDescriptions(String animalType)
public void printInWeightOrder()
public int getTotalWeight()
public Vector findAll(String searchString)

The addToCollection method adds FurryFigure objects to the collection.

The printDescriptions method should print the descriptions of all animals of the given type, for instance,

PrintDescriptions(“Pig”)

Prints the descriptions of all Pigs. The animalType should not be case-sensitive.

The printInWeightOrder method should first sort the ArrayList using the Collections.sort method and then display each object in the ArrayList using an Iterator. Remember to do this you must implement the Comparable interface in the FurryFigure class.

The getTotalWeight method should return the total weight for the entire collection as a number of pounds. (1 pound = 16 ounces) This needs to be a real value such as 1.75 pounds.

The findAll method should return a new ArrayList object containing references to all FurryFigure objects in the collection whose descriptions include the given word, which is case-insensitive. For instance,
FindAll(“tail”)
Will find all objects with the word “tail” somewhere in their descriptions. (You might like to use the indexOf methods of the String class to help with this)

Write a main method that will do the following:

Instantiate a FurryFigureCollection object.
Check to see if a file named Assign3Input.dat exists. If it does, (it is a binary file that was created previously by this program) read in the FurryFigure objects and place them in the ArrayList using the methods in the FurryFigureCollection class if need be.
The main method should then display the following menu.
Add Furry Figure
Print Descriptions
Print the FurryFigure Objects in Weight Order
Display Weight
Find Descriptions
Write to Binary File
Quit

If the Add Furry Figure option is selected the program should prompt for the Type, Description and Weight. Based on the description instantiate the correct FurryFigure(Cat, Dog, Pig) object. Be sure to capture the negative weight exception and if this occurs reprompt for the weight. Once valid input is entered add the object to the FurryFigureCollection using the addToCollection method.

If the Print Descriptions option is selected, the user should be prompted for the type of Furry Figure and then the printDescriptions method should be called to display the objects in the collection with the specified type.

If the Print the FurryFigure Objects in Weight Order option is selected, the printInWeightOrder method should be called..


If the Display Weight option is selected the weight of your collection should be displayed in pounds.

If the Find Descriptions is selected, the user should be prompted for a word to search for in your collection’s descriptions and display each member of the collection returned from the findAll method.

If the Write to Binary File option is selected, the program should open the Assign3Input.dat file as a binary file and write all of the FurryFigure objects to the file.

Below is the FurryFigure class. I'm pretty sure everything here is in order, so you shouldn't have to worry about it.
public abstract class FurryFigure implements Comparable {
	private String description;
	private double weight;

	/**
	 * Sets default parameters for figurines
	 */
	public FurryFigure(){
		description = null;
		weight = 0.0;
	}

	/**
	 * Returns figurine description
	 */
	public String getDescription() {
		return this.description;
	}

	/**
	 * Returns figurine weight
	 */
	public double getWeight() {
		return this.weight;
	}

	/**
	 * Sets the description for the figurine
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * Sets the weight of figurines. Throws exception if weight is false
	 */
	public void setWeight(double weight) throws Exception {
		if (this.weight <= 0)
			throw new Exception("Weight can not be zero or less!");
		else
			this.weight = weight;
	}

	/**
	 * Abstract method that finds the animal type
	 */
	public abstract String getType();

	/**
	 * Compares the weights of the animal figurines
	 */	
	public double compareTo(Object otherObject){
		FurryFigure other = (FurryFigure)otherObject;
		if (weight < other.weight)
			return -1;
		else if (weight > other.weight)
			return 1;
		else
			return 0;
	}
}

*Note: The Dog and Pig classes are exactly the same, just with the appropiate words replaced.
public class Cat extends FurryFigure {
	private final static String type = "Cat";

	/**
	 * Returns the type "Cat"
	 */
	public String getType() {
		return type;
	}
}

Below is the FurryFigureCollection. Once again, for the most part it's done, but I've bolded the problematic portion that's supposed to search through the descriptions for specific words. Not really sure how to word it...
import java.util.*;
public class FurryFigureCollection {
	ArrayList<FurryFigure> figureList = new ArrayList<FurryFigure>();
	
	/**
	 * Adds the figure to the figureList
	 */
	public void addToCollection(FurryFigure figure){
		figureList.add(figure);
	}
	
	/**
	 * Prints the description belonging to an animal type
	 */
	public void printDescription(String animalType){
		Iterator<FurryFigure> i = figureList.iterator();
		while (i.hasNext())
			if (animalType == i.next().getType())
				System.out.println(i.next());
	}

	/**
	 * Sorts the figurine list and prints them in order
	 */
	public void printInWeightOrder(){
		Collections.sort(figureList);
		Iterator<FurryFigure> i = figureList.iterator();
		while (i.hasNext())
			System.out.println(i.next());
	}

	/**
	 * Adds together the weight of all figurines, and converts it to pounds
	 */
	public double getTotalWeight(){
		double totalWeight = 0;
		Iterator<FurryFigure> i = figureList.iterator();
		while (i.hasNext())
			totalWeight += i.next().getWeight();
		return totalWeight/16.0;
	}

	/**
	 * Searches the descriptions of the figurine for one that contains the searchString
	 */
	[b]public Vector findAll(String searchString){
		//Vector<FurryFigure> = new Vector[/b]
	}
}

Finally, the main class. Again, bolded the problematic stuff: I'm trying to code it so that I can set the types, descriptions and weights for the figurines (bolded). There's also the part about reading it into a binary file, but I think I can figure that out later tonight.
import java.util.*;
import java.io.*;
public class Main {
	public static void main(String[] args) {
		FurryFigureCollection object = new FurryFigureCollection();
		Scanner keyboard = new Scanner(System.in);
		int choice;
		do{

			/**
			 * Interface for user
			 */
			System.out.println("1. Add Furry Figure");
			System.out.println("2. Print Descriptions");
			System.out.println("3. Print the FurryFigure Objects in Weight Order");
			System.out.println("4. Display Weight");
			System.out.println("5. Find Description");
			System.out.println("6. Write to Binary File");
			System.out.println("7. Quit");
			choice = keyboard.nextInt();
			switch (choice){

			/**
			 * Allows the user to input information about the figurines
			 */
			case 1:
				[b]System.out.println("Enter the animal type.");
				String inputType = keyboard.next();
				System.out.println("Enter the description.");
				String inputDescription = keyboard.next();
				System.out.println("Enter the weight.");
				double inputWeight = keyboard.nextDouble();[/b]
				break;

				/**
				 * Returns the description the a chosen animal figurine
				 */
			case 2:
				System.out.println("Which description do you want to see?");
				String descriptionChoice = keyboard.next();
				object.printDescription(descriptionChoice);
				break;

				/**
				 * Prints the figurines in weight order, from lightest to heaviest
				 */
			case 3:
				object.printInWeightOrder();
				break;

				/**
				 * Returns the total weight of the figurines in pounds.
				 */
			case 4:
				object.getTotalWeight();
				break;

				/**
				 * Searches all figurine descriptions, and then returns a description matching the input word
				 */
			case 5:
				System.out.println("Which word are you looking for?");
				String inputSearch = keyboard.next();
				object.findAll(inputSearch);
				break;

				/**
				 * Writes the figures into a binary file
				 */
			case 6:
//				 Write each of the pet objects in the list to a binary file.
				String filename = "Assign3Input.dat";
				ObjectOutputStream out = null;
				try{
					out = new ObjectOutputStream(new FileOutputStream(filename));
				}
				catch (IOException e){
					System.out.println(e.getMessage());
				}
				

				break;

				/**
				 * Cancels program and extends a thank you message
				 */
			case 7:
				System.out.println("Thank you!");
				break;

				/**
				 * Prints error message if choice doesn't match interface
				 */
			default:
				System.out.println("That is not a valid option, try again.");
				break;
			}
		}while (choice != 7);
	}
}
I'm sorry if this is all too much.

Another problem I've been having is that for the ArrayList<FurryFigure> part, it tells me "parameterized types are only avaiable if source code 5.0", which I suppose means the Java I have on my computer is outdated. I try the java website, but it tells me I have to register and everything before I can download the source code. Do I have to do that to get 5.0, or am I missing something obvious?

Thank you.

SpeedySwaf on

Posts

  • Mr.FragBaitMr.FragBait Registered User regular
    edited April 2007
    SpeedySwaf wrote: »

    Another problem I've been having is that for the ArrayList<FurryFigure> part, it tells me "parameterized types are only avaiable if source code 5.0", which I suppose means the Java I have on my computer is outdated. I try the java website, but it tells me I have to register and everything before I can download the source code. Do I have to do that to get 5.0, or am I missing something obvious?

    Thank you.

    Yeah, I'm not going to go through all that, but I can help you with incompatibilities.
    Java 6.0

    Second off, surround your code with [ CODE] [ /CODE] (without spaces in the brakets) so that it's formatted and readable.

    Third off, I would advise asking for help with specific tasks and errors. Don't worry about giving all your problems to us at once, just reply when you have another question. What are you having the most trouble with?

    Mr.FragBait on
  • SpeedySwafSpeedySwaf Registered User regular
    edited April 2007
    Thanks for replying.

    To start off, I fixed the source code thing. For some reason my compiler was set at 1.4 instead of 5.0. So now I can compile the code all I want.

    Now, as for specific questions? Well, let's start with a simple one: how would I go about setting up:
    public Vector findAll(String searchString){
            }
    
    ?

    SpeedySwaf on
  • Mr.FragBaitMr.FragBait Registered User regular
    edited April 2007
    SpeedySwaf wrote: »
    Thanks for replying.

    To start off, I fixed the source code thing. For some reason my compiler was set at 1.4 instead of 5.0. So now I can compile the code all I want.

    Now, as for specific questions? Well, let's start with a simple one: how would I go about setting up:
    public Vector findAll(String searchString){
            }
    
    ?
    /**
            * Searches the descriptions of the figurine for one that contains the searchString
            * 
            * The findAll method should return a new ArrayList object containing references to 
            * all FurryFigure objects in the collection whose descriptions include the given 
            * word, which is case-insensitive. For instance, FindAll(&#8220;tail&#8221;) Will find all 
            * objects with the word &#8220;tail&#8221; somewhere in their descriptions. (You might like to 
            * use the indexOf methods of the String class to help with this)
            * 
            */
            public Vector findAll(String searchString){
                    //what do you have to do? RETURN WHAT?
            	
            		//okay, we'll have to look at all of this collection for figures with searchString
            	
            		//is there an easy way to look at each Object in this collection? look at 
            		//ArrayList's javadoc if you don't know
            	
            		//if you are looking at one object in this collection, you gotta find what?
            	
            		//what action will you do if it does contain the right thing?
            	
            		//what action if it doesn't?
            	
            		//once you've looked through all of it, anything else you need to do?
            		//(possibly rhetorical, just go through the whole process in your head to be sure)
            }
    

    this helpful?

    EDIT: I hope I'm not confusing you with "RETURN WHAT?", just saying that at the beginning of the method, think about what you need to return and when you need to initialize it.

    Mr.FragBait on
  • PheezerPheezer Registered User, ClubPA regular
    edited April 2007
    You're not asking for help finishing your homework, you're pretty much asking people to DO your homework for you. If you can't figure it out, you can post specific questions about Java, but you're not going to post the whole damn assignment, some skeleton code, and ask people to tell you what to do.

    Pheezer on
    IT'S GOT ME REACHING IN MY POCKET IT'S GOT ME FORKING OVER CASH
    CUZ THERE'S SOMETHING IN THE MIDDLE AND IT'S GIVING ME A RASH
This discussion has been closed.