Complete the code to declare a procedural method that prints a greeting.
public class Greeting { public static void [1]() { System.out.println("Hello, world!"); } }
The method name should be greet to match the call in procedural style.
Complete the code to create an object of class Person and call its method.
public class Person { public void sayHello() { System.out.println("Hi!"); } public static void main(String[] args) { Person p = new [1](); p.sayHello(); } }
To create an object of the class Person, you use new Person().
Fix the error in the procedural code by completing the method call.
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); } }
The method defined is named add, so the call must use add.
Fill both blanks to complete the OOP class with a constructor and a method call.
public class Car { private String model; public [1](String model) { this.model = model; } public void displayModel() { System.out.println("Model: " + [2]); } }
The constructor name must match the class name Car, and to access the instance variable inside the method, use this.model.
Fill all three blanks to create an object, call a method, and print the result.
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]); } }
We create an object named rect, store the area in result, and print rect.