JetBrains Academy: Runtime type checking - Kamil-Jankowski/Learning-JAVA GitHub Wiki
Who is who:
You are given a class hierarchy consisting of three classes. The base class is Employee
. The first subclass is Developer
, the second subclass is DataAnalyst
.
Implement a method determineWhoIsWho
. The method takes an array of employees. Each element belongs to one of the listed classes. The method should output the type (DEV, EMP or DA) of each element in a new line.
Use the provided template for your method.
public static void determineWhoIsWho(Employee[] employees) {
for (Employee employee : employees) {
if (employee.getClass() == Developer.class){
System.out.println("DEV");
} else if(employee.getClass() == DataAnalyst.class){
System.out.println("DA");
} else {
System.out.println("EMP");
}
}
}
Counting 2D shapes:
Below is a part of a class hierarchy consisting of a lot of classes:
class Shape { }
class Shape2D extends Shape { }
class Shape3D extends Shape { }
class Circle extends Shape2D { }
// ... classes which extends Shape2D
class Cube extends Shape3D { }
// ... classes which extends Shape3D
Implement a method that takes an array of shapes and counts how many objects of classes that extend Shape2D
the array contains. Do not count objects of Shape2D
, only its subclasses.
public static int count2DShapes(Shape[] shapes) {
int counter = 0;
for (Shape shape : shapes){
if (shape instanceof Shape2D){
if (shape.getClass() != Shape2D.class){
counter++;
}
}
}
return counter;
}
Sort out the classes:
You are given 4 classes — Shape
, Polygon
, Square
, Circle
.
Classes Polygon
and Circle
both extend the class Shape
, the class Square
extends the class Polygon
.
You need to implement a method that takes Shape
array and adds every element to one of the provided Lists based on their class.
public static void sortShapes(Shape[] array,
List<Shape> shapes,
List<Polygon> polygons,
List<Square> squares,
List<Circle> circles) {
for (Shape shape : array){
if (shape instanceof Square){
squares.add((Square) shape);
} else if (shape instanceof Polygon){
polygons.add((Polygon) shape);
} else if (shape instanceof Circle){
circles.add((Circle) shape);
} else if (shape instanceof Shape){
shapes.add(shape);
}
}
}
Sum of the areas:
You are given the following classes:
class Shape {}
class Square extends Shape {
private int side;
public int getSide() {
return side;
}
public void setSide(int side) {
this.side = side;
}
}
class Rectangle extends Shape {
private int width, height;
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
Unfortunately, the author of this class hierarchy forgot to add the getArea()
method to these classes. Now you need to implement a method that calculates the sum of areas of the Shape
array. If some elements are instances of the class Shape
, their area equals 0.
public static int sumOfAreas(Shape[] array) {
int area = 0;
for (Shape shape : array){
if (Square.class.isInstance(shape)){
area += ((Square)shape).getSide() * ((Square)shape).getSide();
} else if (Rectangle.class.isInstance(shape)){
area += ((Rectangle)shape).getWidth() * ((Rectangle)shape).getHeight();
} else if (Shape.class.isInstance(shape)){
area += 0;
}
}
return area;
}