Object-oriented programming - Laboratory 5
Schedule
Next
Temat: Java 5
Fifth Java program
Method overriding
super keyword
final keyword
finalize
varargs
Final code from classes
package com.company;
import java.util.Scanner;
import java.util.Random;
public class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Random rand = new Random();
System.out.println(Person.getNumberOfPerson());
Student[] st = new Student[3];
st[0] = new Student("Rafal", rand.nextInt(100), 1);
st[1] = new Student("Random", 44, 1);
System.out.println("Give me the name of the student");
String name = in.nextLine();
System.out.println("Give me the student age");
int age = Integer.parseInt(in.nextLine());
System.out.println("Give me the student degree");
int degree = Integer.parseInt(in.nextLine());
st[2] = new Student(name, age, degree);
coronavirus virus = new coronavirus();
virus.affectPerson(st[rand.nextInt(3)]);
virus.affectPerson(st[rand.nextInt(3)]);
coronavirus virus2 = new coronavirus();
System.out.println("Number of viruses: " + coronavirus.getNumberOfViruses());
virus2 = null;
System.gc();
System.out.println("Number of viruses: " + coronavirus.getNumberOfViruses());
}
}
package com.company;
public class Person
{
protected final String name;
protected int age;
protected String accompanyingDisease;
private static int numberOfPerson;
static
{
numberOfPerson = 0;
}
public static int getNumberOfPerson()
{
return numberOfPerson;
}
public Person(String name, int age)
{
this.name = name;
this.age = age;
numberOfPerson++;
System.out.println("You are the "+this.getNumberOfPerson()+" person in the system.");
}
public Person(String name, int age, String accompanyingDisease)
{
this(name, age);
this.accompanyingDisease = accompanyingDisease;
}
public void presentYourself()
{
System.out.println("My name is "+this.name);
System.out.println("I have "+this.age+" years old");
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
package com.company;
public class Student extends Person{
private int studyDegree;
static final String university = "UAM";
public Student(String name, int age, int studyDegree)
{
super(name, age, "");
this.studyDegree = studyDegree;
}
public void presentYourself()
{
super.presentYourself();
System.out.println("My degree is "+studyDegree);
System.out.println("I'm a student of "+ university);
}
public int getStudyDegree() {
return studyDegree;
}
public void setStudyDegree(int studyDegree) {
this.studyDegree = studyDegree;
}
}
package com.company;
public class coronavirus extends virus
{
private static int numberOfViruses;
coronavirus()
{
numberOfViruses++;
}
@Override
public void finalize() {numberOfViruses--;}
public static int getNumberOfViruses()
{
return numberOfViruses;
}
}
package com.company;
public class fluvirus extends virus
{
private static int numberOfViruses;
fluvirus()
{
numberOfViruses++;
}
public static int getNumberOfViruses()
{
return numberOfViruses;
}
}
package com.company;
public class virus
{
private Person affected;
public void affectPerson(Person person)
{
this.affected = person;
}
protected void cleanup() {}
public String whoIsAffectedName()
{
return affected.getName();
}
}