PRACTICAL NUMBER-4
JAVA LAB BY ROSHANI MALI MAM.
NAME: NAFIS PARWEZ
ENROLL NO: A710145023050
SUBJECT: JAVA LAB
SUBJECT CODE: IFT4108
COURSE: MASTERS OF COMPUTER APPLICATIONS
SESSION: 2023-2025
1)Java Program to Count Number of Objects
Created for Class
1. public class Number_Objects
2. {
3. static int count=0;
4. Number_Objects()
5. {
6. count++;
7. }
8. public static void main(String[] args)
9. {
10. Number_Objects obj1 = new Number_Objects();
11. Number_Objects obj2 = new Number_Objects();
12. Number_Objects obj3 = new Number_Objects();
13. Number_Objects obj4 = new Number_Objects();
14. [Link]("Number of objects created:"+count);
15. }
16. }
Output:
$ javac Number_Objects.java
$ java Number_Objects
Number of objects created:4
2)Passing and Returning Objects in Java.
17. import [Link];
18. public class Object_Pass_Return
19. {
20. int length, breadth, area;
21. Object_Pass_Return area1(Object_Pass_Return object1)
22. {
23. object1 = new Object_Pass_Return();
24. [Link] = [Link];
25. [Link] = [Link];
26. [Link] = [Link] * [Link];
27. return object1;
28. }
29. Object_Pass_Return area2(Object_Pass_Return object2)
30. {
31. object2 = new Object_Pass_Return();
32. [Link] = [Link] + 5;
33. [Link] = [Link] + 6;
34. [Link] = [Link] * [Link];
35. return object2;
36. }
37. public static void main(String[] args)
38. {
39. Object_Pass_Return obj = new Object_Pass_Return();
40. Scanner s = new Scanner([Link]);
41. [Link]("Enter length:");
42. [Link] = [Link]();
43. [Link]("Enter breadth:");
44. [Link] = [Link]();
45. Object_Pass_Return a = obj.area1(obj);
46. Object_Pass_Return b = obj.area2(obj);
47. [Link]("Area:"+[Link]);
48. [Link]("Area:"+[Link]);
49. }
50. }
Output:
$ javac Object_Pass_Return.java
$ java Object_Pass_Return
Enter length:5
Enter breadth:6
Area:30
Area:120
3) Java Program to Illustrate Use of Methods
in a Class. Create different methods to
perform different arithmetic operations.
51. public class Method
52. {
53. void addition(int a,int b)
54. {
55. int c = a + b;
56. [Link]("Result:"+c);
57. }
58. void subtraction(int a,int b)
59. {
60. int c = a - b;
61. [Link]("Result:"+c);
62. }
63. void multiplication(int a,int b)
64. {
65. int c = a * b;
66. [Link]("Result:"+c);
67. }
68. void division(int a,int b)
69. {
70. double c =(double)a / b;
71. [Link]("Result:"+c);
72. }
73. public static void main(String args[])
74. {
75. Method obj = new Method();
76. [Link](10, 4);
77. [Link](10, 4);
78. [Link](10, 4);
79. [Link](10, 4);
80. }
81. }
Output:
$ javac [Link]
$ java Method
Result:14
Result:6
Result:40
Result:2.5
4)Java Program to Create a Method without Parameters
and with Return Type. Create method to calculate the
volume of a cuboid which takes the dimensions length,
breadth and height as input and return the volume as
output back to the main method.
82. class Box
83. {
84. double width;
85. double height;
86. double depth;
87. double volume()
88. {
89. Scanner s = new Scanner([Link]);
90. [Link]("enter width : ");
91. double width = [Link]();
92. [Link]("enter height : ");
93. double height = [Link]();
94. [Link]("enter depth: ");
95. double depth = [Link]();
96. return width * height * depth;
97. }
98. }
99. class ReturnDemo
100. {
101. public static void main(String args[])
102. {
103. Box cuboid = new Box();
104. double vol;
105. vol = [Link]();
106. [Link]("Volume is " + vol);
107. }
108. }
Output:
$ javac [Link]
$ java ReturnDemo
enter width : 10
enter height : 2
enter depth: 23
Volume is 460.0
Java Program to Find Area of Square, Rectangle and
Circle using Method Overloading
109. class OverloadDemo
110. {
111. void area(float x)
112. {
113. [Link]("the area of the square is "+[Link](x,
2)+" sq units");
114. }
115. void area(float x, float y)
116. {
117. [Link]("the area of the rectangle is "+x*y+" sq
units");
118. }
119. void area(double x)
120. {
121. double z = 3.14 * x * x;
122. [Link]("the area of the circle is "+z+" sq units");
123. }
124. }
125. class Overload
126. {
127. public static void main(String args[])
128. {
129. OverloadDemo ob = new OverloadDemo();
130. [Link](5);
131. [Link](11,12);
132. [Link](2.5);
133. }
134. }
Output:
$ javac [Link]
$ java OverloadDemo
the area of the square is 25.0 sq units
the area of the rectangle is 132.0 sq units
the area of the circle is 19.625 sq units
6. Java Program to illustrate constructor
public class Constructor
{
double width;
double height;
double depth;
Constructor()
{
[Link]("Constructor without parameter");
width = 10;
height = 10;
depth = 10;
}
Constructor(int a, int b, int c)
{
[Link]("Constructor with parameter");
width = a;
height = b;
depth = c;
}
double volume()
{
return width * height * depth;
}
}
class ConstructorDemo
{
public static void main(String args[])
{
Constructor cuboid1 = new Constructor();
double vol;
vol = [Link]();
[Link]("Volume is " + vol);
Constructor cuboid2 = new Constructor(8,11,9);
vol = [Link]();
[Link]("Volume is " + vol);
}
}