Java Stack Implementation and Date Class Implementation
Java Stack Implementation and Date Class Implementation
Java Stack Implementation and Date Class Implementation
Exit Enter ur choice 1 Enter element 10 Menu 1.Push 2.Pop 3.Peek 4.Display 5.Exit Enter ur choice 1 Enter element 20 Menu 1.Push
2.Pop 3.Peek 4.Display 5.Exit Enter ur choice 1 Enter element 30 Menu 1.Push 2.Pop 3.Peek 4.Display 5.Exit Enter ur choice 1 Enter element 40 Menu 1.Push 2.Pop 3.Peek 4.Display 5.Exit Enter ur choice
1 Enter element 50 Menu 1.Push 2.Pop 3.Peek 4.Display 5.Exit Enter ur choice 1 Overflow-----maximum linit reached Menu 1.Push 2.Pop 3.Peek 4.Display 5.Exit Enter ur choice 3 Peek element(elemet inserted at last is50 Menu 1.Push 2.Pop 3.Peek
4.Display 5.Exit Enter ur choice 4 Elements are 10 20 30 40 50 Menu 1.Push 2.Pop 3.Peek 4.Display 5.Exit Enter ur choice 2 Popped element50 Menu 1.Push 2.Pop 3.Peek 4.Display 5.Exit Enter ur choice 2 Popped element40 Menu 1.Push
2.Pop 3.Peek 4.Display 5.Exit Enter ur choice 2 Popped element30 Menu 1.Push 2.Pop 3.Peek 4.Display 5.Exit Enter ur choice 2 Popped element20 Menu 1.Push 2.Pop 3.Peek 4.Display 5.Exit Enter ur choice 2 Popped element10
Menu 1.Push 2.Pop 3.Peek 4.Display 5.Exit Enter ur choice 2 Underflow----no element in stack Menu 1.Push 2.Pop 3.Peek 4.Display 5.Exit Enter ur choice
program code
int n=5; public void pop(); public void push(); public void peek(); public void display(); }
class stack implements Mystack { int stack[]=new int[n]; int top=-1; public void push() { try { DataInputStream dis=new DataInputStream(System.in); if(top==(n-1)) { System.out.println("Overflow-----maximum linit reached"); return; } else { System.out.println("Enter element"); int ele=Integer.parseInt(dis.readLine());
stack[++top]=ele; } } catch(Exception e) { System.out.println("e"); } } public void pop() { if(top<0) { System.out.println("Underflow----no element in stack"); return; } else { int popped=stack[top]; top--; System.out.println("Popped element"+popped); } }
if(top<0) { System.out.println("Underflow -- no elememt in stack "); return; } else { int popped=stack[top]; System.out.println("Peek element(elemet inserted at last is"+popped); } }
public void display() { if(top<0) { } else { String str=" "; for(int i=0;i<=top;i++) str=str+" "+stack[i]; System.out.println("Elements are"+str); } }
class Stack {
public static void main(String arg[])throws IOException { DataInputStream dis=new DataInputStream(System.in); stack stk=new stack(); int ch=0; do { System.out.println("Menu\n 1.Push\n 2.Pop\n 3.Peek\n 4.Display\n 5.Exit\n Enter ur choice"); ch=Integer.parseInt(dis.readLine()); switch(ch) { case 1: stk.push(); break; case 2: stk.pop(); break; case 3:
[cs2910007@localhost java]$ vi Dateclass.java [cs2910007@localhost java]$javac Dateclass.java [cs2910007@localhost java]$ java Dateclass MENU 1.LeapYear 2.Difference between 2 dates 3.Remaining Dates 4.exit Enter your choice: 1 Enter the date: 29/10/1991 The year is not a leap year MENU 1.LeapYear 2.Difference between 2 dates 3.Remaining Dates 4.exit Enter your choice: 1 Enter the date: 2/12/1996 The year is a leap year MENU 1.LeapYear 2.Difference between 2 dates
3.Remaining Dates 4.exit Enter your choice: 2 Enter the date: 29/10/1991 Enter the date: 2/12/1995 Difference:1495 MENU 1.LeapYear 2.Difference between 2 dates 3.Remaining Dates 4.exit Enter your choice: 3 Enter the Birthdate: Today's date is 25/7/2011 Enter the date: 29/10/2011 -----------Advance/Belated ***Happy birthday*** wishes----------The Remaining days for ur Bday:96 MENU 1.LeapYear 2.Difference between 2 dates
class Dateman { int day,month,year,nday,nmonth,nyear; String date; String str1[]=new String[3]; void dateseperation(String str) { str1=str.split("/"); day=Integer.parseInt(str1[0]); month=Integer.parseInt(str1[1]);
month=month-1; year=Integer.parseInt(str1[2]); } boolean isLeapYear() { if(year%4==0) return true; else return false; }
long dateDifference()throws IOException { System.out.println("Enter the date:"); BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); date=in.readLine(); str1=date.split("/"); nday=Integer.parseInt(str1[0]); nmonth=Integer.parseInt(str1[1]); nmonth=nmonth-1; nyear=Integer.parseInt(str1[2]); Calendar ca1=Calendar.getInstance(); Calendar ca2=Calendar.getInstance(); ca1.set(year,month,day); ca2.set(nyear,nmonth,nday);
long millis1=ca1.getTimeInMillis(); long millis2=ca2.getTimeInMillis(); long diff=millis2-millis1; long diffdays=diff/(24*60*60*1000); return(diffdays); } void remainingDays()throws IOException { System.out.println("Enter the Birthdate:"); BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); Calendar cd=Calendar.getInstance(); Calendar cd1=Calendar.getInstance(); month=cd.get(Calendar.MONTH); year=cd.get(Calendar.YEAR); day=cd.get(Calendar.DATE); System.out.println("Today's date is "+day+"/"+(month+1)+"/"+year); long diffdays=dateDifference(); if(diffdays<0) { cd1.set(year+1,00,01); long millis1=cd.getTimeInMillis(); long millis2=cd1.getTimeInMillis(); System.out.println(day+month+year); long diff=millis2-millis1; diffdays=diff/(24*60*60*1000);
cd.set(year+1,nmonth-1,nday); long millis3=cd.getTimeInMillis(); diff=millis3-millis2; long diffdays1=diff/(24*60*60*1000); diffdays=diffdays1+diffdays; } System.out.println("-----------Advance/Belated ***Happy birthday*** wishes-----------"); System.out.println("The Remaining days for ur Bday:"+diffdays); } } class Dateclass { public static void main(String args[])throws IOException { String str; int ch=0; long ddiff; BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); while(ch!=4) { System.out.println("\tMENU\n1.LeapYear\n2.Difference between 2 dates\n3.Remaining Dates\n 4.exit \n Enter your choice:"); ch=Integer.parseInt(in.readLine()); Dateman d=new Dateman(); if(ch==1||ch==2)
{ System.out.println("Enter the date:"); str=in.readLine(); d.dateseperation(str); } switch(ch) { case 1: if(d.isLeapYear()) System.out.println("The year is a leap year"); else System.out.println("The year is not a leap year"); break; case 2: ddiff=d.dateDifference(); System.out.println("Difference:"+ddiff); break; case 3: d.remainingDays(); break; case 4: System.exit(0); break;
} } }