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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wshaer
    New Member
    • Feb 2007
    • 12

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

    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(),getHorse Power(), 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 ComputerWindowA rea(). Both the rollUp() and rollDown() method will just display appropriate messages on the screen when called. The ComputerWindowA rea() 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, displayWheelPre ssure(), displayWindowAr ea(), and displayValveNum ber(). 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:

    Code:
    public class Engine{
        // Declare the varibles
    	double cylinderCapacity,horsePower;
    	int numberOfCylinders,numberOfValves;
            Engine obj1= new Engine();
        
        // Default constructor accepts no arguments 
    	Engine(){
    	    cylinderCapacity=200.00;
    	    horsePower=500.00;
    	    numberOfCylinders=6;
    	    numberOfValves=12;
    	}
        // A copy constructor accepts value for the four attributes and assign them to the class data members
    	Engine(double a, double b, int c, int d){
    	    cylinderCapacity=a;
    	    horsePower=b;
    	    numberOfCylinders=c;
    	    numberOfValves=d;
    	}
    
        // Methods will just display, on the screen, appropriate messages when called
    	void start(){
    	    System.out.println("The start  method was called");
    	}
        	void stop(){
    	    System.out.println("The stop method was called");
    	}
    
        // This method receives an integer value for the horse power and assign it to the appropriate data member 
    	void setHorsePower(int horsePower){
    	    int power;
    	    power=horsePower;
    	}
    
        // This method receives no argument but will return the current value of the horse power. 
    
    	 double   getHosrePower(){
    	    return horsePower;
    	}
            int getValveNumber(){
                return numberOfValves;
            }
        }
    Code:
    public class Wheel{
        // Declare the variables
        Wheel obj2 = new Wheel();
        int numberOfWheels;
        double sizeOfWheels,inflationPressure;
        
        // Non-default constructor that accepts values for the three attributes and assign them to corresponding class data members
        Wheel(int a,double b,double c){
    	numberOfWheels=a;
    	sizeOfWheels=b;
    	inflationPressure=c;
        }
        
            
        // This method receives an integer value for the proper psi value and assign it to appropriate data member
        void setPressure(int inflationPressure){
    	int pressure;
    	pressure=inflationPressure;
        }
    
        // This method receives no argument but will return the current value of the tire pressure
        double  getPressure(){
    	return inflationPressure;
        }
    }
    Code:
    public class Window{
        // Declare the variables
        int numberOfWindows;
        double height,width;
        Window obj3= new Window();
        // Non-default constructor that accepts values for the three attributes and assign them to corresponding class data members
        Window(){
    	numberOfWindows=4;
    	height=3.7;
    	width=1.2;
        }
    
        // These methods will just display appropriate messages on the screen when called.
        void rollUp(){
    	System.out.println(" Window was rooled up");
        }
        void rollDown(){
    	System.out.println(" Window was rolled down");
        }
    
        // This method computes the area of a window  and returning the area value to the caller.
        double computerWindowArea(double height,double width){
    	double length_1,length_2,area;
    	length_1=height;
    	length_2=width;
    	area=length_1 * length_2;
    	return area;
        }
    }
    Code:
    public class Car{
        Engine point1=new Engine();
        Wheel point2= new Wheel();
        Window point3=  new Window();
     
        
        Car(Engine obj1,Wheel obj2,Window obj3){
    	point1=obj1;
    	point2=obj2;
    	point3=obj3;
        }
    	
        double displayWheelPressure(){
    	System.out.println(" The pressure is " + point2.getPressure());
        }
    
        double displayWindowArea(){
           	System.out.println(" The area of the window  is " + point3.computerWindowArea(4,4));      
        }
    
        double displayValveNumber(){
    	System.out.println(" The valve number is " + point1.getValveNumber());
        }
    }
    Code:
    public class TestCar{ 
        public static void main(String[] args) {
    	// Create two objects of type car
    	Car firstCar = new Car();
    	Car secondCar= new Car();
    
    	
        }
    }
    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.
  • nmadct
    Recognized Expert New Member
    • Jan 2007
    • 83

    #2
    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.

    Comment

    • wshaer
      New Member
      • Feb 2007
      • 12

      #3
      Originally posted by nmadct
      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

      Comment

      • nmadct
        Recognized Expert New Member
        • Jan 2007
        • 83

        #4
        Originally posted by wshaer
        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.

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          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!

          Comment

          • wshaer
            New Member
            • Feb 2007
            • 12

            #6
            Originally posted by nmadct
            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.

            Comment

            Working...