Write a program in Java to develop overloaded constructor. Also develop the copy constructor to create a new object with the state of the existing object.

// Write a program in Java to develop overloaded constructor. Also develop the copy constructor to create a new object with the state of the existing object.
public class Code11 {
int n;
Code11() {
System.out.println("Default Constructor");
}
Code11(int x, int y) {
System.out.println("Addition is " + (x + y));
}
Code11(int a) {
n = a;
System.out.println("Number is " + n);
int i, j, flag;
for (i = 1; i <= n; i++) {
flag = 0;
for (j = 2; j <= i / 2; j++) {
if (i % j == 0) {
flag++;
break;
}
} if (flag == 0) {
System.out.println("Prime is " + i);
}
} } Code11(Code11 cp, int a) {
n = cp.n;
int max;
max = a > n ? a : n;
System.out.println("Maximum number is " + max);
} public static void main(String arg[]) {
Code11 cp1 = new Code11(5, 6);
Code11 cp2 = new Code11(5);
Code11 cp3 = new Code11(cp2, 5);
}
}

Comments

Popular posts from this blog

Write a program in Java to find second maximum of n numbers without using arrays