Monday, October 18, 2021

HANDS ON JAVA(OOP - based uses) (Part-IV)

            Practice JAVA like Never Before!

                                                                                                     courtesy: Google Images

Day 4 🔥

Objective

Learning to implement menu-driven programs, overloading methods and constructors:-

1. Design a class to represent a Bank Account. Include the following things:

Fields

• Name of the depositor

• Address of the depositor

• Account number

• Balance amount in the account

Methods

• To assign initial values

• To deposit an amount

• To withdraw an amount after checking balance

• To display the name, address, and balance of a customer

From the main() method create an object and call these methods.

  • Program

import java.util.Scanner;
class BankAcc{
String name, address;
long acc_no;
double balance;
BankAcc(String p, String q, long r, double s){
name = p;
address = q;
acc_no = r;
balance = s;
}
void deposit(double x){
System.out.println("Pre-existing Balance = "+balance);
balance += x;
System.out.println("Amount Deposited = "+x);
System.out.println("New Balance = "+balance);
}
void withdraw(double x){
System.out.println("Pre-existing Balance = "+balance);
balance -= x;
System.out.println("Amount Withdrawn = "+x);
System.out.println("New Balance = "+balance);
}
void display(){
System.out.println("Your(Account Holder) name is "+name+". \nYour"+
"account number is "+acc_no+". \nYour address is "+address+
". \nYour present balance = "+balance);
}
}//end class

class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name:- ");
String name = sc.nextLine();
System.out.println("Enter your address:- ");
String add = sc.nextLine();
System.out.println("Enter your account number:- ");
long num = sc.nextLong();
System.out.println("Enter your initial balance:- ");
double bal = sc.nextDouble();
//creating and initializing an instance of class: BanAcc :-
BankAcc ob = new BankAcc(name, add, num, bal);
//MENU:-
System.out.println("OPTIONS MENU:\nEnter \'1\' to deposit money\n"+
"\'2\' to withdraw money\n\'3\' to see account info and \n\'-1\'"+
" to EXIT");
//running infinitely until user wants to break:-
int n, flag=1;
double temp;
while(flag==1){
System.out.println("\nEnter Your Choice:- ");
n = sc.nextInt();
switch(n){
case 1:
System.out.println("Enter the deposit amount:- ");
temp = sc.nextDouble();
ob.deposit(temp);
break;
case 2:
System.out.println("Enter the withdraw amount:- ");
temp = sc.nextDouble();
ob.withdraw(temp);
break;
case 3:
ob.display();
break;
case 4:
System.out.println("Thank You ^_^");
flag=0;
break;
default:
System.out.println("Invalid Choice! \nPlease try again.");
}//end switch case
}//end while
}//end main()
}//end class
  • Output:

       

---------------------------------------------------

2. Create a class and determine if method overloading holds good for the return type of methods or not.

  • Program:

Observation: Method overloading does not work if two methods are different only in terms of their return type.

Reason: when it’s called, it creates confusion on which function is being called since the no. of parameters and their data types are identical.

This is an example:-

class Operate{
int add(int p, int q){
return p+q;
}
double add(int p, int q){
return p+q;
}
}//end class

class Main{
public static void main(String[] args){
Operate obj = new Operate();
int n1 = obj.add(2, 3);
double n2 = obj.add(2, 7);
System.out.println("1st sum = "+n1+" and 2nd sum = "+n2);
}
}

            Error:- 

Solution: For method overloading to hold good,

having only different return types will not help.  

We need either:-

i> different data types of parameters:-

class Operate{
int add(int p, int q){
return p+q;
}
double add(double p, double q){
return p+q;
}
}//end class
class Main{
public static void main(String[] args){
Operate obj = new Operate();
int n1 = obj.add(2, 3);
double n2 = obj.add(100, 7.9);
System.out.println("1st sum = "+n1+" and 2nd sum = "+n2); 
        }
}
  • Output:


ii> different no. of parameters:-
class Operate{
int add(int p, int q){
return p+q;
}
double add(int p, int q, int r){
return p+q+r;
}
}//end class

class Main{
public static void main(String[] args){
Operate obj = new Operate();
int n1 = obj.add(2, 3);
double n2 = obj.add(100, 7, 250);
System.out.println("1st sum = "+n1+" and 2nd sum = "+n2);
}
}
  • Output:

---------------------------------------------------


3. Overload the constructors for classes Cuboid and Rectangle of a rectangular figure and display its area and volume. Rectangle is the superclass and Cuboid is the subclass.

  • Program:

import java.util.Scanner;
//Superclass:-
class Rectangle{
double len, breadth;
Rectangle(double l, double b){
this.len = l;
this.breadth = b;
}
double area(){
return len*breadth;
}
}
//Sub-class:-
class Cuboid extends Rectangle{
double height;
Cuboid(double l, double b, double h){
super(l, b);//parent class constructor call from child class
this.height = h;
}
double vol(){
return area()*height;
}
}
class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
double l, b, h;
System.out.println("Enter length: ");
l = sc.nextDouble();
System.out.println("Enter breadth: ");
b = sc.nextDouble();
System.out.println("Enter height: ");
h = sc.nextDouble();
Cuboid obj = new Cuboid(l, b, h);
System.out.println("Area of Rectangle: "+obj.area());
System.out.println("Volume of Cuboid: "+obj.vol());
}
}
  • Output:


--------------------------------------------------

<---------- End of Part - IV ---------->😋


No comments:

Post a Comment

You may like these:

HANDS ON JAVA(OOP - based uses) (Part-I)

     Practice JAVA like Never Before!                                                                                                     ...