0% found this document useful (0 votes)
71 views

Oops Record

The document contains 7 Java programs to demonstrate different concepts: 1. A program to find the biggest of 3 numbers using if/else statements. 2. A program to check if a number has 1, 2, or 3 digits using if/else. 3. A simple calculator program using switch case. 4. A program to grade a student based on marks percentage using switch case. 5. A program to calculate sum of digits in a number using while loop. 6. A program to count positive, negative and zero numbers input by user using while loop. 7. A program to check if a number is an Armstrong number using while loop.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Oops Record

The document contains 7 Java programs to demonstrate different concepts: 1. A program to find the biggest of 3 numbers using if/else statements. 2. A program to check if a number has 1, 2, or 3 digits using if/else. 3. A simple calculator program using switch case. 4. A program to grade a student based on marks percentage using switch case. 5. A program to calculate sum of digits in a number using while loop. 6. A program to count positive, negative and zero numbers input by user using while loop. 7. A program to check if a number is an Armstrong number using while loop.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 58

EX: 2(a)

THE BIGGEST OF 3 NUMBERS


DATE: 28/08/2021

AIM:
Write a Java program to find the biggest among the three numbers using if else.

ALGORITHM:
1. Start.
2. Initialize 3 int values a,b,c.
3. Get their values from the user.
4. If the a value given by the user is greater than both b and c values, then print a is big.
5. Else, if b is greater than a and c, then print b is big.
6. Else print c is big.
7. Stop.

PROGRAM:
import java.util.Scanner;
public class biggest {
public static void main(String[] args)
{
int a, b, c;
Scanner s = new Scanner(System.in);
System.out.println("Enter the first number:");
a = s.nextInt();
System.out.println("Enter the second number:");
b = s.nextInt();
System.out.println("Enter the third number:");
c = s.nextInt();

if( a > b && a > c)


System.out.println(a + " is the largest number.");

else if (b > a && b > c)


System.out.println(b + " is the largest number.");

else
System.out.println(c + " is the largest number.");
}
}

OUTPUT:

RESULT:
The biggest among the 3 numbers is found and the output is executed.
EX: 2(b)
CHECK WHETHER THE GIVEN NUMBER HAVE 1,2 OR 3 DIGIT(S)
DATE: 28/08/2021

AIM:
Write a Java program to check the given number have one,two or three digits using if
else.

ALGORITHM:
1. Start.
2. Initialize int a.
3. Get the value for a from the user.
4. If the value of a is less than 10, then print that it has one digit.
5. Else, if the value of a is from 10 to 99, then print that it has two digits.
6. Else, if the value of a is from 100 to 999, then print that it has three digits.
7. Else, print it has more than three digits.
8. Stop.

PROGRAM:
import java.util.Scanner;
public class DIGITS {
public static void main(String[] args)
{
int a;
Scanner s = new Scanner(System.in);
System.out.println("Enter the number:");
a = s.nextInt();

if( a >= 0 && a < 10)


System.out.println("Single Digit");
else if ( a >= 10 && a < 99)
System.out.println("Double Digit");
else if ( a >= 100 && a < 999)
System.out.println("Triple Digit");
else
System.out.println("More than 3 digits");
}
}

OUTPUT:

RESULT:
The number of digits of the given program is found and the out is executed.
EX: 2(c)
A SIMPLE CALCULATOR USING SWITCH CASE
DATE: 28/08/2021

AIM:
Write a Java program to make a simple calculator using switch case.

ALGORITHM:
1. Start.
2. Initialise the double values n1,n2 and result.
3. Get the values of n1 and n2 from the user.
4. Initialise the character choice and get the value from the user.
5. Use choice as the case.
6. Do the actions according to the case selected.
7. Print the result value.
8. Stop.

PROGRAM:
import java.util.Scanner;
public class CALC {
public static void main(String[] args) {

char choice;
Double n1, n2, result;
Scanner input = new Scanner(System.in);
System.out.println("Choose an operator: +, -, *, or /");
choice = input.next().charAt(0);
System.out.println("Enter first number");
n1 = input.nextDouble();
System.out.println("Enter second number");
n2 = input.nextDouble();

switch (choice) {
case '+':
result = n1 + n2;
System.out.println(n1 + " + " + n2 + " = " + result);
break;
case '-':
result = n1 - n2;
System.out.println(n1 + " - " + n2 + " = " + result);
break;
case '*':
result = n1 * n2;
System.out.println(n1 + " * " + n2 + " = " + result);
break;
case '/':
result = n1 / n2;
System.out.println(n1 + " / " + n2 + " = " + result);
break;

default:
System.out.println("Invalid operator!");
break;
}

input.close();
}
}
OUTPUT:

RESULT:
The simple calculator is made and the expected output is obtained.
EX: 2(d)
GRADE FOR THE MARK GIVEN USING SWITCH
DATE: 28/08/2021

AIM:
Write a Java program to grade a student with the mark given.

ALGORITHM:
1. Start.
2. Initialise integers total marks and percentage.
3. Get the total mark from the user.
4. Calculate the percentage of total marks.
5. Use the percentage divided by 10 as your case.
6. Grades will be awarded based on the cases.
7. Print the grade the student/user got.
8. Stop.

PROGRAM:
import java.util.Scanner;
public class grade {
public static void main(String[] args) {

float totalMarks, percentage;

Scanner s = new Scanner(System.in);


System.out.println("Enter Total MArks : " );
totalMarks=s.nextFloat();
percentage = (totalMarks / 500) * 100;
switch ((int) percentage / 10) {
case 9:
System.out.println("Grade : A+");
break;
case 8:
case 7:
System.out.println("Grade : A");
break;
case 6:
System.out.println("Grade : B");
break;
case 5:
System.out.println("Grade : C");
break;
default:
System.out.println("Grade : D");
break;
}
}
}

OUTPUT:

RESULT:
The grade for the mark is given and the expected output is obtained.
EX: 2(e)
CALCULATE THE SUM OF DIGITS IN A GIVEN NUMBER
DATE: 28/08/2021

AIM:
Write a Java program to calculate the sum of digits in the number using while loop.

ALGORITHM:
1. Start.
2. Initialise number,digit and sum.
3. Set sum = 0.
4. Get the number from user.
5. While the number is greater than 0 then, find the remainder of the number and store it
in digit.
6. Then add sum with digit.
7. Now divide number by 10.
8. Now continue the process till the condition becomes false.
9. Print the sum value.
10. Stop.

PROGRAM:
import java.util.Scanner;
public class sumofdigits {
public static void main(String args[])
{
int number, digit, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
number = sc.nextInt();
while(number > 0)
{
digit = number % 10;
sum = sum + digit;
number = number / 10;
}
System.out.println("Sum of Digits: "+sum);
}
}

OUTPUT:

RESULT:
The sum of digits of the given number is found and the expected output is obtained.

EX: 2(f)
ENTER THE NUMBER TILL THE USER WANTS & END DISPLAY THE COUNT O
POSITIVE, NEGATIVE AND ZEROS
DATE: 28/08/2021

AIM:
Write a Java program to enter the numbers till the user wants and at the end it should
display the count of positive, negative and zeros entered.

ALGORITHM:
1. Start.
2. Initialise opt=y,counter,nve,pve,zero=0;
3. Get the numbers from the user, Until they give no.
4. Once the user gives no, stop counting and enter the while loop.
5. If the number is greater than 0, increase pve by 1.
6. If the number is lesser than 0,increase nve by 1.
7. If the number is equal to 0, increase zero ny 1.
8. Print the counter,pve,nve,zero counts.
9. Stop.

PROGRAM:
import java.util.Scanner;

public class count {


public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
char opt='Y';
int counter=0,nve=0,pve=0,zero=0,num;
while(opt=='y'||opt=='Y')
{
++counter;
System.out.println("Enter the number: ");
num=scan.nextInt();
if(num==0)
++zero;
else if(num>0)
++pve;
else if(num<0)
++nve;
System.out.println("Enter 'Y' if you wish to continue else enter 'N'!");
opt=scan.next().charAt(0);
}
System.out.println("Total Number Of Entries: "+counter);
System.out.println("Negative Entries: "+nve);
System.out.println("Positive Entries: "+pve);
System.out.println("Zero Entries: "+zero);
scan.close();
}
}

OUTPUT:

RESULT:
The number of positive,negative and zeros and the expected output is obtained.

EX: 2(g)
ARMSTRONG NUMBER
DATE: 28/08/2021

AIM:
Write a java program to check whether the given number is Armstrong or not
using while loop.

ALGORITHM:

1. Start.
2. Input the number.
3. Initialize sum=0 and temp=number.
4. Find the total number of digits in the number.
5. Repeat until (temp != 0).
6. remainder = temp % 10.
7. result = resut + pow(remainder,n).
8. temp = temp/10.
9. if (result == number).
10. Display "Armstrong".
11. Else.
12. Display "Not Armstrong".
13. Stop.

PROGRAM:

import java.util.*;

public class sum_of_primes {

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.print(“Enter a number: “);

int number = sc.nextInt();

int temp, remainder, result = 0, n = 0 ;

temp = number;

while (temp != 0)

temp /= 10;

++n;

temp = number;

while (temp != 0)

{
remainder = temp;

result += Math.pow(remainder, n);

temp /= 10;

if(result == number)

System.out.print(number + ” is an Armstrong number\n”);

else

System.out.print(number + ” is not an Armstrong number\n”);

OUTPUT:

RESULT:
The number is checked whether its amstrong number or not and the expected output is
obtained.
EX: 2(h)
FIBONACCI SERIES
DATE: 28/08/2021

AIM:
Write a program to print the Fibonacci series using for loop.

ALGORITHM:
1. Start.
2. Initialise the integers count,num1=0,num2=1.
3. Get the value of count from the user.
4. Now for i equal to 1 until i lesser than or equal to the count value given, increase i.
5. Add num1 and 2 and store in sum.
6. Now assign num 2 value to num 1.
7. Assign sum value to num 2.
8. Repeat till the condition become false.
9. Stop.

PROGRAM:
import java.util.Scanner;
public class fibonacci{

public static void main(String[] args) {

int count, num1 = 0, num2 = 1;


System.out.println("How may numbers you want in the sequence:");
Scanner scanner = new Scanner(System.in);
count = scanner.nextInt();
scanner.close();
System.out.print("Fibonacci Series of "+count+" numbers:");

int i=1;
while(i=1,i<=count,i++)
{
System.out.print(num1+" ");
int Sum = num1 + num2;
num1 = num2;
num2 =Sum;
i++;
}
}
}

OUTPUT:
RESULT:
The Fibonacci series is found and the expected output is obtained.

EX: 2(i)
PROGRAM TO PRINT PATTERN
DATE: 28/08/2021

AIM:
Write a Java program to print the given pattern.

ALGORITHM:
1. Start.
2. Initialize row_size,i,in1,in2.
3. Initialize integer np=1.
4. For i=0,i<row_size,increase i value.
5. For in1=row_size-1,in1>I,in1--, then print a space.
6. For in2=0,in2<np,in2++,print np value reduced by i value.
7. Now increase np value by 2.
8. Repeat the process.
9. Stop.

PROGRAM:
import java.util.Scanner;
public class SUMOFDIGITS {

public static void main(String[] args) {


Scanner s=new Scanner(System.in);
System.out.println("Enter the row size:");

int row_size,i,in1,in2;
int np=1;
row_size=s.nextInt();

for(i=0;i<row_size;i++)
{
for(in1=row_size-1;in1>i;in1--)
{
System.out.print(" ");
}
for(in2=0;in2<np;in2++)
{
System.out.print(np-i);
}
np+=2;
System.out.println();
}
s.close();
}
}

OUTPUT:

RESULT:
The pattern is printed and the expected output is obtained.

EX: 2(k)
SUM OF THE SERIES
DATE: 28/08/2021

AIM:
Write a program to calculate the sum of following series where n is input by
user. 1 + 1/2
+ 1/3 + 1/4 + 1/5 +............1/n, using for loop.

ALGORITHM:
1. Start.
2. Initialise integers number and sum.
3. Sum=0.
4. Get the value of number from the user.
5. For i equal to 0, i less than or equal to number value then increase i value.
6. Divide 1 by i and add it with the sum value.
7. Now print sum.
8. Stop.

PROGRAM:
import java.util.Scanner;

public class JavaLoopExcercise


{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);

int number;
double sum = 0;
System.out.print("Enter number of terms of series : ");
number = console.nextInt();
for(int i = 1; i <= number; i++)
{
sum += 1.0/i;
}
System.out.println("sum: " + sum);
}

OUTPUT:
RESULT:
The sum of the series is found and the expected output is obtained.

EX: 3
CLASSES AND OBJECTS
DATE: 06/09/2021

AIM:
To create a Food Bill Class and print the bill for the orders done by the cutomer.
ALGORITHM:
1. Start.
2. Create a class in the name ice cream.
3. Initialise variables manufacturer, name and type.
4. Create a constructor for the class.
5. Create getters and finally create a toString() .
6. Same way, create a class for fruit.
7. Initialise variables name and price and create a constructor.
8. Create getters and finally create a toString() .
9. Now do the same for class food item.
10. But initialise 3 other variables, name, price and type=”Food item”.
11.Create a main class.
12.Create objects food,fruit,fruit2,ice.
13.Now create object bill and give these objects as parameters and the name
of the customer.
14.Now print the total amount of the orders along with the class bill.
15.Stop.

PROGRAM:
public class Icecream {
private String manufacturer;
private float price;
private String type;

public Icecream(String manufacturer,String type,float price){


this.manufacturer=manufacturer;
this.type=type;
this.price=price;
}

public float getPrice() {


return price;
}
public String getType() {
return type;
}

public String getManufacturer() {


return manufacturer;
}

@Override
public String toString() {
return "Icecream{" +
"manufacturer='" + manufacturer + '\'' +
", price=" + price +
", type='" + type + '\'' +
'}';
}
}
public class Fruit {
private String name;
private float price;

public Fruit(String name,float price){


this.name=name;
this.price=price;
}

public String getName() {


return name;
}
public float getPrice() {
return price;
}

@Override
public String toString() {
return "Fruit{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
public class Fooditem {
private String name;
private float price;
private String type="food items";

public Fooditem(String name,float price){


this.name=name;
this.price=price;
}

public float getPrice() {


return price;
}

public String getName() {


return name;
}
@Override
public String toString() {
return "Fooditem{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
public class Bill {
private String name;
private Fooditem fooditem;
private Fruit fruit1;
private Fruit fruit2;
private Icecream icecream;

public Bill(String name,Fooditem fooditem,Fruit fruit,Fruit fruit2,Icecream icecream){


this.name=name;
this.fooditem=fooditem;
this.fruit1=fruit;
this.fruit2=fruit2;
this.icecream=icecream;
}

public Bill(Fooditem dosa, Fruit apple, Fruit orange, Icecream icecream) {


}

public intCalculateBill(){
float amount= fooditem.getPrice()+fruit2.getPrice()+fruit1.getPrice()
+icecream.getPrice();
return amount;
}
@Override
public String toString() {
return "Bill{" +
"name='" + name + '\'' +
", fooditem=" + fooditem +
", fruit1=" + fruit1 +
", fruit2=" + fruit2 +
", icecream=" + icecream +
'}';
}
}
public class Main {
public static void main(String [] args) {
Fooditem food=new Fooditem("POORI",40);
Fruit fruit=new Fruit("MANGO",10);
Fruit fruit2=new Fruit("PINEAPPLE",10);
Icecream ice=new Icecream("ARUN","CONE",25);
Bill bill1=new Bill("ROHAN",food,fruit,fruit2,ice);

System.out.println("The bill is"+"\n"+bill1+ "\n The total amount


is:"+bill1.CalculateBill()+"\n Welcome");
}
}

OUTPUT:
RESULT:
The orders from the user are taken in as inputs and the expected output is
obtained.

EX: 4 (a)
STATIC BLOCK TO ADD TWO NUMBERS
DATE: 15-9-2021
AIM:
     The aim is to write a java application which has static block to add two numbers.

ALGORITHM:
     Step1:Start
     Step2:Create a static block 
     Step3:Add two numbers in static block having static varisble (a)
     Step4:Print a in the main class
     Step5:Stop

PROGRAM:
class block {
 static int a;
    static 
    {
      a=10+20;
    }
   public static void main(String args[])
  {
    System.out.println(a);
  }
}
Output:
30

RESULT:
  Program which has static block to add two numbers is verified.
EX: 4 (b)
COUNTS NUMBER OF OBJECTS CREATED USING A STATIC DATA MEMB
DATE: 15-9-2021
AIM:
     The aim is to write a java application which counts number of objects created using a
static data member.
Algorithm:
     Step 1:Start
     Step 2:Create a class which contains a static variable count
     Step 3:A method msg is created inside the class contains count++
     Step 4:Create n number of objects in main class and call msg
     Step 5:Print count
     Step 6:Stop

PROGRAM:
class A
{
  static int count=0;
  void msg()
  {
    count++;
  }
}

public class objectcount {

  public static void main(String[] args)


  {
     A a=new A();
     a.msg();
     A b=new A();
     b.msg();     
     A c=new A();
     c.msg();
     System.out.println(c.count);
  }
}
Output:
3

RESULT:
     Program which counts number of objects created using a static data member is verified.

EX: 4 (c)
STATIC METHOD TO MERGE 2 ARRAYS IN A SORTED MANNER
DATE: 15-9-2021

AIM:
     The aim is to write java application static method to merge two arrays in a sorted manner.
ALGORITHM:
     Step 1:Start
     Step 2:Get array  a,b 
     Step 3:Sort array a and b
     Step 4:Compute length of a and b
     Step 5:Create a empty array c with  length (a+b)
     Step 6:Call static method without creating object
     Step 7:Sort and merge the array a and b 
     Step 8:Store in the empty array c
     Step 9:Print array c
     Step 10:Stop

PROGRAM:
import java.util.Arrays;
class MergeTwoSorted
{
  static void merge(int a[], int b[], int n1, int n2, int c [])
      {
        int i = 0, j = 0, k = 0;
        while (i<n1 && j <n2)
        {
            if (a[i]<b[j])
            {
                c[k] = a[i];
                k++;i++;
            }
           else
           {
                c[k] = b[j];
                k++;j++;
           }
        }
        while (i<n1)
        {
            c[k]=a[i];
            k++;i++;
        }
        while (j<n2)
        {
            c[k] = b[j];
            k++;j++;
        } 
    }
}
class Merge
{
     public static void main (String[] args) 

    {
        int [] a= {10,11,21,9,6};
        int n1 = a.length;  
        Arrays.sort(a);  
        int[] b= {5,12,22,8};
        int n2 = b.length;
        Arrays.sort(b);
        int [] c=new int [n1+n2];
        MergeTwoSorted.merge(a,b,n1,n2,c);
        System.out.println("Array after merging");
        for (int i=0; i < n1+n2; i++)
        {
            System.out.print(c[i] + " ");
        }   
        System.out.println();
    }
}

OUTPUT:
   5 6 8 9 10 11 12 21 22

RESULT:
    Program  static method to merge two arrays in a sorted manner is verified.

EX: 4 (d)
DUPLICATE ELEMENTS IN THE ARRAY
DATE: 15-9-2021

AIM:
    The aim write a java application that finds the duplicate elements in the array.
ALGORITHM:
    Step 1:Start
    Step 2:Get array a
    Step 3:call msg
    Step 4:Create a class with static method msg without creating object
    Step 5:msg:Initialize i=0
    Step 6: msg : for(int i=0;i<5;i++)
    Step 7: msg : for(int j=i+1;j<length of a;j++)
    Step 8:msg:compute if(a[i]==a[j]) and print a[i]
    Step 9:Stop

PROGRAM:
class B
{
  static void msg(int a[])
  {
    for(int i=0;i<5;i++)
     {
     for(int j=i+1;j<a.length;j++)
     {
       if(a[i]==a[j])
       {
         System.out.println(a[i]+" ");
       }
     }
  }
}
}
public class duplicate {
  public static void main(String[] args)
  {
     int a[]={10,10,22,22,11,13};
     B.msg(a);
  }
}

OUTPUT:
10 22

RESULT:
    Program that finds the duplicate elements in the array is verified.

EX: 4 (d)
COMPLEX NUMBER PACKAGE
DATE: 15-9-2021

AIM:
Develop a java application to perform complex number operations using packages.
ALGORITHM:
1. Start.
2. Create a new package named Complex Number inside source package.
3. Now create a class named complex inside this package.
4. Now create a default constructor.
5. Create 3 methods to add real and imaginary numbers of 2 complex numbers and then
print them as a complex number.
6. Do the same for complex subtraction.
7. Now create another package inside the source package.
8. Create a main class.
9. Now import the Complex Number Package.
10. Now create an object for the class Complex created inside the Complex number
package.
11. Now call the 2 methods to add real and imaginary values and store their return values
in variables.
12. Now call the 2 methods to subtract.
13. Now call the method to print the sum of complex numbers and difference and then
pass the variables as parameters.
14. Stop.

PROGRAM:

package complex.number;

public class Complex {

public Complex(){

}
public int real(int r,int e){
int sum=r+e;
return sum;
}
public int img(int i,int m){
int sum=i+m;
return sum;
}
public int reals(int r,int e){
int sub = r-e;
return sub;
}
public int imgs(int i,int m){
int sub=i-m;
return sub;
}
public void complexsum(int s,int u){
System.out.println(s+"+i"+u);
}
public void complexsub(int s,int u){
System.out.println(s+"+i"+u);
}

package MainPackage;
import complex.number.Complex;
class main {
public static void main(String [] args){
Complex com=new Complex();
int r=com.real(4, 5);
int i=com.img(3, 2);
com.complexsum(r, i);
int s=com.reals(4, 5);
int u=com.imgs(1, 2);
com.complexsub(s, u);
}
}
OUTPUT:

RESULT:
The complex number addition and subtraction are performed and the expected output is
obtained.

EX: 6
INHERITANCE AND OVERRIDING
DATE: 4-10-2021

AIM:
Develop a java application with Employee class with Emp_name, Emp_id, Address,
Mail_id,,Mobile_no as members. Inherit the classes, Programmer, Assistant Professor,
Associate Professor and Professor from employee class. Add Basic Pay (BP) as the member
of all the inherited classes with 97% of BP as DA, 10 % of BP as HRA, 12% of BP as PF,
0.1% of BP for staff club fund. Generate pay slips for the employees with their gross and net
salary.

ALGORITHM:
1. Start.
2. Create a class employee with the above mentioned data members.
3. Create few more data members BP,PF,HRA,DA and SCF.
4. Create a constructor for the class.
5. Create a method named details to print the details of the employee.
6. Create a method to calculate and print the allowances and funds.
7. Now create a method to calculate gross and net salary from the above calculated funds
and allowances.
8. Now create classes for Professor,programmer,Assistant professor,Associate Professor
and make them inherit from the employee class by extending them.
9. Create a constructor and override the methods in the employee class in these sub
classes.
10. Now create a main class to print the salalry slip.
11. Create object for the class you want to create a salary slip.
12. Call the methods using the object and run the program.
13. Stop.

PROGRAM:

public class Employee {


private String Emp_name;
private String Emp_id;
private String Emp_Address;
private String Emp_Mail;
private int BP;
private double DA;
private double HRA;
private double PF;
private double SCF;
public Employee(String emp_name, String emp_id, String emp_Address, String
emp_Mail, int BP) {
Emp_name = emp_name;
Emp_id = emp_id;
Emp_Address = emp_Address;
Emp_Mail = emp_Mail;
this.BP = BP;
}

public String getEmp_name() {


return Emp_name;
}

public String getEmp_id() {


return Emp_id;
}

public String getEmp_Address() {


return Emp_Address;
}

public String getEmp_Mail() {


return Emp_Mail;
}

public double getBP() {


return BP;
}

public double getDA() {


return DA;
}

public double getHRA() {


return HRA;
}

public double getPF() {


return PF;
}

public double getSCF() {


return SCF;
}

void calcAddonsndMinuses(){
DA=(0.97*BP);
HRA=(0.10*BP);
PF=(0.12*BP);
SCF=(0.1*BP);
System.out.println(("The Dearness Allowance is="+DA));
System.out.println("The House Rent Allowance is="+HRA);
System.out.println("The PF is="+PF);
System.out.println("The Staff Club Fund is="+SCF);
}
void calcGrossndNet(){
double GrossSalary=BP+HRA+DA+PF;
double NetSalary=GrossSalary-SCF;
System.out.println("The Gross Salary and the Net Salary are:"+"Gross
Salary:"+GrossSalary+"Net Salary:"+NetSalary);
}
void printDetails(){
System.out.println("Name:"+Emp_name +"\
n"+"Id:"+Emp_id+"Address:"+Emp_Address+"Mail:"+Emp_Mail);
}
}
public class Programmer extends Employee{
public Programmer(String emp_name, String emp_id, String emp_Address, String
emp_Mail, int BP) {
super(emp_name, emp_id, emp_Address, emp_Mail, BP);
}

@Override
void printDetails() {
super.printDetails();
}

@Override
void calcAddonsndMinuses() {
super.calcAddonsndMinuses();
}

@Override
void calcGrossndNet() {
super.calcGrossndNet();
}
}
public class Professor extends Employee{
public Professor(String emp_name, String emp_id, String emp_Address, String emp_Mail,
int BP) {
super(emp_name, emp_id, emp_Address, emp_Mail, BP);
}
@Override
void printDetails() {
super.printDetails();
}

@Override
void calcAddonsndMinuses() {
super.calcAddonsndMinuses();
}

@Override
void calcGrossndNet() {
super.calcGrossndNet();
}
}
public class AssociateProfessor extends Employee{
public AssociateProfessor(String emp_name, String emp_id, String emp_Address, String
emp_Mail, int BP) {
super(emp_name, emp_id, emp_Address, emp_Mail, BP);
}

@Override
void printDetails() {
super.printDetails();
}

@Override
void calcAddonsndMinuses() {
super.calcAddonsndMinuses();
}

@Override
void calcGrossndNet() {
super.calcGrossndNet();
}
}
public class AssistantProfessor extends Employee{
public AssistantProfessor(String emp_name, String emp_id, String emp_Address, String
emp_Mail, int BP) {
super(emp_name, emp_id, emp_Address, emp_Mail, BP);
}

@Override
void printDetails() {
super.printDetails();
}

@Override
void calcAddonsndMinuses() {
super.calcAddonsndMinuses();
}

@Override
void calcGrossndNet() {
super.calcGrossndNet();
}
}

public class Main {


public static void main(String[] args) {
Programmer programmer=new
Programmer("Rohan","774511","9790512031,Thirunagar","[email protected]
",100000);
programmer.calcAddonsndMinuses();
programmer.calcGrossndNet();
}
}

OUTPUT:

RESULT:
The Java Application is created and the expected output is obtained.

EX: 7
ABSTRACT CLASSES AND INTERFACES
DATE:11-10-2021

AIM:
Create an abstract class Account with data members Account number, name, email. Create
abstract methods double getBalance(), void deposit(int amount), void withdraw(int
amount).Derive two subclasses currentAccount, SavingsAccount which implements these
methods and also it should also override toString() method.
ALGORITHM:
1. Start.
2. Create an abstract class Account.
3. Create variables Account number, name, email.
4. Create abstract methods getBalance(), void deposit(int amount), void withdraw(int
amount).
5. Now derive two subclasses that extend from Account class- currentAccount,
SavingsAccount.
6. Those subclasses need to implement the methods of parent class.
7. Create a data member Balance in both the sub classes.
8. Create constructor containing super class variables as well as Balance in both the sub
classes.
9. Make get balance to return balance and make make deposit and withdraw to return
balance amount after deposit and withdrawl.
10. Create a main class and a main function.
11. Create objects for current and savings aacount classes and give them the required
details.
12. Now call the methods to get the output.
13. Stop.

PROGRAM:
abstract public class Account {
private String account_Number;
private String name;
private String eMail;

public Account(String account_Number,String name,String eMail){


this.account_Number=account_Number;
this.name=name;
this.eMail=eMail;
}

abstract double getBalance();


abstract void deposit(int amount);
abstract void withdraw(int amount);
}
public class CurrentAccount extends Account{

private double Balance;

public CurrentAccount(String account_Number,String name,String eMail,double Balance)


{
super(account_Number,name,eMail);
this.Balance=Balance;
}

@Override
double getBalance() {
System.out.println(Balance);
return Balance;
}

@Override
void deposit(int amount) {
Balance+=amount;
System.out.println("Balance after deposit:"+Balance);
}

@Override
void withdraw(int amount) {
Balance-=amount;
System.out.println("Balance after withdrawl:"+Balance);
}

@Override
public String toString() {
return "CurrentAccount{" +
"Balance=" + Balance +
'}';
}
}
public class SavingsAccount extends Account{

private double Balance;

public SavingsAccount(String account_Number, String name, String eMail,double


Balance) {
super(account_Number, name, eMail);
this.Balance=Balance;
}

@Override
double getBalance() {
System.out.println(Balance);
return Balance;
}

@Override
void deposit(int amount) {
Balance+=amount;
System.out.println("Balance after deposit:"+Balance);
}

@Override
void withdraw(int amount) {
Balance-=amount;
System.out.println("Balance after Withdrawl:"+Balance);
}

@Override
public String toString() {
return "SavingsAccount{" +
"Balance=" + Balance +
'}';
}
}
public class main {
public static void main(String[] args) {
CurrentAccount currentAccount=new
CurrentAccount("1236jgdh","Rohan","[email protected]",508387.0);
currentAccount.getBalance();
currentAccount.deposit(2000);
currentAccount.withdraw(1000);

SavingsAccount savingsAccount=new
SavingsAccount("2313434juhewtugt","Rohan","[email protected]",137528.0);
savingsAccount.getBalance();
savingsAccount.deposit(1220);
savingsAccount.withdraw(2321);
}
}

OUTPUT:
RESULT:
The abstract class and its sub classes are created and the expected output is obtained.

EX: 7(2)
ABSTRACT CLASSES AND INTERFACES
DATE:11-10-2021

AIM:
Create an interface CompareShapes which has abstract methods like double area(), double
Perimeter() and int Compare(object o). Create a class Rectangle which implements area(),
perimeter and compare methods. In the main method create 5 rectangle object and sort
them based on their area using the compare method.

ALGORITHM:
1. Start.
2. Create an interface named CompareShapes.
3. Create 2 abstract methods area,perimter and compare.
4. Create a class Rectangle.
5. Implement CompareShapes using Rectangle and define the abstract methods.
6. In the compare method, if the area of the current object is greater than the given
object, then return 1.
7. Now create a main function.
8. Create a array to store the Rectangle object.
9. Inside a for loop, check the arrangement of the objects based on area using the
compare method we created.
10. If it returns 1, then interchange the position of the objects.
11. Now print the sorted Array.
12. Stop.

PROGRAM:
package Interfaces;

public interface CompareShapes {


public double area();
public double perimeter();
public int compare(Object O);
}
package Interfaces;

public class Rectangle implements CompareShapes{

private int length;


private int breadth;
private double area;
private double perimeter;

public Rectangle(){

public Rectangle(int length,int breadth){


this.length=length;
this.breadth=breadth;
}

@Override
public double area() {
area=length*breadth;
return area;
}

@Override
public double perimeter() {
perimeter=2*(length*breadth);
return perimeter;
}

@Override
public int compare(Object O) {
return 0;
}

public int compare(Rectangle O) {


if (this.area > O.area) {
return 1;
}
return 0;
}
}
package Interfaces;

public class ma {
public static void main(String[] args) {
Rectangle r1=new Rectangle(16,20);
Rectangle r2=new Rectangle(12,20);
Rectangle r3=new Rectangle(14,20);
Rectangle r4=new Rectangle(8,20);
Rectangle r5=new Rectangle(20,20);

Rectangle a[]=new Rectangle[5];


a[0]=r1;
a[1]=r2;
a[2]=r3;
a[3]=r4;
a[4]=r5;
for(int i=0;i<5;i++){
for (int j=0;j<4-i;j++) {
if (a[j].compare(a[j + 1]) == 1) {
Rectangle ob = new Rectangle();
ob = a[j];
a[j] = a[j + 1];
a[j + 1] = ob;
}
}
}
for (int j=0;j<5;j++){
System.out.println(a[j].area());
System.out.println(a[j].perimeter());
}
}
}
OUTPUT:
2.0
6.0
4.0
8.0
6.0
10.0
8.0
12.0
10.0
14.0

RESULT:
The interface is created and implemented by a class. The objects are created,compared and
the expected output is obtained.

You might also like