0% found this document useful (0 votes)
11 views4 pages

PSOOP - Expt 2a - 014 - HogadeDhruv

Uploaded by

sumitpatelreso
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)
11 views4 pages

PSOOP - Expt 2a - 014 - HogadeDhruv

Uploaded by

sumitpatelreso
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/ 4

Name Hogade Dhruv Abhijeet

UID no. 2022700014


Experiment No. 2a

AIM: Use the concept of constructor to execute given programs.


Program 1

PROBLEM A program to simulate a simple banking system in which the initial


STATEMENT : balance and rate of interest are read from the keyboard and these
values are initialized using the constructor member function. The
program consists of the following methods:
To initialize the balance amount and the rate of interest using
constructor member function
To make deposit
To withdraw an amount for the balance
To find compound interest based on the rate of interest
To know the balance amount
To display the menu options
THEORY: Constructor:
Constructor is a block of codes similar to the method. It is called
when an instance of the class is created. At the time of calling the
constructor, memory for the object is allocated in the memory. It is a
special type of method that is used to initialize the object. Every time
an object is created using the new() keyword, at least one
constructor is called.
Syntax:
className{
// body of the constructor
}

Default constructor:
If there are no constructors written for a class, Java provides a no-
argument default constructor where the instance variables are set to
their default values. For int and double variables, the default value
used is 0 and for String and other object variables, the default is null.

Parameterized constructor:
Constructors with parameters that can be used to initialize the internal
state (fields) of the newly created object are known as Parameterized
Constructors. If we want to initialize fields of the class with our own
values, then use a parameterized constructor.

Power function:
The syntax of the pow() method is: Math.pow(double num1, double num2)
Power function in Java is used to calculate a number raised to the
power of some other number. This function accepts two parameters
and returns the value of the first parameter raised to the second
parameter.

PROGRAM: import java.util.*;

public class Bankacc{


double inibal,roi;

public Bankacc(double inibal,double roi)


{
this.inibal=inibal;
this.roi=roi;
}

public static void main(String args[]){


Menu m=new Menu();

m.menuoption();
}
}
class Menu{
Scanner sc=new Scanner(System.in);
void deposit(double depo){

System.out.println("Enter the amount to be Deposited:");


double d=sc.nextInt();
double e=d+depo;
System.out.println("The account balance after deposition:"+e);
}

void withdraw(double with){

System.out.println("Enter the amount to be Withdrawn:");


double a=sc.nextInt();
if(with>=a){
double b=a-with;
System.out.println("The account balance after withdrawal:"+b);
}
else{
System.out.println("Your balance is insufficient.");
}
}

void knowbalance(double acc){


System.out.println("Your account balance is Rs"+acc);
}

void compoundInterest(double roi){


System.out.println("Enter the amount and period in yrs on which
compound interest is to be found:");
double amount=sc.nextInt();
double years=sc.nextInt();

double ci=amount*Math.pow(1+roi/100, years)-amount;


System.out.println("The compound interest is "+ci);
}

void menuoption(){
System.out.println("Enter the account balance and rate
of interest:");
Bankacc u1=new
Bankacc(sc.nextDouble(),sc.nextDouble());
System.out.println("Enter 1 to continue to menuoption or 0
to exit.");
int i=sc.nextInt();
while(i!=0)
{

System.out.println("Enter 1 to know your account balance:");


System.out.println("Enter 2 to deposit money in your account:");
System.out.println("Enter 3 to withdraw money from your account");
System.out.println("Enter 4 to calculate compound interest:");
System.out.println("Enter 5 to exit");
int n=sc.nextInt();
switch(n)
{
case 1:
knowbalance(u1.inibal);
break;
case 2:
deposit(u1.inibal);
break;
case 3:
withdraw(u1.inibal);
break;
case 4:
compoundInterest(u1.roi);
break;
case 5:
i=0;
break;

}
}
}
}
RESULT:

CONCLUSION: This experiment is based on the use of constructor .I learnt how to create a
Parameterized constructor ,to initialize the variables created and call it to use by creating
object

You might also like