In: Computer Science
For this assignment, you will modify existing code to create a single Java™ program named BicycleDemo.java that incorporates the following:
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:
Using the NetBeans editor, adapt the BicycleDemo class as follows:
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(); } }
/*********************************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