JAVA In the first part of this assignment, you will design and write a class tha
ID: 650028 • Letter: J
Question
JAVA
In the first part of this assignment, you will design and write a class that represents a real-world object of your choice. In the second part, you will write a program that demonstrates the use of the class.
Part I:
Select a "real-world" object that has not been used in class lecture and/or the textbook.
The object you choose must be defined by at least:
Have at least two characteristics (attributes).
Have at least two behaviors (operations).
The class that you write to represent the object must minimally meet the requirements of:
The "data" part must declare the attributes correctly.
The "operations" part must include at least the following methods:
A no-arg constructor method that creates an object with reasonable defaults for the initial state.
The set and get methods for each attribute.
Two methods that appropriately carry out the behaviors identified in the design.
Bonus (10 points) : A constructor that takes the initial values for the object's attributes from the user rather than setting defaults.
Part II: Write a "client" class that demonstrates the use of the object. Examples from class: RectangleDemo class & CircleDemo.
Explanation / Answer
public class Bike {
private double litres;
private double mileage;
private double speed;
public Bike(double l, double m,double s){ // constructor with parametres
litres = l;
mileage = m;
speed=s;
}
public Bike(){ //constructor without parametres
litres = 2;
mileage = 50;
speed=20;
}
public void setlitres(double litres){ // setter method for litres
litres = litres;
}
public void setBikemileage(double mileage){// setter method for mileage
mileage =mileage;
}
public void setspeed(double speed){// setter method for speed
speed =speed;
}
public double getLitres(){// getter method for litres
return litres;
}
public double getMileage(){// getter method for mileage
return mileage;
}
public double getSpeed(){// getter method for speed
return speed;
}
public double increaseSpeed() {// method to increas speed
return speed+1;
}
public double maxDistance() { // method to calculate distance
return litres*mileage;
}
}
-----------------------------------------------------------------------------------------
public class Client{
public static void main(String []args){
Bike R1=new Bike(5.0, 60.0,30.0);//create bike object with params
Bike R2=new Bike();//without params
System.out.println(R1.increaseSpeed());//calling methods
System.out.println(R1.maxDistance());
System.out.println(R2.increaseSpeed());
System.out.println(R2.maxDistance());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.