Object-oriented programming - Laboratory 7


Schedule Next
Temat: Java 7

Seventh Java program

Abstract class

Final code from classes

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);

        Semester2 sem = new Semester2(2020); // composition
        sem.getOOPClass().addStudents(st[2], st[0]); // aggregation

//        Person whoAmI = new Student(name, age, degree);
//        whoAmI.presentYourself();
//        if (whoAmI instanceof Person) System.out.println("I'm a Person");
//        if (whoAmI instanceof Student) System.out.println("I'm a Student");

//        System.out.println("Number of Person in the system: "+Person.getNumberOfPerson());

        Virus[] viruses = new Virus[100];
        viruses[0] = new Coronavirus();
        viruses[0].affectPerson(st[rand.nextInt(3)]);
        viruses[0].affectPerson(st[rand.nextInt(3)]);
        viruses[1] = new Coronavirus();
        viruses[2] = viruses[1].createNewVirus(); // association
        viruses[3] = new Fluvirus();

        System.out.println("Number of viruses: " + Coronavirus.getNumberOfViruses());
        viruses[2] = null;
        System.gc();
        System.out.println("Number of viruses: " + Coronavirus.getNumberOfViruses());
    }
}
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;
    }
}
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;
    }
}
public class OOPClass
{
    private Student[] students;
    private int year;

    public OOPClass(int year, Student...s)
    {
        this.year = year;
        this.addStudents(s);
    }

    public void addStudents(Student...s)
    {
        int n = s.length;
        students = new Student[n];
        int i = 0;
        //Student x;
        for (Student x : s)
        {
            students[i] = x;
            i++;
        }
    }
}
public class Semester2
{
    private OOPClass objectOrientedProgrammingClass;
    private int year;

    public Semester2(int year)
    {
        objectOrientedProgrammingClass = new OOPClass(year);
        this.year = year;
    }

    public OOPClass getOOPClass()
    {
        return this.objectOrientedProgrammingClass;
    }
}
abstract class Virus
{
    private Person affected;

    public void affectPerson(Person person)
    {
        this.affected = person;
    }

    public String whoIsAffectedName()
    {
        return affected.getName();
    }

    abstract void tryInfectPerson(Person person);
    abstract Virus createNewVirus();

}
import java.util.Random;
import java.util.Scanner;

public class Fluvirus extends Virus
{
    private static int numberOfViruses;

    Fluvirus()
    {
        numberOfViruses++;
    }

    public static int getNumberOfViruses()
    {
        return numberOfViruses;
    }

    void tryInfectPerson(Person person)
    {
        Scanner in = new Scanner(System.in);
        Random rand = new Random();

        int x;
        System.out.println("Did you get cold in last 3 days. Give me 0-1 answer");
        x = Integer.parseInt(in.nextLine());
        if (x == 1) this.affectPerson(person);
    }

    public Virus createNewVirus()
    {
        Virus newVirus = new Fluvirus();
        return newVirus;
    }
}
public class Coronavirus extends Virus
{
    final static private float probabilityInfection = (float) 0.9;
    private static int numberOfViruses;

    Coronavirus()
    {
        numberOfViruses++;
    }

    public static int getNumberOfViruses()
    {
        return numberOfViruses;
    }

    public Virus createNewVirus()
    {
        Virus newVirus = new Coronavirus();
        return newVirus;
    }

    @Override
    void tryInfectPerson(Person person)
    {
         this.affectPerson(person);
    }
}