Java OOP Assessment - Rectangle Program
Based on the given Rectangle program, answer the following questions.
import [Link].*;
class Rectangle{
int w; //width
int h; //height
//LINE-1: write the function setw(int) to initialize w
public void setw(int x)
{
w = x;
}
public void seth(int x)
{
h = x;
}
//LINE-2: write the function seth(int) to initialize h
public int area()
{
return w*h ;
}
//LINE-3: write the function area() to return area of rectangle
}
public class FClass{
public static void main(String[] args) {
Scanner sc= new Scanner([Link]);
int w = [Link]([Link]());
int h = [Link]([Link]());
Rectangle \ = new Rectangle();
[Link](w);
[Link](h);
int area = [Link]();
[Link](area);
}
}
Question 1
Identify the class and object in the program. Explain how this program demonstrates the
concept of Object-Oriented Programming (OOP).
Question 2
Explain the purpose of the methods setw(int x), seth(int x), and area(). What will happen if
the return type of area() is changed from int to void?
Question 3
If the input given is:
5
10
What will be the output? Justify your answer.
Question 4
If the variables w and h are changed to private, will the program still work? Explain your
reasoning.
Question 5
If the user inputs:
5
abc
What will happen?
A) Compilation Error
B) Runtime Error - NumberFormatException
C) Runtime Error - InputMismatchException
D) Output will be 0
Choose the correct answer and justify.