1.
Write a program in java to create an abstract class Shape with two abstract
methods getArea() , getPerimeter() and a non-abstract printInfo() method. Create two
subclasses Circle and Rectangle, each implementing these abstract methods. The
application Demo class demonstrates the usage of these classes by creating objects of
Circle and Rectangle and invoking their methods.
Ans:
/ Abstract class
abstract class Shape {
// Abstract method to get area
public abstract double getArea();
// Abstract method to get perimeter
public abstract double getPerimeter();
// Concrete method
public void printInfo() {
[Link]("This is a shape.");
// Subclass Circle
class Circle extends Shape {
private double radius;
// Constructor
public Circle(double radius) {
[Link] = radius;
// Implementing abstract method to get area
@Override
public double getArea() {
return [Link] * radius * radius;
// Implementing abstract method to get perimeter
@Override
public double getPerimeter() {
return 2 * [Link] * radius;
// Subclass Rectangle
class Rectangle extends Shape {
private double width;
private double height;
// Constructor
public Rectangle(double width, double height) {
[Link] = width;
[Link] = height;
// Implementing abstract method to get area
@Override
public double getArea() {
return width * height;
// Implementing abstract method to get perimeter
@Override
public double getPerimeter() {
return 2 * (width + height);
}
public class Main {
public static void main(String[] args) {
Circle circle = new Circle(5);
[Link]("Circle Area: " + [Link]());
[Link]("Circle Perimeter: " + [Link]());
[Link]();
Rectangle rectangle = new Rectangle(4, 6);
[Link]("\nRectangle Area: " + [Link]());
[Link]("Rectangle Perimeter: " + [Link]());
[Link]();
2. Write a program in java having three classes Apple, Banana and Cherry. Class
Banana and Cherry are inherited from class Apple and each class has its own member
function show() . Using Dynamic Method Dispatch concept, display all the show()
method of each class.
Ans:
class Apple
{
void show()
{
[Link]("Apple show method");
}
}
class Banana extends Apple
{
void show()
{
[Link]("Banana show method");
}
}
class Cherry extends Apple
{
void show()
{
[Link]("Cherry show method");
}
}
class Demo
{
public static void main(String args[])
{
Apple ob1=new Apple();
Apple ob2=new Banana();
Apple ob3=new Cherry();
[Link]();
[Link]();
[Link]();
}
}
3. WAP to create an user defined exception named as NegativeValueException. This exception
is thrown when user enters negative value.
import [Link].*;
class NegativeValueException extends Exception{
NegativeValueException(){
[Link]("From const");
}
}
class negativeno{
public static void main (String data[]){
Scanner sc= new Scanner([Link]);
try{
[Link]("Enter an integer");
int n= [Link]();
if(n<0)
throw new NegativeValueException();
}
catch(NegativeValueException e){
[Link]("Caught "+ e);
}
finally{
[Link]("Exception successfully handled");
}
}
}
4. Create a user-defined exception named CheckArgumentException to check whether the
number of arguments passed through the command line is less than five. If the number of
arguments is less than five, throw the exception. Otherwise, find the largest among the five
numbers.
class CheckArgumentException extends Exception {
public CheckArgumentException(String message) {
super(message);
public class ArgumentCheck {
public static void main(String[] args) {
try {
if ([Link] < 5) {
throw new CheckArgumentException("At least 5 arguments are required.");
int max = Integer.MIN_VALUE;
for (int i = 0; i < 5; i++) {
int num = [Link](args[i]);
if (num > max)
max = num;
[Link]("Maximum number is: " + max);
} catch (CheckArgumentException e) {
[Link]("Exception: " + [Link]());
} catch (NumberFormatException e) {
[Link]("Please enter valid integers.");
5. Write a program in Java having a class called Address with 2 methods called getAddress()
and
setAddress(). The Address class will have three child classes named HomeAddress, OfficeAddress
and SchoolAddress having same functions as Address [Link] dynamic method dispatch
concept to override the derived class methods and display the address of home, office and
school accordingly.
class Address {
String address=" , Bhubaneswar, Odisha";
String getAddress()
{return address;
void setAddress(String address)
{[Link] = address;
}}
class HomeAddress extends Address {
String homeaddress;
String getAddress()
{return homeaddress+address;
void setAddress(String homeaddress)
{[Link] = homeaddress;
}}
class OfficeAddress extends Address {
String officeaddress;
String getAddress()
{return officeaddress;
void setAddress(String officeaddress)
{[Link] = officeaddress+address;
}}
class SchoolAddress extends Address {
String schooladdress;
String getAddress()
return schooladdress;
void setAddress(String schooladdress)
{[Link] =
schooladdress+address;
}}
public class Test {
public static void main(String args[])
Address a ;
a = new HomeAddress();
[Link]("Plot no-2,Gandhinagar");
String homeaddress = [Link]();
[Link]("Home address = "+homeaddress);
a = new OfficeAddress();
[Link]("KIIT,Patia Square");
String officeaddress = [Link]();
[Link]("Office address = "+officeaddress);
a = new SchoolAddress();
[Link]("DAV, ChandraSekharpur");
String schooladdress = [Link]();
[Link]("School address = "+schooladdress);
}}
6. Write a program in java to create a class complex having data members real and
imaginary and member functions add(complex c2)-- used to add two complex numbers and
display()-- used to display result of added complex numbers.
class Complex
double real, imaginary;
Complex(double r, double i)
[Link] = r;
[Link] = i;
Complex add(Complex c2)
Complex temp = new Complex(0, 0);
[Link] = [Link] + [Link];
[Link] = [Link] + [Link];
return temp;
void display()
[Link]("Sum of the complex numbers is: "+ [Link]+" + "+
[Link] +"i");
}
public class Demo
public static void main(String args[])
Complex c1 = new Complex(3.8, 5);
Complex c2 = new Complex(7.5, 7.6);
Complex c3 = [Link](c2);
[Link]();
7. WAP to create user defined exception named as InsufficientBalanceException. This
exception is thrown when the user’s account has insufficient balance.
class InsufficientBalanceException extends Exception{
// Constructor with custom message
public InsufficientBalanceException(String message)
super(message);
}}
class BankAccount{
private double balance;
public BankAccount(double balance)
[Link] = balance;
// Withdraw method which may throw the user-defined exception
public void withdraw(double amount) throws InsufficientBalanceException
if(amount > balance)
{
throw new InsufficientBalanceException("Withdrawal failed: Insufficient balance!");
else
balance -= amount;
[Link]("Withdrawal successful. Remaining balance: " + balance);
}}
public class MainApp{
public static void main(String[] args)
BankAccount account = new BankAccount(5000);
try
[Link](6000); // Trying to withdraw more than balance
catch (InsufficientBalanceException e)
[Link]("Exception caught: " + [Link]());
try
[Link](3000); // Valid withdrawal
catch (InsufficientBalanceException e)
[Link]("Exception caught: " + [Link]());
}}