Question
/* * Bicycle class * This class models basic state and behavior * for Bicycles. The class contains: * - 2 example private members, speed
/* * Bicycle class * This class models basic state and behavior * for Bicycles. The class contains: * - 2 example private members, speed and gear * - 2 constructors * - Accessor methods * - Mutator methods * - Other general purpose methods * * */ public class Bicycle { /* * members */ private int speed = 0; private int gear = 1; /* * constructors */ // no argument constructor public Bicycle() { } public Bicycle( int sp, int gr) { this.speed = sp; this.gear = gr; } /* * Methods */ public void speedUp(int amount) { speed = speed + amount; } public void slowDown(int amount) { speed = speed - amount; } /* * accessor methods */ public int getSpeed() { return speed; } public int getGear() { return gear; } /* * mutator methods */ public void setGear(int newValue) { this.gear = newValue; } public void setSpeed(int newSpeed) { this.speed = newSpeed; } } ///~
Create a class that uses the Bicycle class (download Bicycle.java from above )
Call the class BicycleDemo.java
This class will contain the main method
BicycleDemo.java must be in the same directory as Bicycle.java from above
Compile with javac *.java
Create two new Bicycle objects using constructors
Use the no-arg constructor
Use the constructor with arguments
Use accessors and mutators
set gear and speed of bike 1
set gear and speed of bike 2
get gear and speed of bike 1 and print
get gear and speed of bike 2 and print
Attempt to access members directly from demo program. What happens?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started