Вы находитесь на странице: 1из 18

Lab 5

Pre-Lab

1.

import java.util.*;

public class StopWatch {

private Calendar startTime,endTime;

public StopWatch()

startTime=Calendar.getInstance();

public void start()

startTime=Calendar.getInstance();

public void stop()

endTime=Calendar.getInstance();

public long getElapsedTime()

return endTime.getTimeInMillis()-startTime.getTimeInMillis();

public Calendar getStartTime() {

return startTime;

public Calendar getEndTime() {

return endTime;

}
public class StopWatchDemo {

public static void main(String args[])

StopWatch sw=new StopWatch();

sw.stop();

System.out.println(sw.getElapsedTime());

2.

import java.util.*;

public class Restaurant {

public static void main(String args[])

Scanner s=new Scanner(System.in);

while(true)

System.out.println("***MENU***");

System.out.println("1.Green");

System.out.println("2.Brown");

System.out.println("3.Red");

System.out.println("4.Exit");

System.out.println("Enter your Choice Colour");

int ch=s.nextInt();

switch(ch)

case 1:System.out.println("***VEG MENU***");

System.out.println("1.Veg-Manchuria");

System.out.println("2.Veg-Fried Rice");

System.out.println("3.Veg rolls");

System.out.println("4.Veg Cutlet");
System.out.println("");

break;

case 2:System.out.println("***EGG MENU***");

System.out.println("1.egg-Manchuria");

System.out.println("2.egg-Fried Rice");

System.out.println("3.egg rolls");

System.out.println("4.egg Puff");

System.out.println("");

break;

case 3:System.out.println("***NON-VEG MENU***");

System.out.println("1.Chicken Manchuria");

System.out.println("2.Chicken Biryani");

System.out.println("3.Chicken rolls");

System.out.println("4.Chicken Popcorn");

System.out.println("");

break;

default:System.exit(0);

In-Lab

1.

public class Account

private long id;

private double balance;

private static Account a=null;

private Account()

}
public long getId()

return id;

public void setId(long id)

this.id = id;

public void setBalance(double balance)

this.balance = balance;

public static Account getInstance()

if(a==null)

a=new Account();

return a;

public void withdraw(double amt)

if(balance>=amt)

this.balance=balance-amt;

else

System.out.println("Insufficient Balance");

public void deposit(double amt)

if(amt>=0)

balance=balance+amt;
}

public double balenq()

return this.balance;

public class AccDemo

public static void main(String args[])

Account ac=Account.getInstance();

ac.setId(123);

ac.setBalance(12340);

System.out.println(ac.balenq());

ac.withdraw(12340);

System.out.println(ac.balenq());

ac.deposit(3000);

System.out.println(ac.balenq());

Account ac2=Account.getInstance();

System.out.println(ac2.getId());

System.out.println(ac2.balenq());

2.

public class Employee {

private long id;

private String name,dept;

public Employee()
{

this.id=0;

this.name="#";

this.dept="#";

public Employee(long id, String name, String dept)

this.id=id;

this.name=name;

this.dept=dept;

public long getId() {

return id;

public void setId(long id) {

this.id = id;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

public String getDept() {

return dept;

public void setDept(String dept) {

this.dept = dept;

public String toString()

{
String s=String.format("ID:%d%nName:%s%nDepartment:%s%n",this.id,this.name,this.dept);

return s;

import java.util.Scanner;

public class EmployeeDemo {

public static void main(String args[])

Scanner s=new Scanner(System.in);

Employee e[]=new Employee[10];

int ecount=0;

while(true)

System.out.println("***MENU***");

System.out.println("1.Create a new Employee record");

System.out.println("2.Update Name based on Employee ID");

System.out.println("3.Print Details of all employees");

System.out.println("4.Print Details of all employees in given department");

System.out.println("5.exit");

System.out.println("Enter your choice");

int ch=s.nextInt();

switch(ch)

case 1:e[ecount]= new Employee(s.nextLong(),s.next(),s.next());

ecount++;

break;

case 2:System.out.println("Enter key ID");

long key=s.nextLong();

int i;

for(i=0;i<ecount;i++)
{

if(key==e[i].getId())

e[i].setName(s.next());

break;

if(i==ecount)

System.out.println("Not Found");

break;

case 3:for(i=0;i<ecount;i++)

System.out.println(e[i]);

break;

case 4:System.out.println("Enter department");

String d=s.next();

for(i=0;i<ecount;i++)

if((e[i].getDept()).equals(d))

System.out.println(e[i]);

break;

default:System.exit(0);

}
Post-Lab Task

1.

public class Student

private String name,gender,department;

private long id;

public boolean setName(String n)

int flag=0;

for(int i=0;i<n.length();i++)

if((n.charAt(i)>='A'&&n.charAt(i)<='Z')||(n.charAt(i)>='a'&&n.charAt(i)<='z'))

{}

else

flag=1;

break;

if(flag==1)

return false;

else

name=n;

return true;

public boolean setGender(String g)

if(g.equals("M")||g.equals("F"))

{
gender=g;

return true;

else

return false;

public boolean setDepartment(String d)

if(d.equals("BT")||d.equals("CE")||d.equals("CSE")||d.equals("ECE")||d.equals("EEE")||d.e
quals("ECSE")||d.equals("ME")||d.equals("PE"))

department=d;

return true;

else

return false;

public boolean setId(long i)

if(i>99999999&&i<1000000000)

id=i;

return true;

else

return false;

public boolean setData(long i,String n,String g,String d)

boolean a=setId(i);

boolean b=setName(n);
boolean c=setGender(g);

boolean z=setDepartment(d);

if(a && b && c && z)

return true;

else

return false;

public long getId()

return this.id;

public String toString()

String s;

s=String.format("ID : %d %nName : %s %nGender : %s %nDepartment :


%s",id,name,gender,department);

return s;

import java.util.Scanner;

public class StudentDemo {

static Scanner s=new Scanner(System.in);

public static void main(String args[])

Student s1[]=new Student[10];

int scount=0;

while(true)

int ch;

System.out.println("***MENU***");
System.out.println("1.Create a new Student");

System.out.println("2.Print Details of all Students");

System.out.println("3.Print Details based on ID");

System.out.println("4.Modify Student name based on ID");

System.out.println("5.Remove a student based on ID");

System.out.println("6.Exit");

System.out.println("Enter your choice");

ch=s.nextInt();

switch(ch)

case 1:s1[scount]=new Student();

s1[scount].setData(s.nextLong(),s.next(),s.next(),s.next());

scount++;

break;

case 2:for(int i=0;i<scount;i++)

System.out.println(s1[i]);

break;

case 3:System.out.println("Enter Key ID");

int i,key=s.nextInt();

for(i=0;i<scount;i++)

if(key==s1[i].getId())

System.out.println(s1[i]);

break;

if(i==scount)

System.out.println("Key ID not found");

break;

case 4:System.out.println("Enter Key ID");


int j,key1=s.nextInt();

for(j=0;j<scount;j++)

if(key1==s1[j].getId())

s1[j].setName(s.next());

break;

if(j==scount)

System.out.println("Key ID not found");

break;

case 5:System.out.println("Enter Key ID");

key=s.nextInt();

for(i=0;i<scount;i++)

if(s1[i].getId()==key)

for(j=i;j<scount;j++)

s1[j]=s1[j+1];

break;

if(i==scount)

System.out.println("Key ID not found");

break;

case 6:System.exit(0);

default:System.out.println("Invalid Choice");

}
}

2.

import java.util.Calendar;

public class Employee {

private long id;

private String name,dept;

private Calendar doj;

public Employee()

this.id=0;

this.name="#";

this.dept="#";

public Employee(long id, String name, String dept,Calendar doj)

this.id=id;

this.name=name;

this.dept=dept;

this.doj=doj;

public long getId() {

return id;

public void setId(long id) {

this.id = id;

public String getName() {

return name;

}
public void setName(String name) {

this.name = name;

public String getDept() {

return dept;

public void setDept(String dept) {

this.dept = dept;

public Calendar getDoj() {

return doj;

public void setDoj(Calendar doj) {

this.doj = doj;

public String toString()

String s=String.format("ID:%d%nName:%s%nDepartment:%s%n Date of Joining:%d-


%d-
%d",this.id,this.name,this.dept,doj.get(Calendar.DATE),doj.get(Calendar.MONTH),doj.get(Calendar.Y
EAR));

return s;

}
import java.util.Calendar;

import java.util.Scanner;

public class EmployeeDemo {

public static void main(String args[])

Scanner s=new Scanner(System.in);

Employee e[]=new Employee[10];

int ecount=0;

while(true)

System.out.println("***MENU***");

System.out.println("1.Create a new Employee record");

System.out.println("2.Update Name based on Employee ID");

System.out.println("3.Print Details of all employees");

System.out.println("4.Print Details of all employees in given department");

System.out.println("5.Search based on joining year");

System.out.println("6.exit");

System.out.println("Enter your choice");

int ch=s.nextInt();

switch(ch)

case 1:System.out.println("Enter id,name and department");

long id=s.nextLong();

String n=s.next();

String dpt=s.next();

System.out.println("Enter Date of joining");

int d=s.nextInt();

int m=s.nextInt();

int y=s.nextInt();

Calendar doj=Calendar.getInstance();

doj.set(y, m, d);
e[ecount]= new Employee(id,n,dpt,doj);

ecount++;

break;

case 2:System.out.println("Enter key ID");

long key=s.nextLong();

int i;

for(i=0;i<ecount;i++)

if(key==e[i].getId())

e[i].setName(s.next());

break;

if(i==ecount)

System.out.println("Not Found");

break;

case 3:for(i=0;i<ecount;i++)

System.out.println(e[i]);

break;

case 4:System.out.println("Enter key department");

String dept=s.next();

for(i=0;i<ecount;i++)

if((e[i].getDept()).equals(dept))

System.out.println(e[i]);

break;

case 5: System.out.println("Enter year");


int yr=s.nextInt();

for(i=0;i<ecount;i++)

if(e[i].getDoj().get(Calendar.YEAR)==yr)

System.out.println(e[i]);

break;

default:System.exit(0);

Вам также может понравиться