Question

In: Computer Science

Create a class called Vehicle that includes four instance variables:      name, type,     tank size and...

Create a class called Vehicle that includes four instance variables:

     name, type,

    tank size and average petrol consumption.

  1. Provide 2 constructors, the first takes name and type as parameter, the second takes four parameters for the four instance variables. (2 pt)
  2. Provide also a method called distancePerTank that calculate the average distance that a vehicle can travel if the tank is full (multiplies the tank size by the average petrol consumption), then returns the value. (2 pt)
  3. Provide a toString method, where its returned value

If tank size and average petrol consumption are not given, then return:

Vehicle [name=”Corolla”, type=”Sedan” ]

Otherwise its returned value:

Vehicle [name=”Corolla”, type=”Sedan”, tank Size=50.00 liters, average petrol consumption =22.00 liter/KM, can cross 1100.00 per tank]

(3 pt)

  1. Write a driver class to test the class Vehicle using different versions of the constructor and the toString method. (3 pt)

Running example:

Vehicle [name=”Corolla”, type=”Sedan” ]

Vehicle [name=”A5”, type=”Coupe” ]

Vehicle [name=”Mustange”, type=”Sport”, tank Size=60.00 liters, average petrol consumption =8.00 liter/KM, can cross 480.00 per tank ]

Vehicle [name=”Civic”, type=”Sedan”, tank Size=47.00 liters, average petrol consumption =15.00 liter/KM, can cross 705.00 per tank ]

Solutions

Expert Solution

code:

public class Main_vehicle
{
public static void main(String[] args) {

   Vehicle ob = new Vehicle("Corolla","Sedan");
   System.out.println(ob);

   Vehicle ob1 = new Vehicle("A5","Coupe");
   System.out.println(ob1);

   Vehicle ob2 = new Vehicle("Mustange","Sport",60,8);
   System.out.println(ob2);

   Vehicle ob3 = new Vehicle("Civic","Sedan",47,15);
   System.out.println(ob3);

}
}

class Vehicle
{
    String name;
    String type;
    double tank_size=0;
    double avg_petrol_consumption=0;

    public Vehicle(String name, String type)
    {
        this.name = name;
        this.type = type;
    }

    public Vehicle(String name, String type, double tank_size, double avg_petrol_consumption)
    {
        this.name = name;
        this.type = type;
        this.tank_size = tank_size;
        this.avg_petrol_consumption = avg_petrol_consumption;
    }

    double distancePerTank()
    {
        return tank_size*avg_petrol_consumption;
    }

    public String toString()
    {
        if((tank_size!=0)&&(avg_petrol_consumption!=0))
        {
            return "Vehicle [name=\""+name+"\", type=\""+type+"\", tank Size="+tank_size+" liters, average petrol consumption ="+avg_petrol_consumption+" liter/KM, can cross "+distancePerTank()+" per tank]";
        }
        else
        {
            return "Vehicle [name=\""+name+"\", type=\""+type+"\"]";
        }
    }
}

output:


Related Solutions

with PHP Create a class called Employee that includes three instance variables—a first name (type String),...
with PHP Create a class called Employee that includes three instance variables—a first name (type String), a last name (type String) and a monthly salary int). Provide a constructor that initializes the three instance data member. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its 0. Write a test app named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary....
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class Create another class called CourseSection Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of the methods required for a standard user defined...
#Create a class called FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make...
#Create a class called FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make sure the variable names match those #words. size will be a character, either "S", "M", or "L". #extra_shots will be an integer. # #FrapOrder should have a constructor with two required #parameters, one for each of those attributes (size and #extra_shots, in that order). # #FrapOrder should also have a method called get_total. #get_total should calculate the total cost of the order. #If size...
Exercise #1: Create an abstract class called GameTester. The GameTester class includes a name for the...
Exercise #1: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the user to choose game tester type...
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
Using C# Create a class called Artist that contains 4 pieces of information as instance variables:...
Using C# Create a class called Artist that contains 4 pieces of information as instance variables: name (datatype string), specialization – i.e., music, pottery, literature, paintings (datatype string), number of art items (datatype int), country of birth (datatype string). Create a constructor that initializes the 4 instance variables. The Artist class must contain a property for each instance variable with get and set accessors. The property for number should verify that the Artist contributions is greater than zero. If a...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
Create a C# Application. Create a class object called “Employee” which includes the following private variables:...
Create a C# Application. Create a class object called “Employee” which includes the following private variables: firstN lastN idNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods: constructor properties CalcPay(): Calculate the regular pay and overtime pay. Create...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT