Question 1:
In the given WrapperClassExample code, the user is asked to input an integer, and then the code
calculates the square of that integer using a wrapper class. However, let's extend this code to
create a program that not only calculates the square of the entered integer but also checks whether
the original number is a perfect square or not.
A perfect square is an integer that can be expressed as the product of another integer with itself.
For example, 1, 4, 9, 16, etc., are perfect squares because they can be obtained by multiplying an
integer by itself.
Your Task:
Modify the provided code to include an additional step to determine if the entered integer is a
perfect square or not. Print an appropriate message indicating whether the input number is a
perfect square or not.
Hints:
After calculating the square value using the wrapper class, you need to check if the square value is
equal to the original input value multiplied by itself. If they are equal, the input value is a perfect
square; otherwise, it is not.
You can use an if statement to check the condition and then print the appropriate message based
on the outcome.
Title for Question 1: Perfect Square Check
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//System.out.print("Enter an integer: ");
int userInput = scanner.nextInt();
Integer wrappedInteger = Integer.valueOf(userInput); // Wrapping the int
int squaredValue = wrappedInteger * wrappedInteger; // Calculating the s
System.out.println("The square of " + wrappedInteger + " is: " + squared
scanner.close();
}
}
TestCases:
S.No Inputs Outputs
1 5 The square of 5 is: 25
2 -9 The square of -9 is: 81
3 40 The square of 40 is: 1600
4 25 The square of 25 is: 625
5 6 The square of 6 is: 36
6 -40 The square of -40 is: 1600
Question 2:
Problem: Car Inventory System
You are tasked with extending the provided Main class to create a basic car inventory system. In
the current implementation, the code captures information about a single car and displays its
details. Your goal is to modify the code to handle multiple cars and allow the user to input
information for multiple cars, storing them in an array or a similar data structure. After inputting the
car information, the program should display details for all the cars in the inventory.
Your Task:
Modify the provided code to create a basic car inventory system as described above.
Hints:
1. Consider using an array, ArrayList, or another appropriate data structure to store information
about multiple cars.
2. You will need to loop through the data structure to input information for each car and display
details for all the cars in the inventory.
Input Format:
The program expects the following inputs in the order mentioned:
Make of the car (a string)
Model of the car (a string)
Year of the car (an integer)
Output Format:
The program will output the information about the car in the following format.
Make: [make]
Model: [model]
Year: [year]
Title for Question 2: CLASS AND OBJECT
Solution:
import java.util.Scanner;
class Car {
String make;
String model;
int year;
void displayInfo() {
System.out.println("Make: " + make);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//System.out.print("Enter the make of the car: ");
String make = scanner.nextLine();
//System.out.print("Enter the model of the car: ");
String model = scanner.nextLine();
//System.out.print("Enter the year of the car: ");
int year = scanner.nextInt();
Car car = new Car();
car.make = make;
car.model = model;
car.year = year;
car.displayInfo();
scanner.close();
}
}
TestCases:
S.No Inputs Outputs
1 Ford F-150 Raptor 2023 Make: Ford Model: F-150 Raptor Year: 2023
2 Toyota Corolla 2022 Make: Toyota Model: Corolla Year: 2022
3 Honda City 2021 Make: Honda Model: City Year: 2021
Mercedes-Benz S-Class Maybach Make: Mercedes-Benz Model: S-Class Maybach
4
600 2030 600 Year: 2030
5 XX0 Make: X Model: X Year: 0
6 (empty) (empty) -2000 Make: (empty) Model: (empty) Year: -2000
Question 3:
Problem: Extend the given program to create a simple rectangle class that can also calculate and
display the perimeter of the rectangle.
Requirements:
Modify the Rectangle class to include a method calculatePerimeter() that calculates and returns the
perimeter of the rectangle using the formula: 2 * (width + height).
In the Main class, after calculating and displaying the area of the rectangle, modify the code to also
calculate and display the perimeter of the rectangle using the new method.
Run the program with different values for the width and height of the rectangle to verify that both
the area and perimeter calculations are correct.
Ensure that the program handles user input for the width and height of the rectangle as it does for
the area calculation.
Hint: You'll need to define the calculatePerimeter() method in the Rectangle class and call it from
the Main class after calculating the area.
Example Input:
Enter the width of the rectangle: 5.2
Enter the height of the rectangle: 3.8
Example Output:
Area: 19.76
Perimeter: 18.0
Title for Question 3: Finding Rectangle length using Class and Object
Solution:
import java.util.Scanner;
class Rectangle {
double width;
double height;
double calculateArea() {
return width * height;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//System.out.print("Enter the width of the rectangle: ");
double width = scanner.nextDouble();
//System.out.print("Enter the height of the rectangle: ");
double height = scanner.nextDouble();
Rectangle rectangle = new Rectangle();
rectangle.width = width;
rectangle.height = height;
double area = rectangle.calculateArea();
System.out.println("Area: " + area);
scanner.close();
}
}
TestCases:
S.No Inputs Outputs
1 5 10 Area: 50.0
2 48 Area: 32.0
3 12.5 8.12 Area: 101.49999999999999
4 08 Area: 0.0
5 9 18 Area: 162.0
6 30 40 Area: 1200.0
Question 4:
Problem: Modify the given program to add two additional operations: multiplication and division, to
the Calculator.Operations class.
Requirements:
Inside the Operations class in the Calculator class, create methods multiply and divide that take
two integer parameters and return their multiplication and division results respectively.
In the Main class, after displaying the addition and subtraction results, call the new multiply and
divide methods to calculate and display the multiplication and division of the two input numbers.
Run the program with different pairs of input numbers to verify that the new multiplication and
division operations are working correctly.
Hint: You'll need to define the multiply and divide methods in the Operations class, similar to how
the add and subtract methods are defined.
Input Format:
Input Format:
The program expects two integer inputs from the user. The inputs are read using the
scanner.nextInt() method. There are no specific prompts for input in the provided code, but you can
assume that the program will pause and wait for the user to input two integer values.
Output Format:
The program calculates and displays the results of addition and subtraction of the two input
numbers. The results are printed using System.out.println(). There are no specific labels for the
results, but you can assume that the output will be formatted as follows.
Addition: [Result of Addition]
Subtraction: [Result of Subtraction]
Result:
10
Sample Output:
Addition: 15
Subtraction: 5
Title for Question 4: Calculator
Solution:
import java.util.Scanner;
class Calculator {
class Operations {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
//System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
Calculator.Operations operations = new Calculator().new Operations()
System.out.println("Addition: " + operations.add(num1, num2));
System.out.println("Subtraction: " + operations.subtract(num1, num2)
scanner.close();
}
}
TestCases:
S.No Inputs Outputs
1 5 10 Addition: 15 Subtraction: -5
2 30 7 Addition: 37 Subtraction: 23
3 60 30 Addition: 90 Subtraction: 30
4 50 22 Addition: 72 Subtraction: 28
5 33 13 Addition: 46 Subtraction: 20
6 44 44 Addition: 88 Subtraction: 0
Question 5:
Problem: Modify the given program to calculate and display the product and the average of the two
input integers.
Requirements:
After calculating the sum of the two input integers, calculate their product (multiplication) and
display it along with a relevant label.
Calculate the average of the two input integers (sum divided by 2) and display it along with a
relevant label.
Run the program with different pairs of input integers to verify that the sum, product, and average
calculations are correct.
Hint: To calculate the product, you can use the multiplication operator *. To calculate the average,
sum up the two integers and then divide by 2.
Input Format:
The program expects two integer inputs from the user. The inputs are read using the
scanner.nextInt() method. There are no specific prompts for input in the provided code, but you can
assume that the program will pause and wait for the user to input two integer values.
Output Format:
The program calculates the sum of the two input numbers and displays the result. The result is
printed using System.out.println(). There is a label for the result, which is "Sum:", followed by a
space and the calculated sum.
Sum: [Calculated Sum]
Title for Question 5: Sum of two integer using Wrapper Class
Solution:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//System.out.print("Enter the first integer: ");
Integer num1 = scanner.nextInt(); // Using Integer wrapper class
// System.out.print("Enter the second integer: ");
Integer num2 = scanner.nextInt();
Integer sum = num1 + num2;
System.out.println("Sum: " + sum);
}
}
TestCases:
S.No Inputs Outputs
1 10 5 Sum: 15
2 0 20 Sum: 20
S.No Inputs Outputs
3 30 20 Sum: 50
4 20 22 Sum: 42
5 10 30 Sum: 40
6 30 34 Sum: 64