Given a partial class definition: public class Bike { private String color; //co
ID: 3658700 • Letter: G
Question
Given a partial class definition: public class Bike { private String color; //color of the bicycle private int numberOfWheels; //number of wheels for the bike private static int numberOfBikes = 0; //count of bicycles created } (1) Write a constructor for the BIKE class that will take in two input arguments as the initial values of the variables (2) Write an overload constructor for the color of the bike. (3) Write a mutator method to set the color of the bike. (4) Write a accessor method that will get the color of the bike. (5) Write a main method that will use what you create in steps 1Explanation / Answer
public class Bike
{
private String color; //color of the bicycle
private int numberOfWheels; //number of wheels for the bike
private static int numberOfBikes = 0; //count of bicycles created
// (1) Write a constructor for the BIKE class that will take in two input arguments as the initial values of the variables
public Bike(String color, int numberOfWheels)
{
this.color = color;
this.numberOfWheels = numberOfWheels;
numberOfBikes++; // increment class variable
}
// (2) Write an overload constructor for the color of the bike.
public Bike(String color)
{
this(color, 2);
}
// (3) Write a mutator method to set the color of the bike.
public void setColor(String color)
{
this.color = color;
}
// (4) Write a accessor method that will get the color of the bike.
public String getColor()
{
return color;
}
// (5) Write a main method that will use what you create in steps 1 – 4
public static void main(String[] args)
{
// Create two bikes (give them your own names)
// i. One has one wheel and is purple
Bike bike1 = new Bike("purple", 1);
// ii. One has two wheels and is grey
Bike bike2 = new Bike("grey", 2);
// iii. Change the number of wheels on one of the bikes
bike2.numberOfWheels = 3; // no mutator requested
// iv. Then print out each bike with words stating “Bike …yourname… is …color… and has …x… wheels”)
System.out.println("Bike 1 is "+bike1.getColor()+" and has "+bike1.numberOfWheels+" wheels");
System.out.println("Bike 2 is "+bike2.getColor()+" and has "+bike2.numberOfWheels+" wheels");
// v. Then print out the number of bikes you created with words to explain your output “I created …..”
System.out.println("I created "+Bike.numberOfBikes+" bikes");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.