Experiment : 6
Aim:Program to create class with members and methods, accept and display details for single object.define a
class to represent a bank account. Include the following members: name of the depositor, account number,
type of account balance amount in the account Methods:
1.to assign initial values
2.to deposit an amount
3.to withdraw an amount after checking balance.
4.to display the name & balance
OBJECTIVE : To make students understand the basics of class,methods and how to retrieve data in Java
Description :
What is class and object in java. How to create object
How to define the structure of java program
How to define method and how to access method of a class
CONCLUSION: Students are expected to List the error they are facing along with their solution
REFERENCES : www.learnjavaonline.org/ www.javaworld.com www.w3schools.com
www.codejava.net/books/4-best-free-java-e-books-for-beginners
Books:
For Beginner:
● Head First Java, 2nd Edition
● Thinking in Java (4th Edition)
● Think Java
● Introduction to Java by Sedgewick
● Java in a Nutshell
● Core Java Volume I--Fundamentals (9th Edition) (Core Series): Cay S. Horstmann
● Java How To Program (late objects) by Paul Deitel, Harvey Deitel
Intermediate:
● Effective Java (2nd Edition): Joshua Bloch
● Java Performance: Charlie Hunt, Binu John
● Head First Servlets and JSP
● SCJP by Kathy and Sierra
● Java - The Complete Reference by Herbert Schildt.
● Java Concurrency in Practice
● Java Performance
● The Java Programming Language, 4th Edition
Advanced:
● Java Puzzlers : Traps, Pitfalls, And Corner Cases
Program to create class with members and methods, accept and display details for single object.define a
class to represent a bank account. Include the following members: name of the depositor, account
number, type of account balance amount in the account Methods:
1.to assign initial values
2.to deposit an amount
3.to withdraw an amount after checking balance.
4.to display the name & balance
import java.util.Scanner; public
class BankAccount {
System.out.println(“Om Gaikwad”);
private String depositorName;
private int accountNumber; private
String accountType;
private double balance;
public BankAccount(String depositorName, int accountNumber, String accountType, double balance) {
this.depositorName = depositorName; this.accountNumber = accountNumber;
this.accountType = accountType;
this.balance = balance;
}
public void deposit(double amount) {
balance += amount;
System.out.println("Deposit successful. New balance: " + balance);
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("Withdrawal successful. New balance: " + balance);
} else {
System.out.println("Insufficient balance.");
}
}
public void display() {
System.out.println("Depositor Name: " + depositorName);
System.out.println("Account Number: " + accountNumber);
System.out.println("Account Type: " + accountType);
System.out.println("Balance: " + balance);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter depositor name:");
String depositorName = scanner.nextLine();
System.out.println("Enter account number:");
int accountNumber = scanner.nextInt();
scanner.nextLine(); // Consume newline left-over
System.out.println("Enter account type:");
String accountType = scanner.nextLine();
System.out.println("Enter initial balance:");
double balance = scanner.nextDouble();
scanner.nextLine(); // Consume newline left-over
BankAccount account = new BankAccount(depositorName, accountNumber, accountType, balance);
account.display();
System.out.println("Enter amount to deposit:");
double depositAmount = scanner.nextDouble();
scanner.nextLine(); // Consume newline left-over
account.deposit(depositAmount);
System.out.println("Enter amount to withdraw:");
double withdrawAmount = scanner.nextDouble();
scanner.nextLine(); // Consume newline left-over
account.withdraw(withdrawAmount);
account.display();
}
}
Output: