Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

assignment on JAVA object oriented programming, Assignments of Object Oriented Programming

assignment on JAVA object oriented programming

Typology: Assignments

2021/2022

Uploaded on 07/02/2024

nusrot-jahan
nusrot-jahan 🇧🇩

1 / 6

Toggle sidebar

Related documents


Partial preview of the text

Download assignment on JAVA object oriented programming and more Assignments Object Oriented Programming in PDF only on Docsity! ID: 2154901108 Section:B Lab Date:5/3/22 Lab No:03 Lab Work: Task 1: Write an Account Class with following features- Attributes- Int accountNumber String accountName Double accountBalance Methods- Void deposit(double money); //this method will deposit money in the account balance Double getBalance(); //this method will return current account balance Void printInfo(); //this method will print all the values of the attributes. package labworks; public class Account { int accountNumber; String accountName; double accountBalance; void deposit(double money) { accountBalance=accountBalance+money; } void deposit(double money,double duePayment) { accountBalance=accountBalance+(money-duePayment); } double getBalance() { return accountBalance; } void printInfo() { System.out.println("Account no is"+ accountNumber); System.out.println("Account name is"+ accountName); System.out.println("Account balance is"+ accountBalance); } } Task 2: Write overloaded constructors of the class Accounting as following- Constructors- Account(); Account(int id); Account(String name); Account(int id,String name); Account(int id, String name, double initialBalance); Account(){ accountNumber=-5; accountName="Anonymous"; accountBalance=-100; } Account(int id){ accountNumber=id; } Account(String name){ accountName=name; } Account(int id,String name){ accountNumber=id; accountName=name; } Account(int id,String name,double initialBalance){ accountNumber=id; accountName=name; accountBalance=initialBalance; } } Task 3: From the main method create five different objects using the above five constructors and call the printInfo() method once for each of the objects. package labworks; public class RunnerForAccount { public static void main(String[] args) { Account ac1=new Account(); ac1.printInfo(); Account ac2=new Account(1); Account a3=new Account(); a3.printInfo("Nus"); Account a4=new Account(); a4.printInfo(20,"Nus"); } /*Account ac1=new Account(); ac1.printInfo(); Account ac2=new Account(1); ac2.printInfo(); Account ac3=new Account("Nusrot"); ac3.printInfo(); Account ac4=new Account(11,"Jahan"); ac4.printInfo(); Account ac5=new Account(12,"John",700); ac5.printInfo(); }*/ } Output: Task 5: Write overloaded method in the Account class as following: Void deposit (double money,double duePayment); //it will deposit money after extracting the duePayment from it. package labworks; public class Account { int accountNumber; String accountName; double accountBalance=108.0; void deposit(double money) { accountBalance=accountBalance+money; } void deposit(double money,double duePayment) { accountBalance=accountBalance+(money-duePayment); } double getBalance() { return accountBalance; } void printInfo() { System.out.println("Account no is"+ accountNumber); System.out.println("Account name is"+ accountName); System.out.println("Account balance is"+ accountBalance); } void printInfo(int id){ accountNumber=id; int accountBalance=108; System.out.println("Account Number "+id+" has balance "+accountBalance); } } Main Method: package labworks; public class RunnerForAccount { public static void main(String[] args) { Account ac2=new Account(); ac2.deposit(20); ac2.getBalance(); ac2.printInfo(); ac2.deposit(20,5); ac2.getBalance(); ac2.printInfo(); } } Output: