Friday, 27 November 2015

Number Check Exception


     Create a class called CalcAverage that has the following method:

public double avgFirstN(int N)


This method receives an integer as a parameter and calculates the average of first N natural numbers. If N is not a natural number, throw an exception IllegalArgumentException with an appropriate message.


package om;
public class Test {
    int c;
   
    public double avgFirstN(int N)
    {
        if(N<0)
        {
            try{
                throw new NumberFormatException();
            }
            catch(NumberFormatException nf){
                System.out.println("We Must To Enter The POsitive Number");
            }
        }
        else
        {
            int c=N*10;
       
        System.out.println("THe Average is a = "+c);
        }
       
        return N;   
    }
   
public static void main(String[] args) {
       
       
        Test t=new Test();
        t.avgFirstN(10);   
    }

}

Bank...Exception



Create a class  BankAccount having the members as given below:
accNo  integer
custName string
accType string (indicates ‘Savings’ or ‘Current’)
balance double

Include the following methods in the BankAccount class:
void deposit(double amt);
void withdraw(double amt);
double getBalance();
           
deposit(double amt) method allows you to credit an amount into the current balance. If amount is negative, throw an exception NegativeAmount to block the operation from being performed.

withdraw(double amt) method allows you to debit an amount from the current balance. Please ensure a minimum balance of Rs. 1000/- in the account for savings account and Rs. 5000/- for current account, else throw an exception InsufficientFunds and block the withdrawal operation. Also throw an exception NegativeAmount to block the operation from being performed if the amt parameter passed to this function is negative.

getBalance() method returns the current balance. If the current balance is below the minimum required balance, then throw an exception LowBalanceException accordingly.

Have constructor to which you will pass, accno, cust_name, acctype and initial balance.
And check whether the balance is less than 1000 or not in case of savings account and less than 5000 in case of a current account. If so, then raise a LowBalanceException.
In either case if the balance is negative then raise the NegativeAmount exception accordingly.     




package amol1;

class BankAccount {
    int account_number;
    String name;
    String account_type;
    double balance;
   
    public int getAccount_number() {
        return account_number;
    }
   
    public void setAccount_number(int account_number) {
        this.account_number = account_number;
    }
   
    public String getName() {
        return name;
    }
   
    public void setName(String name) {
        this.name = name;
    }
   
    public String getAccount_type() {
        return account_type;
    }
   
    public void setAccount_type(String account_type) {
        this.account_type = account_type;
    }
   
    public double getBalance() {
       
        if( balance <1000)
        {
        try
        {   
            throw new NumberFormatException();
        }
        catch(NumberFormatException nw)
        {
            System.out.println("Balance is low"+balance);
        }
        }
       
       
        return balance;
       
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }//end setter and getter

    public BankAccount() {
       
        this.account_number = 100;
        this.name = "AMOl";
        this.account_type = "Saving";
        this.balance = 500;
    }
   
   
   
   
    public BankAccount(int account_number, String name, String account_type,
            double balance) {
       
        this.account_number = account_number;
        this.name = name;
        this.account_type = account_type;
        this.balance = balance;
    }
    void deposit(double amt)
    {
        if(amt<0)
        {
            try
            {
                throw new NumberFormatException();
            }
            catch(NumberFormatException nf)
            {
                System.out.println("Negaive Amount cant be deposited");
            }
        }
        else
        {
            balance=getBalance()+amt;
            System.out.println("Current balance is ="+balance);
           
        }
       
       
       
    }
     public void withdraw(double amt){
         if(amt>1000)
            {
                try
                {
                    throw new NumberFormatException();
                }
                catch(NumberFormatException nf)
                {
                    System.out.println("WE CANT DEPOSITE AMOUNT INSUFFICENT BALANCE ");
                }
            }
            else
            {
                balance=getBalance()-amt;
                System.out.println("Current balance is ="+balance);
               
            }
       
       
       
       
       
    }
     void display()
     {
    System.out.println("Balance is ="+getBalance());   
     }
   
   
   
   
    public static void main(String[] args) {
       
       
        BankAccount b=new BankAccount();
        b.deposit(2000);
        b.display();
        b.withdraw(500);
        b.display();
        b.withdraw(2000);
        b.getBalance();
        b.display();
       
       
       
    }
   
   
   
   
}

Static ..

package amol;

class Amoo {
    int acc_no;
    String Name;
    double bal;
    static double interstrate;
    static{
        interstrate=10.3;
    }
   
    public int getAcc_no() {
        return acc_no;
    }
   
    public void setAcc_no(int acc_no) {
        this.acc_no = acc_no;
    }
   
    public String getName() {
        return Name;
    }
   
    public void setName(String name) {
        Name = name;
    }
   
    public double getBal() {
        return bal;
    }
   
    public void setBal(double bal) {
        this.bal = bal;
    }
   
    public static double getInterstrate() {
        return interstrate;
    }
   
    public static void setInterstrate(double interstrate) {
        Amoo.interstrate = interstrate;
    } // END HERE SETTER AND GETTR
   
    public Amoo() {
        acc_no = 100;
        Name = "Amol";
        bal = 500;
    }
   
   
   
   
    public Amoo(int acc_no, String name, double bal) {
        this.acc_no = acc_no;
        Name = name;
        this.bal = bal;
    } // PARAMETR CONSUTOR

    void display()
    {
        System.out.println("Account Number is ="+acc_no+"\nNAme is ="+Name+"\nBalance is a = "+bal+"\ninterstrate "+interstrate);
       
    }
   
    public static void main(String[] args) {
       
        Amoo a=new Amoo(101,"Akshay",5000);
        a.display();
        System.out.println();
        Amoo a1=new Amoo(102,"Amol",50000);
        a1.display();
        System.out.println();
        Amoo a2=new Amoo(103,"Tejas",50000);
        a2.display();
       
       
       
        }
   

}

Employee Details Exceptions



Create a class with following specifications.                                                                         
Class Emp                                                                                                           
           
empId              int                   
empName        string      
designation      string             
            basic                double              
            hra                   double readOnly

Methods
printDET()
                        calculateHRA()
                        printDET() methods will show details of the EMP.                                             
calculateHRA() method will calculate HRA based on basic.

There will 3 designations supported by the application.                                                                                            
If designation  is “Manager”  - HRA will be 10% of BASIC
if designation  is “Officer”  - HRA will be 12% of BASIC
if category is “CLERK”  - HRA will be 5% of BASIC

 Have constructor to which you will pass, empId, designation, basic and price.

And checks whether the BASIC is less than 500 or not. If it is less than 500 raise a custom Exception as given below

Create LowSalException class with proper user message to handle BASIC less than 500.  





package emp3;

class Emp {

    int empid;
    String Name;
    String designation;
    double basic;
    double hra;

    int getEmpid() {
        return empid;
    }

    void setEmpid(int empid) {
        this.empid = empid;
    }

    String getName() {
        return Name;
    }

    void setName(String name) {
        Name = name;
    }

    String getDesignation() {
        return designation;
    }

    void setDesignation(String designation) {
        this.designation = designation;
    }

    double getBasic() {
        if (basic < 500) {
            try {
                throw new LowSalException();

            } catch (LowSalException e) {
                // TODO Auto-generated catch block
                // e.printStackTrace();
                System.out.println(e);
            }
        }
        return basic;
    }

    void setBasic(double basic) {
        this.basic = basic;
    }

    public Emp() {

        this.empid = 1;
        Name = "Abhi";
        this.designation = "SOFT ENGG";
        this.basic = 300;
    }

    public Emp(int empid, String name, String designation, double basic) {

        this.empid = empid;
        Name = name;
        this.designation = designation;
        this.basic = basic;
    }

    public void printDET() {
        System.out.println("Id Of Employye :" + empid
                + "\n Name of Employee is :" + Name
                + "\n Designation Of Employee is:" + designation
                + "\nSalary Of Employee :" + basic);

    }

    public void calculateHRA() {
        if (designation == "Manager") {

            hra = basic * 0.1;
            System.out.println("HRA OF MANAGER : " + hra);
        }

        else if (designation == "Officer") {

            hra = basic * 0.12;
            System.out.println("HRA OF OFFICER : " + hra);
        }

        else {

            hra = basic * 0.05;
            System.out.println("HRA OF CLEARK : " + hra);
        }
    }

    public static void main(String[] args) {
        Emp e = new Emp();
        e.getBasic();
        e.printDET();
        System.out.println();
        Emp e1 = new Emp(2, "Amol", "Manager", 50000);
        e1.printDET();
        e1.calculateHRA();
        System.out.println();
        Emp e2=new Emp(3,"Suraj","officer",10000);
        e2.printDET();
        e2.calculateHRA();
        System.out.println();
        System.out.println();
        Emp e3=new Emp(4,"Sushant","cleark",15000);
        e3.printDET();
        e3.calculateHRA();

    }

}




package emp3;

import java.util.Arrays;

public class LowSalException extends Exception {

    @Override
    public String toString() {
        return "Opps There is Low Balance Than 500 Reppues";
               
    }
   
   

}

Exception Programs..


Create a class USERTRAIL with following specifications.
                        val1, val2 type    int
Method boolean show () will check if val1 and val2 are greater or less than Zero Have constructor which will val1, val2 and check whether if it is less than 0  then raise a custom Exception (name: Illegal value exception.)

package uni;

class Usertrail {
    int val1;
    int val2;
    int getVal1() {
        return val1;
    }
    void setVal1(int val1) {
        this.val1 = val1;
    }
    int getVal2() {
        return val2;
    }
    void setVal2(int val2) {
        this.val2 = val2;
    }
     void booleanshow(){
     }

    public Usertrail(int val1, int val2) {
         if(val1 < 0 || val2 < 0)
         {
             try{
                 throw new IllegalValueException();
             }catch(IllegalValueException e)
             {
                 System.out.println(e);
             }
         }
   
        this.val1 = val1;
        this.val2 = val2;
    }
    void display()
    {
        System.out.println("Number is "+val1+"\n Number is "+val2);
    }
    public static void main(String[] args) {
       
        Usertrail u=new Usertrail(-1,20);
        u.display();
       
    }
   

}






package uni;

public class IllegalValueException extends Exception {
public String toString(){
    return "Illegal Value Exception";
   
}


}