AZ oop 3
AZ oop 3
URK23CO2018
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]);
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);}
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;
3
23DC2002 - Object Oriented Programming Lab Reg.No.URK23CO2018
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