[go: up one dir, main page]

0% found this document useful (0 votes)
52 views7 pages

EATULAB

1. The document describes creating two Java classes - Employee and EmployeeTest. 2. The Employee class contains private variables for an employee's name and salary along with getter/setter methods. 3. The EmployeeTest class demonstrates creating Employee objects, displaying salary information, and giving employees a 10% raise.
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)
52 views7 pages

EATULAB

1. The document describes creating two Java classes - Employee and EmployeeTest. 2. The Employee class contains private variables for an employee's name and salary along with getter/setter methods. 3. The EmployeeTest class demonstrates creating Employee objects, displaying salary information, and giving employees a 10% raise.
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/ 7

Statement

Modify class Account to provide a method called withdraw that withdraws money from an
account. Ensure that withdrawal amount doesn’t exceed the Account’s balance. If it does, the
balance should be left unchanged and the method should print a message indicating “Withdrawal
amount exceeded account balance”. Modify class AccountTest to test method withdraw.

Account Class

Package BankAccess;

public class Account {


private String name;
private double balance;
public Account(String name, double balance){
this.name = name;
if (balance > 0.0)
this.balance = balance ;
}
public void deposit(double depositAmount)
{
if (depositAmount > 0.0)
balance = balance + depositAmount;
}
public void withdraw (double withdrawAmount)
{
if(withdrawAmount> balance)
System.out.print(" " + " OPPS!!! withdraw balance exceed account balance " + " ");
else
balance = balance - withdrawAmount;
}
public double getBalance(){
return balance;
}

public void setName(String name)


{
this.name = name;
}
public String getName(){
return name;
}
}

AccountTest Class
Package BankAccess;
import java.util.Scanner;
public class AccountTest {
public static void main(String[] args) {

Account account1 = new Account ("Jane Green",50.00);


Account account2 = new Account ("John blue",50.00);
System.out.printf("%s balance:
$%.2f%n",account1.getName(),account1.getBalance());
System.out.printf("%s balance: $%.2f
%n",account2.getName(),account2.getBalance());

System.out.println();

System.out.println("if You want to deposite press 1\n if you want to


withdraw press 0");
Scanner input = new Scanner(System.in);
int a = input.nextInt();
if (a==1) {
System.out.print("Enter deposite amount for account1: ");
double depositAmount = input.nextDouble();
System.out . printf("%n''''adding %.2f to account1 balance''''%n%n",depositAmount);
account1.deposit(depositAmount);

System.out.printf("%s balance: $%.2f


%n",account1.getName(),account1.getBalance());
System.out.printf("%s balance: $%.2f
%n",account2.getName(),account2.getBalance());

System.out.println("Enter deposite amount for account2: ");


depositAmount = input.nextDouble();

System.out.printf("%n'''''adding %.2f to account1 balance''''''%n%n",depositAmount);


account2.deposit(depositAmount);
System.out.println("````````````````````````````````````````````````````````````````````````````````");
System.out.printf("%s balance: $%.2f%n",account1.getName(),account1.getBalance());
System.out.printf("%s balance: $%.2f%n",account2.getName(),account2.getBalance());
}
else {
System.out.print("Enter withdraw amount for account1: ");
double withdrawAmount = input.nextDouble();

System.out.printf("%n'''''withdraw %.2f to account1 balance''''%n%n",withdrawAmount);


account1.withdraw(withdrawAmount);
System.out.println();
System.out.printf("%s balance: $%.2f%n",account1.getName(),account1.getBalance());
System.out.printf("%s balance: $%.2f
%n",account2.getName(),account2.getBalance());

System.out.println();
System.out.print("Enter withdraw amount for account2: ");
withdrawAmount = input.nextDouble();

System.out.printf("%n''''withdraw %.2f to account2 balance%n'''''%n",withdrawAmount);


account2.withdraw(withdrawAmount);

System.out.println();

System.out.printf("%s balance: $%.2f%n",account1.getName(),account1.getBalance());


System.out.printf("%s balance: $%.2f%n",account2.getName(),account2.getBalance());

}
}
}

Statement
Create a class called Employee that includes three instance variables – a first name
(type String), a last name (type String) and a monthly salary (double). Provide a
constructor that initializes the three variables. Provide a set and get method for each
instance variable. If the monthly salary isn’t positive, don’t set its value. Write a test app
named EmployeeTest that demonstrates class Employee’s capabilities. Create 2
Employee object and display each object’s yearly salary. Then give each employee a
10% raise and display each employee’s yearly salary again

Employee Class
package OfficeSalary;

public class EmployeeTest {

String firstName, lastName;


double monthlySalary, yearlySalary;
public EmployeeTest (String firstName, String lastName, double monthlySalary) {
this.firstName = firstName;
this.lastName = lastName;
if(monthlySalary> 0 ) {
this.monthlySalary = monthlySalary;
setSalary ( monthlySalary);
}
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName ) {
this.lastName = lastName;
}
public void setSalary(double monthlySalary) {
this.yearlySalary = monthlySalary * 12;
}

public String getFullName() {


return firstName + " " + lastName;
}
public double getSalary() {
return yearlySalary;
}
public void salaryIncrement(double incrementRate) {
if ( incrementRate > 0 )
yearlySalary = yearlySalary + ((incrementRate * yearlySalary)/100);
}
}

EmployeeTest Class

package OfficeSalary;
import java.util.Scanner;

public class Income {


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

EmployeeTest employee1 = new EmployeeTest( "Armanul " , "Hai" ,10000);


EmployeeTest employee2 = new EmployeeTest("Aysha ", "Hossien",8000 );

System.out.printf("Name: %s%nYearly Salary: $%.2f


%n",employee1.getFullName() ,employee1.getSalary());

System.out.printf("%nName: %s%nYearly Salary: $%.2f


%n",employee2.getFullName() ,employee2.getSalary());

System.out.printf("%nSet new salary for %s: ", employee1.getFullName());


double newSalary1 = input.nextDouble();

employee1.setSalary(newSalary1);
System.out.printf("New yearly salary for %s is: $%.2f%n", employee2.getFullName(),
employee2.getSalary());
System.out.printf("%nSet new salary for %s: ", employee2.getFullName());
double newSalary = input.nextDouble();

employee2.setSalary(newSalary);
System.out.printf("New yearly salary for %s is: $%.2f%n", employee2.getFullName(),
employee2.getSalary());

System.out.printf("%nEnter salary increment rate for %s %s: ",


employee1.getFullName(),employee2.getFullName());

int rate = input.nextInt();


employee1.salaryIncrement((double)rate);

System.out.printf("%d%% increment to %s's salary is successful! %%",


rate, employee1.getFullName());

System.out.printf("%nNew yearly salary: %.2f %n %n",


employee1.getSalary());
employee2.salaryIncrement((double)rate);
System.out.printf("%d%% increment to %s's salary is successful!", rate,
employee2.getFullName());
System.out.printf("%nNew yearly salary: %.2f", employee2.getSalary());
}

Calculator code

import java.io.InputStream;
import java.util.Scanner;
public class Calculator {

public static void main(String[] args) {


Scanner input = new Scanner (System.in);

System.out.println("enter operator +,-,/,*,%");


char x =input.next().charAt(0);
int a=input.nextInt();
int b=input.nextInt();

if (x=='+')
System.out.printf("%d ",a+b);
else
if (x=='-')
System.out.printf("%d ",a-b);
else
if (x=='*')
System.out.printf("%d ",a*b);
else
if (x=='/')
System.out.printf("%d ",a/b);
else
if (x=='%')
System.out.printf("%d ",a%b);
else
System.out.println("try again");
}
}

Grade average
import java.util.Scanner;

public class Average {

public static void main(String[] args) {


Scanner input = new Scanner (System.in);
int total =0;
int gradecounter=0;
System.out.println("enter grade or press -1 to quit:");
int grade=input.nextInt();
while(grade!=-1) {
total = total + grade;
gradecounter=gradecounter + 1;
System.out.println("enter grade or press -1 to quit:");
grade=input.nextInt();
}
if(gradecounter!=0) {
double average=(double) total/gradecounter;
System.out.printf("total grade %d grades entered is
%d %n ",gradecounter,total);
System.out.printf("Av erage is %.2f",average);
}
else
System.out.printf("no grade input ");

}
Sumation of number

import java.util.Scanner;

public class Sumation {

public static void main(String[] args) {


Scanner input = new Scanner (System.in);

int num = input.nextInt();


int num2 = input.nextInt();

int sum = num + num2;

System.out.println("Sum = " + sum);


}
}

You might also like