0% found this document useful (0 votes)
3 views2 pages

Java Assessment1

The document presents a Java program that defines a Rectangle class with methods to set its width and height, and to calculate its area. It poses questions about the program's OOP concepts, method purposes, expected outputs based on user input, and the effects of changing variable access modifiers. Additionally, it explores error handling when invalid input is provided.

Uploaded by

V. Arun Sundar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Java Assessment1

The document presents a Java program that defines a Rectangle class with methods to set its width and height, and to calculate its area. It poses questions about the program's OOP concepts, method purposes, expected outputs based on user input, and the effects of changing variable access modifiers. Additionally, it explores error handling when invalid input is provided.

Uploaded by

V. Arun Sundar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

You might also like