Java Lab Manual 2 with solution
Java Lab Manual 2 with solution
G MAHAVIDYALAYA
Java Technology
Assignments-1
Q1. Write a Java program to demonstrate the use of static methods. The program should contain a class
MathOperations that performs basic mathematical operations such as addition, subtraction, multiplication, and
division. Implement each of these operations as static methods.
class MathOperations {
OUTPUT
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0
Warning: Division by zero!
Division: 0.0
Q2 Write a Java program to demonstrate the use of a simple constructor. Create a class Person that represents a
person with attributes such as name and age
class Person {
// Instance variables
private String name;
private int age;
// Simple constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
Name: Alice
Age: 30
Name: Bob
Age: 25
Q3. Write a Java program to demonstrate the use of a simple constructor along with the this keyword. Create a
class Student that represents a student with attributes such as name, studentId, and grade.
class Student {
// Instance variables
private String name;
private int studentId;
private char grade;
OUTPUT
Name: John Doe
Student ID: 101
Grade: A
Q4. Write a Java program to demonstrate the use of wrapper classes. Create a class called
WrapperExample that includes methods to perform the following tasks:
1. Convert primitive data types (int, double, boolean) to their corresponding wrapper classes.
2. Convert wrapper class objects back to primitive data types.
3. Use the wrapper classes to perform some basic operations, such as checking if a number is odd or
even.
4. class WrapperExample {
5.
6. // Method to convert int to Integer
7. public Integer convertIntToInteger(int num) {
8. return Integer.valueOf(num);
9. }
10.
11. // Method to convert double to Double
12. public Double convertDoubleToDouble(double num) {
13. return Double.valueOf(num);
14. }
15.
16. // Method to convert boolean to Boolean
17. public Boolean convertBooleanToBoolean(boolean flag) {
18. return Boolean.valueOf(flag);
19. }
20.
21. // Method to convert Integer back to int
22. public int convertIntegerToInt(Integer num) {
23. return num.intValue();
24. }
25.
26. // Method to convert Double back to double
27. public double convertDoubleToDouble(Double num) {
28. return num.doubleValue();
29. }
30.
31. // Method to convert Boolean back to boolean
32. public boolean convertBooleanToBoolean(Boolean flag) {
33. return flag.booleanValue();
34. }
35.
36. // Method to check if a number is odd or even
37. public String checkOddOrEven(Integer num) {
38. if (num % 2 == 0) {
39. return num + " is even.";
40. } else {
41. return num + " is odd.";
42. }
43. }
44. }
45.
46. public class Main {
47. public static void main(String[] args) {
48. WrapperExample wrapperExample = new WrapperExample();
49.
50. // Primitive to Wrapper
51. int primitiveInt = 10;
52. Integer wrappedInt =
wrapperExample.convertIntToInteger(primitiveInt);
53. System.out.println("Wrapped Integer: " + wrappedInt);
54.
55. double primitiveDouble = 5.5;
56. Double wrappedDouble =
wrapperExample.convertDoubleToDouble(primitiveDouble);
57. System.out.println("Wrapped Double: " + wrappedDouble);
58.
59. boolean primitiveBoolean = true;
60. Boolean wrappedBoolean =
wrapperExample.convertBooleanToBoolean(primitiveBoolean);
61. System.out.println("Wrapped Boolean: " + wrappedBoolean);
62.
63. // Wrapper to Primitive
64. int unwrappedInt =
wrapperExample.convertIntegerToInt(wrappedInt);
65. System.out.println("Unwrapped Integer: " + unwrappedInt);
66.
67. double unwrappedDouble =
wrapperExample.convertDoubleToDouble(wrappedDouble);
68. System.out.println("Unwrapped Double: " + unwrappedDouble);
69.
70. boolean unwrappedBoolean =
wrapperExample.convertBooleanToBoolean(wrappedBoolean);
71. System.out.println("Unwrapped Boolean: " + unwrappedBoolean);
72.
73. // Check if wrapped integer is odd or even
74.
System.out.println(wrapperExample.checkOddOrEven(wrappedInt));
75. }
76. }
OUTPUT
Wrapped Integer: 10
Wrapped Double: 5.5
Wrapped Boolean: true
Unwrapped Integer: 10
Unwrapped Double: 5.5
Unwrapped Boolean: true
10 is even.
Q5. Write a Java program to demonstrate the concept of method overloading. Create a class called Calculator
that includes overloaded methods for performing addition.
class Calculator {