Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-19-2008, 10:35 AM
Member
 
Join Date: Apr 2008
Posts: 3
Rep Power: 0
freswood is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 01:23 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 540
Rep Power: 3
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Default
What have you done so far?

regards,
sukatoa
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 01:50 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 838
Rep Power: 4
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Default
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)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-20-2008, 02:36 AM
Member
 
Join Date: Apr 2008
Posts: 3
Rep Power: 0
freswood is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-21-2008, 01:12 PM
Member
 
Join Date: Apr 2008
Posts: 3
Rep Power: 0
freswood is on a distinguished road
Default
Sorry for the bump

*is getting kinda desperate*
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-21-2008, 04:28 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 241
Rep Power: 4
DonCash will become famous soon enoughDonCash will become famous soon enough
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:

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:

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:

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:

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.
__________________
Did this post help you? Please me!

Last edited by DonCash; 04-21-2008 at 05:11 PM. Reason: spring cleaning..
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +2. The time now is 10:38 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org