java lab
java lab
Output
Note: The return types of the above methods are not the
same. It is because method overloading is not associated with
return types. Overloaded methods may have the same or
different return types, but they must differ in parameters.
Why method overloading?
Suppose, you have to perform the addition of given numbers
but there can be any number of arguments (let’s say either 2
or 3 arguments for simplicity).
Output:
Arguments: 1
Arguments: 1 and 4
2. Method Overloading by changing the data type of
parameters
class MethodOverloading {
Output:
class HelperService {
Constructors Overloading
in Java
Similar to Java method overloading,
we can also create two or more
constructors with different parameters.
This is called constructors overloading.
Example 6: Java Constructor
Overloading
class Main {
String language;
obj1.getName();
obj2.getName();
}
}
Run Code
Output:
Programming Language: Java
Programming Language: Python
this Keyword
In Java, this keyword is used to refer to the current object
inside a method or a constructor. For example,
class Main {
int instVar;
Main(int instVar){
this.instVar = instVar;
System.out.println("this reference = " + this);
}
Output:
class MyClass {
// instance variable
int age;
// parameter
MyClass(int age){
age = age;
}
}
int age;
Main(int age){
age = age;
}
Output:
obj.age = 0
int age;
Main(int age){
this.age = age;
}
Output:
obj.age = 8
private int a, b;
@Override
public String toString(){
return this.a + " + " + this.b + "i";
}
// print objects
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
}
}
Run Code
Output:
2 + 3i
3 + 3i
0 + 0i
System.out.println(c1);
Output
Output
Where,
arrayName: is an identifier.
ArrayInputExample1.java
1. import java.util.Scanner;
2. public class ArrayInputExample1
3. {
4. public static void main(String[] args)
5. {
6. int n;
7. Scanner sc=new Scanner(System.in);
8. System.out.print("Enter the number of elements you want to
store: ");
9. //reading the number of elements from the that we want to e
nter
10. n=sc.nextInt();
11. //creates an array in the memory of length 10
12. int[] array = new int[10];
13. System.out.println("Enter the elements of the array: ");
14. for(int i=0; i<n; i++)
15. {
16. //reading array elements from the user
17. array[i]=sc.nextInt();
18. }
19. System.out.println("Array elements are: ");
20. // accessing array elements using the for loop
21. for (int i=0; i<n; i++)
22. {
23. System.out.println(array[i]);
24. }
25. }
26. }
Output:
Two-dimensional array input in Java
A two-dimensional array is an array that contains elements in
the form of rows and columns. It means we need both row
and column to populate a two-dimensional array. Matrix is
the best example of a 2D array. We can declare a two-
dimensional array by using the following statement.
Where,
arrayName: is an identifier.
ArrayInputExample2.java
1. import java.util.Scanner;
2. public class ArrayInputExample2
3. {
4. public static void main(String args[])
5. {
6. int m, n, i, j;
7. Scanner sc=new Scanner(System.in);
8. System.out.print("Enter the number of rows: ");
9. //taking row as input
10. m = sc.nextInt();
11. System.out.print("Enter the number of columns: ");
12. //taking column as input
13. n = sc.nextInt();
14. // Declaring the two-dimensional matrix
15. int array[][] = new int[m][n];
16. // Read the matrix values
17. System.out.println("Enter the elements of the array: ");
18. //loop for row
19. for (i = 0; i < m; i++)
20. //inner for loop for column
21. for (j = 0; j < n; j++)
22. array[i][j] = sc.nextInt();
23. //accessing array elements
24. System.out.println("Elements of the array are: ");
25. for (i = 0; i < m; i++)
26. {
27. for (j = 0; j < n; j++)
28. //prints the array elements
29. System.out.print(array[i][j] + " ");
30. //throws the cursor to the next line
31. System.out.println();
32. }
33. }
34. }
Output:
1. class FibonacciExample1{
2. public static void main(String args[])
3. {
4. int n1=0,n2=1,n3,i,count=10;
5. System.out.print(n1+" "+n2);//printing 0 and 1
6.
7. for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are
already printed
8. {
9. n3=n1+n2;
10. System.out.print(" "+n3);
11. n1=n2;
12. n2=n3;
13. }
14.
15. }}
Test it Now
Output:
0 1 1 2 3 5 8 13 21 34
Prime Number Program in Java
Prime number in Java: Prime number is a number that is
greater than 1 and divided by 1 or itself only. In other words,
prime numbers can't be divided by other numbers than itself
or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime
numbers.
Note: 0 and 1 are not prime numbers. The 2 is the only even prime number
because all the other even numbers can be divided by 2.