Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS1180 Lab 10 Inheritance This lab will give you an opportunity to explore the u

ID: 3702474 • Letter: C

Question

CS1180 Lab 10 Inheritance This lab will give you an opportunity to explore the use of inheritance. This lab involves two classes, a Vehicle class which will be the base class for a GasGuzzler class. There is no I/O in either of these classes. There will also be a test class. The Vehicle class contains properties and methods that are common to all vehicles. This class contains two specific properties-speed- the rate at which the vehicle is traveling in miles/hour, and- weight-the weight of the vehicle in pounds. The Vehicle class has the following public methods: . Vehicle objects can be constructed 2 different ways: by specifying the weight and the speed, or by specifying the weight only in which case the speed defaults to 0. 2. There are accessor (getter) methods for both weight and speed. There are no setter methods. 3. The speedup method takes 1 parameter, the amount to increase speed by, and returns the new speed of the vehicle. The increase must be positive to have any affect. 4. The slowdown method takes 1 parameter, the amount to decrease the speed by, and returns the new speed of the vehicle-which can NEVER be a negative number! The decrease must be positive to have any affect. s. The brake method sets the speed to 0, and returns nothing. 6. The mom entum method returns the calculated momentum of the vehicle. The formula for calculating momentum is: weight 0.4536 *speed 0.447 7. The toString method should return a string indicating the weight of the vehicle, the current speed of the vehicle, and the momentum of the vehicle. Each piece of information should appear on a line by itself. A GasGuzzler IS A Vehicle, so the GasGuzzler class inherits from the Vehicle class.

Explanation / Answer

Hi, Please rate the code.

Thanks


class Vehicle {
int speed;
int weight;
//Constructor
Vehicle(int w)
{ weight=w;}
//Constructor
Vehicle(int speed, int weight)
{
this.speed= speed;
this.weight=weight;
}
// Getter Method for returning speed
int getspeed()
{
return speed;
}
// Getter Method for returning speed
int getweight()
{
return weight;
}
//Method for Increasing Speed
int speedup(int s)
{
speed= speed+s;
return speed;
}
// Method for decreasing speed
int speeddown(int s)
{
speed= speed-s;
return speed;
}
//Method for setting brake value to 0
void brake()
{
speed=0;
}
//Method for calculating momemtum
double momemtum()
{
double momemtum= weight*0.4536*speed*0.447;
return momemtum;
}
public String toString(){//overriding the toString() method  
double p=momemtum();
return speed+" "+weight+ " "+p;
}  
}
class GasGuzzler extends Vehicle
{
private int fuelcapacity;
private int currentfuellevel;
//constructor
GasGuzzler(int s, int w, int f, int c)
{
super(s,w);
fuelcapacity=f;
currentfuellevel=c;
}
//Getter method for returning fuel capacity
int getfuelcapacity()
{
return fuelcapacity;
}
//Getter method for returning current fuel level
int getcurrentfl()
{
return currentfuellevel;
}
//Method for increasing Gas
int gasUp(int g)
{
currentfuellevel=currentfuellevel+g;
return currentfuellevel;
}
////Method for increasing Gas
int useGas(int g)
{
currentfuellevel=currentfuellevel-g;
return currentfuellevel;
}
p ublic String toString(){//overriding the toString() method  
return super.toString()+fuelcapacity + " "+currentfuellevel;
}  
}
class Driver
{
public static void main(String args[])
{
//For Testing Base class
Vehicle g= new Vehicle(10,20);
int a= g.getspeed();
System.out.println(a);
System.out.println("The weight of Vehicle is " +g.getweight());
System.out.println(" The speed of Vehicle is " +g.speedup(4));
System.out.println("The increased speed of vehicle is" +g.speeddown(2));
System.out.println(" The momemtum of vehicle is" +g.momemtum());
System.out.println(g);
// For Testing Derived class
GasGuzzler d= new GasGuzzler(100,200,300,400);
System.out.println("The fuel capcity of GasGuzzler is " +d.getfuelcapacity());
System.out.println(" The increaed capacity of GasGuzzleris " +d.gasUp(4));
System.out.println("The increaed capacity of GasGuzzleris is" +d.useGas(2));
System.out.println(d);
}
}