0% found this document useful (0 votes)
59 views4 pages

Lab NO: 3: Enum and Interface

The document describes 5 tasks related to enums and interfaces in Java. Task 1 creates an interface for fast food and implements it in a Sandwich class. Task 2 creates an enum for university departments with a code field and getter. Task 3 implements an interface with currency classes. Task 4 creates an enum for playing cards. Task 5 creates an enum for games and prints the values.

Uploaded by

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

Lab NO: 3: Enum and Interface

The document describes 5 tasks related to enums and interfaces in Java. Task 1 creates an interface for fast food and implements it in a Sandwich class. Task 2 creates an enum for university departments with a code field and getter. Task 3 implements an interface with currency classes. Task 4 creates an enum for playing cards. Task 5 creates an enum for games and prints the values.

Uploaded by

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

ADVANCED OBJECT ORIENTED PROGRAMMING

Lab NO: 3
Enum and Interface
OBJECTIVE:
To become familiar with the concept of Enums and Interface
TASK#1:
Create an interface called FastFood (with appropriate methods) and change Sandwich so that it
also implements FastFood.
CODE:
class Roll{
public Roll(){
System.out.println("\nRoll");
}}
class Bread{
public Bread(){
System.out.println("\nBread"); }}
class Cheese{
public Cheese(){
System.out.println("\nCheese");
}}
interface fastfood{
void newOne();
} class Sandwhich implements fastfood{
@Override
public void newOne(){
Roll roll = new Roll();
Bread bread = new Bread();
Cheese cheese = new Cheese();
}}
public class Lab3t1 {
public static void main(String[] args) {
System.out.println("Fastfoods:");
System.out.println("\n Sandwhich");
new Sandwhich().newOne() }}
OUTPUT:

Page
1
ADVANCED OBJECT ORIENTED PROGRAMMING

TASK#2:
Create enum for departments in an university – FINANCE, NETWORKS, EXAMINATION, are all
departments of a university. Use the department enum in employee class.Traversed the
department enum and prints the oridinal values.Enhanced enum with a new private instance
variable deptCode holding the department code, a getter method for deptCode named
getDeptCode(), and a constructor which accepts the department code at the time of enum
creation
CODE:
enum Department {
Finance("DEPT-01"){
@Override
public String getDeptCode(){
return "NOT ACCESSIBLE";}
}, Networks("DEPT-02"), Examinations("DEPT-03");//semi-colon is no longer optional here
Department(String deptCode){
this.deptCode=deptCode; }
private final String deptCode;
public String getDeptCode(){
return deptCode;}}
public class T2 {
public static void main(String[] args) {
for(Department dept:Department.values()){
System.out.println("Department name: "+dept+" Department Code: "+dept.getDeptCode());}}}
OUTPUT:

TASK#3:
Implement following interface

CODE:
class Rupees{
public Rupees(){
System.out.println(" pakistani rupees is paisa");
}}
class Dollars{
public Dollars(){

Page
2
ADVANCED OBJECT ORIENTED PROGRAMMING

System.out.println("In foreign countries people use dollars");


}}
class Pounds{
public Pounds(){
System.out.println("US peoples use pounds"); }}
interface creditcard{
void creditcard();
}
class BankAccount implements creditcard{
public void newOne(){
}
Rupees r=new Rupees();
Dollars d= new Dollars();
Pounds p=new Pounds();
@Override
public void creditcard() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of
generated methods, choose Tools | Templates.
}}
public class T6 {
public static void main(String[] args) {
System.out.println("Bank Account");
System.out.println("\n Bank Account");
new BankAccount().newOne(); }}
OUTPUT:

TASK#4:
Create enum for spade, heart, diamond, and club belonging to an enumerated type called cards
stored each of the enumerators in the reference variables a1,a2,a3,a4 respectively.
CODE:
enum cards{
spade,club,heart,diamond;
}
public class T4 {
public static void main(String[] args) {
cards a1=cards.spade;
cards a2=cards.club;
cards a3=cards.heart;
cards a4=cards.diamond;
System.out.println("Enumerators are: \n"+ a1 + "\n" + a2 + "\n" +
a3 + "\n" + a4)}}

Page
3
ADVANCED OBJECT ORIENTED PROGRAMMING

OUTPUT:

TASK#5:
created an enumeration called
games with four enumerators(ludo,
Chess, Badminton, Cricket) initialize
a for each loop (inside a class) and
fetch the value of the enumerators.
CODE:
enum games{
chess,ludo,Batminton,Cricket;
}
public class T5 {
public static void main(String[] args) {
for(games index:games.values()){
System.out.println(index);
}}}
OUTPUT:

Page
4

You might also like