5) To the Bicycle class, add a method with the following signature: public boole
ID: 3757993 • 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
Declare a class in java using below example.
//Bicycle.java
public class Bicycle {
private String color;
public String getColor(){
return color;
}
public void setColor(String color){
this.color=color;
}
}
Constructor
Let's write same program by adding constructor.
//Bicycle.java
public class Bicycle {
private String color;
public Bicycle(){
super();
}
public Bicycle(String color){
super();
this.color=color;
}
public String getColor(){
return color;
}
public void setColor(String color){
this.color=color;
}
}
Now we have constructor so we can create objects now.
Creating an Object
//Factory.java
import com.javahunger.Bicycle;
public class Factory {
public static void main(String[] args){
Bicycle bicycleInstance = new Bicycle();
System.out.print(bicycleInstance.getColor());
}
}
Now we have instance so we access its attributes values.
Access variables and methods
//Bicycle.java
public class Bicycle {
public int numberOfGears;
private String color;
private static final String BICYCLE_TYPE = "Mountain";
public String getColor(){
return color;
}
}
//Factory.java
import com.javahunger.Bicycle;
public class Factory {
public static void main(String[] args){
Bicycle bicycleInstance = new Bicycle();
System.out.print(bicycleInstance.getColor());
System.out.print(bicycleInstance.BICYCLE_TYPE);
System.out.print(Bicycle.numberOfGears);
}
}
inheritance
The keyword extends is used to denote this inheritance relationship. Example
//Bicycle.java
public class Bicycle {
private String color;
public String getColor(){
return color;
}
public void setColor(String color){
this.color=color;
}
}
//MountainBike.java
public class MountainBike extends Bicycle{
public static void main(String[] args){
MountainBike mBike = new MountainBike();
System.out.println(mBike.getColor());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.