Question

JAVA PROGRAMING - TESTING CLASSES AND INHERITANCE Create two classes - a vehicle class and gasguzzler...

JAVA PROGRAMING - TESTING CLASSES AND INHERITANCE

Create two classes - a vehicle class and gasguzzler class. Neither classes have I/O. Use a test class as well.

Vehicle class has methods and properties that all vehicles have. This class has two properties - speed (rate of travel in miles per hour) and weight (in pounds)

Vehicle Class 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.

There are accessor (getter) methods for both weight and speed. There are no setter methods.

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.

The slowdown method takes 1 parameter, the amount to decrease the speed by, and returns the new speed of the vehicle – can not be negative. The decrease must be positive to have any affect.

The brake method sets the speed to 0, and returns nothing.

The momentum method returns the calculated momentum of the vehicle. The formula for calculating momentum is:
weight * 0.4536 * speed * 0.447

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.

The gasguzzler class INHERITS from the vehicle class.

The GasGuzzler class adds 2 properties that are common to all gas guzzlers – fuel capacity and current fuel level. These are private member variables.

gasguzzler public methods:

A GasGuzzler object can be constructed in two different ways:

Specify the weight, speed, fuel capacity, and fuel level

Specify the weight and the capacity (fuel level defaults to capacity value)

There are accessor methods for both properties.

The gasUp method takes 1 parameter, the amount of gas to add to the current fuel level. The parameter must be positive. The fuel level cannot exceed the capacity. This method returns the new fuel level.

The useGas method takes 1 parameter, the amount of gas to subtract from the fuel level. The amount must be positive. The fuel level cannot go below 0. This method returns the new fuel level.

The toString method overrides the Vehicle class version of this method. This method must call the Vehicle class version to get the string containing base class information. It must add to that string information about the fuel capacity and fuel level. Each piece of information should appear on a line by itself.

Create a test program which thoroughly tests both classes. Verify that all the methods of the base class work first. Then add test cases for the derived class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Output screen shot is attached at the end

package learning;


class Vehicle{
private double speed;
private double weight;

public Vehicle(double weight, double speed){
  this.weight = weight;
  this.speed = speed;
}

public Vehicle(double weight){
  this.weight = weight;
  this.speed = 0;
}

public double getSpeed() {
  return speed;
}

public double getWeight() {
  return weight;
}

public double speedup(double amount){
  if(amount <=0){
   return speed;
  }else{
   speed = speed + amount;
   return speed;
  }
}

public double slowdown(double amount){
  if((speed - amount) < 0){
   return speed;
  }else{
   speed = speed - amount;
   return speed;
  }
}


public void brake(){
  speed = 0;
}

public double momentum (){
  return weight * 0.4536 * speed * 0.447;
}

public String toString(){
  StringBuilder sb = new StringBuilder();
  sb.append("weight: ").append(weight).append("\n");
  sb.append("speed: ").append(speed).append("\n");
  sb.append("momentum: ").append(momentum()).append("\n");
  return sb.toString();
}
}

class GasGuzzler extends Vehicle{
double fuelCapacity;
double currentFuelLevel;

public GasGuzzler(double weight, double speed, double fuelCapacity, double currentFuelLevel){
  super(weight, speed);
  this.fuelCapacity = fuelCapacity;
  this.currentFuelLevel = currentFuelLevel;
}

public GasGuzzler(double weight, double fuelCapacity){
  super(weight);
  this.fuelCapacity = fuelCapacity;
  this.currentFuelLevel = fuelCapacity;
}

public double getFuelCapacity() {
  return fuelCapacity;
}

public double getCurrentFuelLevel() {
  return currentFuelLevel;
}

public double gasUp(double amount){
  if((currentFuelLevel + amount) > fuelCapacity){
   return currentFuelLevel;
  }else{
   currentFuelLevel = currentFuelLevel + amount;
   return currentFuelLevel;
  }
}

public double useGas(double amount){
  if((currentFuelLevel - amount) < 0){
   return currentFuelLevel;
  }else{
   currentFuelLevel = currentFuelLevel - amount;
   return currentFuelLevel;
  }
}

public String toString(){
  StringBuilder sb = new StringBuilder();
  sb.append(super.toString());
  sb.append("current fuel level: ").append(currentFuelLevel).append("\n");
  sb.append("capacity: ").append(fuelCapacity).append("\n");
  return sb.toString();
}
}

public class VehicleTest {

public static void main(String args[]){
  System.out.println("--------------- Testing Vehicle -------------------");
  Vehicle vh = new Vehicle(230.23, 150);
  
  System.out.println("Speed : " + vh.getSpeed());
  System.out.println("Weight: " + vh.getWeight());
  System.out.println("Momentum : " + vh.momentum());
  // Increasing the speed
  vh.speedup(23);
  System.out.println("After increasing the Speed, speed is" + vh.getSpeed());
  // slowing down the speed
  vh.slowdown(12);
  // Printing Vehicle Object
  System.out.println("After slowing Down , the speed is: " + vh.getSpeed());
  System.out.println("Vehicle Object is:");
  System.out.println(vh);
  
  // Testing GasGuzzler
  System.out.println("-----------Testing GasGuzzler -------------");
  GasGuzzler gasGuzzler = new GasGuzzler(120, 456);
  System.out.println("Speed: " + gasGuzzler.getSpeed());
  System.out.println("Weight: " + gasGuzzler.getWeight());
  System.out.println("FuelCapacity: " + gasGuzzler.getFuelCapacity());
  System.out.println("fuel Level: " + gasGuzzler.getCurrentFuelLevel());
  System.out.println("Increasing the speed");
  gasGuzzler.speedup(123);
  System.out.println("After increasing the speed, speed is" + gasGuzzler.getSpeed());
  gasGuzzler.gasUp(500);
  gasGuzzler.useGas(34);
  System.out.println("After using the Gas, fuel level " + gasGuzzler.getCurrentFuelLevel());
  System.out.println("Printing GasGuzzler Object");
  System.out.println(gasGuzzler);
  
}


}

sterminated> VehicleTest [Java Application] C:Javaljdk1.7.0_80%bin javaw.exe (02-Apr-2018 11:39:48 pm) Testing Vehicle Speed

Add a comment
Know the answer?
Add Answer to:
JAVA PROGRAMING - TESTING CLASSES AND INHERITANCE Create two classes - a vehicle class and gasguzzler...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a...

    Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a float Speed and string Name, an abstract method Drive, and include a constructor. Create a Tesla class - Tesla inherits from Vehicle. - Tesla has an int Capacity. - Include a constructor with the variables, and use super to set the parent class members. - Override Drive to increase the speed by 1 each time. - Add a toString to print the name, speed,...

  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • Required in Java. Thank you. 1. Create a base class called Vehicle that has the manufacturer's...

    Required in Java. Thank you. 1. Create a base class called Vehicle that has the manufacturer's name (type String), number of cylinders in the engine (type int), and owner (type Person given in Listing 8.1 in the textbook and in LMS). Then create a class called Truck that is derived from Vehicle and has additional properties: the load capacity in tons (type double, since it may contain a fractional part) and towing capacity in tons (type double). Give your classes...

  • Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and...

    Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and Bird 2.      A Bird class that is a descendant of Animal 3.      A Dog class that is a descendant of Animal 4.      A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object. The Animal class has: ·        one instance variable, a private String variable named   name ·        a single constructor that takes one argument, a String, used to set...

  • Using C#: Create an application class named BillDemo that instantiates objects of two classes named Bill...

    Using C#: Create an application class named BillDemo that instantiates objects of two classes named Bill and OverdueBill, and that demonstrates all their methods. The Bill class includes auto-implemented properties for the name of the company or person to whom the bill is owed and for the amount due. Also, include a ToString() method and returns a string that contains the name of the class (using GetType()) and the Bill’s data fields values. Create a child class named OverdueBill that...

  • JAVA :The following are descriptions of classes that you will create. Think of these as service...

    JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing...

  • Write a program to pack boxes with blobs. Write two classes and a driver program. A...

    Write a program to pack boxes with blobs. Write two classes and a driver program. A Blob class implements the following interface. (Defines the following methods.) public interface BlobInterface { /** getWeight accessor method. @return the weight of the blob as a double */ public double getWeight(); /** toString method @return a String showing the weight of the Blob */ public String toString(); } A Blob must be between 1 and 4 pounds, with fractional weights allowed. A default constructor...

  • Java Problem 2: Employee (10 points) (Software Design) Create an abstract class that represents an Employee and contains abstract method payment. Create concrete classes: SalaryEmployee, CommissionEm...

    Java Problem 2: Employee (10 points) (Software Design) Create an abstract class that represents an Employee and contains abstract method payment. Create concrete classes: SalaryEmployee, CommissionEmployee, HourlyEmployee which extend the Employee class and implements that abstract method. A Salary Employee has a salary, a Commision Employee has a commission rate and total sales, and Hourly Employee as an hourly rate and hours worked Software Architecture: The Employee class is the abstract super class and must be instantiated by one of...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT