Tutorial 2
Some material by Alvin Cherk (z5311001)
Assignment I is out, please have a read of the spec and start early!
Ensure that you open the correct folder
example: cs2511-project
Good
Bad
Name of folder open
The folder I want to actually open
How to view pipeline output?
View linting output
View tests output
Lines of code that caused the code to fail the test
Bonus: interface & abstract classes
What is it?
In object-oriented languages like Java, a class can inherit attributes and methods from another class
Inheritance represents a "is-a" relationship
Example
Animal
Tiger, Dog, Cat, ...
Tiger is an Animal, a Dog is an Animal, ...Tiger should have all the properties and functionalities of an AnimalAnimal when creating a class like Dog because this has obvious problemsSolution: make Dog a subclass of Animal using
the extends keyword in Java ✅
Superclass: Animal
Subclasses: Tiger, Dog, Cat, ...
Example
Animal
Tiger, Dog, Cat, ...
Change in requirements
Solution: make Animal into an abstract class ✅
Superclass: Animal
Subclasses: Tiger, Dog, Cat, ...
Animal must implement a method called makeSoundAnimal
Example
Animal
Tiger, Dog, Cat, ...
Whoops, another change in requirements
Solution:
Dog classSuperclass: Animal
Subclasses: Tiger, Dog, Cat, ...
Some subclasses of Animal (i.e. Dog) can play Catch with its owner
package shapes;
public class Shape {
public String color;
public Shape(String color) {
System.out.println("Inside Shape constructor");
this.color = color;
}
}
public class Rectangle extends Shape {
public int height;
public int width;
public Rectangle(String color) {
super(color);
System.out.println("Inside Rectangle constructor with one argument");
}
public Rectangle(String name, int width, int height) {
this(name);
this.width = width;
this.height = height;
System.out.println("Inside Rectangle constructor with three arguments");
}
public static void main(String[] args) {
Rectangle r = new Rectangle("red", 10, 20);
System.out.println(r.color);
System.out.println(r.width);
System.out.println(r.height);
}
}super and this?
super refers to the immediate parent classthis refers to the current classsuper(...) and this(...)?
super(...) is a constructor from the parent class and should be the first line in a child class constructorthis(...) acts as a current class constructor (can be used for overloading)package shapes;
public class Shape {
public String color;
public Shape(String color) {
System.out.println("Inside Shape constructor");
this.color = color;
}
}
public class Rectangle extends Shape {
public int height;
public int width;
public Rectangle(String color) {
super(color); // => Calling constructor of parent `Shape(String color)`
System.out.println("Inside Rectangle constructor with one argument");
}
public Rectangle(String name, int width, int height) {
this(name); // => Calling constructor `Rectangle(String color)`
this.width = width;
this.height = height;
System.out.println("Inside Rectangle constructor with three arguments");
}
public static void main(String[] args) {
// Rectangle(3 arguments) => Rectangle(1 argument) => Shape(1 argument)
Rectangle r = new Rectangle("red", 10, 20);
System.out.println(r.color);
System.out.println(r.width);
System.out.println(r.height);
}
}super and this?
super refers to the immediate parent classthis refers to the current classsuper(...) and this(...)?
super() is a constructor from the parent class and should be the first line in a child class constructorthis() acts as a current class constructor (can be used for method overloading)package circle;
public class Circle extends Object {
// Every class extends Object, it is not needed though
private static final double pi = 3.14159;
private int x, y;
private int r;
// Only 1 variable for all Circle objects
static int no_circles = 0;
public Circle() {
super(); // not needed
no_circles++;
}
public double circumference() {
return 2 * pi * r;
}
}What are static fields and methods?
Static fields: variables that are common and available to all instances of a class
They belong to the class, rather than a particular instance
JavaDoc
Single Line
// Single line commentMulti-line comment
/**
* This is multi-line
* documentation
*/JavaDoc Documentation
/**
* Constructor used to create a file
* @param fileName the name of the file
* @param content contents of the file
*//**
* File class that stores content under a file name
*/
public class File {
/**
* Constructor used to create a file
* @param fileName the name of the file
* @param content contents of the file
*/
public File(String fileName, String content) {}
/**
* Constructor used to make a partial file when receiving a new file
* I.e., content.length() != fileSize with no compression
* @param fileName
* @param fileSize
*/
protected File(String fileName, int fileSize) {}
/**
* Checks if transfer has been completed
* @return true if it has been completed
*/
public boolean hasTransferBeenCompleted() {}
}Employee.java & Manager.java
The java extension packs come with some features you can use to generate boilerplate code
Please do not generate the equals method. Write it yourself. This applies for Lab02 marking.
How do you write a sound equals method?
Since we are overriding an existing method (in the super most class called Object), we must follow its conditions
The conditions can be found in the Java Docs
The semantics of this was explored in a recent exam
static constants that are to be shared