473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

A problem with objects in java to represent the real-world entity Car

12
Hi

This is the task:

You are asked to represent the real-world entity Car in an object orient program. For the purpose of this exercise, only assume that an object of type Car can be created out of three different parts, engine, wheel, and window. To represent these components you need to create a separate class to represent each one. The specifications for each of the required classes are given below:

Class Engine: This class should describe an engine in terms of its Cylinder Capacity (CC), number of cylinders, number of valves, and horse power. The class needs to define two constructors, a default one that accepts no arguments but initializes class members to reasonable values while the second one, a copy constructor, that accepts value for the four attributes and assign them to the class data members. The class should also define four methods, start(), stop(),getHorsePower(), and setHorsePower(). The start() and stop() methods will just display, on the screen, appropriate messages when called. The setHorsePower() will receive an integer value for the horse power and assign it to the appropriate data member while the getHorsePower() will receive no argument but will return the current value of the horse power.

Class Wheel: This class should describe the wheels in a Car in terms of how many wheels a car has, the size of the wheel, and proper inflation pressure. The class needs to define a non-default constructor that accepts values for the three attributes and assign them to corresponding class data members. The class should also define two methods, getPressure() and setPressure(). The setPressure() will receive an integer value for the proper psi value and assign it to appropriate data member while the getPressure() will receive no argument but will return the current value of the tire pressure.

Class Window: This class should describe the Windows in a car in terms of how many windows a car has and the width and the height of a window (assuming all windows have the same dimension) The class needs to define a non-default constructor that accepts values for the three attributes and assign them to corresponding class data members. The class should also define three methods, rollUp(), rollDown(), and ComputerWindowArea(). Both the rollUp() and rollDown() method will just display appropriate messages on the screen when called. The ComputerWindowArea() will compute the area of a window (assuming it is a rectangle) and returning the area value to the caller.

Class Car: This class should represent a car, a real-word entity. Each car must have an engine, a wheel, and a window objects. To construct an object of type Car, you need to invoke a constructor that accepts objects of types Engine, Wheel, and Window respectively then copy these objects to the appropriate class members. The class should also define three methods, displayWheelPressure(), displayWindowArea(), and displayValveNumber(). The task of each of these methods is to display, on the screen, the value described by the method name.

Class TestCar: This is a driver class which will have the main() method. The class will create two objects of type Car. The first one has an Engine with the following specifications (CC = 2400, Cylinder # = 4, # of valves = 16, and hp = 166). This car object should have 4 wheels, of size 175, and proper pressure of 31. Also it has 2 windows, each is 45X50 in size. For the second Car object, your program should assign appropriate value for the engine specifications, This car object should have 4 wheels, of size 215, and proper pressure of 35. Also it has a 4 windows, each is 50X60 in size. After creating the two objects the program should display (using appropriate messages) for every object: the wheel pressure, the area of the window, the number of valves, and the horse power of the engine of that object.


Note: Every class must be in a separate file but all of them under the same directory.
and these are my classes:

Expand|Select|Wrap|Line Numbers
  1. public class Engine{
  2.     // Declare the varibles
  3.     double cylinderCapacity,horsePower;
  4.     int numberOfCylinders,numberOfValves;
  5.         Engine obj1= new Engine();
  6.  
  7.     // Default constructor accepts no arguments 
  8.     Engine(){
  9.         cylinderCapacity=200.00;
  10.         horsePower=500.00;
  11.         numberOfCylinders=6;
  12.         numberOfValves=12;
  13.     }
  14.     // A copy constructor accepts value for the four attributes and assign them to the class data members
  15.     Engine(double a, double b, int c, int d){
  16.         cylinderCapacity=a;
  17.         horsePower=b;
  18.         numberOfCylinders=c;
  19.         numberOfValves=d;
  20.     }
  21.  
  22.     // Methods will just display, on the screen, appropriate messages when called
  23.     void start(){
  24.         System.out.println("The start  method was called");
  25.     }
  26.         void stop(){
  27.         System.out.println("The stop method was called");
  28.     }
  29.  
  30.     // This method receives an integer value for the horse power and assign it to the appropriate data member 
  31.     void setHorsePower(int horsePower){
  32.         int power;
  33.         power=horsePower;
  34.     }
  35.  
  36.     // This method receives no argument but will return the current value of the horse power. 
  37.  
  38.      double   getHosrePower(){
  39.         return horsePower;
  40.     }
  41.         int getValveNumber(){
  42.             return numberOfValves;
  43.         }
  44.     }
Expand|Select|Wrap|Line Numbers
  1. public class Wheel{
  2.     // Declare the variables
  3.     Wheel obj2 = new Wheel();
  4.     int numberOfWheels;
  5.     double sizeOfWheels,inflationPressure;
  6.  
  7.     // Non-default constructor that accepts values for the three attributes and assign them to corresponding class data members
  8.     Wheel(int a,double b,double c){
  9.     numberOfWheels=a;
  10.     sizeOfWheels=b;
  11.     inflationPressure=c;
  12.     }
  13.  
  14.  
  15.     // This method receives an integer value for the proper psi value and assign it to appropriate data member
  16.     void setPressure(int inflationPressure){
  17.     int pressure;
  18.     pressure=inflationPressure;
  19.     }
  20.  
  21.     // This method receives no argument but will return the current value of the tire pressure
  22.     double  getPressure(){
  23.     return inflationPressure;
  24.     }
  25. }
  26.  
Expand|Select|Wrap|Line Numbers
  1. public class Window{
  2.     // Declare the variables
  3.     int numberOfWindows;
  4.     double height,width;
  5.     Window obj3= new Window();
  6.     // Non-default constructor that accepts values for the three attributes and assign them to corresponding class data members
  7.     Window(){
  8.     numberOfWindows=4;
  9.     height=3.7;
  10.     width=1.2;
  11.     }
  12.  
  13.     // These methods will just display appropriate messages on the screen when called.
  14.     void rollUp(){
  15.     System.out.println(" Window was rooled up");
  16.     }
  17.     void rollDown(){
  18.     System.out.println(" Window was rolled down");
  19.     }
  20.  
  21.     // This method computes the area of a window  and returning the area value to the caller.
  22.     double computerWindowArea(double height,double width){
  23.     double length_1,length_2,area;
  24.     length_1=height;
  25.     length_2=width;
  26.     area=length_1 * length_2;
  27.     return area;
  28.     }
  29. }
  30.  
Expand|Select|Wrap|Line Numbers
  1. public class Car{
  2.     Engine point1=new Engine();
  3.     Wheel point2= new Wheel();
  4.     Window point3=  new Window();
  5.  
  6.  
  7.     Car(Engine obj1,Wheel obj2,Window obj3){
  8.     point1=obj1;
  9.     point2=obj2;
  10.     point3=obj3;
  11.     }
  12.  
  13.     double displayWheelPressure(){
  14.     System.out.println(" The pressure is " + point2.getPressure());
  15.     }
  16.  
  17.     double displayWindowArea(){
  18.            System.out.println(" The area of the window  is " + point3.computerWindowArea(4,4));      
  19.     }
  20.  
  21.     double displayValveNumber(){
  22.     System.out.println(" The valve number is " + point1.getValveNumber());
  23.     }
  24. }
  25.  
Expand|Select|Wrap|Line Numbers
  1. public class TestCar{ 
  2.     public static void main(String[] args) {
  3.     // Create two objects of type car
  4.     Car firstCar = new Car();
  5.     Car secondCar= new Car();
  6.  
  7.  
  8.     }
  9. }
  10.  
I have proglems with object I don't know how to pass them from one class to another. And I think that my last two classes are wrong.

can any one give my a hent.
Apr 10 '07 #1
5 3695
nmadct
83 Expert
You're right, there are some problems with the last 2 classes.

In class Car, you are initially assigning default objects to your engine, wheel and window variables, but there's no need for that because they get assigned by the constructor. Also, "point1" and "obj1" are not appropriate names for variables, give them names that relate to what they represent.

In class TestCar, you can't just instantiate a Car with "new Car()", because the constructor for Car requires arguments. The assignment calls for you to create two cars with certain specifications, use the constructors to assign those values.
Apr 10 '07 #2
wshaer
12
You're right, there are some problems with the last 2 classes.

In class Car, you are initially assigning default objects to your engine, wheel and window variables, but there's no need for that because they get assigned by the constructor. Also, "point1" and "obj1" are not appropriate names for variables, give them names that relate to what they represent.

In class TestCar, you can't just instantiate a Car with "new Car()", because the constructor for Car requires arguments. The assignment calls for you to create two cars with certain specifications, use the constructors to assign those values.
Thank you for your fast reply
But I am wondring should I move the three objects that are declared in Car class to TestCar class?
Regard
Apr 10 '07 #3
nmadct
83 Expert
Thank you for your fast reply
But I am wondring should I move the three objects that are declared in Car class to TestCar class?
Regard
If you mean that you should instantiate Engine, Window and Wheel in the main method of your TestCar class, then yes, I agree.
Apr 10 '07 #4
RedSon
5,000 Expert 4TB
In the future please try to be a bit more specific about the title of your thread to help others who are trying to do a search on it.

Thanks!
Apr 10 '07 #5
wshaer
12
If you mean that you should instantiate Engine, Window and Wheel in the main method of your TestCar class, then yes, I agree.
Thanks a lot for your reply and usefull help. I gut a full mark on that assignment.
Apr 12 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: Claudio Jolowicz | last post by:
Is it possible to store unique objects in an STL container? Suppose an object of class C is unique: class C { public: C() {} ~C() {} private:
4
by: Marc Tanner | last post by:
Hello, I am currently working on a eventhandling system or something similar, and have the problem of loosing scope. I have read many interesting posts on this group and the faq article about...
19
by: s.subbarayan | last post by:
Dear all, I had this following doubt,while java is able to carryon with out pointers why C language cant be modified to remove pointer?Hows java able to do this with out pointers? I jus plead...
7
by: Citrus | last post by:
Hey, i'm looking for the best possible way for connecting to my database without too much of an effort. The example works fine and fast. What i need to know is if it's safe, stable and the way...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
5
by: kylemort | last post by:
I am currently using a class (Card) to represent a card in a deck of cards. The class has a constructor: Card::Card(int num, int su) { suit = su; number = num; } and I am trying to create...
3
by: R69D | last post by:
Hi, I'm real new to Java and need some help...was going through some old tutorials and need help with these questions: 1)What is “composition of methods?” How do u define the composition of...
6
by: penny | last post by:
In this assignment we shall look at a possible representation of rational numbers in java using objects. The java language represents rational numbers using the same representation used for other...
3
by: sophie_newbie | last post by:
Hi, I want to store python text strings that characters like "é" "Č" in a mysql varchar text field. Now my problem is that mysql does not seem to accept these characters. I'm wondering if there...
2
by: cioccolatina | last post by:
Hey guys, is there anyone who could help me..? I have file ExpressionBinaryTree.java : /** class ExpressionBinaryTree * uses a binary tree to represent binary expressions * does not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.