SRI BALAJI P.
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 {
// Static method for addition
public static int add(int a, int b) {
return a + b;
}
// Static method for subtraction
public static int subtract(int a, int b) {
return a - b;
}
// Static method for multiplication
public static int multiply(int a, int b) {
return a * b;
}
// Static method for division
public static double divide(int a, int b) {
if (b == 0) {
System.out.println("Warning: Division by zero!");
return 0.0;
}
return (double) a / b;
}
}
public class Main {
public static void main(String[] args) {
// Test the static methods
int a = 10;
int b = 5;
System.out.println("Addition: " + MathOperations.add(a, b));
System.out.println("Subtraction: " + MathOperations.subtract(a, b));
System.out.println("Multiplication: " + MathOperations.multiply(a, b));
System.out.println("Division: " + MathOperations.divide(a, b));
// Test division by zero
System.out.println("Division: " + MathOperations.divide(a, 0));
}
}
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;
}
// Method to display person details
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println();
}
}
public class Main {
public static void main(String[] args) {
// Create a person using the simple constructor
Person person1 = new Person("Alice", 30);
person1.displayInfo();
// Create another person using the simple constructor
Person person2 = new Person("Bob", 25);
person2.displayInfo();
}
}
OUTPUT
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;
// Simple constructor using 'this' keyword
public Student(String name, int studentId, char grade) {
this.name = name; // 'this.name' refers to the instance variable
this.studentId = studentId; // 'this.studentId' refers to the instance
variable
this.grade = grade; // 'this.grade' refers to the instance variable
}
// Method to display student details
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Student ID: " + studentId);
System.out.println("Grade: " + grade);
System.out.println();
}
}
public class Main {
public static void main(String[] args) {
// Create a student using the simple constructor
Student student1 = new Student("John Doe", 101, 'A');
student1.displayInfo();
// Create another student using the simple constructor
Student student2 = new Student("Jane Smith", 102, 'B');
student2.displayInfo();
}
}
OUTPUT
Name: John Doe
Student ID: 101
Grade: A
Name: Jane Smith
Student ID: 102
Grade: B
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 {
// Method to add two integers
public int add(int a, int b) {
return a + b;
}
// Method to add three integers
public int add(int a, int b, int c) {
return a + b + c;
}
// Method to add two double values
public double add(double a, double b) {
return a + b;
}
// Method to add an integer and a double
public double add(int a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
// Calling the overloaded add methods
int sum1 = calculator.add(5, 10); // two integers
System.out.println("Sum of two integers: " + sum1);
int sum2 = calculator.add(1, 2, 3); // three integers
System.out.println("Sum of three integers: " + sum2);
double sum3 = calculator.add(2.5, 3.5); // two double values
System.out.println("Sum of two doubles: " + sum3);
double sum4 = calculator.add(5, 4.5); // integer and double
System.out.println("Sum of an integer and a double: " + sum4);
}
}
OUTPUT
Sum of two integers: 15
Sum of three integers: 6
Sum of two doubles: 6.0
Sum of an integer and a double: 9.5