Professional Documents
Culture Documents
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
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