Ex 5 To 7
Ex 5 To 7
: 5
AIM:
To write a JAVA program with an interface Interest.
ALGORITHM:
Step 1: Start.
Step 2: Display the list of choices and get the choice from the user.
Step 3: If the choice is 1, compute get the principle amount, time and rate interest
and compute the simple interest.
Step 4: If the choice is 2, get the principle amount, time, rate of interest and no. of
times the interest is applied and compute the compound interest.
Step 5: Repeat the steps 2-4, if the user wants to continue.
Step 5: End.
PROGRAM:
import java.util.Scanner;
interface interest
{
public double simpleinterest();
public double compoundinterest();
double rate=25;
}
public class loan implements interest
{
private double p;
private int t,n;
loan(double p,int t)
{
this.p=p;
this.t=t;
}
loan(double p,int t,int n)
{
this.p=p;
this.t=t;
this.n=n;
}
public double simpleinterest()
{
return p*rate*t/100;
}
public double compoundinterest()
{
double r=rate/100.0;
return p*(Math.pow((1+(r/n)),(n*t)))-p;
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
char c='y';
while(c=='y'||c=='Y')
{
System.out.println("Interest Menu");
System.out.println("1. Simple Interest");
System.out.println("2. Compound Interest");
System.out.println("Enter your choice(1/2) : ");
int ch=s.nextInt();
if(ch==1||ch==2)
{
System.out.println("Enter the principle amount : ");
double p=s.nextDouble();
System.out.println("Enter the time period in years : ");
int t=s.nextInt();
s.nextLine();
if(ch==1)
{
loan si=new loan(p,t);
System.out.printf("The simple interest is %.2f\n",si.simpl
einterest());
}
else
{
System.out.println("Enter the no. of times interest applie
d per year : ");
int n=s.nextInt();
s.nextLine();
loan ci=new loan(p,t,n);
System.out.printf("The compound interest is %.2f\n",ci.com
poundinterest());
}
}
else
System.out.println("Invalid command!");
System.out.println("Would you like to continue(y/n) : ");
c=s.nextLine().charAt(0);
}
s.close();
}
}
OUTPUT:
RESULT:
The required program was executed successfully.
2. Write an interface to find area and perimeter. Using interface find area and
perimeter of square and circle.
AIM:
To write a JAVA program with an interface to find area and perimeter.
ALGORITHM:
Step 1: Start.
Step 2: Display the list of operations and get the choice from the user.
Step 3: If the choice is 1, get the side of the square and calculate its area and
perimeter.
Step 4: If the choice is 2, get the radius of the circle and calculate its area and
circumference.
Step 5: Repeat steps 2-4, if the user wants to continue.
Step 6: End.
PROGRAM:
import java.util.Scanner;
interface aandp
{
public double area();
public double perimeter();
}
class square implements aandp
{
private double side;
square(double side)
{
this.side=side;
}
public double area()
{
return Math.pow(side,2.0);
}
public double perimeter()
{
return 4*side;
}
}
class circle implements aandp
{
private double radius;
circle(double radius)
{
this.radius=radius;
}
public double area()
{
return 3.14*Math.pow(radius,2.0);
}
public double perimeter()
{
return 2*3.14*radius;
}
}
public class measure
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
char c='y';
while(c=='y'||c=='Y')
{
System.out.println("Measurement Menu");
System.out.println("1. Square");
System.out.println("2. Circle");
System.out.println("Enter the shape of your choice(1/2) : ");
int ch=s.nextInt();
if(ch==1)
{
System.out.println("Enter the side of square : ");
double side=s.nextDouble();
square sq=new square(side);
System.out.printf("The area of the sqaure is %.2f\n",sq.area()
);
System.out.printf("The perimeter of the square is %.2f\n",sq.p
erimeter());
}
else if(ch==2)
{
System.out.println("Enter the radius of circle : ");
double radius=s.nextDouble();
circle ci=new circle(radius);
System.out.printf("The area of the circle is %.2f\n",ci.area()
);
System.out.printf("The circumference of the circle is %.2f\n",
ci.perimeter());
}
else
System.out.println("Invalid command!");
s.nextLine();
System.out.println("Would you like to continue(y/n) : ");
c=s.nextLine().charAt(0);
}
s.close();
}
}
OUTPUT:
RESULT:
The required program was executed successfully.
3. Write a program to create interface named customer. In this keep the methods
called information(),show() and also maintain in the Tax rate. Implement this
interface in employee class and calculate the tax of the employee based on their
Income.
AIM:
To write a JAVA program with a interface customer.
ALGORITHM:
Step 1: Start.
Step 2: Get the employee details from the user.
Step 3: Display the employee details along with the tax he/she has to pay.
Step 4: Repeat the steps 2-3, if the user wants to continue.
Step 5: End.
PROGRAM:
import java.util.Scanner;
interface customer
{
public double information();
public void show();
double trate=5.0;
}
public class employees implements customer
{
private String name;
private double sal,tax;
employees(String name,double sal)
{
this.name=name;
this.sal=sal;
this.tax=information();
}
public double information()
{
return (sal*tax)/100.0;
}
public void show()
{
System.out.println("Employee Details\n");
System.out.println("Name : "+this.name);
System.out.printf("Salary : %.2f\n",this.sal);
System.out.printf("Tax : %.2f\n\n",this.tax);
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
char c='y';
while(c=='y'||c=='Y')
{
System.out.println("Enter the employee details : ");
System.out.println("Enter the name : ");
String name=s.nextLine();
System.out.println("Enter the salary : ");
double sal=s.nextDouble();
s.nextLine();
employees e=new employees(name,sal);
e.show();
System.out.println("Would you like to continue(y/n) :");
c=s.nextLine().charAt(0);
}
s.close();
}
}
OUTPUT:
RESULT:
The required JAVA program was executed successfully.
Exp No.: 6
Construct user defined packages
Aim:
To design a package for complex number addition and subtraction.
ALGORITHM:
a) Package:
Step 1: Start.
Step 2: Create a package ComplexNumbers, with a class ComplexNumbers.
Step 3: Define a method add(), to perform addition of complex numbers.
Step 4: Define a method sub(), to perform subtraction of complex numbers.
Step 5: End.
b) Implementation:
Step 1: Start.
Step 2: Import required packages.
Step 3: Get the two complex numbers from the user.
Step 4: Display the list of operations and get the user’s choice.
Step 5: If the choice is 1, perform the addition for the complex numbers.
Step 6: If the choice is 2, perform the subtraction for the complex numbers.
Step 7: Repeat steps 3-6, if the user wants to continue.
Step 8: End.
PROGRAM:
a) Package:
package ComplexNumbers;
public class ComplexNumbers
{
private int real,image;
public ComplexNumbers()
{ }
public ComplexNumbers(int real,int image)
{
this.real=real;
this.image=image;
}
public static ComplexNumbers add(ComplexNumbers a,ComplexNumbers b)
{
ComplexNumbers sum=new ComplexNumbers();
sum.real=a.real+b.real;
sum.image=a.image+b.image;
return sum;
}
public static ComplexNumbers sub(ComplexNumbers a,ComplexNumbers b)
{
ComplexNumbers diff=new ComplexNumbers();
diff.real=a.real-b.real;
diff.image=a.image-b.image;
return diff;
}
public String toString()
{
if(this.image<0)
return String.format("(%d - %di)",this.real,Math.abs(this.image));
else
return String.format("(%d + %di)",this.real,this.image);
}
}
Implementation:
import java.util.Scanner;
import ComplexNumbers.ComplexNumbers;
public class comp_nos
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
char c;
do
{
System.out.println("Enter the first complex no. : ");
System.out.println("Enter the real part : ");
int real1=s.nextInt();
System.out.println("Enter the imaginary part : ");
int image1=s.nextInt();
ComplexNumbers op1=new ComplexNumbers(real1,image1);
System.out.println("Enter the second complex no. : ");
System.out.println("Enter the real part : ");
int real2=s.nextInt();
System.out.println("Enter the imaginary part : ");
int image2=s.nextInt();
ComplexNumbers op2=new ComplexNumbers(real2,image2);
System.out.println("What is the operation? (1. Add, 2.Subtraction)
: ");
int ch=s.nextInt();
s.nextLine();
switch (ch)
{
case 1: ComplexNumbers sum=ComplexNumbers.add(op1,op2);
System.out.println(op1+" + "+op2+" = "+sum);
break;
case 2: ComplexNumbers diff=ComplexNumbers.sub(op1,op2);
System.out.println(op1+" - "+op2+" = "+diff);
break;
default:System.out.println("Invalid Command!");
}
System.out.println("Would you like to continue(y/n) : ");
c=s.nextLine().charAt(0);
}while(c=='y'||c=='Y');
s.close();
}
}
OUTPUT:
RESULT:
The required JAVA program was implemented and executed successfully.
Exp No.: 9
Programs using exception handling mechanisms
1. Write a java program with exceptional handler which handles three exceptions.
AIM:
To write a JAVA program with exceptional handler which handles three exception.
ALGORITHM:
Step 1: Start.
Step 2: Import required packages.
Step 3: Get the name of the park from the admin.
Step 4: Get the average daily revenue and customers.
Step 5: Details such as the length of the park’s name, if the park is zoo, 20 th
character in the park’s name, first 10 characters of the park’s name and average
contribution by a customer to revenue is displayed.
Step 6: In case of any discrepancy in data, corresponding message is displayed.
Step 7: End.
PROGRAM:
import java.util.Scanner;
public class park
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
try
{
System.out.println("Enter the name of the Park : ");
String pname=s.nextLine();
System.out.println("Enter the daily revenue : ");
int rev=s.nextInt();
System.out.println("Enter the approx. no of customers per day : ")
;
int n=s.nextInt();
s.nextLine();
System.out.println("Length of Park name : "+pname.length());
System.out.println("Is the park a Zoo : "+pname.contains("Zoo"));
System.out.println("The 20th character in Park's name : "+pname.ch
arAt(19));
System.out.println("The 10 characters of Park's name : "+pname.sub
string(0,10));
System.out.println("Avg. revenue per person : "+(rev/n));
}
catch(ArithmeticException a)
{
System.out.println(a);
}
catch(NullPointerException n)
{
System.out.println(n);
}
catch(StringIndexOutOfBoundsException st)
{
System.out.println(st);
}
s.close();
}
}
OUTPUT:
RESULT:
The required JAVA program was implemented and executed successfully.
2. Write a java program to input age from user and throw user-defined exception if
entered age is negative or age <18.
AIM:
To write a JAVA program to get input from the user and throw an user-defined
exception, if the age is negative or age<18.
ALGORITHM:
Step 1: Start.
Step 2: Import required packages.
Step 3: Get the age of the user.
Step 4: If the age is greater than or equal to 18, the user is granted entry acces.
Step 5: Else an exception is thrown prompting the minimum age for entry.
Step 6: Repeat steps 3-5, until an user is granted access.
Step 7: End.
PROGRAM:
import java.util.Scanner;
class AgeLessThanException extends Exception
{
public String toString()
{
return "Age has to be greater than or equal to 18 for access";
}
}
public class entry
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
while(true)
{
try
{
System.out.println("Enter the age : ");
int age=s.nextInt();
if(age<18)
{
throw new AgeLessThanException();
}
else
{
System.out.println("Access granted!");
break;
}
}
catch(AgeLessThanException e)
{
System.out.println(e);
continue;
}
}
s.close();
}
}
OUTPUT:
RESULT:
The required JAVA program was implemented and executed successfully.
3. Write a java program to register the students when their age is less than 12 and
weight is less than 40, if any of the condition is not met then the user should get an
ArithmeticException with the warning message “Student is not eligible for registration”.
AIM:
To write a JAVA program to register the students when their age is less than 12 and
weight is less than 40, throw an ArithmeticException if any of the conditions is not
meant.
ALGORITHM:
Step 1: Start.
Step 2: Import required packages.
Step 3: Get the age and weight of the student from user.
Step 4: If the age is less than 12 and weight is less than 40, the student can register
himself/herself.
Step 5: Else, an ArithmeticException with a prompt “Student is not eligible for
registration”, is thrown.
Step 6: Repeat steps 3-5, if the user wants to continue.
Step 7: End.
PROGRAM:
import java.util.Scanner;
public class register
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
char c;
do
{
try
{
System.out.println("Enter the age : ");
int age=s.nextInt();
System.out.println("Enter the weight : ");
int wei=s.nextInt();
if(age<12 && wei<40)
System.out.println("Registration success!");
else
throw new ArithmeticException("Student is not eligible for
registration");
}
catch(ArithmeticException a)
{
System.out.println(a);
System.out.println("Registration denied!");
}
System.out.println("Would you like to continue?(y/n) : ");
c=s.nextLine().charAt(0);
}while(c=='y'||c=='Y');
s.close();
}
}
OUTPUT:
RESULT:
The required JAVA program was implemented and executed successfully.