Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Sunday 15 July 2012

Create a class called Stem and Leaf, which should have a provision to maintain the stem size and the number of intervals between the two stems. It should also have a method to load the data from the file and a method to display the stem-and-leaf diagram for the loaded data. Test the class to display data regarding the marks of students of your class in the previous exam.


[ stemleaf.java ]
import java.io.*;
class stemleaf
{
public static void main(String args[]) throws IOException
{
int i=0;
int no[]=new int[100];
String line;
File f=new File(args[0]);
/*
// WRITING DATA TO FILE
System.out.println("WRITING DATA TO FILE");
BufferedWriter br=new BufferedWriter(new FileWriter(f));
while(i<10)
{
line=""+i;
br.write(line,0,line.length());
br.newLine();
i++;
}
br.close();
*/
System.out.println("READING FROM FILE");
BufferedReader bf=new BufferedReader(new FileReader(f));
int j=0,temp;
while(  (line=bf.readLine())!=null)
{
temp=Integer.parseInt(line);
if(temp<1000 && temp>=10)
{
no[j]=Integer.parseInt(line);
j++;
}
}
int leaf,stem;
sort(no,j);
System.out.println("===========================");
System.out.println("STEM \t | \t LEAF");
System.out.println("===========================");
for(i=0; i<j;)
{
stem=no[i]/10;
System.out.print(stem + "\t | \t" );
while(no[i]/10==stem)
{
leaf=no[i]%10;
System.out.print(leaf);
if (no[i+1]/10==stem)
{
System.out.print(",");
}
i++;
}
System.out.println();
}
System.out.println("===========================");
}
static void sort(int arr[],int size)
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(arr[i]<arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
}

No comments:

Post a Comment