Question

In: Computer Science

In java, design a class named Motorcycle that has the following fields: • year – The...


In java, design a class named Motorcycle that has the following fields:
• year – The year field is an int that holds the motorcycle’s year
• make – The make field references a String object that holds the make of the motorcycle.
• speed – The speed field is an int that holds the motorcycle’s current speed
The class should have the following constructor and other methods:
• Constructor – the constructor should accept the motorcycle’s year and make as arguments. These values should be
assigned to the object's year and make fields. The constructor should also assign 0 to the speed field.
• Accessors (getters) – Appropriate accessor methods should get the values stored in an objects year, make, and speed
fields.
• accelerate – the accelerate method should add 5 to the speed field each time it is called
• brake – the break method should subtract 5 from the speed field each time it is called.

Demonstrate the Motorcycle class in a program that creates a Motorcycle object, and then calls the accelerate method four times. After each call to the accelerate method, get the current speed of the motorcycle and display it. Then call the brake method three times. After each call to the brake method, get the current speed of the motorcycle and display it.

Create a UML class diagram for the Motorcycle class & include javadoc comments for each methods.

A sample run of demo program:

Enter the year your motorcycle was made
(User inputs year)

Enter the make of your motorcycle
(User inputs brand name)

Then Program prints to screen:

A 2017 (user inputted brand name) going 0.0 miles per hour.
A 2017 (user inputted brand name) going 10.00 miles per hour.
A 2017 (user inputted brand name) going 15.00 miles per hour.
A 2017 (user inputted brand name) going 10.00 miles per hour.

Solutions

Expert Solution

Hi,

Please find below code as per your requirement.

Added driver class MotorcycleTest.java to test functionality.

Let me know if you have any doubt/concern in answer via comments.

Hope this answer helps you.

Thanks.

/***********************JAVA CODE**********************/

/********************Motorcycle.java**********************/

/**
* This class used to perform accelerate and brake operation on motorcycle
*
*/
public class Motorcycle {

   //instance variable
   private int year;
   private String make;
   private int speed;

   /**
   * Parameterized constructor which accepting 2 parameters and speed set to 0.
   * @param year - year of motorcycle
   * @param make - make of motorcycle
   */
   public Motorcycle(int year, String make) {
       this.year = year;
       this.make = make;
       this.speed = 0;
   }

   /**
   * This is getter method for year
   * @return the year
   */
   public int getYear() {
       return year;
   }

   /**
   * This is getter method for make
   * @return the make
   */
   public String getMake() {
       return make;
   }

   /**
   * This is getter method for speed
   * @return the speed
   */
   public int getSpeed() {
       return speed;
   }

   /**
   * This method used to increase speed by 5.
   */
   public void accelerate() {
       this.speed += 5;
   }

   /**
   * This method used to decrease the speed by 5 till 0.
   */
   public void brake() {
       if(this.speed <= 0) {
           this.speed = 0;
       } else {
           this.speed -= 5;
       }
   }

}

/********************MotorcycleTest.java**********************/

import java.util.Scanner;

/**
* This class is driver class for Motorcycle class to test functionality
*
*/
public class MotorcycleTest {

   /**
   * This main method is starting point of this driver class which contains code to be tested.
   * @param args
   */
   public static void main(String[] args) {

       //Scanner class is used to take user input from console.
       Scanner sc = new Scanner(System.in);
      
       //Prompting user to give input for year
       System.out.print("Enter the year your motorcycle was made :");
       //Storing user input in variable
       int year = sc.nextInt();
      
       //Prompting user to give input for make
       System.out.print("Enter the make of your motorcycle :");
       //Storing user input in variable
       String make = sc.next();
      
       //Created class using constructor with user input given above
       Motorcycle motorcycle = new Motorcycle(year, make);
      
       //Calling accelerate method 4 times each time printing speed of motorcycle
       motorcycle.accelerate();
       System.out.println("A "+year+" "+make+" going "+ motorcycle.getSpeed() +" miles per hour.");
       motorcycle.accelerate();
       System.out.println("A "+year+" "+make+" going "+ motorcycle.getSpeed() +" miles per hour.");
       motorcycle.accelerate();
       System.out.println("A "+year+" "+make+" going "+ motorcycle.getSpeed() +" miles per hour.");
       motorcycle.accelerate();
       System.out.println("A "+year+" "+make+" going "+ motorcycle.getSpeed() +" miles per hour.");
      
       //Calling brake method 3 times each time printing speed of motorcycle
       motorcycle.brake();
       System.out.println("A "+year+" "+make+" going "+ motorcycle.getSpeed() +" miles per hour.");
       motorcycle.brake();
       System.out.println("A "+year+" "+make+" going "+ motorcycle.getSpeed() +" miles per hour.");
       motorcycle.brake();
       System.out.println("A "+year+" "+make+" going "+ motorcycle.getSpeed() +" miles per hour.");
      
   }

}


Related Solutions

Problem Statement Design a class named Car that has the following fields: year: The year field...
Problem Statement Design a class named Car that has the following fields: year: The year field is an integer that holds the car’s year. make: the make field is a String that holds the make of the car. speed: the speed field is an integer that holds the car’s current speed. In addition, the class should have the following constructor and other methods: Constructor: the constructor should accept the car’s year >model and make as parameters. These values should be...
In java, (include javadoc comments for each method) design a class named Contacts that has fields...
In java, (include javadoc comments for each method) design a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contacts objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to...
(Java) Design a class named Person with fields for holding a person’s name, address, and telephone...
(Java) Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods for the class’s fields. Next, design a class named Customer, which extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write one or more constructors and the appropriate mutator and...
Complete the following C++ tasks: a. Design a class named BaseballGame that has fields for two...
Complete the following C++ tasks: a. Design a class named BaseballGame that has fields for two team names and a final score for each team. Include methods to set and get the values for each data field. Create the class diagram and write the pseudocode that defines the class. b. Design an application that declares three BaseballGame objects and sets and displays their values. c. Design an application that declares an array of 12 BaseballGame objects. Prompt the user for...
JAVA/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone...
JAVA/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone number (all Strings) –Write a constructor that takes all the required information. –Write a constructor that only takes the name of the person and uses this to call the first constructor you wrote. –Implement the accessor and mutator methods. Make them final so subclasses cannot override them –Implement the toString() method •Design a class named Customer, which extends the Person class. It should have...
Design a class named Employee. The class should keep the following information in fields: ·         Employee...
Design a class named Employee. The class should keep the following information in fields: ·         Employee name ·         Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. ·         Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields...
Design a class named Employee. The class should keep the following information in fields: ·         Employee...
Design a class named Employee. The class should keep the following information in fields: ·         Employee name ·         Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. ·         Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
Write a java program using the following information Design a LandTract class that has two fields...
Write a java program using the following information Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a double), and one for the width (a double). The class should have:  a constructor that accepts arguments for the two fields  a method that returns the tract’s area(i.e. length * width)  an equals method that accepts a LandTract object as an argument. If the argument object holds the same data (i.e. length and...
Create a class named “Car” which has the following fields. The fields correspond to the columns...
Create a class named “Car” which has the following fields. The fields correspond to the columns in the text file except the last one. i. Vehicle_Name : String ii. Engine_Number : String iii. Vehicle_Price : double iv. Profit : double v. Total_Price : double (Total_Price = Vehicle_Price + Vehicle_Price* Profit/100) 2. Write a Java program to read the content of the text file. Each row has the attributes of one Car Object (except Total_Price). 3. After reading the instances of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT