Help with Java homework. Thank you. 1. Abstract Class Write an abstract class fo
ID: 3882477 • Letter: H
Question
Help with Java homework. Thank you.
1. Abstract Class
Write an abstract class for bicycle. You can call it Bike or Bicycle. Declare fields numberOfTires (Wait... what? A bicycle has 2 tires. Except when it doesn't!
and numberOfFlats.
Declare 2 abstract methods ride and brake.
Remember you only declare the method, you do not implement it!
Next write a non-abstract subclass TwoWheeledBike. This will extend Bike (or Bicycle). Notice will need to now declare (write the body) of the methods.
Finally, we need a tester/driver to use our methods. It is the only class with the main.
2. Interfaces and Polymorphism
Write an interface Measurements that contains methods perimeter and area.
Write two classes which use the interface:
Rectangle
Square (include Math.pow in your Square class)
Write a driver(tester) class to accept user input necessary for the square and rectangle and print its calculations.
If you google compute the area of square using square calc: find P or rectangle rectangle calc: find P, you can find a nifty calculator.
Thank you again.
Explanation / Answer
1)
Bicycle.java
public abstract class Bicycle {
//Declaring variables
int numberOfTires = 2;
int numberOfFlats;
//Declaring abstract methods
abstract void ride();
abstract void brake();
}
________________
TwoWheeledBike.java
public class TwoWheeledBike extends Bicycle {
@Override
void ride() {
System.out.println("Riding a Bike");
}
@Override
void brake() {
System.out.println("Brakes Applied");
}
}
___________________
Driver.java
public class Driver {
public static void main(String[] args) {
//Creating An instance of TwoWheeledBike class
TwoWheeledBike twb = new TwoWheeledBike();
//Calling the methods on the TwoWheeledBike
twb.ride();
twb.brake();
}
}
__________________
Output:
Riding a Bike
Brakes Applied
___________________
2)
Measurements.java
public interface Measurements {
double area();
double perimeter();
}
_______________
Rectangle.java
public class Rectangle implements Measurements {
double length, width;
public Rectangle(double length, double width) {
super();
this.length = length;
this.width = width;
}
@Override
public double area() {
return length * width;
}
@Override
public double perimeter() {
return 2 * (length + width);
}
}
____________________
Square.java
public class Square implements Measurements {
double side;
public Square(double side) {
super();
this.side = side;
}
@Override
public double area() {
return Math.pow(side, 2);
}
@Override
public double perimeter() {
return 4 * side;
}
}
________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
double len, width, side;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
System.out.println(":: RECTANGLE ::");
System.out.print("Enter the length :");
len = sc.nextDouble();
System.out.print("Enter the width :");
width = sc.nextDouble();
Rectangle r = new Rectangle(len, width);
System.out.println("Dispalying Area and Perimeter of Rectangle");
//Displaying the output
double ar = r.area();
System.out.println("Area of the Rectangle :" + ar);
double peri = r.perimeter();
System.out.println("Perimeter of the Rectangle :" + peri);
System.out.println(":: SQUARE ::");
System.out.print("Enter the Side of Square :");
side = sc.nextDouble();
Square sq = new Square(side);
System.out.println("Dispalying Area and Perimeter of Square");
//Displaying the output
ar = sq.area();
System.out.println("Area of the Square :" + ar);
peri = sq.perimeter();
System.out.println("Perimeter of the Square :" + peri);
}
}
________________
Output:
:: RECTANGLE ::
Enter the length :6
Enter the width :7
Dispalying Area and Perimeter of Rectangle
Area of the Rectangle :42.0
Perimeter of the Rectangle :26.0
:: SQUARE ::
Enter the Side of Square :5
Dispalying Area and Perimeter of Square
Area of the Square :25.0
Perimeter of the Square :20.0
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.