/***********************************************************************************/ /* Class Query */ import java.io.*; import java.util.*; import java.awt.Rectangle; import java.awt.Dimension; import java.awt.geom.Arc2D; public class Query { private static String[] RollNo; private static String[] Name; private static double[] Quiz1; private static double[] Quiz2; private static double[] Quiz3; private static double[] Midsem1; private static double[] Midsem2; private static double[] Endsem; private static double[] Project; private static int numRecords; private static double[] MaxMarks = {10,10,10,25,25,50,25}; private static String[] Colors = {"red","black","blue","magenta","green"}; private static Canvas c = Canvas.getCanvas(); private static void ReadData() { RollNo = new String[250]; Name = new String[250]; Quiz1 = new double[250]; Quiz2 = new double[250]; Quiz3 = new double[250]; Midsem1 = new double[250]; Midsem2 = new double[250]; Endsem = new double[250]; Project = new double[250]; File input = new File("marks.dat"); try { RandomAccessFile raf_in = new RandomAccessFile(input,"r"); String str; int i=0; raf_in.readLine(); // Throw away the first line. byte[] roll = new byte[6]; raf_in.read(roll); RollNo[i] = new String(roll); byte[] name = new byte[30]; raf_in.read(name); Name[i] = new String(name); String token; while((str=raf_in.readLine()) != null) { StringTokenizer tok = new StringTokenizer(str); token = tok.nextToken(); // This is the Quiz1. Quiz1[i] = Double.parseDouble(token); token = tok.nextToken(); // This is the Quiz1. Quiz2[i] = Double.parseDouble(token); token = tok.nextToken(); // This is the Quiz3. Quiz3[i] = Double.parseDouble(token); token = tok.nextToken(); // This is the Midsem1. Midsem1[i] = Double.parseDouble(token); token = tok.nextToken(); // This is the Midsem2. Midsem2[i] = Double.parseDouble(token); token = tok.nextToken(); // This is the Endsem. Endsem[i] = Double.parseDouble(token); token = tok.nextToken(); // This is the Project. Project[i] = Double.parseDouble(token); i++; if(raf_in.read(roll) == -1) break; RollNo[i] = new String(roll); raf_in.read(name); Name[i] = new String(name); } numRecords = i; raf_in.close(); } catch(IOException e) { System.out.println("IOException " + e); } } private static void printPrompt() { System.out.println("Supported commands :"); System.out.println(""); System.out.println("BarPlot "); System.out.println("Print "); System.out.println("PiePlot Name "); System.out.println(" For Example: PiePlot Name A-F G-O P-V W-Z"); System.out.println(""); } private static void BarPlot(int index, int numBars) { Dimension d = c.getSize(); // Get dimensions of the canvas. // Will help in erasing the canvas. Rectangle e = new Rectangle(0,0,d.width,d.height); c.erase(e); c.setVisible(true); // Draw Y-axis. c.drawLine(30, 0, 30, d.height); // Draw X-axiz. c.drawLine(0, d.height-30, d.width, d.height-30); // Mark the intervals along Y-axis. for(int i=1; i<=20; i++) { if(i<10) { c.drawString(Integer.toString(10*i)+"__", 3, d.height-30-i*20); } else { c.drawString(Integer.toString(10*i)+"_", 0, d.height-30-i*20); } } int barWidth=60; for(int i=0; i maxMarks/numBars * i && Quiz1[j] <= maxMarks/numBars * (i+1)) { numStudents ++; } if(i==0 && Quiz1[j]==0) numStudents++; } break; case 1: for(int j=0; j maxMarks/numBars * i && Quiz2[j] <= maxMarks/numBars * (i+1)) { numStudents ++; } if(i==0 && Quiz2[j]==0) numStudents++; } break; case 2: for(int j=0; j maxMarks/numBars * i && Quiz3[j] <= maxMarks/numBars * (i+1)) { numStudents ++; } if(i==0 && Quiz3[j]==0) numStudents++; } break; case 3: for(int j=0; j maxMarks/numBars * i && Midsem1[j] <= maxMarks/numBars * (i+1)) { numStudents ++; } if(i==0 && Midsem1[j]==0) numStudents++; } break; case 4: for(int j=0; j maxMarks/numBars * i && Midsem2[j] <= maxMarks/numBars * (i+1)) { numStudents ++; } if(i==0 && Midsem2[j]==0) numStudents++; } break; case 5: for(int j=0; j maxMarks/numBars * i && Endsem[j] <= maxMarks/numBars * (i+1)) { numStudents ++; } if(i==0 && Endsem[j]==0) numStudents++; } break; case 6: for(int j=0; j maxMarks/numBars * i && Project[j] <= maxMarks/numBars * (i+1)) { numStudents ++; } if(i==0 && Project[j]==0) numStudents++; } break; } Rectangle temp = new Rectangle(30+i*barWidth, d.height-30-2*numStudents, barWidth, 2*numStudents); c.setForegroundColour(Colors[i%Colors.length]); c.fill(temp); if(i==0) { // Mark the "first" interval on X-axis. c.drawString("["+ Double.toString(maxMarks/numBars*i).substring(0,3) + "-" + Double.toString(maxMarks/numBars*(i+1)).substring(0,3) + "]", 32+i*barWidth, d.height-20); } else { // Mark the "other" intervals on X-axis. c.drawString("("+ Double.toString(maxMarks/numBars*i).substring(0,3) + "-" + Double.toString(maxMarks/numBars*(i+1)).substring(0,3) + "]", 32+i*barWidth, d.height-20); } // Mark the count on top of the bar. c.drawString(Integer.toString(numStudents), 30+i*barWidth+20, d.height-30-2*numStudents); } } private static void print(String str1, String str2) { for(int i=0; i=token.charAt(0) && Name[i].charAt(0)<=token.charAt(2)) { count++; } } angExt = 360.0*count/numRecords; c.setForegroundColour(Colors[j%Colors.length]); c.drawString(token + " " + Integer.toString(count),5,j*10+20); Arc2D arc = new Arc2D.Double(Arc2D.PIE); arc.setArcByCenter(x, y, radius, angSt, angExt, Arc2D.PIE); c.fill(arc); angSt += angExt; } } public static void query() { ReadData(); int numBars = 0; InputLab9 in = new InputLab9(); printPrompt(); System.out.println("\n\n"); System.out.print("Query : "); String command = in.readChars(); StringTokenizer tok = new StringTokenizer(command); String token = tok.nextToken(); if(token.equalsIgnoreCase("barplot")) { token = tok.nextToken(); if(token.equalsIgnoreCase("Q1")) { numBars = Integer.parseInt(tok.nextToken()); BarPlot(0, numBars); return; } if(token.equalsIgnoreCase("Q2")) { numBars = Integer.parseInt(tok.nextToken()); BarPlot(1, numBars); return; } if(token.equalsIgnoreCase("Q3")) { numBars = Integer.parseInt(tok.nextToken()); BarPlot(2, numBars); return; } if(token.equalsIgnoreCase("Mid1")) { numBars = Integer.parseInt(tok.nextToken()); BarPlot(3, numBars); return; } if(token.equalsIgnoreCase("Mid2")) { numBars = Integer.parseInt(tok.nextToken()); BarPlot(4, numBars); return; } if(token.equalsIgnoreCase("Endsem")) { numBars = Integer.parseInt(tok.nextToken()); BarPlot(5, numBars); return; } if(token.equalsIgnoreCase("Proj")) { numBars = Integer.parseInt(tok.nextToken()); BarPlot(6, numBars); return; } System.out.println("Unknown parameter : " + token); return; } if(token.equalsIgnoreCase("print")) { String str; token = tok.nextToken(); if(token.equalsIgnoreCase("RollNo")) { str = "RollNo"; } else { if(token.equalsIgnoreCase("Name")) { str = "Name"; } else { System.out.println("Unknown parameter : " + token); return; } } token = tok.nextToken(); if(token.equalsIgnoreCase("Q1")) { print(str,"Q1"); return; } if(token.equalsIgnoreCase("Q2")) { print(str,"Q2"); return; } if(token.equalsIgnoreCase("Q3")) { print(str,"Q3"); return; } if(token.equalsIgnoreCase("Mid1")) { print(str,"Mid1"); return; } if(token.equalsIgnoreCase("Mid2")) { print(str,"Mid2"); return; } if(token.equalsIgnoreCase("Endsem")) { print(str,"Endsem"); return; } if(token.equalsIgnoreCase("Proj")) { print(str,"Proj"); return; } if(token.equalsIgnoreCase("Total")) { print(str,"Total"); return; } System.out.println("Unknown argument : " + token); return; } if(token.equalsIgnoreCase("pieplot")) { String str; token = tok.nextToken(); if(!token.equalsIgnoreCase("Name")) { System.out.println("PiePlot implemented only for Name Field...."); return; } piePlot(tok); return; } System.out.println("Unknown command"); } } /***********************************************************************************/ /* InputLab9 */ import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; /** InputLab9 - Demo code for Input/Output Console input/output in Java are handled with great caution due to importance on conceptual partitioning of classes, (the class that reads the input returns only one type of data; processing it is someone else's job etc.) Also due to need for higher security (Java tries to be a more secure language than most others) errors are handled very carefully and explicitly. This makes it incumbent on the programmer to write exception handlers. How to handle "exceptions" will be covered later in the course, but for the purpose of understanding this code, you can look at Horstmann sections 13.1 and 13.3.2. The basic I/O module is explained in Horstmann section 2.8. @author M.Imtiaz ur rahaman. @version 02-Oct-02. */ public class InputLab9 { static InputStreamReader reader = new InputStreamReader(System.in); static BufferedReader console = new BufferedReader(reader); /** Demonstrates the use of the parseInt() method in the Integer class to convert an input text into an integer. THINK: Looking at how it is invoked, can you tell if the method parseInt() is a static or non-static method? */ public static int readInt() { boolean done = false; String input; int age=0; while (!done) { try { input = console.readLine(); age = Integer.parseInt(input); done = true; } catch (IOException e) { done = true; } catch (NumberFormatException e) { System.out.println("You must type an integer"); } } return age; } /** This is straightforward from Horstmann Section 2.8 */ public static void readInput() { try { System.out.println("What is your name?"); String input = console.readLine(); String name = input; System.out.println("What is your age?"); input = console.readLine(); int age = Integer.parseInt(input); System.out.println("Hello, " + name + ". So you are to die at the meager age of "+ age + "!"); } catch(IOException e) { System.out.println("Input/Output Error" + e); System.exit(1); } catch(NumberFormatException e) { System.out.println("EXITING! Number wasn't an integer!! " + e); System.exit(1); } } /* Demo of how to read one character at a time. You can also read a String directly by readLine as in the above method, readInput(). The read() operation on a Reader object returns an integer version of the unicode character. End of line is indicated as -1 which is checked by the (ch<0) test. */ public static String readChars() { boolean done = false; int ch; String name = ""; String input; while (!done) { try { ch = console.read(); if ((ch<0) || ( (char)ch == '\n')) done = true; else name += (char)ch; } catch (IOException e) { done = true; } } return name; } } /***********************************************************************************/