0% found this document useful (0 votes)
1 views

AZ oop 3

The document outlines exercises for an Object Oriented Programming lab, focusing on classes and objects in Java. It includes the creation of a Time class with methods for initialization and display, as well as an ATM application with functionalities for balance enquiry, withdrawal, and PIN change. The exercises emphasize the use of constructors, getter and setter functions, and include specific withdrawal conditions.

Uploaded by

rohith07b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

AZ oop 3

The document outlines exercises for an Object Oriented Programming lab, focusing on classes and objects in Java. It includes the creation of a Time class with methods for initialization and display, as well as an ATM application with functionalities for balance enquiry, withdrawal, and PIN change. The exercises emphasize the use of constructors, getter and setter functions, and include specific withdrawal conditions.

Uploaded by

rohith07b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

23DC2002 - Object Oriented Programming Lab Reg.No.

URK23CO2018

Ex. No. 3 CLASSES AND OBJECTS

Date of Exercise 12.08.2024

Aim:
To use different classes and objects methods to solve the program

Description:
Classes:
• A class is like a blueprint or template for creating objects. It defines the structure and
behavior that objects of that class will exhibit.
• syntax for creating a class:
class ClassName {
// Fields (variables)
// Methods (functions)
}
• For example, if we want to create a Student class
class Student {
int id; // Field (instance variable)
String name; // Field (instance variable)
}
Objects:
• Objects are instances of a class—actual entities created based on the class blueprint.
• To create an object, use the new keyword followed by the class name and any necessary
constructor arguments.
• Syntax to create an object:
ClassName objectName = new ClassName([constructor_arguments]);

• For example, creating a Student object:


Student s1 = new Student();
Remember, classes define the structure, and objects are the tangible instances created from those
classes

1
23DC2002 - Object Oriented Programming Lab Reg.No.URK23CO2018

Q1. Create a class called time that has separate int member data for hours, minutes and
seconds. One constructor should initialize this data to 0, and another should initialize
it to fixed values. Another member function should display it, in 11:59:59 format.

Program:
public class Time {
int hours;
int minutes;
int seconds;
Time(){
hours=0;
minutes=0;
seconds=0;
}
Time(int h, int m, int s){
hours=h;
minutes=m;
seconds=s;}
void display() {
System.out.println(hours + ":" + minutes + ":" + seconds);}

public Time add(Time t) {


return new Time(t.hours+this.hours , t.minutes+this.minutes , t.seconds + this.seconds);}
public static void main(String[] args) {
new Time();
Time anna = new Time();
anna.display();
Time jeni = new Time(4 , 30, 28);
jeni.display();
Time shal = anna.add(jeni);
shal.display();
System.out.println("URK23CO2018");
}
}

2
23DC2002 - Object Oriented Programming Lab Reg.No.URK23CO2018

Output Screenshot:

Q11.Write a menu driven application to perform the ATM operations using JAVA. Your
application must contain the following functionalities. Use constructors, getter and
setter functions.
a) In the menu give options for Balance Enquiry, Withdrawal and PIN change
b) Do not allow to withdraw money if the balance is < = 500
c) Do not allow to withdraw money if the amount is not a multiple of 100.

Program:
import java.util.Scanner;

class ATM {
double balance;
int pswd;

public ATM(double initialBalance, int initialPswd) {


balance = initialBalance;
pswd = initialPswd;
}

public double getBalance() {


return balance;
}

3
23DC2002 - Object Oriented Programming Lab Reg.No.URK23CO2018

public void setPswd(int newPswd) {


pswd = newPswd;
}

public boolean withdraw(double amount) {


if (amount % 100 != 0) {
System.out.println("The withdrawal amount must be a multiple of 100.");
return false;
}
if (balance - amount < 500) {
System.out.println("Insufficient Balance - Minimum balance should be 500.");
return false;
}
balance -= amount;
System.out.println("Withdrawal successful. New balance: " + balance);
return true;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

ATM myATM = new ATM(1000.0, 4321);

int choice;
do {
System.out.println("\n URK23CO2018 THIS IS ATM MENU");
System.out.println("1. Balance Enquiry");
System.out.println("2. Withdrawal");
System.out.println("3. Change PIN");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();

switch (choice) {

4
23DC2002 - Object Oriented Programming Lab Reg.No.URK23CO2018

case 1:
System.out.println("Current Balance: " + myATM.getBalance());
break;
case 2:
System.out.print("Enter withdrawal amount: ");
double withdrawalAmount = sc.nextDouble();
myATM.withdraw(withdrawalAmount);
break;
case 3:
System.out.print("Enter your new PIN: ");
int newPswd = sc.nextInt();
myATM.setPswd(newPswd);
System.out.println("PIN changed successfully.");
break;
case 4:
System.out.println("Have a Wonderful day!!");
break;
default:
System.out.println("Invalid choice. Kindly try again.");
}
} while (choice != 4);

sc.close();
}
}

5
23DC2002 - Object Oriented Programming Lab Reg.No.URK23CO2018

Output Screenshot:

Result:
Thus the output is successfully verified

You might also like