Question

In: Computer Science

For this assignment, you will modify existing code to create a single Java™ program named BicycleDemo.java...

For this assignment, you will modify existing code to create a single Java™ program named BicycleDemo.java that incorporates the following:

  • An abstract Bicycle class that contains private data relevant to all types of bicycles (cadence, speed, and gear) in addition to one new static variable: bicycleCount. The private data must be made visible via public getter and setter methods; the static variable must be set/manipulated in the Bicycle constructor and made visible via a public getter method.
  • Two concrete classes named MountainBike and RoadBike, both of which derive from the abstract Bicycle class and both of which add their own class-specific data and getter/setter methods.

Read through the "Lesson: Object-Oriented Programming Concepts" on The Java™ Tutorials website.

Download the linked Bicycle class, or cut-and-paste it at the top of a new Java™ project named BicycleDemo.

Download the linked BicycleDemo class, or cut-and-paste it beneath the Bicycle class in the BicycleDemo.java file.

Optionally, review this week's Individual "Week One Analyze Assignment," to refresh your understanding of how to code derived classes.

Adapt the Bicycle class by cutting and pasting the class into the NetBeans editor and completing the following:

  • Change the Bicycle class to be an abstract class.
  • Add a private variable of type integer named bicycleCount, and initialize this variable to 0.
  • Change the Bicycle constructor to add 1 to the bicycleCount each time a new object of type Bicycle is created.
  • Add a public getter method to return the current value of bicycleCount.
  • Derive two classes from Bicycle: MountainBike and RoadBike. To the MountainBike class, add the private variables tireTread (String) and mountainRating (int). To the RoadBike class, add the private variable maximumMPH (int).

Using the NetBeans editor, adapt the BicycleDemo class as follows:

  • Create two instances each of MountainBike and RoadBike.
  • Display the value of bicycleCount on the console.

Comment each line of code you add to explain what you added and why. Be sure to include a header comment that includes the name of the program, your name, PRG/421, and the date.

Rename your JAVA file to have a .txt file extension.

Submit your TXT file.

*******************CODE**************************

class Bicycle {

    int cadence = 0;
    int speed = 0;
    int gear = 1;

    void changeCadence(int newValue) {
         cadence = newValue;
    }

    void changeGear(int newValue) {
         gear = newValue;
    }

    void speedUp(int increment) {
         speed = speed + increment;   
    }

    void applyBrakes(int decrement) {
         speed = speed - decrement;
    }

    void printStates() {
         System.out.println("cadence:" +
             cadence + " speed:" + 
             speed + " gear:" + gear);
    }
}

***********************************SECOND CLASS********************************************

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

        // Create two different 
        // Bicycle objects
        Bicycle bike1 = new Bicycle();
        Bicycle bike2 = new Bicycle();

        // Invoke methods on 
        // those objects
        bike1.changeCadence(50);
        bike1.speedUp(10);
        bike1.changeGear(2);
        bike1.printStates();

        bike2.changeCadence(50);
        bike2.speedUp(10);
        bike2.changeGear(2);
        bike2.changeCadence(40);
        bike2.speedUp(10);
        bike2.changeGear(3);
        bike2.printStates();
    }
}

Solutions

Expert Solution

/*********************************Bicycle.java**************************/

package bicycle;
public abstract class Bicycle {

   private int cadence;
   private int speed;
   private int gear;
   private static int bicycleCount = 0;

   public Bicycle() {

       this.cadence = 0;
       this.speed = 0;
       this.gear = 0;
       bicycleCount++;
   }

   public static int getBicycleCount() {
       return bicycleCount;
   }

   void changeCadence(int newValue) {
       cadence = newValue;
   }

   void changeGear(int newValue) {
       gear = newValue;
   }

   void speedUp(int increment) {
       speed = speed + increment;
   }

   void applyBrakes(int decrement) {
       speed = speed - decrement;
   }

   void printStates() {
       System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear);
   }
}

/*******************************MountainBike.java*********************************/

package bicycle;

public class MountainBike extends Bicycle {

   private String tireTread;
   private int mountainRating;

   public MountainBike() {
       super();
       this.tireTread = "";
       this.mountainRating = 0;
   }

   public String getTireTread() {
       return tireTread;
   }

   public void setTireTread(String tireTread) {
       this.tireTread = tireTread;
   }

   public int getMountainRating() {
       return mountainRating;
   }

   public void setMountainRating(int mountainRating) {
       this.mountainRating = mountainRating;
   }
}

/*******************************RoadBike.java**************************/

package bicycle;

public class RoadBike extends Bicycle {

   private int maximumMPH;

   public RoadBike() {
       super();
       this.maximumMPH = 0;
   }

   public int getMaximumMPH() {
       return maximumMPH;
   }

   public void setMaximumMPH(int maximumMPH) {
       this.maximumMPH = maximumMPH;
   }

}

/******************************BicycleDemo.java*****************/

package bicycle;
class BicycleDemo {
   public static void main(String[] args) {

// Create two different
// Bicycle objects
Bicycle bike1 = new MountainBike();
Bicycle bike2 = new MountainBike();
Bicycle bike3 = new RoadBike();
Bicycle bike4 = new RoadBike();

// Invoke methods on
// those objects
bike1.changeCadence(50);
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();
bike2.changeCadence(50);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
bike3.changeCadence(50);
bike3.speedUp(10);
bike3.changeGear(2);
bike3.printStates();
  
System.out.println("Total Bikes: "+Bicycle.getBicycleCount());
}
}

/*************************output*********************/

cadence:50 speed:10 gear:2
cadence:40 speed:20 gear:3
cadence:50 speed:10 gear:2
Total Bikes: 4

Please let me know if you have any doubt or modify the answer, Thanks :)

Console X <terminated> BicycleDemo (1) [Java Application] C:\Pro cadence:50 speed: 10 gear:2 cadence:40 speed: 20 gear:3 cadence:50 speed: 10 gear:2 Total Bikes: 4


Related Solutions

Assignment Content Resource: ****************************CODE PASTED BELOW******************************* For this assignment, you will develop Java™ code that relies...
Assignment Content Resource: ****************************CODE PASTED BELOW******************************* For this assignment, you will develop Java™ code that relies on localization to format currencies and dates. In NetBeans, copy the linked code to a file named "Startercode.java". Read through the code carefully and replace all occurrences of "___?___" with Java™ code. Note: Refer to "Working with Dates and Times" in Ch. 5, "Dates, Strings, and Localization," in OCP: Oracle® Certified Professional Java® SE 8 Programmer II Study Guide for help. Run and debug...
For this assignment, you will be analyzing the Java™ code in the linked Week 3 Analyze...
For this assignment, you will be analyzing the Java™ code in the linked Week 3 Analyze Assignment Zip File, and predicting the results. You will also examine both the code and the output for inconsistencies and clarity. This Java™ code includes examples of for, while, and do-while loops. Carefully read through the code line by line, then answer the following questions in a Microsoft® Word document: What is the output of the program as it is written? What improvement(s) could...
For this assignment, you will apply what you learned in analyzing a simple Java™ program by...
For this assignment, you will apply what you learned in analyzing a simple Java™ program by writing your own Java™ program. The Java™ program you write should do the following: Display a prompt on the console asking the user to type in his or her first name Construct the greeting string "Hello, nameEntered!" Display the constructed greeting on the console Complete this assignment by doing the following: Download and unzip the linked zip file. Add comments to the code by...
For this assignment, you will apply what you learned in analyzing a simple Java™ program by...
For this assignment, you will apply what you learned in analyzing a simple Java™ program by writing your own Java™ program that creates and accesses an array of integers. The Java™ program you write should do the following: Create an array to hold 10 integers Ask the user for an integer. Note: This code has already been written for you. Populate the array. Note: The first element should be the integer input by the user. The second through tenth elements...
For this assignment, you will apply what you learned in analyzing Java™ code so far in...
For this assignment, you will apply what you learned in analyzing Java™ code so far in this course by writing your own Java™ program. The Java™ program you write should do the following: Accept user input that represents the number of sides in a polygon. Note: The code to do this is already written for you. If input value is not between 3 and 5, display an informative error message If input value is between 3 and 5, use a...
For this assignment, you will apply what you learned in analyzing Java™ code so far in...
For this assignment, you will apply what you learned in analyzing Java™ code so far in this course by writing your own Java™ program. The Java™ program you write should do the following: Accept user input that represents the number of sides in a polygon. Note: The code to do this is already written for you. If input value is not between 3 and 5, display an informative error message If input value is between 3 and 5, use a...
Create a new Java program named AllAboutMe (For JAVA we use blue J) Write code to...
Create a new Java program named AllAboutMe (For JAVA we use blue J) Write code to have the program print your name, favorite color, and three hobbies to a new text file called “AllAboutMe” using PrintStream. Submit code.
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
3. For this week’s assignment, you are asked to modify the above Java program to include...
3. For this week’s assignment, you are asked to modify the above Java program to include the following: - When the user clicks on the help menu, a drop-down list will appear with an item called About, then when the user clicks on it, the window will show some instructions about the functionality of the menu, e.g, what the edit menu does, etc. - When the user clicks on the File menu, a drop-down list will appear with one item...
In this assignment, you will create a Java program to search recursively for a file in...
In this assignment, you will create a Java program to search recursively for a file in a directory. • The program must take two command line parameters. First parameter is the folder to search for. The second parameter is the filename to look for, which may only be a partial name. • If incorrect number of parameters are given, your program should print an error message and show the correct format. • Your program must search recursively in the given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT