a)
What is Object? How to declare an Object in a JAVA program? Explain the Syntax
of Object Declaration.
b) Write Syntax to
assign an Object Reference Variable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
| //java class public class GCD { public static int gcd( int num1, int num2) { if (num2 == 0 ) { return num1; } else { return gcd(num2, num1%num2); } } } //main class import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print( "Enter the base number: " ); int num1 = input.nextInt(); System.out.print( "Enter the power: " ); int num2 = input.nextInt(); GCD access = new GCD(); System.out.print( "The GCD of 2 numbers is: " + access.gcd(num1, num2)); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
| //java class public class BubbleSort { public void bubbleSort( int [] arr){ for ( int i= 0 ; i<arr.length; i++){ for ( int j= 1 ; j<arr.length; j++){ if (arr[j]< arr[j- 1 ] ){ int temp = arr[j]; arr[j] = arr[j- 1 ]; arr[j- 1 ] = temp; } } } for ( int i= 0 ; i<arr.length; i++) { System.out.print(arr[i] + " " ); } } } //main class import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print( "Enter the size of the array: " ); int n = input.nextInt(); int [] x = new int [n]; System.out.print( "Enter " + n + " numbers: " ); for ( int i= 0 ; i<n; i++) { x[i] = input.nextInt(); } BubbleSort access = new BubbleSort(); System.out.print( "The Sorted numbers: " ); access.bubbleSort(x); } } |