Lab Assignment – 09
1. WAP in java to create an abstract class figure(dim1,dim2) with appropriate constructor and
methods area and perimeter. Also create Rectangle and Triangle class which will inherit
figure class. In main method create objects of class and display area and perimeter.
abstract class Figure {
double dim1, dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
abstract double area();
abstract double perimeter();
}
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
double area() {
return dim1 * dim2;
}
double perimeter() {
return 2 * (dim1 + dim2);
}
}
class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
double area() {
return 0.5 * dim1 * dim2;
}
double perimeter() {
return dim1 + dim2 + [Link](dim1*dim1 + dim2*dim2);
}
}
public class AbstractDemo {
public static void main(String[] args) {
Figure r = new Rectangle(10, 5);
Figure t = new Triangle(3, 4);
[Link]("Rectangle Area: " + [Link]());
[Link]("Rectangle Perimeter: " + [Link]());
[Link]("Triangle Area: " + [Link]());
[Link]("Triangle Perimeter: " + [Link]());
}
}
2. WAP in java to show the use of interface, how to extend an interface and partial
implementation of an interface.
interface Shape {
void draw();
void color();
}
interface AdvancedShape extends Shape {
void resize();
}
// partial implementation
abstract class PartialImpl implements AdvancedShape {
public void draw() {
[Link]("Drawing shape...");
}
}
class Circle extends PartialImpl {
public void color() {
[Link]("Coloring circle...");
}
public void resize() {
[Link]("Resizing circle...");
}
}
public class InterfaceDemo {
public static void main(String[] args) {
Circle c = new Circle();
[Link]();
[Link]();
[Link]();
}
}
3. WAP to explain any checked and unchecked exception.
public class ExceptionDemo {
public static void main(String[] args) {
// Unchecked Exception
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Unchecked Exception: " + e);
}
// Checked Exception
try {
[Link]("Test"); // may throw ClassNotFoundException
} catch (ClassNotFoundException e) {
[Link]("Checked Exception: " + e);
}
}
}
4. WAP to explain user defined exception and use of throw, throws and finally.
class AgeException extends Exception {
AgeException(String msg) {
super(msg);
}
}
public class UserDefinedExceptionDemo {
static void checkAge(int age) throws AgeException {
if (age < 18) {
throw new AgeException("Not eligible");
} else {
[Link]("Eligible to vote");
}
}
public static void main(String[] args) {
try {
checkAge(16);
} catch (AgeException e) {
[Link]("Exception: " + [Link]());
} finally {
[Link]("Finally block executed");
}
}
}
5. WAP to create login page using AWT.
import [Link].*;
import [Link].*;
public class LoginPage extends Frame implements ActionListener {
Label l1, l2;
TextField t1, t2;
Button b;
LoginPage() {
setLayout(new FlowLayout());
l1 = new Label("Username:");
l2 = new Label("Password:");
t1 = new TextField(20);
t2 = new TextField(20);
[Link]('*');
b = new Button("Login");
[Link](this);
add(l1); add(t1);
add(l2); add(t2);
add(b);
setSize(300, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
[Link]("Login button clicked");
}
public static void main(String[] args) {
new LoginPage();
}
}
6. WAP to create registration of student form for a university using AWT container and
component.
import [Link].*;
public class StudentForm extends Frame {
StudentForm() {
setLayout(new GridLayout(6,2));
add(new Label("Name:"));
add(new TextField());
add(new Label("Email:"));
add(new TextField());
add(new Label("Course:"));
add(new TextField());
add(new Label("Gender:"));
Panel p = new Panel();
[Link](new Checkbox("Male"));
[Link](new Checkbox("Female"));
add(p);
add(new Label("Country:"));
Choice c = new Choice();
[Link]("India");
[Link]("USA");
[Link]("UK");
add(c);
add(new Button("Submit"));
setSize(400, 300);
setVisible(true);
}
public static void main(String[] args) {
new StudentForm();
}
}