This is a CIS 200 level project. I dont understand how to write whats asked for,
ID: 3758006 • Letter: T
Question
This is a CIS 200 level project. I dont understand how to write whats asked for, and need help!
The language used is : (Java)
This is the starter code for Bicycle.java:
/*
Defines the Bicycle class containing the following data propeties:
1) color of the bike
2) current gear the bike is in
Includes 4 constructors, a setColor and setGear methods, and toString method
Part 2 modification adds an equals method
*/
import java.util.Scanner;
public class Bicycle
{//instance variables
private int gear;
private String color;
// Constructor #1: set default gear to 1 and the color to primer gray
public Bicycle ()
{
} // end 'default' no-argument constructor
// Constructor #2: sets the gear to a passed-in value
// and color to same default color as constructor #1
public Bicycle (int newGear)
{
} // end one-argument constructor (sets the gear)
// Constructor #3: sets the color to the passed-in value
// and the gear to the same default gear as constructor #1
public Bicycle (String newColor)
{
} // end one-argument constructor (sets the color)
// Constructor #4: sets the gear to the passed-in value
// and sets the color as the passed-in value
public Bicycle (int newGear, String newColor)
{
} // end two-argument constructor (sets gear and color)
//Sets the private data property 'gear' with the value passed in
public void setGear(int newGear)
{
} //end of method setGear
//Sets the private data property 'color' with the value passed in
public void setColor(String newColor)
{
} //end of method setGear
//This method displays the value of the String variable 'color'
public void displayColor()
{ System.out.println ("The color of the bike is " + color);
} //end method displayColor
//This method displays the value of the int variable 'gear'
public void displayGear()
{ System.out.println ("The bike is in gear #" + gear);
} //end method displayGear
// Comment out the two methods above...
// Create a toString method that returns a String that can
// be used to display the data properties of a bicycle in the
// same format you are commenting out
// Create an equals method that returns true if the bikes are
// in the same gear and are the same color, otherwise returns false...
} //end class Bicycle
And the starter code for BicycleApp.java:
/* BicycleApp.java - application class for the Bicycle class
Creates an array of 4 bike objects, displays data properties of all using the toString method,
changes the gear and color on bike[0] then displays
the new data properties of all using the toString method
Part 2 - Create another bicycle object that is NOT part of the array
and use the equals method to determine if it is equal to any of the
other bikes in the array
*/
import java.util.*;
public class BicycleApp
{
public static void main(String[] args)
{
// declare an array of 4 Bicycle objects
// Instantiate the 4 bike objects (Done for you)
bikes[0] = new Bicycle();
bikes[1] = new Bicycle(6);
bikes[2] = new Bicycle ("purple");
bikes[3] = new Bicycle (10, "red");
// display initial state of all bicycles in the array using the toString method
// Done for you
for (int i = 0; i < bikes.length; i++)
System.out.println("Bike [" + i + "] " + bikes[i]);
// Read in a new color and gear for Bike[0] and
// use the set methods to change the 'state' of bike
Scanner in = new Scanner(System.in); // LOCAL variable
System.out.print ("Please enter the new color of the bike[0]: ");
String color = in.nextLine();
System.out.print ("Please enter the new current gear that the bike[0] is in: ");
int gear = in.nextInt();
// validate data
while ( (gear < 1) || (gear > 10) )
{ System.out.println (gear + " is an invalid gear ...");
System.out.print ("Valid gears are 1-10... please re-enter: ");
gear = in.nextInt();
} // end while
System.out.println("Changing the gear and color of bike[0]... ");
// call setGear method on bike[0]
// call setColor method on bike[0]
// display the current state of all bicycles in the array using the toString method
// see earlier code in this file
// Part 2 - Create another bicycle object (newBike) that is NOT part of the array
// Color is purple and the gear is 1
// Use a loop and equals method to determine if newBike is equal to any
// of the other bikes in the array
// If so, display "newBike object matches a current bike in the array"
// oherwise display "newBike object does NOT match a current bike in the array"
} //end of method main
} //end of class BicycleApp
Here are the instructions:
Thank you for your help in advance!
Explanation / Answer
package mani;
import java.util.Scanner;
public class Bicycle
{//instance variables
private int gear;
private String color;
// Constructor #1: set default gear to 1 and the color to primer gray
public Bicycle ()
{
this.color="gray";
this.gear=1;
} // end 'default' no-argument constructor
// Constructor #2: sets the gear to a passed-in value
// and color to same default color as constructor #1
public Bicycle (int newGear)
{
this.gear=newGear;
this.color="gray";
} // end one-argument constructor (sets the gear)
// Constructor #3: sets the color to the passed-in value
// and the gear to the same default gear as constructor #1
public Bicycle (String newColor)
{
this.color=newColor;
this.gear=1;
} // end one-argument constructor (sets the color)
// Constructor #4: sets the gear to the passed-in value
// and sets the color as the passed-in value
public Bicycle (int newGear, String newColor)
{
this.color=newColor;
this.gear=newGear;
} // end two-argument constructor (sets gear and color)
//Sets the private data property 'gear' with the value passed in
public void setGear(int newGear)
{
this.gear=newGear;
} //end of method setGear
//Sets the private data property 'color' with the value passed in
public void setColor(String newColor)
{
this.color=newColor;
} //end of method setGear
//This method displays the value of the String variable 'color'
public void displayColor()
{ System.out.println ("The color of the bike is " + color);
} //end method displayColor
//This method displays the value of the int variable 'gear'
public void displayGear()
{ System.out.println ("The bike is in gear #" + gear);
} //end method displayGear
// Comment out the two methods above...
// Create a toString method that returns a String that can
// be used to display the data properties of a bicycle in the
// same format you are commenting out
public String toString(){
return "Color of Bike: "+color+" The bike is in gear: "+gear;
}
// Create an equals method that returns true if the bikes are
// in the same gear and are the same color, otherwise returns false...
public boolean equals(Bicycle b){
if(this.gear==b.gear&&this.color.equalsIgnoreCase(b.color)){
return true;
}else{
return false;
}
}
} //end class Bicycle
//And the starter code for BicycleApp.java:
/* BicycleApp.java - application class for the Bicycle class
Creates an array of 4 bike objects, displays data properties of all using the toString method,
changes the gear and color on bike[0] then displays
the new data properties of all using the toString method
Part 2 - Create another bicycle object that is NOT part of the array
and use the equals method to determine if it is equal to any of the
other bikes in the array
*/
package mani;
import java.util.*;
public class BicycleApp
{
public static void main(String[] args)
{
Bicycle[] bikes=new Bicycle[4];
// declare an array of 4 Bicycle objects
// Instantiate the 4 bike objects (Done for you)
bikes[0] = new Bicycle();
bikes[1] = new Bicycle(6);
bikes[2] = new Bicycle ("purple");
bikes[3] = new Bicycle (10, "red");
// display initial state of all bicycles in the array using the toString method
// Done for you
for(Bicycle b:bikes){
System.out.println(b.toString());
}
for (int i = 0; i < bikes.length; i++)
System.out.println("Bike [" + i + "] " + bikes[i]);
// Read in a new color and gear for Bike[0] and
// use the set methods to change the 'state' of bike
Scanner in = new Scanner(System.in); // LOCAL variable
System.out.print ("Please enter the new color of the bike[0]: ");
String color = in.nextLine();
System.out.print ("Please enter the new current gear that the bike[0] is in: ");
int gear = in.nextInt();
// validate data
while ( (gear < 1) || (gear > 10) )
{ System.out.println (gear + " is an invalid gear ...");
System.out.print ("Valid gears are 1-10... please re-enter: ");
gear = in.nextInt();
} // end while
System.out.println("Changing the gear and color of bike[0]... ");
// call setGear method on bike[0]
bikes[0].setGear(gear);
// call setColor method on bike[0]
bikes[0].setColor(color);
for(Bicycle b:bikes){
System.out.println(b.toString());
}
// display the current state of all bicycles in the array using the toString method
// see earlier code in this file
// Part 2 - Create another bicycle object (newBike) that is NOT part of the array
Bicycle newBike=new Bicycle("purple");
for(Bicycle b:bikes){
if(b.equals(newBike)){
System.out.println("One of the bicycle from bikes array is equal to newBike");
}
}
// Color is purple and the gear is 1
// Use a loop and equals method to determine if newBike is equal to any
// of the other bikes in the array
// If so, display "newBike object matches a current bike in the array"
// oherwise display "newBike object does NOT match a current bike in the array"
} //end of method main
} //end of class BicycleApp
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.