By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,386 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,386 IT Pros & Developers. It's quick & easy.

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

P: 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
Share this Question
Share on Google+
5 Replies


Expert
P: 83
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

P: 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

Expert
P: 83
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
Expert 5K+
P: 5,000
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

P: 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

Post your reply

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