Back to all lessons


Lesson 11

OOP

Task 0

Read the snippet below. What are differences between structured and object oriented programming?


// Structured programming
public class Main {
    public static void main(String[] args) {
        Point3D point = new Point3D();
        point.x = 10.0;
        point.y = 20.0;
        point.z = 30.0;
        System.out.println(point.distanceFromCenter());
    }
}

class Point3D {
    public double x, y, z;

    public double distanceFromCenter() {
        return Math.sqrt(x*x + y*y + z*z);
    }
}
                    

// Object oriented programming
public class Main {
    public static void main(String[] args) {
        Point3D point = new Point3D();
        point.x = 10.0;
        point.y = 20.0;
        point.z = 30.0;
        System.out.println(Point3D.distanceFromCenter(point));
    }
}

class Point3D {
    public double x, y, z;

    public static double distanceFromCenter(Point3D point) {
        return Math.sqrt(point.x*point.x + point.y*point.y + point.z*point.z);
    }
}
                    
Task 1

Read the snippet below. Create a program, that:

  1. replaces all 'a' with 'z'
  2. changes all upper cases to lower cases
  3. removes all whitespaces from the begin and end of the string
  4. checks if the string contains any 'c'

All functions you need you can find here: String class documentation.


public class StringFunctions {

    public static void main(String[] args) {   
        String sentence = "Dominus vitae necisque";

        String[] words = sentence.split(" ");
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            System.out.println(word.toUpperCase());
        }
    }
}
    
Task 2

Create a class "Student". Create fields for "name", "studentIndex" and one extra. Create an object of the class and fill with values. Create method "sayHello", which prints basic student's information and use the method in main.

Task 3

Create constructor of Student class. It should take all properties of object as arguments. Properties should be private. After you complete it: what is this keyword?


public class Main {
    public static void main(String[] args) {
        Point3D point = new Point3D(10.0, 20.0, 30.0);
        System.out.println(point.distanceFromCenter());
    }
}

class Point3D {
    private double x, y, z;

    Point3D(double x, double y, double z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public double distanceFromCenter() {
        return Math.sqrt(x*x + y*y + z*z);
    }
}
            
Task 4

Read snippet below. Find: object, constructor invoking, calling nonstatic method. Create a new class called EmailReader and a static method inside. The method reads email as a string from standard input (Scanner). Check if the value is valid or not and display a message i.e. "E-mail must contain '@' character.". If user passess valid email, the method returns a string. Otherwise, the method returns null.


import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter name");

        String name = scanner.nextLine();
        System.out.println("Your name is: " + name);
    }
}
            
Task 5

Create static method "readFromScanner" for the class Student, which:

  1. Reads name of student (use Scanner)
  2. Checks if name is valid
  3. Changes format to: one upper case + lower cases, i.e.: "John" instead of "jOHN"

Make this solution object oriented.

Task 6

Create a Profesor class, add some properties and constructor.

Task 7

Create an University class, add some properties and constructor. It should store information about many students and profesors.

There should be:

  • addProfesor(Profesor)
  • addStudent(Student)

Students identifiers should be generated automatically and uniquely (like s123, s124, s125, ...).

Task 8

Create a MainMenu class. It should store university and allow to manipulate data via standard input. User should select one action to do. After action has been completed, program displays menu one more time.

  • Add new profesor
  • Add new student
  • List all profesors
  • List all students
  • End program