Question

In: Computer Science

Simple Java class. Create a class Sportscar that inherits from the Car class includes the following:...

Simple Java class.

Create a class Sportscar that inherits from the Car class includes the following:

a variable roof that holds type of roof (ex: convertible, hard-top,softtop)

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 its called

add exception handling to the changespeed method to keep soeed under 65

implement the sound method to print "brooom" to the screen.

create one constructor that accepts the car's roof,doors, year, and make as arguments and assigns the values appropriately. do not create any other constructors.

the interface methods should print messages using system.out.println()

car class:

abstract clas Car implements Vehicle(

private int year;

private int speed;

private string make;

private static int count=0;

public car(int year, string amake){

year = aYaer;

make = aMake;

speed =0;

count++;}

public void sound();

public static int getcount90{

return count;}

}

Solutions

Expert Solution

Implement the requirements as follows:

  1. define method accelerate() that increases the speed
  2. define method getSpeed() that returns the current speed of the car
  3. implement class Sportscar
  4. Add an instance variable roof of type String
  5. Add an instance variable doors of type int
  6. define a constructor that accepts roos, doors, year and make
  7. call super class constructor and pass year and make to it
  8. assign roof and doors
  9. define the method changeSpeed()
  10. check if the speed increases beyond 65
  11. print an error message if the speed increases beyond 65
  12. otherwise increase the speed by 20 using accelerate() method
  13. implement the method sound()
  14. display "brooom" on the screen
  15. create an object of type Sportscar
  16. call the method sound()
  17. increase the speed
  18. call method changeSpeed()
  19. print speed
  20. increase the speed
  21. call method changeSpeed()

Program: Sportscar.java

interface Vehicle{}

abstract class Car implements Vehicle{                                                                          /* class Car */
        private int year;
        private int speed;
        private String make;
        private static int count=0;
        public Car(int aYear, String aMake){
                year = aYear;
                make = aMake;
                speed = 0;
                count++;
        }
        public void accelerate(int aSpeed){                                                                     /* Method accelerate that increases the speed */
                this.speed += aSpeed;                           
        }                               
        public int getSpeed(){                                                                                                  /* method getSpeed() that returns the current speed of the car */
                return this.speed;
        }
        public abstract void sound();
        public static int getcount90(){
                return count;
        }
}

public class Sportscar extends Car{                                                                             /* class Sportscar */
        private String roof;                                                                                                    /* Add an instance variable roof of type String */
        private int doors;                                                                                                              /* Add an instance variable doors pf type int */
        
        public Sportscar(String roof, int doors, int year, String make){                /* define a constructor that accepts roos, doors, year and make */
                super(year, make);                                                                                                      /* call super class constructor and pass year and make to it */
                this.roof = roof;                                                                                                       /* assign roof and doors */
                this.doors = doors;
        }
        public void changeSpeed(){                                                                                              /* define the method changeSpeed() */
                if(getSpeed()+20>65){                                                                                                /* check if the speed increases beyond 65 */
                        System.out.println("Can not increate the speed ");                              /* print an error message if the speed increases beyond 65 */
                }
                else{
                        super.accelerate(20);                                                                                   /* otherwise increase the speed by 20 using accelerate() method */
                }
        }
        public void sound(){                                                                                                    /* implement the method sound() */
                System.out.println("brooom");                                                                           /* display "brooom" on the screen */
        }
        
        
        /*** THIS IS FOR TESTING *****/
        public static void main(String[] args){ 
                Sportscar sportscar = new Sportscar("softtop", 2, 2020, "Toyota");      /* create an object of type Sportscar */
                sportscar.sound();                                                                                                      /* call the method sound() */
                System.out.println("Increasing speed...");                                                      /* increase the speed */
                sportscar.changeSpeed();                                                                                        /* call method changeSpeed() */
                System.out.println("Speed :" + sportscar.getSpeed());                           /* print speed */
                System.out.println("Increasing speed...");                                                      /* increase the speed */
                sportscar.changeSpeed();                                                                                        /* call method changeSpeed() */
                System.out.println("Speed :" + sportscar.getSpeed());                           /* print speed */
                System.out.println("Increasing speed...");                                                      /* increase the speed */
                sportscar.changeSpeed();                                                                                        /* call method changeSpeed() */
                System.out.println("Speed :" + sportscar.getSpeed());                           /* print speed */
                System.out.println("Increasing speed...");                                                      /* increase the speed */
                sportscar.changeSpeed();                                                                                        /* call method changeSpeed() */
                System.out.println("Speed :" + sportscar.getSpeed());                           /* print speed */
        }
}

        
        
        

Screenshot:

Output:

Please don't forget to give a Thumbs Up.


Related Solutions

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 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...
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 Create an HourlyEmployee class that inherits from Employee and has two new instance variables: hours,...
JAVA Create an HourlyEmployee class that inherits from Employee and has two new instance variables: hours, which represents the hours worked, and wage, which represents the employee's pay per hour. (Both are doubles.) Create a constructor that takes the arguments first name, last name, social security number, hourly wage, and the number of hours worked. Also create accessors, mutators, an earnings method that returns the money earned by the employee this week, and a toString method that returns information about...
Create a class to represent a Mammal object that inherits from (extends) the Animal class. View...
Create a class to represent a Mammal object that inherits from (extends) the Animal class. View javadoc for the Mammal class and updated Animal class in homework 4 http://comet.lehman.cuny.edu/sfakhouri/teaching/cmp/cmp326/s19/hw/hw4/ Use the description provided below in UML. Mammal - tailLength : double - numLegs : int + Mammal() + Mammal(double, int) + Mammal(String, int, double, double, char, double, int) //pass values to parent’s overloaded constructor //and assign valid values to tailLength, numLegs or -1 if invalid + setTailLength(double) : void //if...
Java Project Tasks: Create a Coin.java class that includes the following:             Takes in a coin...
Java Project Tasks: Create a Coin.java class that includes the following:             Takes in a coin name as part of the constructor and stores it in a private string             Has a method that returns the coins name             Has an abstract getvalue method Create four children classes of Coin.java with the names Penny, Nickle, Dime, and Quarter that includes the following:             A constructor that passes the coins name to the parent             A private variable that defines the...
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...
Java (a) Create a class Router which stores the information of a router. It includes the...
Java (a) Create a class Router which stores the information of a router. It includes the brand, the model number (String) and the price (double, in dollars). Write a constructor of the class to so that the information mentioned is initialized when a Router object is created. Also write the getter methods for those variables. Finally add a method toString() to return the router information in the following string form. "brand: Linksys, model number: RVS4000, price: 1080.0" Copy the content...
Using the Java programming language: Create and implement a class Car that models a car. A...
Using the Java programming language: Create and implement a class Car that models a car. A Car is invented to have a gasoline tank that can hold a constant max of 12.5 gallons, and an odometer that is set to 0 for a new car. All cars have an original fuel economy of 23.4 miles per gallon, but the value is not constant. Provide methods for constructing an instance of a Car (one should be zero parameter, and the other...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT