Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-19-2008, 11:35 AM
Member
 
Join Date: Apr 2008
Posts: 3
freswood is on a distinguished road
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
Sponsored Links
  #2 (permalink)  
Old 04-19-2008, 02:23 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
What have you done so far?

regards,
sukatoa
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 02:50 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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!
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-20-2008, 03:36 AM
Member
 
Join Date: Apr 2008
Posts: 3
freswood is on a distinguished road
^ 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, 02:12 PM
Member
 
Join Date: Apr 2008
Posts: 3
freswood is on a distinguished road
Sorry for the bump

*is getting kinda desperate*
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-21-2008, 05:28 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
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
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

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


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

vB 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 11:11 PM
using elements from other classes Camden New To Java 1 03-21-2008 09:25 AM
Using a JAR from other classes Joe2003 Advanced Java 1 01-02-2008 09:08 PM
When do we use inner classes? cruxblack New To Java 5 08-10-2007 07:00 PM
EJB, classes Model Felissa Enterprise JavaBeans 1 07-06-2007 05:17 PM


All times are GMT +3. The time now is 11:28 AM.


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