Let’s consider a superclass Vehicle. Different vehicles have different features
ID: 3765961 • Letter: L
Question
Let’s consider a superclass Vehicle. Different vehicles have different features and properties however there few of them are common to all. Speed, color, fuel used, size are few which are common to all. Hence we can create a class ‘Vehicle’ with states and actions that are common to all vehicles. The subclass of this superclass can be any type of vehicle. Example: Class Car A has all the features of a vehicle. But it has its own attributes which makes it different from other subclasses. By using inheritance we need not rewrite the code that we’ve already used with the Vehicle. The subclass can also be extended. We can make a class ‘Sports Car’ which extends ‘Car’. It inherits the features of both ‘Vehicle’ and ‘Car’.
TAKE A ONE DIMENSIONAL ARRAY INITIALIZER. TAKE SOME VALUES IN IT. FIND THE MAXIMUM VALUE AND TOTAL VALUE FOR THAT LIST.
Explanation / Answer
//Vehicle.java
public class Vehicle
{
private String model;
private String color;
private int speed;
private int fuel;
//Costructor that sets the intance variable values
public Vehicle(String model, String color,int speed, int fuel)
{
this.model=model;
this.color=color;
this.speed=speed;
this.fuel=fuel;
}
//Setter methods
//Set model
public void setModel(String model)
{
this.model=model;
}
//Set color
public void setColor(String color)
{
this.color=color;
}
//Set speed
public void setSpeed(int speed)
{
this.speed=speed;
}
//Set fuel
public void setFuel(int fuel)
{
this.fuel=fuel;
}
//Getter methods
//Returns model
public String getModel()
{
return model;
}
//Returns color
public String getColor()
{
return color;
}
//Returns speed
public int getSpeed()
{
return speed;
}
//Returns fuel
public int getFuel()
{
return fuel;
}
}//end of class Vehicle
-----------------------------------
//Car.java
//The java class Car that extends the Vehicle
public class Car extends Vehicle
{
//instance variables
private int engineCapacity;
public Car(String model, String color,int speed, int fuel,
int engineCapacity)
{
/*Call super class constructor to set model,color,
speed and fuel */
super(model, color, speed, fuel);
//this engineCapacity
this.engineCapacity=engineCapacity;
}
//Returns capacity
public int getCapacity()
{
return engineCapacity;
}
}//end of Car
------------------------------------------
//SportsCar.java
//The class SportsCar that extends the Car clas
public class SportsCar extends Car
{
//instance variable numCylinders
private int numCylinders;
//Constructor
public SportsCar(String model, String color,int speed,
int fuel,int engineCapacity, int numCylinders)
{
//Call super class Car
super(model, color, speed, fuel,engineCapacity);
//number of cylinders
this.numCylinders=numCylinders;
}
//Returns number of cylinders
public int getCylinders()
{
return numCylinders;
}
}//end of class SportsCar
-------------------------------------------
/**The java program that tests the class Vehicle ,
* Car and SprotsCar and print the results */
//TestInheritance.java
public class TestInheritance
{
public static void main(String[] args)
{
//Create an instance of Vechicle class
Vehicle vehicle=new Vehicle("Tata Motots",
"white", 80 , 20);
System.out.println("Vehicle object");
System.out.println("Model : "+vehicle.getModel());
System.out.println("Color : "+vehicle.getColor());
System.out.println("Speed : "+vehicle.getSpeed());
System.out.println("Fuel : "+vehicle.getFuel());
//Create an instance of Car class
Car car=new Car("Tata Motots",
"white", 80 , 20,800);
System.out.println("Car object");
System.out.println("Model : "+car.getModel());
System.out.println("Color : "+car.getColor());
System.out.println("Speed : "+car.getSpeed());
System.out.println("Fuel : "+car.getFuel());
System.out.println("Engine Capacity : "+car.getCapacity());
//Create an instance of SportsCar class
SportsCar sportsCar=new SportsCar("Tata Motots",
"white", 80 , 20,800, 4);
System.out.println("Sports Car object");
System.out.println("Model : "+sportsCar.getModel());
System.out.println("Color : "+sportsCar.getColor());
System.out.println("Speed : "+sportsCar.getSpeed());
System.out.println("Fuel : "+sportsCar.getFuel());
System.out.println("Engine Capacity : "+sportsCar.getCapacity());
System.out.println("Cylinders : "+sportsCar.getCylinders());
}
}//end of the class
--------------------------------------------------------------------
Sample output:
Vehicle object
Model : Tata Motots
Color : white
Speed : 80
Fuel : 20
Car object
Model : Tata Motots
Color : white
Speed : 80
Fuel : 20
Engine Capacity : 800
Sports Car object
Model : Tata Motots
Color : white
Speed : 80
Fuel : 20
Engine Capacity : 800
Cylinders : 4
------------------------------------------------------------------------
/**
* The java program that intiallizes an arry with values.
* Finds total and maximum values and print to console
* */
//SampleArrayProgram.java
public class SampleArrayProgram
{
public static void main(String[] args)
{
//Create an array of values
int arr[]={5,6,7,8,2,3};
//print values
print(arr);
//call getTotal
int total=getTotal(arr);
System.out.println("Total : "+total);
//call getMaximum
int max=getMaximum(arr);
System.out.println("Maximum : "+max);
}
//Prints array elements
private static void print(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println("arr["+(i+1)+"] = "+arr[i]);
}
}
//Returns total value of array,arr
private static int getTotal(int[] arr) {
int total=0;
for (int i = 0; i < arr.length; i++) {
total+=arr[i];
}
return total;
}
//Returns maximum value of arr
private static int getMaximum(int[] arr) {
//assume starting element is maximum
int max=arr[0];
for (int i = 1; i < arr.length; i++) {
if(arr[i]>max)
max=arr[i];
}
//return maximum
return max;
}
}//end of the class
---------------------------------------------------------------
Sample Output:
arr[1] = 5
arr[2] = 6
arr[3] = 7
arr[4] = 8
arr[5] = 2
arr[6] = 3
Total : 31
Maximum : 8
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.