AIM:
To write a Java program for changing the height and width using cloning.
ALGORITHM:
Step 1: Start
Step 2: Define the class clonetest which implements cloneable
Step 3: Define the constructor clonetest
Step 4: Define the object clone
Step 5: Return super.clone()
Step 6: Create the object s1 for the class clonetest by assigning the value (4,2)
Step 7: Declare the object s2
Step 8: Display 1st and 2nd height and width before change
Step 9: Display 1st and 2nd height and width after change
Step 10: Stop
SOURCE CODE:
clonetest.java
import java.io.*;class clonetest implements Cloneable
{
int w,h;
clonetest(int w,int h)
{
this.w=w;
this.h=h;
}
public Object clone()
{
try
{
return super.clone();
}
catch(CloneNotSupportedException e)
{
System.out.println("Clone Error");
return this;
}
}
public static void main(String args[])
{
clonetest s1=new clonetest(4,2);
clonetest s2;
s2=(clonetest)s1.clone();
System.out.println("\nBefore change");
System.out.println("\n1st width = "+s1.w+" 1st height = "+s1.h);
System.out.println("\n2nd width = "+s2.w+" 2nd height = "+s2.h);
System.out.println("\nAfter change");
System.out.println("\n2nd width = "+(s2.w*10)+" 2nd height = "+(s2.h*10));
}
}
OUTPUT:
RESULT:
Thus the Java program for clonetest was performed and the output was verified.
.
EmoticonEmoticon