Ex. No: Date Utility
Aim:
To design a Date class similar to the one provided in the java.util package.
Algorithm:
Step 1. Start
Step 2. Create a class DateUtility.
Step 3. Define function addToDate() that adds 20 days to the date.
Step 4. Define function subToDate() that rolls down one from the given date.
Step 5. Define function daysBetween2Dates() that finds the number of days between 2 given dates.
Step 6. Define function daysInMonth() that finds the number of days in a month.
Step 7. Define function compare2Dates()that finds if the 2 dates are equal or less than or greater than each other.
Step 8. Define function getDayofTheDate() that finds the day in the date.
Step 9. Define function boolean isLeapYear(int year)that finds whether the year is leap year or not.
Step 10. Create object and call all the functions.
Step 11. Stop.
Program:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtility {
/* Add Day/Month/Year to a Date
add() is used to add values to a Calendar object.
You specify which Calendar field is to be affected by the operation
(Calendar.YEAR, Calendar.MONTH, Calendar.DATE).
*/
public static final String DATE_FORMAT = "dd-MM-yyyy";
// public static final String DATE_FORMAT = "yyyy-MM-dd";
public static void addToDate(){
System.out.println("1. Add to a Date Operation\n"); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
// Gets a calendar using the default time zone and locale. Calendar c1 = Calendar.getInstance();
Date d1 = new Date();
System.out.println("Todays date in Date Format : "+d1); c1.set(2010,07 ,05); //(year,month,date) System.out.println(c1.getTime()); c1.add(Calendar.DATE,20);
System.out.println("Date + 20 days is : " + sdf.format(c1.getTime())); System.out.println();
System.out.println("-------------------------------------------------------");
}
/*Substract Day/Month/Year to a Date*/
public static void subToDate(){
System.out.println("2. Subtract to a date Operation\n");
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); Calendar c1 = Calendar.getInstance();
c1.set(2010,07 ,05); //(year,month,date) System.out.println("Date is : " + sdf.format(c1.getTime()));
// roll down, substract 1 month c1.roll(Calendar.MONTH, false);
System.out.println("Date roll down 1 month : " + sdf.format(c1.getTime()));
c1.set(2010,07 ,05); //(year,month,date)
System.out.println("Date is : " + sdf.format(c1.getTime()));
c1.add(Calendar.MONTH, -1);
// substract 1 month
System.out.println("Date minus 1 month : " + sdf.format(c1.getTime())); System.out.println();
System.out.println("-------------------------------------------------------");
}
public static void daysBetween2Dates(){ System.out.println("3. No of Days between 2 dates\n");
Calendar c1 = Calendar.getInstance(); //new GregorianCalendar(); Calendar c2 = Calendar.getInstance(); //new GregorianCalendar(); c1.set(2010,07 ,05); //(year,month,date)
c2.set(2010,08 ,05); //(year,month,date) System.out.println("Days Between "+c1.getTime()+" and "+ c2.getTime()+" is");
System.out.println((c2.getTime().getTime()-c1.getTime().getTime())/(24*3600*1000)); System.out.println();
System.out.println("-------------------------------------------------------");
}
public static void daysInMonth() {
System.out.println("4. No of Days in a month for a given date\n"); Calendar c1 = Calendar.getInstance(); //new GregorianCalendar();
c1.set(2010,07 ,05); //(year,month,date)
int year = c1.get(Calendar.YEAR);
int month = c1.get(Calendar.MONTH);
// int days = c1.get(Calendar.DATE);
int [] daysInMonths = {31,28,31,30,31,30,31,31,30,31,30,31};
daysInMonths[1] += DateUtility.isLeapYear(year) ? 1 : 0; System.out.println("Days in "+month+"th month for year "+year+" is "+
daysInMonths[c1.get(Calendar.MONTH)]); System.out.println();
System.out.println("-------------------------------------------------------");
}
public static void compare2Dates(){ System.out.println("6. Comparision of 2 dates\n"); SimpleDateFormat fm = new SimpleDateFormat("dd-MM-yyyy"); Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.set(2000, 02, 15);
c2.set(2001, 02, 15);
System.out.print(fm.format(c1.getTime())+" is ");
if(c1.before(c2)){
System.out.println("less than "+fm.format(c2.getTime()));
}else if(c1.after(c2)){
System.out.println("greater than "+fm.format(c2.getTime()));
}else if(c1.equals(c2)){
System.out.println("is equal to "+fm.format(c2.getTime()));
}
System.out.println();
System.out.println("-------------------------------------------------------");
}
public static void getDayofTheDate() { System.out.println("7. Get the day for a given date\n"); Date d1 = new Date();
String day = null;
DateFormat f = new SimpleDateFormat("EEEE");
try {
day = f.format(d1);
}
catch(Exception e) {
e.printStackTrace();
}
System.out.println("The day for "+d1+" is "+day); System.out.println();
System.out.println("-------------------------------------------------------");
}
//Utility Method to find whether an Year is a Leap year or Not public static boolean isLeapYear(int year){
if((year%100 != 0) || (year%400 == 0)){
return true;
}
return false;
}
public static void main(String args[]){
addToDate(); //Add day, month or year to a date field. subToDate(); //Subtract day, month or year to a date field.
daysBetween2Dates(); //The "right" way would be to compute the julian day number
of both dates and then do the substraction.
daysInMonth(); //Find the number of days in a month for a given date compare2Dates(); //Compare 2 dates
getDayofTheDate();
}
}
Result:
Thus a Date class similar to the one provided in the java.util package.was designed, implemented and executed successfully.
Output:
C:\Program Files\Java\jdk1.6.0\bin>javac DateUtility.java
C:\Program Files\Java\jdk1.6.0\bin>java DateUtility
1. Add to a Date Operation
Todays date in Date Format : Mon Jul 05 15:00:00 GMT+05:30 2010
Thu Aug 05 15:00:00 GMT+05:30 2010
Date + 20 days is : 25-08-2010
-------------------------------------------------------
2. Subtract to a date Operation
Date is : 05-08-2010
Date roll down 1 month : 05-07-2010
Date is : 05-08-2010
Date minus 1 month : 05-07-2010
-------------------------------------------------------
3. No of Days between 2 dates
Days Between Thu Aug 05 15:00:00 GMT+05:30 2010 and Mon Jul 05 15:00:00 GMT+05:3
0 2010 is
-31
-------------------------------------------------------
4. No of Days in a month for a given date
Days in 7th month for year 2010 is 31
------------------------------------------------------
6. Comparision of 2 dates
15-03-2000 is less than 15-03-2001
-------------------------------------------------------
7. Get the day for a given date
The day for Mon Jul 05 15:00:00 GMT+05:30 2010 is Monday
------------------------------------------------------ C:\Program Files\Java\jdk1.6.0\bin>
Ex. No: Dynamic Polymorphism
Aim:
To develop with suitable hierarchy, classes for Point, Shape, Rectangle, Square, Circle, Ellipse, Triangle, and Polygon and to design a simple test application to demonstrate
Dynamic polymorphism.
Algorithm:
Step 1. Start
Step 2. Create a class Point with a function callme().
Step 3. Create a class Shape over riding base class and have a function callme () with its own values.
Step 4. Create a class Rectangle over riding base class and have a function callme () with its own values.
Step 5. Create a class Square over riding base class and have a function callme () with its own values.
Step 6. Create an object for the base class Point.
Step 7. Create an object for each of the class and call the function callme () with reference to the base object.
Step 8. Then the corresponding callme function will get displayed.
Step 9. Stop.
Program:
class Point{
void callme(){
System.out.println("Inside point method");}
}
class Shape extends Point{
void callme(){
System.out.println("Inside Shape's method");}
}
class Rectangle extends Point{
void callme(){
System.out.println("Inside Rectangle's method");}
}
class Square extends Point{
void callme(){
System.out.println("Inside Square's method");}
}
class DynPoly
{
public static void main(String args[])
{
Point a= new Point(); //Objcet of type Point
Shape b= new Shape(); //Objcet of type Shape
Rectangle c= new Rectangle(); //Objcet of type Rectangle
Square d= new Square(); //Objcet of type Square
Point r; // Obtain a reference of type Point r=a;
r.callme(); r=b; r.callme(); r=c; r.callme(); r=d; r.callme();
}
}
Result:
Thus classes for Point, Shape, Rectangle, Square, Circle, Ellipse, Triangle, Polygon were designed with suitable hierarchy and dynamic polymorphism was demonstrated.
Output:
C:\Program Files\Java\jdk1.6.0\bin>java DynPoly
Inside point method Inside Shape's method Inside Rectangle's method Inside Square's method
Ex. No: Java interface for ADT Stack
Aim:
To design a Java interface for ADT Stack and use that for stack using array and the other using linked-list.
Algorithm:
Step 1. Start
Step 2. Create an interface Stack with the declaration of 2 functions Push and Pop.
Step 3. Create a class IntStack which implements the interface Stack. Add the definition for the functions Push and Pop which is implemented using linked list.
Step 4. Create a class StackArr which implements the interface Stack. Add the definition for the functions Push and Pop which is implemented using array.
Step 5. Create a class TestSt. Create objects for IntStack and StackArr.Call the corresponding push and pop functions.
Step 6. Display the outputs
Step 7. Stop.
Program:
/*Stack.java*/
public interface Stack{ public void push(int i); public void pop();
}
/*IntStack.java*/
public class IntStack implements Stack {
public class Node { Node next; Object data;
public Node(Object data) {
this.data = data;
}
}
Node stack;
IntStack() {
this.stack = null;
}
public void push(int val) {
Node value = new Node(val); value.next = this.stack; this.stack = value;
}
public void pop() {
if (stack == null) { System.out.println("No value");
}
else {
Node value = this.stack; this.stack = this.stack.next; System.out.println(“Popped value”+value.data);
}
}}
/*StackArr.java*/
public class StackArr implements Stack{
int stck[] = new int[10];
int tos;
// Initialize top-of-stack public StackArr() {
tos = -1;
}
// Push an item onto the stack public void push(int item) { if(tos==9) System.out.println("Stack is full."); else
stck[++tos] = item;
}
// Pop an item from the stack public void pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
}
else
System.out.println("Popped value:"+stck[tos--]);
}}
/*TestSt.java*/
public class TestSt{
public static void main(String args[]){ IntStack stack=new IntStack(); StackArr stack1=new StackArr();
System.out.println("Push and pop using Stack array:");
stack.push(4); stack.push(5); stack.push(6); stack.pop(); stack.pop(); stack.pop();
System.out.println("Push and pop using Stack Linked list:");
stack1.push(3); stack1.push(2); stack1.push(1); stack1.pop(); stack1.pop(); stack1.pop();
}}
Result:
Thus a Java interface for ADT Stack was designed and used for stack using array and the other using linked-list.
Output:
C:\Program Files\Java\jdk1.6.0\bin>javac Stack.java
C:\Program Files\Java\jdk1.6.0\bin>javac IntStack.java C:\Program Files\Java\jdk1.6.0\bin>javac StackArr.java C:\Program Files\Java\jdk1.6.0\bin>javac TestSt.java
C:\Program Files\Java\jdk1.6.0\bin>java TestSt
Push and pop using Stack array: Popped value6
Popped value5
Popped value4
Push and pop using Stack Linked list: Popped value:1
Popped value:2
Popped value:3
Ex. No: File Handling Application
Aim:
To write a Java program to read a file that contains DNA sequences of arbitrary length one per line ,sort the sequences in descending order with respect to the number of 'TATA, .subsequences present and finally write the sequences in sorted order into another file.
Algorithm:
Step 1. Start
Step 2. Create an input text file “DNA.txt” and add the sequence as needed.
Step 3. Read each line from the file to a string array.
Step 4. Count the number of TATA sequence in each line and store the count in another
array.
Step 5. Arrange the data in the count array in descending order and also arrange the data in the string array in parallel with the count array.
Step 6. Write the sorted DNA sequence to the output file “File.txt”
Step 7. View the output file. Step 8. Stop.
Program:
/*FileDemo.java*/
import java.util.*;
import java.io.*;
import java.util.Arrays; import java.util.Collections; class FileDemo
{
public static void main(String args[])throws IOException
{
String fileLine[] = new String[6];
BufferedReader br = new BufferedReader(new FileReader("dna.txt"));
int i=0,z=0,k=0;;
int[] count =new int[6];
int[] order =new int[6];
int result=0;
String searchFor = "tata";
int len = searchFor.length();
System.out.println ("Reading data from DNA file............. ");
while ((fileLine[k]=br.readLine()) != null )
{
result=0;
if (len > 0)
{
int start = fileLine[k].indexOf(searchFor);
while (start != -1)
{
result++;
start = fileLine[k].indexOf(searchFor, start+len);
count[k]=result;
z++;
}
}
if(k<5) k++; else break;
}
int temp,j; String temps;
System.out.println ("Swapping data from DNA file............. ");
for(i=0;i<(count.length-1);i++)
{
for(j=i;j<=(count.length-1);j++)
{
if(count[i]<count[j])
{ temp=count[i]; count[i]=count[j]; count[j]=temp; temps=fileLine[i]; fileLine[i]=fileLine[j]; fileLine[j]=temps;
}
}
}
System.out.println ("Writing Sorted DNA sequence to Output file................. ");
try {
BufferedWriter writer=new BufferedWriter(new FileWriter("file1.txt"));
for(i=0;i<6;i++)
{
writer.write(fileLine[i]);
writer.newLine();
}
writer.close();
System.out.println ("Writing of Sorted DNA sequence to Output file has been completed................. ");
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
Result:
Thus the DNA sequences were read sorted and output was given to an output file.
/*dna.txt*/
attaaattaaaaaaaattata
taaattaataaattagaattaaa aaaaaaacatcgtaaa tcaatatatataat ctaaaataaac gataaaaacaaataa
Output:
C:\Program Files\Java\jdk1.6.0\bin>javac FileDemo.java C:\Program Files\Java\jdk1.6.0\bin>java FileDemo Reading data from DNA file.............
Swapping data from DNA file.............
Writing Sorted DNA sequence to Output file.................
Writing of Sorted DNA sequence to Output file has been completed................
/*File.txt*/
tcaatatatataat attaaattaaaaaaaattata aaaaaaacatcgtaaa taaattaataaattagaattaaa ctaaaataaac gataaaaacaaataa
No comments:
Post a Comment