Back to all lessons


Lesson 14

Objects - learning

I. String class

Task 0

Name all new java language mechanisms. Next create a java program, that contains your name in a string object.


public class StringUsage {

    public static void main(String[] args) {   
        
        String a = new String("Hello there!");
        String b = "General Kenobi!";

        System.out.println("a: " + a);
        System.out.println("length of a: " + a.length());
        System.out.println("a + b: " + a + b);
    }

}
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

Read 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.".

II. Object Oriented Programming

Task 3

Read the snippet below. What is similar to string class usage? What is different?


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

Point3D object representation
Task 4

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.

Create a graph for object in notation from Task 3.

Task 5

Analyze snippet below. What exactly "static" means? Can we use fields in static method?


public class Main {
    public static void main(String[] args) {
        Point3D p = Point3D.generateRandomPoint();
        System.out.println(p.distanceFromCenter());
    }
}

class Point3D {
    public double x, y, z;

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

    public static Point3D generateRandomPoint() {
        Point3D randomPoint = new Point3D();
        randomPoint.x = Math.random() * 100;
        randomPoint.y = Math.random() * 100;
        randomPoint.z = Math.random() * 100;
        return randomPoint;
    }
}
                        

Point3D object representation
Task 6

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.