Professional Documents
Culture Documents
Java Progs
Java Progs
class Circle
{
double rad;
final double pi=3.14;
Circle(double r)
{
rad=r;
}
double area()
{
return pi*rad*rad;
}
double circum()
{
return 2*pi*rad;
}
}
class Cylinder extends Circle
{
double ht;
Cylinder(double r,double h)
{
super(r);
ht=h;
}
double area()
{
return circum()*ht;
}
double volume()
{
return area()*ht;
}
}
class Polytest
{
public static void main(String args[])
{
double area,surface_area,volume;
Circle cir=new Circle(5.5);
Cylinder cyl=new Cylinder(3.2,12.5);
area=cir.area();
System.out.println("area of the circle is"+area);
surface_area=cyl.area();
System.out.println("area of the cylinder is"+surface_area);
volume=cyl.volume();
System.out.println("volume of the cylinder is"+volume);
}
}
Output:
area of the circle is94.985
area of the cylinder is251.20000000000005
volume of the cylinder is3140.0000000000005
//inner class
class OuterClass
{
public OuterClass()
{
System.out.println("Outer Class");
//constructor for inner class
new InnerClass();
}
//defining Inner Class
private class InnerClass
{
InnerClass()
{
System.out.println("Inner Class or Local Class");
}
}
}
class inner
{
public static void main(String[] args)
{
new OuterClass();
}
}
Output:
Outer Class
Inner Class or Local Class
//Anonymous class
/*An anonymous class is a local class without a name.
//An anonymous class is defined and instantiated in a single succinct expression using the new
operator.
//While a local class definition is a statement in a block of Java code, an anonymous class definition
is an expression, which means that it can be included as part of a larger expression, such as a
method call.
//When a local class is used only once, consider using anonymous class syntax, which places the
definition and use of the class in exactly the same place. */
public class Anonymous
{
public static void main(String[] args)
{
// Here is where the anonymous class is defined and instantiated
Runnable runner = new Runnable()
{
int Total = 0;
public void run()
{
for (int i=0; i<10000000; i++)
{
Total++;
}
System.out.println(Integer.toString(Total));
}
};
// Here is where the anonymous class is defined and instantiated
runner.run();
}
}
OUTPUT:
10000000
class OuterClass
{
public OuterClass()
{
System.out.println("Outer Class");
//constructor for inner class
new InnerClass();
}
//defining Inner Class
private static class InnerClass
{
InnerClass()
{
System.out.println("Inner Class or Local Class");
}
}
}
class Staticinner
{
public static void main(String[] args)
{
new OuterClass();
}
}
OUTPUT
10000000
//Event Handling and Actions
import java.awt.*;
import java.awt.event.*;
public AwtEvent()
{
super("Event in Java awt");
setLayout(new BorderLayout());
try
{
Button button = new Button("INSERT_AN_URL_HERE");
button.addActionListener(this);
add(button, BorderLayout.NORTH);
Button button1 = new Button("INSERT_A_FILENAME_HERE");
button1.addActionListener(this);
add(button1, BorderLayout.SOUTH);
lbl = new Label("Roseindia.net");
add(lbl, BorderLayout.CENTER);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
catch (Exception e){}
setSize(400,400);
setVisible(true);
}
//Mouse Handler
//mouse event
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
getContentPane().add(textArea);
}
Output
:
//Object
Cloning
public class
MainClass2 {
public static void
main(String[] args)
{
Employee emp1
= new
Employee("MNO",
"A");
emp1.setSalary(40000.0);
Employee emp2 = emp1;
System.out.println("Before cloning");
System.out.println("Unchanged Value");
System.out.println(emp1);
System.out.println(emp2);
emp1.setLastName("ABC");
System.out.println("changed Value");
System.out.println(emp1);
System.out.println(emp2);
emp2=(Employee) emp1.clone();
System.out.println("After cloning");
System.out.println("Unchanged Value");
System.out.println(emp1);
System.out.println(emp2);
emp1.setLastName("XYZ");
System.out.println("changed Value");
System.out.println(emp1);
System.out.println(emp2);
}
}
class Employee {
private String lastName;
this.lastName = lastName;
}
Output:
Before cloning
Unchanged Value
Employee[A MNO, 40000.0]
Employee[A MNO, 40000.0]
changed Value
Employee[A ABC, 40000.0]
Employee[A ABC, 40000.0]
After cloning
Unchanged Value
Employee[A ABC, 40000.0]
Employee[A ABC, 40000.0]
changed Value
Employee[A XYZ, 40000.0]
Employee[A ABC, 40000.0]
//Type Parameters
Output:
//JLabel
import javax.swing.*;
import java.awt.*;