1st programe:Encapsulation:
public class Circle1
{
private double radius ;
public void setRadius(double radius)
{
this.radius = radius;
}
public double getRadius()
{
return radius;
}
}
copy this code then paste in new file:
public class Circle2
{
public static void main(String args[])
{
Circle1 ob1 = new Circle1();
ob1.setRadius(10.9);
System.out.println("The value of radius:" +ob1.getRadius());
}
}
2nd programe:
Write a class Account, which will model the functionality of a bank account.1. Attributes
=> AccountTitle
=> TotalBalance
2. Methods
=> deposit(double amountToDeposit): To deposit amount to account
=> withdraw(double amountToWithDraw): To withdraw amount from account. In
this method, you have check withdrawal amount should be less than total amount.
public class Account
{
public void deposit(double amountToDeposit)
{
String accountTital = "current";
double totalBalance = 200000;
double total = totalBalance+amountToDeposit;
System.out.println("Your deposit:" +amountToDeposit);
System.out.println("Now your total balance:" +total);
return total;
}
public double withdraw(double amountToWithDraw double total )
{
if(total<amountToWithDraw)
{
System.out.println("Your balance is low! please retype withdraw balance");
}
else
{
double remaining = total-amountToWithDraw;
System.out.println("Your withdraw balance:" +amountToWithDraw);
System.out.println("Your remaining balance:" +remaining);
}
}
}
copy this code then paste in new file:
public class AccountTest
{
public static void main(String args[])
{
Account F1 = new Account();
double total = F1.deposit(50000);
F1.withdraw(1000000,total);
}
}
0 Comments