JAVA CSE PROGRAMS

๐Ÿ˜‡JAVA PROGRAMS



1. Create a class called Employee that includes three pieces of information as instance variables- first name, a last name and a monthly salary. Your class should have a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, set it to 0.0. Write a test application named EmployeeTest that demonstrates class Employee's capabilities. Create two Employee objects and display each object's yearly salary. Then give each Employee a 10% raise and display each Employee's yearly salary again. 

➽  code:


public class Employee 
{
private String firstName; // instance variable that stores the first name
private String lastName; // instance variable that stores the last name
private double monthlySalary; // instance variable

// constructor initializes firstName, lastName and monthlySalary with String and double supplied as argument
public Employee (String fname, String lname, double msalary)
{
firstName = fname; // initialize firstName
lastName = lname; // initialize lastName
monthlySalary = msalary; // initialize monthlySalary

// if the monthly salary is not positive, set it to 0.0.
if (msalary < 0.0)
monthlySalary = 0.0;
} // end constructor

// method to set the first name
public void setFirstName (String fname)
{
firstName = fname; // store the first name

} // end method setFirstName

// method to retrieve first name
public String getFirstName ()
{
return firstName;
} // end method getFirstName

// method to set the last name
public void setLastName (String lname)
{
lastName = lname; // store the last name
} // end method setLastName

// method to retrieve last name
public String getLastName ()
{
return lastName;
} // end method getLastName

// method to set the monthly salary
public void setMonthlySalary (double msalary)
{
monthlySalary = msalary; // store the monthly salary


} // end method setMonthlySalary

// method to retrieve monthly salary
public double getMonthlySalary ()
{
return monthlySalary;
} // end method getMonthlySalary

// method to retrieve yearly salary
public double getYearlySalary()
{
double yearlySalary = monthlySalary * 12;
return yearlySalary;
} // end method getYearlySalary

// method to retrieve yearly salary after giving 10% raise
public double getRaiseSalary()
{
double raise =  monthlySalary * 0.1 ;
double raiseSalary = ( monthlySalary + raise ) * 12;
return raiseSalary;
} // end method getRaiseSalary


} // end class Employee

2.Create class SavingsAccount. Use a static variable annualInterestRate to store the annual interest rate for all account holders. Each object of the class contains a private instance variable savingsBalance indicating the amount the saver currently has on deposit. Provide method calculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12this interest should be added to savingsBalance. Provide a static method modifyInterestRate that sets the annualInterestRate to a new value.  
Write a program to test class SavingsAccount.  Instantiate two savingsAccount objects, saver1 and saver2, with balances of Rs 2000.00 and Rs 3000.00, respectively. Set annualInterestRate to 4%, then calculate the monthly interest and print the new balances for both savers. Then set the annualInterestRate  to 5%, calculate the next month's interest and print the new balances for both savers. 

➽code:

class SavingsAccount
{
    static double annualInterestRate;
    private double savingsBalance;

    public SavingsAccount(double balance)
    {
        savingsBalance = balance;
    }

  public double calculateMonthlyInterest()
    {
        return (savingsBalance*annualInterestRate)/12;
    }

    public static void modifyInterestRate(double rate)
    {
        annualInterestRate = rate;
    }


    public static double getannualInterestRate(){return annualInterestRate;}

    public double getsavingsBalance(){return savingsBalance;}

}

public class SavingsTest
{
    public static void main(String args[])
    {
        SavingsAccount saver1 = new SavingsAccount(2000.0);
        SavingsAccount saver2 = new SavingsAccount(3000.0);

        SeavingsAccount.modifyInterestRate(4);

        System.out.printf("Balance for Saver1 = %.2f\nBalance for Saver2 = %.2f\nInterest Rate = %.2f\n\n",saver1.getsavingsBalance()+saver1.calculateMonthlyInterest(),saver2.getsavingsBalance(),SavingsAccount.getannualInterestRate());

        SavingsAccount.modifyInterestRate(5);

        System.out.printf("New Balance for Saver1 = %.2f\nNew Balance for Saver2 = %.2f\nInterest Rate = %.2f\n\n",saver1.getsavingsBalance(),saver2.getsavingsBalance(),SavingsAccount.getannualInterestRate());
    }

}


3. Create Vehicle Interface with name, maxPassanger, and  maxSpeed variables. Create LandVehicle and SeaVehicle Inteface from Vehicle interface. LandVehicle has numWheels variable and drive method. SeaVehicle has displacement variable and launch method. Create Car class from LandVehicle, HoverCraft from LandVehicle and SeaVehicle interface. Also create Ship from SeaVehicle. Provide additional methods in HoverCraft as enterLand and enterSea. Similarly provide other methods for class Car and Ship. Demonstrate all classes in a application. 


➽code:



class Engine
{
Scanner in = new Scanner(System.in);
protected String fuletype;
protected int cylinder;
protected double capacity;
Engine()
{
System.out.println("Enter the capacity no. of cylinder and fuel type");
setCapacity();
setNoOfCylinder();
setFuelType();
}
void setCapacity()
{
capacity = in.nextDouble();
}
void setNoOfCylinder()
{
cylinder = in.nextInt();
}
void setFuelType()
{
fuletype = in.next();
}
double getCapacity()
{
return capacity;
}
double getNoOfCylinder()
{
return cylinder;
}
String getFuelType()
{
return fuletype;
}
void getEngine()
{
System.out.println("Capacity of Engine = " + capacity+"cc");
System.out.println("no of cylinder = " + cylinder);
System.out.println("fuel type = " + fuletype);
}
}
class Tyre
{
Scanner in = new Scanner(System.in);
protected double tyreHeight,tyreWidth;
Tyre()
{
System.out.println("enter the height and Width of tyre");
setHeight();
setWidth();
}
void setHeight()
{
tyreHeight = in.nextDouble();
}
void setWidth()
{
tyreWidth = in.nextDouble();
}
double getHeight()
{
return tyreHeight;
}
double getWidth()
{
return tyreWidth;
}
void getTyre()
{
System.out.println("height of tyre = "+ tyreHeight );
System.out.println("width of tyre = "+ tyreWidth);
}
}
class Door
{
Scanner in = new Scanner(System.in);
protected String doorType;
Door()
{ System.out.println("enter the door type");
setType();
}
void setType()
{
doorType = in.next();
}
String getType()
{
return doorType;
}
void getDoor()
{
System.out.println("door type of car = "+ doorType);
}
}
class Car
{
protected String carName;
Scanner in = new Scanner(System.in);
Car()
{
setCarName();
Engine e = new Engine();
Tyre t = new Tyre();
Door d = new Door();
System.out.println("\n"+"name of the car = "+ carName);
e.getEngine();
t.getTyre();
d.getDoor();
}
void setCarName()
{
System.out.println("enter name of the car");
carName = in.next();
}
}
public class CarTest
{
public static void main (String[] args)
{
Car c1 = new Car();
Car c2 = new Car();
}
}

Comments