Tasks studies - laboratory
Abstract Classes
A class that declares at least one abstract method is an abstract class and must be marked with the abstract
specifier.
An abstract method does not have an implementation (body) and must be declared with the abstract
specifier. Abstract methods are methods for which we do not yet know the exact implementation (or do not want to specify it), but we know they should be included in the set of methods for any specific class inheriting the abstract class.
The concrete implementation (method definition) can vary greatly depending on the specific type of objects described by the class. Abstract classes cannot be instantiated (you cannot create objects of an abstract class).
Modify the Product
class from the previous lab to make it an abstract class (see the code below). Add encapsulation for the data in the class and create a constructor.
public abstract class Product {
private double price;
private String name;
private String describe;
public Product(String name, double price, String describe) {
this.name = name;
this.price = price;
this.describe = describe;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public String getDescribe() {
return describe;
}
public abstract void Buy();
public abstract void ShowInfo();
}
Create a subclass of Product
that is no longer abstract (see the code below).
public class Tractor extends Product {
public Tractor(String name, double price, String describe) {
super(name, price, describe);
}
public void Buy() {
System.out.println("I bought a black tractor, capacity 2400!");
}
public void ShowInfo() {
System.out.println("Price: " + getPrice() + " Name: " + getName() + " Description: " + getDescribe());
}
}
Using the Product
class, define the following products: book
, jam
, aspirin
, pen
, chocolate
.
Should products like clothes, medicines, or tools be described using abstract or non-abstract classes? Discuss.
Create a 10-element array of products and add the following items to the array: book
, chocolate
, book
, pen
, jam
, aspirin
, jam
, chocolate
, pen
, book
, tractor
.
Iterate through the collection using a loop and call the Buy()
and ShowInfo()
methods for each element. Analyze the displayed results and describe them.
Create an abstract class FiguraGeometryczna
(Geometric Figure) containing an abstract method obliczPole()
(calculate area). For each geometric figure, it should be possible to calculate its area.
Create two subclasses of FiguraGeometryczna
:
FiguraPlaska
(Flat Figure): Add an abstract method obliczObwod()
(calculate perimeter). Override the toString()
method to return the message: “Calculating parameters of a flat figure”.FiguraPrzestrzenna
(Spatial Figure): Add an abstract method obliczObjetosc()
(calculate volume). Override the toString()
method to return the message: “Calculating parameters of a spatial figure”.Create the following subclasses inheriting from FiguraPlaska
and FiguraPrzestrzenna
respectively:
Kwadrat
(Square), Prostokat
(Rectangle), Trojkat
(Triangle), Trapez
(Trapezoid), Rownoleglobok
(Parallelogram), Romb
(Rhombus), Kolo
(Circle), Szescian
(Cube), Prostopaloscian
(Cuboid), Kula
(Sphere), Walec
(Cylinder), Stozek
(Cone).
For these classes:
r
).toString()
method for each class. The returned string should concatenate:
toString()
method.area
, perimeter
, or volume
.Create a 15-element array containing various geometric figures. Iterate through the array using a while
loop and call the toString()
method for each element. Analyze the displayed results and describe them.