0% found this document useful (0 votes)
4 views12 pages

Java Banking System and Constructor Guide

The document outlines three programming experiments focused on Java constructors and their applications. Experiment 1 involves creating a simple banking system using constructors for initialization, while Experiment 2 requires developing a four-function calculator for fractions. Experiment 3 entails a time conversion program that converts seconds to hours, minutes, and seconds, demonstrating the practical use of constructors in Java.

Uploaded by

Ramesh Pawar
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)
4 views12 pages

Java Banking System and Constructor Guide

The document outlines three programming experiments focused on Java constructors and their applications. Experiment 1 involves creating a simple banking system using constructors for initialization, while Experiment 2 requires developing a four-function calculator for fractions. Experiment 3 entails a time conversion program that converts seconds to hours, minutes, and seconds, demonstrating the practical use of constructors in Java.

Uploaded by

Ramesh Pawar
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

Name SHUBHAM MAHENDRAKUMAR VISHWAKARMA

UID no. 2021700071


Experiment No. 2

AIM:
Program 1

PROBLEM A program to simulate a simple banking system in which the initial balance
STATEMENT: 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: What is a Constructor?


A constructor in Java is similar to a method that is invoked when an object of
the class is created. Unlike Java methods, a constructor has the same name as
that of the class and does not have any return type. For example:
class test{
test(){
// constructor body
}
}
Here, test() is a constructor that has the same name as that of the class and it
does not have an return type.

Types of Constructors:-
In Java, constructors can be divided into 3 types:-
1. No-Arg Constructor
2. Parameterized Constructor
3. Default Constructor

1] No-Arg Constructor:- Similar to methods, a Java constructor may or may


not have any parameters (arguments). If a constructor does not accept any
parameters, it is known as a no-argument constructor. For example,
class Test{
Test(){
//body of constructor
}
}
class Main{
public static void main(){
Test T = new Test();
}
}
In the above example, we have created a constructor Test(). Here, the
constructor does not accept any parameters. Hence, it is known as a no-arg
constructor.
2] Parameterized Constructors:- A Java constructor can also accept one or
more parameters. Such constructors are known as parameterized constructors
(constructor with parameters). For example,
class Main{
String languages;
// constructor accepting single value
Main(String lang) {
languages = lang;
[Link](languages + " Programming Language");
}
public static void main(String[] args) {
// call constructor by passing a single value
Main obj1 = new Main("Java");
Main obj2 = new Main("Python");
Main obj3 = new Main("C");
}
}
In the above example, we have created a constructor named Main(). Here, the
constructor takes a single parameter. Here, we are passing the single value to the
constructor. Based on the argument passed, the language variable is initialized
inside the constructor.

3] Default Constructors:- If we do not create any constructor, the Java


compiler automatically create a no-arg constructor during the execution of the
program. This constructor is called default constructor. For example,
class Main {
int a;
boolean b;
public static void main(String[] args) {
// A default constructor is called
Main obj = new Main();
[Link]("Default Value:");
[Link]("a = " + obj.a);
[Link]("b = " + obj.b);
}
}
Here, we haven't created any constructors. Hence, the Java compiler
automatically creates the default constructor. The default constructor initializes
any uninitialized instance variables with default values.
PROGRAM:
RESULT:

Program 2
PROBLEM Create a four-function calculator for fractions. Here are the formulas for
STATEMENT: the four arithmetic operations applied to fractions:

Addition: a/b + c/d = (a*d + b*c) / (b*d)

Subtraction: a/b - c/d = (a*d - b*c) / (b*d)

Multiplication: a/b * c/d = (a*c) / (b*d)

Division: a/b / c/d = (a*d) / (b*c)

Create the class fraction. Use default constructor to set numerator and
denominator to 1. Program generates a multiplication table for fractions.
Let the user input a denominator, and then generate all combinations of
two such fractions that are between 0 and 1, and multiply them together.
Here’s an example of the output if the denominator is 6:
PROGRAM:
RESULT:
Program 3
PROBLEM Time class : Hours, Minutes and Seconds
STATEMENT:
a)Write a program that asks the user to enter seconds as integer. The
program should compute and display the number of hours, number of
minutes and number of seconds in that seconds.

For example if user enters 4205 seconds as a input called totSeconds. The
answer should be

Hours : 1

Minutes : 10

Seconds : 5

These calculations can be done in a method name: conversion1() which will


also display the result as Hour,minutes and seconds.

Similarly- if user input is Hour, Minute and Second then write another
method conversion2() which will have these three parameters and the
conversion2 method will compute and display the to Seconds.

Note - Use any class (ie scanner /BufferedReader class) for input through
console as hour/min/seconds/totSeconds
PROGRAM:
RESULT:

CONCLUSION: The above programs help us understand about the types of constructors and how
to use them.

You might also like