Results 1 to 6 of 6
  1. #1
    freswood is offline Member
    Join Date
    Apr 2008
    Posts
    3
    Rep Power
    0

    Default Help with classes

    I have a verrrry limited knowledge of Java. One of the components of an assessed task I have to complete is to create a method which displays the attributes of all objects of the class.

    Here's an example to show you what I mean:
    Class = Car
    Attributes = model, colour

    .... and in a driver class, objects of 'Car' type have been created: car1, car2, car3

    How would you go about creating a method in the Car class which displays the model and colour of each object?

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    What have you done so far?

    regards,
    sukatoa

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by freswood View Post
    How would you go about creating a method in the Car class which displays the model and colour of each object?
    First, welcome to the Java Forums community!

    Please review the FAQ to get yourself acquainted.

    Your answer depends on how you want the model and colour displayed... are you simply outputting to the console? If so, call your method on a Car object and have your method simply return the model and colour via an object or have it output it from within the method.

    See you around!
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  4. #4
    freswood is offline Member
    Join Date
    Apr 2008
    Posts
    3
    Rep Power
    0

    Default

    ^ That's what I did, but I need a method that displays that stuff for all classes (or at least that's what it implies in the wording of the question. So is there a method I can write that does this? Or is the wording misleading and this is not possible?

    So I guess what I'm asking is: can you do something like this:

    getAllCarDetails()
    which prints to screen every car object's colour and model

    rather than this

    car1.getDetails()
    car2.getDetails()...etc

    Thanks for your help :) I really appreciate it

  5. #5
    freswood is offline Member
    Join Date
    Apr 2008
    Posts
    3
    Rep Power
    0

    Default

    Sorry for the bump

    *is getting kinda desperate*

  6. #6
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    First you need to setup methods in each class containing the information you want to return. For example, add this to car1, car2, car3:

    Java Code:
    public String model(){
    		
    String model = "BMW";
    return model;	
    	
    }
    
    public String colour(){
    		
    String colour = "Red";
    return colour;	
    	
    }
    To be able to read between classes you need to create objects of each class.

    For example add this to your Driver class:

    Java Code:
    public static void main(String[] args) {
    
    car1 c1 = new car1();
    car2 c2 = new car2();
    car3 c3 = new car3();
    
    }
    Then you can add System.out.println() to print out the information contained in each classes method:

    Java Code:
    class Driver {
    
    public static void main(String[] args) {
    
    car1 c1 = new car1();
    car2 c2 = new car2();
    car3 c3 = new car3();
    
    System.out.println("Car1 model: " + c1.model);
    System.out.println("Car1 colour: " + c1.colour);
    
    System.out.println("Car2 model: " + c2.model);
    System.out.println("Car2 colour: " + c2.colour);
    
    System.out.println("Car3 model: " + c3.model);
    System.out.println("Car3 colour: " + c3.colour);
    
     }
    }
    Output:

    Java Code:
    Car1 model: BMW
    Car1 colour: Red
    etc etc..

    This seems to be a very common exercise when learning Java. I remember doing it myself so im sure this is exactly what you need. :D
    Last edited by DonCash; 04-21-2008 at 05:11 PM. Reason: spring cleaning..
    Did this post help you? Please me! :cool:

Similar Threads

  1. Classes in graphics
    By CyberFrog in forum New To Java
    Replies: 0
    Last Post: 04-02-2008, 10:11 PM
  2. using elements from other classes
    By Camden in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 08:25 AM
  3. Using a JAR from other classes
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-02-2008, 08:08 PM
  4. When do we use inner classes?
    By cruxblack in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 06:00 PM
  5. EJB, classes Model
    By Felissa in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 07-06-2007, 04:17 PM

Posting Permissions

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