Problem Statement You are asked to represent the realworld e
Problem Statement:
You are asked to represent the real-world entity Car in an object oriented program. For the purpose of this exercise, only assume that an object of type Car has a make and 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 Vehicle: This should be an abstract class that is the parent class of class Car, described below. Class Vehicle should represent any moving object used for transportation. This class, Vehicle, should define a constructor the will accept an engine, a wheel, and a window objects. This class should also define two abstract methods called displayWheelPressure() and displayWindowArea(). In addition, it will define a regular method called displayValveNumber() that will print a message on the screen indicating how many valves are in this vehicle.
Class Car: This class should represent a car, a real-word entity. Each car is a vehicle that must have an engine, a wheel, and a window objects in addition to a string data filed that represents the make of this car. To construct an object of type Car, you need to invoke a constructor that accepts objects of types Engine, Wheel, and Window in addition to a string, make. The class should also override its parent’s methods displayWheelPressure() and displayWindowArea() as well as providing its own version of 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 but store them into objects of type Vehicle. The first car has an Engine with the following specifications (CC = 2400, Cylinder # = 4, # of valves = 16, and hp = 180). This car object should have 4 wheels, of size 265, and proper pressure of 33. Also it has 2 windows, each is 45X50 in size and its make is “Mercedes-Benz”. For the second Car object, your program should assign appropriate value for the engine specifications. This second car object should have 4 wheels, of size 225, and proper pressure of 35. Also it has a 4 windows, each is 50X60 in size and its make “Honda”. After creating the two objects the program should display (using appropriate messages) for every object: the car make, the wheel pressure, the area of the window, the number of valves, and the horse power of the engine of that object.
Solution
package com.chegg;
public class Engine {
int CC;
int noofCylinders;
int noofValves;
int horsePower;
public Engine() {
super();
// TODO Auto-generated constructor stub
}
public Engine(int cC, int noofCylinders, int noofValves, int horsePower) {
super();
CC = cC;
this.noofCylinders = noofCylinders;
this.noofValves = noofValves;
this.horsePower = horsePower;
}
public int getHorsePower() {
return horsePower;
}
public void setHorsePower(int horsePower) {
this.horsePower = horsePower;
}
public void start(){
System.out.println(\"Start\");
}
public void stop(){
System.out.println(\"Stop\");
}
}
package com.chegg;
public class Wheel {
int noofWheels;
int sizeofWheel;
int Pressure;
public Wheel(int noofWheels, int sizeofWheel, int Pressure) {
super();
this.noofWheels = noofWheels;
this.sizeofWheel = sizeofWheel;
this.Pressure = Pressure;
}
public int getPressure() {
return Pressure;
}
public void setPressure(int pressure) {
Pressure = pressure;
}
}
package com.chegg;
public class Window {
int noofWindow;
float width;
float height;
public Window(int noofWindow, float width, float height) {
super();
this.noofWindow = noofWindow;
this.width = width;
this.height = height;
}
public void rollUp(){
System.out.println(\"Roll uP\");
}
public void rollDown(){
System.out.println(\"Roll Down\");
}
public float ComputerWindowArea(){
return (width*height);
}
}
package com.chegg;
public abstract class Vehicle {
Engine engine;
Wheel wheel;
Window window;
public Vehicle(Engine engine, Wheel wheel, Window window) {
this.engine = engine;
this.wheel = wheel;
this.window = window;
}
abstract void displayWheelPressure();
abstract void displayWindowArea();
public void displayValveNumber(){
System.out.println(\"number of valves\\t\"+ engine.noofValves);
}
}
package com.chegg;
public class Car extends Vehicle {
Engine engine;
Wheel wheel;
Window window;
String makeofCar;
public String getMakeofCar() {
return makeofCar;
}
public Car(Engine engine, Wheel wheel, Window window, String makeofCar) {
super(engine, wheel, window);
this.engine = engine;
this.wheel = wheel;
this.window = window;
this.makeofCar = makeofCar;
}
@Override
void displayWheelPressure() {
// TODO Auto-generated method stub
System.out.println(\"wheel pressure\\t\"+wheel.Pressure);
}
@Override
void displayWindowArea() {
// TODO Auto-generated method stub
System.out.println(\"window Area\\t\"+window.ComputerWindowArea());
}
public void displayValveNumber(){
System.out.print(\"no of valves of Car\\t\");
displayValveNumber();
}
}
package com.chegg;
public class TestCar {
public static void main(){
Engine engine =new Engine(2400,4,16,180);
Wheel wheel=new Wheel(4,265,33);
Window window= new Window(2,45,50);
Engine engine2 =new Engine(3000,4,16,180);
Wheel wheel2=new Wheel(4,225,35);
Window window2= new Window(4,50,60);
Car v= new Car(engine, wheel, window, \"Mercedes-Benz\");
Car v2= new Car(engine2, wheel2, window2,\"Honda\");
System.out.println(\"Car Make of 1\"+ v.makeofCar );
System.out.println(\"Wheel Pressure of 1\");
v.displayWheelPressure();
System.out.println(\"Area of Window\");
v.displayWindowArea();
System.out.println(\"no of valves\"+v.engine.noofValves);
System.out.println(\"Horse Power\"+v.engine.getHorsePower());
System.out.println(\"Car Make of 2\"+ v2.makeofCar );
System.out.println(\"Wheel Pressure of 2\");
v2.displayWheelPressure();
System.out.println(\"Area of Window\");
v2.displayWindowArea();
System.out.println(\"no of valves\"+v2.engine.noofValves);
System.out.println(\"Horse Power\"+v2.engine.getHorsePower());
}
}



