Question

In: Computer Science

In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class....

In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class. The vehicle class should contain at least 2 variables that could pertain to ANY vehicle and two methods. The truck class should contain 2 variables that only apply to trucks and one method. Create a console program that will instantiate a truck with provided member information then call one method from the truck and one method contained from the inherited vehicle class. Have these methods print something to the console.

Solutions

Expert Solution

PROGRAM:

abstract class Vehicle //abstract class
{
   String name;
   String brand;
  
   public abstract String getName(); //abstract method to get name of vehicle
   public abstract String getBrand(); //abstract method to get brand of vehicle

}
class Truck extends Vehicle //truck class is inherited from abstract vehicle class using extends keyword
{
   int capacity; //capacity of truck
   int noAxels; //number of wheels of truck
   public Truck(int capacity,int noAxels,String name,String brand) //parameterized constructoe to initialise variables
   {
       this.capacity=capacity;
       this.noAxels=noAxels;
       super.name=name; // super keyword is used to access parent class methods and variables
       super.brand=brand; //setting brand value
   }
   public String getName() // to get name of the truck
   {
       return this.name;
   }
   public String getBrand() //to get brand of truck
   {
       return this.brand;
   }

// to get the information of truck
   public String toString() {
       String str="Truck "+getName()+" has "+this.noAxels+" Axels with capacity "+this.capacity;
return str;
      
   }
   public static void main(String args[]){
       Vehicle v=new Truck(10,4,"ABC","XYZ"); // truck class is instantiated by using vehicle class
       System.out.println("Truck name: "+v.getName()); //print name of truck
       System.out.println("Truck BRand: "+v.getBrand()); //print bran of truck
       System.out.println(v.toString()); //print truck information
   }
  
}

OUTPUT:


Related Solutions

(JAVA) Create three classes, Vehicle, Truck, and Person. The Vehicle class has a make and model,...
(JAVA) Create three classes, Vehicle, Truck, and Person. The Vehicle class has a make and model, test emissions per mile, and a driver/owner (= Person object). (Each vehicle can only have one driver/owner.) The class Truck is a derived class from the Vehicle class and has additional properties load capacity in tons (type double since it can contain a fractional part) and a towing capacity in pounds (type int). Be sure that your classes have appropriate constructors, accessors, mutators, equals(),...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED BELOW THIS QUESTION). Include the following: A variable Roof that holds the type of roof (Ex: Convertible, Hard-Top, Soft-Top) A variable Doors that holds the car’s number of doors (Ex: 2, 4) Implement the ChangeSpeed method to add 20 to the speed each time it is called. Add exception handling to the ChangeSpeed method to keep the speed under 65. Implement the sound method...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all the fields and methods from the Case class. It should also contain:  One field for a CellPhone object. Be sure to put in the appropriate access modifier. (private, public)  A constructor with 3 parameters – owner’s name, Case color, the phone number of the cell phone that will be in the Case. This constructor MUST call the super class’s constructor. Then set...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length,...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length, width and colour as arguments. Define enough functions to make the class not abstract
A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritance before attempting this challenge.
1. Java Abstract Class (2 pts) () • A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritance before attempting this challenge.• Following is an example of abstract class:abstract class Book{String title;abstract void setTitle(String s);String getTitle(){return title;}}If you try to create an instance of this class like the following line you will get an error:Book...
Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return...
Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item. The method has two parameters, count and itemCost. Derive a class BulkDiscount from DiscountPolicy. It should have a constructor that has two parameters minimum and percent. It should define the method computeDiscount so that if the quantity purchased of an item is more then minimum, the discount is percent percent.
In java: -Create a class named Animal
In java: -Create a class named Animal
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private...
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private data for name, selection, wins, and losses. It must have a non-default constructor that requires name. It may not contain a default constructor. Create overloaded functions for the ++ and - - operator. The overloaded ++operator will add to the number of wins, while the - - operator will add to the losses. You will create two different child classes of player, Human and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT