import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.io.*; // Q2 and Q3 are not executable. class FESolns { public static void main( String [] args ) { System.out.println(); Q4(); System.out.println(); Q5(); System.out.println(); Q6(); System.out.println(); //Q7Q8Q9Q10Q11(); System.out.println(); } // Test Q4 public static void Q4() { System.out.println( "Testing Q4"); System.out.println( "=========="); FEQ4Q5 q4 = new FEQ4Q5(); q4.method(); } // Test Q5 public static void Q5() { System.out.println( "Testing Q5"); System.out.println( "=========="); FEQ4Q5 q5 = new FEQ4Q5(); try { q5.method2(); } catch (Exception e) { System.out.println( "The exception caught is \"" + e + "\"" ); } } // Test Q6 public static void Q6() { System.out.println( "Testing Q6"); System.out.println( "=========="); FEQ6 q6 = new FEQ6(); System.out.println( "\"public static void\" in \"FinalExamSoln_Q2toQ11.java\" is at line# " + q6.fileSearch( "FinalExamSoln_Q2toQ11.java", "public static void" ) ); System.out.println(); } // Test Q7~Q11 public static void Q7Q8Q9Q10Q11() { System.out.println( "Testing Q7Q8Q9Q10Q11"); System.out.println( "===================="); } } /** * Q4. [4 pts] and Q5. [6 pts] */ class FEQ2Q3 { // Q2 public void insert(SLL list, Comparable element) { Node prev = null; Node curr = list.head; while( curr != null ) { if ( element.compareTo( curr ) < 0 ) { // Insert element between prev & curr node if ( prev == null ) { // prepend element to SLL element.link = curr; list.head = element; } else { element.link = curr; prev.link = element; } return; } // Next node prev = curr; curr = curr.link; } // the SLL is empty or // the element must be inserted at the end if ( curr == null ) { list.append( node ); } } // Q3 public SLL insertionSort(SLL list) { SLL sorted = new SLL(); while( list.head != null ) { insert( sorted, list.remove(head) ); // the method from Q2 } return sorted; } } /** * Q4. [4 pts] and Q5. [6 pts] */ class FEQ4Q5 { // Q4 public void method() { try { System.out.println("Welcome to Java"); int[] a = new int[10]; a[10] = 1; System.out.println("Welcome to HTML"); } catch (Exception e) { System.out.println("An exception has been caught"); } } // Q5 // Can be with or without throws ArrayIndexOutOfBoundsException, // since ArrayIndexOutOfBoundsException is a subclass of RuntimeException public void method2() throws ArrayIndexOutOfBoundsException { try { System.out.println("Welcome to Java"); int[] a = new int[10]; a[10] = 1; System.out.println("Welcome to HTML"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("An exception has been caught"); throw e; } } } /** * Q6. [12 pts] * Actually StringTokenizer will not give you a correct result. * So use indexOf method or a similar method. * Or code a (simple) search algorithm for a given string inside a string. */ class FEQ6 { public int fileSearch(String filename, String value) { try { int num = 1; // Use either BufferedReader or RandomAccessFile //BufferedReader rd = new BufferedReader(new FileReader(filename)); RandomAccessFile rd = new RandomAccessFile( filename, "r" ); String line = null; while( (line = rd.readLine())!=null ) { if ( line.indexOf( value ) >= 0 ) { return num; } ++num; } rd.close(); return -1; } catch (IOException e) { System.out.println( "IOException caught" ); } return 0; } } /** * Q7. [10 pts] * the constructor and accessors are for this and subsequence questions */ class Computer implements Cloneable { private String brand; private double viewable; public Computer( String b, double v ) { brand = b; viewable = v; } public String toString() { return brand + " with " + viewable + "\'\' viewable size."; } public Object clone() { return new Computer( new String( brand ), viewable ); } public String getBrand() { return brand; } public double getViewable() { return viewable; } } /** * Q8. [10 pts] * the constructor and toString are for this and subsequence questions */ class Desktop extends Computer implements Comparator { private int wires; public Desktop( String b, double v, int w ) { super( b, v ); wires = w; } public String toString() { return "Desktop (with " + wires + " wires): " + getBrand() + " with " + getViewable() + "\'\' viewable size."; } public int compare( Object t1, Object t2 ) { return ((Desktop)t1).wires - ((Desktop)t2).wires; } public boolean equals( Object t ) { return this.compare( this, t ) == 0; } } /** * Q9. [10 pts] * the constructor and toString are for this and subsequence questions */ class Laptop extends Computer implements Comparable { private double weight; public Laptop( String b, double v, double w ) { super( b, v ); weight = w; } public String toString() { return "Laptop (weight " + weight + " lbs): " + getBrand() + " with " + getViewable() + "\'\' viewable size."; } public int compareTo( Object t ) { double diff = weight - ((Laptop)t).weight; if ( diff > 0.0 ) return 1; else if ( diff < 0.0 ) return -1; return 0; } } /** * Q10. [12 pts] */ class Inventory extends JFrame implements ActionListener { private Computer [] computers; private JComboBox type; public static void main( String [] args ) { Inventory inventory = new Inventory(); inventory.setSpecificInstances(); inventory.setTitle( "Q10 -- Inventory" ); inventory.pack(); inventory.setVisible( true ); inventory.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } public void setSpecificInstances() { // Instances computers = new Computer[]{ new Desktop( "Dell", 17.1, 4 ), new Laptop( "Dell", 15.4, 15 ), new Desktop( "Gateway", 19.0, 6 ) }; } public Inventory() { // GUI String [] strs = { "desktops", "laptops" }; type = new JComboBox( strs ); JButton select = new JButton( "select" ); select.addActionListener( this ); // Layout getContentPane().setLayout( new FlowLayout() ); getContentPane().add( type ); getContentPane().add( select ); } public void actionPerformed( ActionEvent e ) { Q11(); } public void Q11() { JFrame frame = new JFrame(); JTextArea texts = new JTextArea(); switch ( type.getSelectedIndex() ) { case 0: // Desktops frame.setTitle( "Q11 -- Desktops" ); for ( int i = 0; i < computers.length; ++i ) { if ( computers[i] instanceof Desktop ) { texts.append( computers[i].toString() + "\n" ); } } break; case 1: // Laptops frame.setTitle( "Q11 -- Laptops" ); for ( int i = 0; i < computers.length; ++i ) { if ( computers[i] instanceof Laptop ) { texts.append( computers[i].toString() + "\n" ); } } break; } frame.getContentPane().add( texts ); frame.pack(); frame.setVisible( true ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }