5) To the Bicycle class, add a method with the following signature: public boole
ID: 3758039 • Letter: 5
Question
5) To the Bicycle class, add a method with the following signature: public boolean equals (Bicycle other) - Methods returns true if the color and gear of the calling object (this) is the same as the color and gear of the bike passed to the method (other), otherwise returns false 6) Within the BicycleApp class, create another bicycle object (newBike) that is NOT part of the array. Color is purple and the gear is 1. - Use the equals method to determine if newBike is equal to any other bike in the array. If so, display “newBike object matches a current bike in the array” otherwise display "newBike object does NOT match a current bike in the array"
/* BicycleApp.java - application class for the Bicycle class
Explanation / Answer
import java.util.*;
public class HelloWorld
{
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.