Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ / instance variable declarations private int gears = 0 ; private double cost = 0 . 0 ; private double weight = 0 .

// instance variable declarations
private int gears =0;
private double cost =0.0;
private double weight =0.0;
private String color ="";
// constructor - default
public Bicycle(){
}
// constructor - String parameter
public Bicycle(String aColor){
this.color = aColor;
}
// constructor - int parameter
public Bicycle(int nbrOfGears){
this.gears = nbrOfGears;
}
// constructor - int, double, double, String parameters
public Bicycle(int nbrOfGears, double theCost, double theWeight, String aColor){
this.gears = nbrOfGears;
this.cost = theCost;
this.weight = theWeight;
this.color = aColor;
}
// method to output Bicycle's information
public void outputData(){
System.out.println("
Bicycle Details:");
System.out.println("Gears : "+ this.gears);
System.out.println("Cost : "+ this.cost);
System.out.println("Weight : "+ this.weight +" lbs");
System.out.println("Color : "+ this.color);
}
// method to output Bicycle's information - overloaded
//- method call chaining enabled
public Bicycle outputData(String bikeText){
System.out.println("
Bicycle "+ bikeText +" Details:");
System.out.println("Gears : "+ this.gears);
System.out.println("Cost : "+ this.cost);
System.out.println("Weight : "+ this.weight +" lbs");
System.out.println("Color : "+ this.color);
return this;
}
// Accessors (Getters)
public int getGears(){
return this.gears;
}
public double getCost(){
return this.cost;
}
public double getWeight(){
return this.weight;
}
public String getColor(){
return this.color;
}
// Mutators (Setters)- method call chaining enabled
public Bicycle setGears(int nbr){
this.gears = nbr;
return this;
}
public Bicycle setCost(double amt){
this.cost = amt;
return this;
}
public Bicycle setWeight(double lbs){
this.weight = lbs;
return this;
}
public Bicycle setColor(String theColor){
this.color = theColor;
return this;
}
}
package com.three19;
public class Driver {
public static void main(String[] args){
// Example calls to mutators
Bicycle myBike = new Bicycle();
myBike.setGears(24);
myBike.setCost(319.99);
myBike.setWeight(13.5);
myBike.setColor("Purple");
System.out.println("
myBike's color is "+ myBike.getColor());
// Example of calls to overloaded constructor
Bicycle myBike1= new Bicycle();
Bicycle myBike2= new Bicycle("Brown");
Bicycle myBike3= new Bicycle(22);
Bicycle myBike4= new Bicycle(22,319.99,13.5, "White");
myBike1.outputData("Nbr 1");
myBike2.outputData("Nbr 2");
myBike3.outputData("Nbr 3");
myBike4.outputData("Nbr 4");
// Example using method call chaining
Bicycle myBike5= new Bicycle(24,418.50,17.2, "Green");
myBike5.setColor("Peach").setGears(32).outputData("Number 5");
//"IS A" Checks
System.out.println("
\"IS A\" CHECKS");
// focus on myBike6
Bicycle myBike6= new Bicycle();
if (myBike6 instanceof Bicycle)
System.out.println("myBike6 Instance of Bicycle: True");
else
System.out.println("myBike6 Instance of Bicycle: False");
if (myBike6 instanceof TwoWheeled)
System.out.println("myBike6 Instance of TwoWheeled: True");
else
System.out.println("myBike6 Instance of TwoWheeled: False");
if (myBike6 instanceof Vehicle)
System.out.println("myBike6 Instance of Vehicle: True");
else
System.out.println("myBike6 Instance of Vehicle: False");
if (myBike6 instanceof Object)
System.out.println("myBike6 Instance of Object: True");
else
System.out.println("myBike6 Instance of Object: False");
// focus on TwoWheeled
TwoWheeled myTwoWheeled = new TwoWheeled();
if (myTwoWheeled instanceof Bicycle)
System.out.println("
myTwoWheeled Instance of Bicycle: True");
else
System.out.println("
myTwoWheeled Instance of Bicycle: False");
if (myTwoWheeled instanceof TwoWheeled)
System.out.println("myTwoWheeled Instance of TwoWheeled: True");
else
System.out.println("myTwoWheeled Instance of TwoWheeled: False");
if (myTwoWheeled instanceof Vehicle)
System.out.println("myTwoWheeled Instance of Vehicle: True");
else
System.out.print

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions