Question

Java For this assignment you need to create at least 5 classes. 1.Create a vehicle class...

Java

For this assignment you need to create at least 5 classes.

1.Create a vehicle class ( super class)
2,3. Create two sub classes ( car, bus , truck train or any) for vehicle class
4 Create a subclass (sportscar or schoolbus or Goodstrain or any) for car or bus

You need to use the following atleast for one class.

Use a protected variable
Add constructor for superclass and subclass
Override a method which displays a description about the vehicle
Use super keyword to call a superclass constructor
Use super keyword to call a superclass method to return the number of wheels


5.Create a vehicle test class to create object of subclass (sportscar or schoolbus or any) and an object for subclass (car or bus or any)

You need to invoke all classes using only 2 objects

Homework Answers

Answer #1

Code is Given Below:

===========================

Vehicle.java

============================

//here vehicle is base class
public class Vehicle {
   //variables
   protected String name;
   protected int max_speed;
   protected int wheels;
  
   public Vehicle(String name, int max_speed) {
       this.name = name;
       this.max_speed = max_speed;
   }
   //getter and setter methods
  
   public String getName() {
       return name;
   }
   public int getWheels() {
       return wheels;
   }

   public void setWheels(int wheels) {
       this.wheels = wheels;
   }

   public void setName(String name) {
       this.name = name;
   }
   public int getMax_speed() {
       return max_speed;
   }
   public void setMax_speed(int max_speed) {
       this.max_speed = max_speed;
   }
   //tostring method
   public String toString() {
       return "Name is : "+name+"\nMax Speed is : "+max_speed;
       }

}

Car.java

=======================

//here Car is child class of vehicle
public class Car extends Vehicle {
   //variable
   protected int number_of_cylinders;
  
   public Car(String name, int max_speed, int number_of_cylinders) {
       super(name, max_speed);
       this.number_of_cylinders = number_of_cylinders;
   }

   //getter and setter method
   public int getNumber_of_cylinders() {
       return number_of_cylinders;
   }

   public void setNumber_of_cylinders(int number_of_cylinders) {
       this.number_of_cylinders = number_of_cylinders;
   }
   //tostring method
   public String toString() {
       return super.toString()+"\nNumber of Cylinders Are: "+number_of_cylinders+"\nNo. of Wheels : "+super.getWheels();
   }

}

Bus.java

=====================

public class Bus extends Vehicle{
   protected String fuelType;

   public Bus(String name, int max_speed, String fuelType) {
       super(name, max_speed);//calling super class constructor
       this.fuelType = fuelType;
   }
   //getter and setter method
   public String getFuelType() {
       return fuelType;
   }

   public void setFuelType(String fuelType) {
       this.fuelType = fuelType;
   }
   public String toString() {
       return super.toString()+"\nFuel Type: "+fuelType;
   }

}

SchoolBus.java

=====================

public class SchoolBus extends Bus {
   protected int seats;

   public SchoolBus(String name, int max_speed, String fuelType, int seats) {
       super(name, max_speed, fuelType);
       this.seats = seats;
   }
   public String toString() {
       return super.toString()+"\nSeats : "+seats;
   }
  
}

SportsCar.java

====================

public class SportsCar extends Car {
   protected double milage;

   public SportsCar(String name, int max_speed, int number_of_cylinders, double milage) {
       super(name, max_speed, number_of_cylinders);
       this.milage = milage;
   }
   public String toString() {
       return super.toString()+"\nMilage is : "+milage;
   }

}

Test.java

================

public class Test {
   public static void main(String[] args) {
       //creating object
       SportsCar sc=new SportsCar("BMW", 320, 12, 8);
       SchoolBus sb=new SchoolBus("mercedes", 120, "CNG", 32);
       Car c=new Car("Fiat", 180, 10);
       Bus b=new Bus("F8",120, "Petrol");
       Vehicle v=new Vehicle("Van",80);
       c.setWheels(4);
       //printing details
       System.out.println("Sport car ");
       System.out.println(sc);
       System.out.println("===========================");
       System.out.println("School bus ");
       System.out.println(sb);
       System.out.println("===========================");
       System.out.println("Car ");
       System.out.println(c);
       System.out.println("===========================");
       System.out.println("Bus ");
       System.out.println(b);
       System.out.println("===========================");
       System.out.println("Vehicle ");
       System.out.println(v);
       System.out.println("===========================");
      
   }

}

Output:

===========

Code Snapshot:

==================

Know the answer?
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
Similar Questions
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Choose any entity of your choice to represent a superclass. It must not be any of...
Choose any entity of your choice to represent a superclass. It must not be any of the following: - Human, Student, Vehicle, Person, Car, Animal, Book • Create a class for your entity with the following: - 3 instance variables - 3 instance methods - 2 of the instance methods must show overloading - A default constructor - A parameterized constructor that initializes all instance variables - Getters and setters for the instance variables - Method to print all the...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it. The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and numberAxels. Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong. Class vehicle should have...
Coding in Java Create an Airplane class (not abstract) that uses the Vehicle interface in Q1....
Coding in Java Create an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for all methods should print simple messages to the screen using System.out.println(). Add an integer speed variable that is changed using ChangeSpeed method. ChangeSpeed adds 5 to the speed each time it is called. Create a default constructor that sets the initial speed to 0. Don't create other constructors or any setter/getter methods. // code from Q1: interface Vehicle { void Start();...
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...
JAVA QUIZ Question 1 Which of the following is false about a "super" call in a...
JAVA QUIZ Question 1 Which of the following is false about a "super" call in a sub class's constructor? Select one: a. It must be the first statement in the constructor b. If you don't include it Java will c. If you don't include it you must have a 0 parameter constructor coded in the super class or no constructors coded at all in the super class d. The sub class constructor containing the super call and the called super...
1- Use inheritance to implement the following classes: A: A Car that is a Vehicle and...
1- Use inheritance to implement the following classes: A: A Car that is a Vehicle and has a name, a max_speed value and an instance variable called the number_of_cylinders in its engine. Add public methods to set and get the values of these variables. When a car is printed (using the toString method), its name, max_speed and number_of_cylinders are shown. B: An Airplane that is also a vehicle and has a name, a max_speed value and an instance variable called...
a. The Talk-A-Lot Cell Phone Company provides phone services for its customers. Create an abstract class...
a. The Talk-A-Lot Cell Phone Company provides phone services for its customers. Create an abstract class named PhoneCall that includes a String field for a phone number and a double field for the price of the call. Also include a constructor that requires a phone number parameter and that sets the price to 0.0. Include a set method for the price. Also include three abstract get methods - one that returns the phone number, another that returns the price of...
(JAVA) Create a class that represents a Customer. Use good OO programming technique. A Customer has...
(JAVA) Create a class that represents a Customer. Use good OO programming technique. A Customer has a firstName, lastName, customerNumber, emailAddress. You can add some more fields if you want to, but those 4 are a minimum. Override the equals method, and the toString method from the Object class. Implement the Comparable interface. In the main method, create some instances of the Customer class, and demonstrate the use of the accessor and mutator methods, as well as the compareTo method.
Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String str = programmer.toString(); Assume...
Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String str = programmer.toString(); Assume that the Employee class has not implemented its own toString() method. What value will the variable str contain when this code is executed? Group of answer choices the variable will become a null reference the code will not compile because Employee has not implemented toString() the default from Object, which is the class name of the object and its hash code the string representations...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT