Results 1 to 6 of 6
  1. #1
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default Tree Set question

    Good afternoon everyone.

    I have a question on the tree set data structures, and if this is for the advanced forum, I apologize.

    I am not looking for answers, just hints on what I should do. I am supposed to:


    Please use the class TreeSet with comparator(s) to complete the following task:
    Design an abstract class called “Vehicle” which has members:

    String color
    Double speed
    String nickname

    and an abstract method:

    public abstract double getNumberOfWheels();


    I am stuck on the Tree Set part. Below is the code I have created, but i am not certain as to what should be used. I have played around with it, but I could not get any information from the book on what exactly to use. Hints?

    import java.util.*;
    import java.awt.Color;
    import java.util.Comparator;
    import java.util.TreeSet;


    This is one option I am thinking about doing
    //TreeSet<Vehicle> toset= new TreeSet<Vehicle>(new TreeObjectComparator())


    public abstract class Vehicle<TestObject>
    implements Comparator<TestObject>, java.io.Serializable

    {
    private String color;
    private double speed;
    private String nickname;


    public Vehicle(String color, double speed, String nickname)
    {

    }



    public abstract double getNumberOfWheels();


    This is the other option, put in the Main method
    public void Main(String[] args)
    {
    // TreeSet<String> ts = new TreeSet<String>(new Vehicle());



    Vehicle s1 = new Sedan("WHITE", 101, "SEDAN");
    Vehicle s2 = new Motorcycle("RED", 110, "TWOWHEELS");
    Vehicle s3 = new Truck("GREEN", 65, "FOURWHEELS");
    }

    } // end program

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    First, please use the code tags to make your code more readable.

    As for the task, it seems to be missing something. A TreeSet is a Collection and thus must hold something (which the task posted does not seem to define but I presume is a bunch of Vehicles), but more importantly a TreeSet sorts that something based upon the ordering (defined by the comparator), and how you wish to order is not defined (by speed, nickname, number of wheels, etc...?)

  3. #3
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    Hello doWhile,

    I apologize for the code tags. The assignment is below. My code is below that. I hope that helps - I am supposed to compare vehicles based upon speed, color, etc.

    Please use the class TreeSet with comparator(s) to complete the following task:
    1. Design an abstract class called “Vehicle” which has members:

    String color
    Double speed
    String nickname

    and an abstract method:

    public abstract double getNumberOfWheels();

    2. Design concrete classes “Sedan”, “Motorcycle”, and “Truck” as subclasses of “Vehicle” and output the following information*
    a. The type of the vehicle
    b. The color of the vehicle
    c. The nickname of the vehicle

    In the order of
    a. Speed
    b. Color
    c. Number of Wheels

    For example, if you create:
    (1) Sedan object: nickname “wind” color “white” speed 100
    (2) Sedan object: nickname “nerd” color “red” speed 101
    (3) Motorcycle object: nickname “flash” color “black” speed 120
    (4) Truck object: nickname “grandma” color “yellow” speed 88

    In the order of Speed, you should output information* of (4) (1) (2) (3)
    In the order of Color, you should output information* of (3) (2) (1) (4)
    In the order of Number of Wheels, you should output information* of (3) (1) (2) (4)



    The code I have is :

    Java Code:
    import java.util.*;
    import java.awt.Color;
    import java.util.Comparator;
    import java.util.TreeSet;
    
    
    
    //TreeSet<Vehicle> toset= new TreeSet<Vehicle>(new TreeObjectComparator())
    
    
    public abstract class Vehicle<TestObject> 
    implements Comparator<TestObject>, java.io.Serializable 
    
    {
    	private String color;
    	private double speed;
    	private String nickname;
    	
    	
    	public Vehicle(String color, double speed, String nickname)
    	{
    		
    	}
    	
    	
    	
    	public abstract double getNumberOfWheels();
    	
    	
    	
    	public void Main(String[] args)
    	{
    	// TreeSet<String> ts = new TreeSet<String>(new Vehicle());
    
    		    
    		
           Vehicle s1 = new Sedan("WHITE", 101, "SEDAN");
           Vehicle s2 = new Motorcycle("RED", 110, "TWOWHEELS");
           Vehicle s3 = new Truck("GREEN", 65, "FOURWHEELS");
    	}
    
    }

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    So what is your question?

    I imagine that once you have created a bunch of Vehicle objects you add them to the TreeSet. Since you have to output the Vehicles in different order then you might want to have more than one TreeSet. Mind you placing your objects into a List and sorting that multiple times would be better but that is not what you teacher wants.

  5. #5
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    My original question was where do i put the tree set. In my first post, I was asking where the tree set code portion goes. (I asked that in the code I provided, i.e. is it in the Main method, or do I put it in the class)

    My second question is do I add vehicle objects like I created in my code, or should I do

    t1.add(Sudan), etc. I hope I explained what I am trying to do.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Quote Originally Posted by Adomini View Post
    do I put it in the class
    Well all code goes inside a class. If you specifically mean the Vehicle class then think about the real world. Does the car you drive (or are a passenger in) have a collection of other cars inside it?

    As I alluded to in my previous post you will probably need more than one TreeSet but ask your teacher for clarification. If you do need multiple TreeSets then I would create a Vehicle object once and then add it to as many TreeSets as you need.

Similar Threads

  1. Question with a Tree and Breadth First Search
    By Ing. Balderas in forum New To Java
    Replies: 1
    Last Post: 05-17-2010, 05:46 AM
  2. Replies: 0
    Last Post: 04-04-2010, 07:40 AM
  3. 2-4 tree
    By number22 in forum New To Java
    Replies: 3
    Last Post: 12-09-2009, 03:52 PM
  4. Creating a Tree and then saving the Tree
    By jackmatt2 in forum New To Java
    Replies: 0
    Last Post: 08-22-2009, 12:51 PM
  5. Need Help With Tree Adt
    By avi in forum New To Java
    Replies: 0
    Last Post: 03-20-2008, 03:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •