0
0
Javaprogramming~10 mins

Procedural vs OOP approach in Java - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a procedural method that prints a greeting.

Java
public class Greeting {
    public static void [1]() {
        System.out.println("Hello, world!");
    }
}
Drag options to blanks, or click blank then click option'
Amain
Bgreet
Cprint
Dhello
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'main' instead of a custom method name.
Using 'print' which is not defined here.
2fill in blank
medium

Complete the code to create an object of class Person and call its method.

Java
public class Person {
    public void sayHello() {
        System.out.println("Hi!");
    }

    public static void main(String[] args) {
        Person p = new [1]();
        p.sayHello();
    }
}
Drag options to blanks, or click blank then click option'
APerson
BHuman
CObject
DString
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a different class name like 'Human' or 'Object' which is incorrect here.
3fill in blank
hard

Fix the error in the procedural code by completing the method call.

Java
public class Calculator {
    public static int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        int result = Calculator.[1](5, 3);
        System.out.println(result);
    }
}
Drag options to blanks, or click blank then click option'
Aplus
Bsum
Cadd
Dcalculate
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using method names not defined like 'sum' or 'plus'.
4fill in blank
hard

Fill both blanks to complete the OOP class with a constructor and a method call.

Java
public class Car {
    private String model;

    public [1](String model) {
        this.model = model;
    }

    public void displayModel() {
        System.out.println("Model: " + [2]);
    }
}
Drag options to blanks, or click blank then click option'
ACar
Bmodel
Cthis.model
DdisplayModel
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a different constructor name.
Using just 'model' instead of 'this.model' inside the method.
5fill in blank
hard

Fill all three blanks to create an object, call a method, and print the result.

Java
public class Rectangle {
    private int width;
    private int height;

    public Rectangle(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public int area() {
        return width * height;
    }

    public static void main(String[] args) {
        Rectangle [1] = new Rectangle(4, 5);
        int [2] = [1].area();
        System.out.println([2]);
    }
}
Drag options to blanks, or click blank then click option'
Arect
Bresult
Darea
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using the same variable name for object and result.
Not calling the method correctly.