JetBrains Academy: Abstract class - Kamil-Jankowski/Learning-JAVA GitHub Wiki
JetBrains Academy: Abstract class
IntBinaryOperation:
You are given an abstract class IntBinaryOperation
. It has one abstract method perform()
and two fields representing the operation arguments. The fields are initialized in the constructor. See the provided code template.
You need to declare and implement two classes representing concrete operations: Addition
and Multiplication
. The classes must extend the abstract class and implement the method perform()
. The implementations should work in accordance with the class name. Do not forget to write two-argument constructors in your classes.
Do NOT make your classes public
.
abstract class IntBinaryOperation {
protected int firstArg;
protected int secondArg;
public IntBinaryOperation(int firstArg, int secondArg) {
this.firstArg = firstArg;
this.secondArg = secondArg;
}
public abstract int perform();
}
class Addition extends IntBinaryOperation{
public Addition(int firstArg, int secondArg){
super(firstArg, secondArg);
}
public int perform(){
return firstArg + secondArg;
}
}
class Multiplication extends IntBinaryOperation{
public Multiplication(int firstArg, int secondArg){
super(firstArg, secondArg);
}
public int perform(){
return firstArg * secondArg;
}
}
Users and WebSites:
You are writing the application which collects information about what sites were visited by what users.
There are three classes: User
, WebSite
and Visit
. Many fields and methods of these classes are the same.
Write a new base abstract class named BaseEntity
. Provided classes must extend it. Move all repeating fields and methods to the new class.
abstract class BaseEntity{
protected long id;
protected long version;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
}
class User extends BaseEntity{
protected String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Visit extends BaseEntity{
protected User user;
protected WebSite site;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public WebSite getSite() {
return site;
}
public void setSite(WebSite site) {
this.site = site;
}
}
class WebSite extends BaseEntity{
protected String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
Task:
You have an abstract class Shape with two abstract methods: getPerimeter()
and getArea()
. See the provided code template.
You need to declare and implement three classes: Triangle
, Rectangle
and Circle. The classes must extend the Shape class and implement all abstract methods. To implement the methods the standard class Math may help you.
Note: do NOT make your classes public
.
- The class
Triangle
must have a constructor with three double arguments for setting the length of each side. To calculate the area of this shape you may use the Heron's formula. - The class
Rectangle
must have a constructor with two double arguments for setting the length of different sides. - The class
Circle
must have a constructor with one double argument for setting the radius. You may useMath.PI
as the PI constant or you can declare it yourself.
Examples: A circle has a radius = 10: perimeter = 62.831853..., area = 314.159265.... A triangle has three sides a = 3, b = 4 and c = 5: perimeter = 12.0, area = 6.0. A rectangle has two different sides a = 5, b = 10: perimeter = 30.0, area = 50.0.
abstract class Shape {
abstract double getPerimeter();
abstract double getArea();
}
class Triangle extends Shape{
private double a, b, c;
public Triangle(double a, double b, double c){
this.a = a;
this.b = b;
this.c = c;
}
public double getPerimeter(){
return a + b + c;
}
public double getArea(){
double heron = (a+b+c)/2;
return Math.sqrt(heron * (heron - a) * (heron - b) * (heron - c));
}
}
class Rectangle extends Shape{
private double a, b;
public Rectangle(double a, double b){
this.a = a;
this.b = b;
}
public double getPerimeter(){
return 2 * a + 2 * b;
}
public double getArea(){
return a * b;
}
}
class Circle extends Shape{
private double r;
public Circle(double r){
this.r = r;
}
public double getPerimeter(){
return 2 * Math.PI * r;
}
public double getArea(){
return Math.PI * Math.pow(r, 2);
}
}