java program
1.create a abstract class Acounts with the following details: data members: (a) balance (b) accountholder (c) acountholderName (d) address
Method: a. withdrawal () b. deposit () c. display create a subclass of this class SavingAcount and add the following details Data Members: a.rateofInterest Method a. calculateAmount() b. display () create object of these two classes and call their methods. use appropriate constructors.
abstract class Accounts {
// Data members
protected double balance;
protected String accountholder;
protected String accountholderName;
protected String address;
// Constructor to initialize data members
public Accounts(double balance, String accountholder, String accountholderName, String address)
{
this.balance = balance;
this.accountholder = accountholder;
this.accountholderName = accountholderName;
this.address = address;
}
// Abstract methods
public abstract void withdrawal(double amount);
public abstract void deposit(double amount);
public abstract void display();
}
public class SavingAccount extends Accounts
{
// Data member
double rateofInterest;
// Constructor to initialize data members from the superclass and this class
SavingAccount(double balance, String accountholder,
String accountholderName, String address, double rateofInterest)
{
// Call the constructor of the superclass using super
super(balance, accountholder, accountholderName, address);
// Initialize the data member of this class
this.rateofInterest = rateofInterest;
}
// Method to calculate the amount in the account after a certain period of time
void calculateAmount(int years)
{
balance += balance * rateofInterest * years / 100;
}
// Implement the abstract methods of the superclass
@Override
public void withdrawal(double amount)
{
balance -= amount;
}
@Override
public void deposit(double amount)
{
balance += amount;
}
@Override
public void display()
{
System.out.println("Account holder name: " + accountholderName);
System.out.println("Account holder address: " + address);
System.out.println("Account balance: " + balance);
System.out.println("Rate of interest: " + rateofInterest);
}
public static void main(String[] args)
{
SavingAccount sa =new SavingAccount(5000,"lam","jam", "delhi", 0.5);//double balance, String accountholder,
//String accountholderName, String address, double rateofInterest
sa.display();
}
}
2.create a class complex which should be able to multiply two complex numbers. the class should have a constructor to initialize its variable using this keyword. you will be passing the second complex variable as object in the method of the first object to initiate operation for example, ob1.mul(ob2);
public class Complex
{
double c3;
private double real;
private double imaginary;
// Constructor to initialize the real and imaginary parts of the complex number
public Complex(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
// Method to multiply two complex numbers
public Complex multiply(Complex other) {
// Multiply the real parts and subtract the product of the imaginary parts
double real = this.real * other.real - this.imaginary * other.imaginary;
// Multiply the real part of one complex number by the imaginary part of the other, and
// multiply the imaginary part of one complex number by the real part of the other, then
// add those two products together
double imaginary = this.real * other.imaginary + this.imaginary * other.real;
// Return a new Complex object with the result of the multiplication
return new Complex(real, imaginary);
}
public static void main(String[]args)
{
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex(3, 4);
//Complex c3=c1.multiply(c2);
System.out.println("c1: " + c1 + " + " + c1 + "i");
System.out.println("c2: " + c2 + " + " + c2 + "i");
}}
Comments
Post a Comment