Object-oriented programming - Laboratory 2
Schedule
Next
Temat: IntelliJ - Java 2
IntelliJ
We will use IntelliJ on create Java program.
Second Java program
2 Classes
Constructor
static field
console interaction for constructor
Final code from classes
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Person firstPerson;
firstPerson = new Person("Rafal", 35);
Person secondPerson = new Person("Random", 44);
System.out.println("Give me the name");
String name = in.nextLine();
System.out.println("Give me the age");
int age = Integer.parseInt(in.nextLine());
Person thirdPerson = new Person(name, age);
number.coronavirus virus = new number.coronavirus();
virus.affectPerson(firstPerson);
virus.affectPerson(thirdPerson);
number.coronavirus virus2 = new number.coronavirus();
System.out.println("Number of viruses: " + virus.getNumberOfViruses());
}
}
public class Person
{
private String name;
private int age;
private static int numberOfPerson;
public Person(String name, int age)
{
this.name = name;
this.age = age;
numberOfPerson++;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class coronavirus
{
private Person affected;
private static int numberOfViruses;
coronavirus()
{
numberOfViruses++;
}
public int getNumberOfViruses()
{
return numberOfViruses;
}
}