Tuesday, November 23, 2010

Lab Test 3 - Section 6 - Input File

10
Shirt;blue;S;$40
Shirt;yellow;M;$50
Pant;black;M;$100
Shirt;blue;S;$60
Pant;grey;L;$80
Jacket;black;M;$50
Pant;grey;S;$60
Pant;black;M;$110
Shirt;blue;S;$80
T-Shirt;red;S;$60
3
Shirt;blue;S
Pant;black;M
T-Shirt;green;M

LabTest3 -- Section 5


INPUT FILE FORMAT
--------------------------------
1) First Line : 10 -- Number of Employees to be added to an ArrayList
2) 10 Lines follow -- each line corresponds to data on an Employee
    Employee Data Format :
    ----------------------------------- 
    Name;Age;Salary;Office
3) Line 12 : 4 -- Number of names to search in ArrayList
4) 4 Names follow 
5) Line 17 : 3 -- Number of names to remove from the ArrayList
6) 3 Names follow

7) Check the Output File to see what to display.

-------------------------------------------------------------------------------------------------------
Things To Do:
--------------------
a) Read the data from the Input File
b) Store informattion about each employee in an ArrayList
c) Calculate the Average Salary
d) Read the names which are to be searched to see if they are contained in the list
    If contained in the list then print whether salary above or below average salary
e) Read the names which are to be removed from the ArrayList
   if contained in the list then remove them.
f) Finally display the Final List.

** Check the Output File for the Output Format

LabTest3 -- Output


********* READING EMPLOYEE DATA **************

Employee Data Added: Herbert 32 10000 2A41

Employee Data Added: Lander 45 15000 2A47

Employee Data Added: Winthrop 27 0 3A71

Employee Data Added: Marion 23 3000 4B25

Employee Data Added: Adams 31 4765 5A67

Employee Data Added: Richard 38 20000 6A11

Employee Data Added: Jason 34 12456 4C32

Employee Data Added: Karthik 39 14567 3B33

Employee Data Added: Bernard 40 34234 4A22

Employee Data Added: Sofia 26 45676 3C56
********* EMPLOYEE DATA READ **************

********* BEGIN SEARCH **************
Lander is contained in the list
Employee Details :Lander 45 15000 2A47
Below Average Salary

Arnold is not contained in the list

Marion is contained in the list
Employee Details :Marion 23 3000 4B25
Below Average Salary

Shenoy is not contained in the list

********* END SEARCH **************

********* REMOVING SPECIFIC EMPLOYEE DATA **************
Lander is contained in List - hence removing ...
Jason is contained in List - hence removing ...
Sopna not contained in list
********* SPECIFIC EMPLOYEE DATA REMOVED **************

********** THE FINAL LIST IS: **************
Herbert 32 10000 2A41 
Winthrop 27 0 3A71 
Marion 23 3000 4B25 
Adams 31 4765 5A67 
Richard 38 20000 6A11 
Karthik 39 14567 3B33 
Bernard 40 34234 4A22 
Sofia 26 45676 3C56 

LabTest3 -- Input File


10
Herbert;32;$10000;2A41
Lander;45;$15000;2A47
Winthrop;27;7000;3A71
Marion;23;$3000;4B25
Adams;31;$4765;5A67
Richard;38;$20000;6A11
Jason;34;$12456;4C32
Karthik;39;$14567;3B33
Bernard;40;$34234;4A22
Sofia;26;$45676;3C56
4
Lander
Arnold
Marion
Shenoy
3
Lander
Jason
Sopna

Test 3 Solutions

  1. (20%) Implement a method called isFileThere(String fileName) which returns true if either fileName exists (is the name of a file in the filesystem) or filename.png exists. For example, if the file is "apple" then it should return true if either "apple" or "apple.png" refer to a file in the filesystem. Return false otherwise.
    Answer:
     public static boolean isFileThere(String fileName) {
      File f = new File(fileName);
      if (f.exists()) {
       return true;
      }
      f = new File(fileName + ".png");
      if (f.exists()) {
       return true;
      }
      return false;
     }
     
  2. (20%) What does the following program print out?
    public class Exceptional {
    
     public void happyDays(int x, int y) throws Exception {
      int a[] = {4}; //array with one item.
      try {
       int d = a[x];
       if (y == 0) {
        throw new Exception("Heeeeyyyy");
       }
       System.out.println("Hello Fonzie");
      }
      catch (IndexOutOfBoundsException e) {
       System.out.println("Hello Richie");
      }
     }
    
     public static void main(String[] args) {
      Exceptional e = new Exceptional();
      try {
       e.happyDays(0,1);
       e.happyDays(1,1);
       e.happyDays(0,0);
      } catch (Exception e1) {
       System.out.println("Hello Potsie");
      }
     }
    
    }
    
    Answer: There where three different tests, each with a different answer:
    public static void main(String[] args) {
     Exceptional e = new Exceptional();
     try {
      e.happyDays(0,1);
      e.happyDays(1,1);
      e.happyDays(0,0);
     } catch (Exception e1) {
      System.out.println("Hellow Potsie");
     }
     System.out.println("-------");
     try {
      e.happyDays(1,1);
      e.happyDays(0,1);
      e.happyDays(0,0);
     } catch (Exception e1) {
      System.out.println("Hellow Potsie");
     }
     System.out.println("--------");
     try {
      e.happyDays(1,1);
      e.happyDays(0,0);
      e.happyDays(1,0);
     } catch (Exception e1) {
      System.out.println("Hellow Potsie");
     }
    }
    Hello Fonzie
    Hello Richie
    Hellow Potsie
    -------
    Hello Richie
    Hello Fonzie
    Hellow Potsie
    --------
    Hello Richie
    Hellow Potsie
      
      
  3. (30%) Implement a recursive method that returns the mutant power of a Person. The mutant power of a Person depends on whether or not that person has the X-Gene, and how many of her ancestors have the X-Gene, with maternal lineage being doubly important.
    Specifically, you are given the class
    public class Person {
     public Person mother;
     public Person father;
     /** True if person had the X-Gene */
     public boolean xgene;
    } 
    
    where either mother or father could be null, signifying that no one up that lineage has the X-Gene (ignore them). The mutantPower of a Person Y is given by the formula:
    • mutant-power(Y) = (1 if Y has X-Gene, 0 otherwise) + 2 * mutant-power(Y's mom) + mutant-power(Y's dad).
    Implement int Person.calculateMutantPower(). For example, the code below prints out xavier's mutant power which is 3.
    public static void main(String[] args) {
     Person xavier = new Person();
     xavier.xgene = true;
     Person mom = new Person();
     mom.xgene = true;
     xavier.mother = mom;
     System.out.println(xavier.calculateMutantPower()); // 3
    }
    
    Answer:
    public int calculateMutantPower () {
     int power = (xgene) ? 1 : 0;
     int motherPower = 0;
     int fatherPower = 0;
     //The base case is when both mother and father are null, in which case we just return 1.
     if (mother != null) {
      motherPower = mother.calculateMutantPower();
     }
     if (father != null) {
      fatherPower = father.calculateMutantPower();
     }
     return power + 2*motherPower + fatherPower;
    }
      
  4. (30%) Implement a class called Set which implements a mathematical set, which we define as a bunch of elements of the same type where no element appears more than once (no repeats allowed). You will use an ArrayList to implement your Set.
    The Set will implement 3 methods:
    1. a no-argument constructor (Tip: see Listing 12.10 in your textbook)
    2. an add(...) method which adds a new element to the set, if its not already there,
    3. a toString() method which turns the whole set into a string, with a dash '-' between each element.
    The following is an example usage of the Set class you will write:
    public static void main(String[] args) {
     Set<String> s = new Set<String>();
     s.add("Hi");
     s.add("Hello");
     s.add("Hi");
     s.add("Hi");
     s.add("how are you?");
     System.out.println(s); //prints Hi-Hello-how are you?-
    }
         
    Answer:
    import java.util.ArrayList;
    
    public class Set<T> {
    
     private ArrayList<T> list;
     
     public Set() {
      list = new ArrayList<T>(10); 
     }
     
     public void add(T element) {
      if (!list.contains(element)) {
       list.add(element);
      }
     }
     
     public String toString() {
      String result = "";
      for (int i=0; i < list.size();i++) {
       result += list.get(i) + "-";
      }
      return result;
     }
    }
     

Thursday, November 18, 2010

HW7 Solution

Below is my solution to the homework:

import java.awt.Graphics;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class Fractal extends JPanel {

 private double length;

 public int numLevels;

 public void paintComponent(Graphics g) {
  g.setColor(Color.black);
  System.out.println("Drawing");
  Point from = new Point(150,200);
  Point to = new Point(450,200);
  length = 0;
//  drawFractalLineDragon(g,from,to,numLevels,Direction.Right);
  drawFractalLine(g,from,to,numLevels);
  
//  from = new Point(100,400);
//  Point top = new Point(300,100); 
//  to = new Point(500,400);
//  drawFractalLineSierpinsky(g,from,top,to,numLevels);
  System.out.println("Length of curve is " + length);
 }
 
 
 /**
  * Draw the Sierpinsky triangle http://en.wikipedia.org/wiki/Sierpinski_triangle
  * @param g
  * @param from bottom-left
  * @param top 
  * @param to bottom-right
  * @param level
  */
 public void drawFractalLineSierpinsky(Graphics g, Point from, Point top, Point to, int level) {
  if (level == 0) {
   g.drawLine(from.getX(), from.getY(), to.getX(), to.getY());
   length += from.getVectorTo(to).getMagnitude();
   g.drawLine(from.getX(), from.getY(), top.getX(), top.getY());
   length += from.getVectorTo(top).getMagnitude();
   g.drawLine(top.getX(), top.getY(), to.getX(), to.getY());
   length += top.getVectorTo(to).getMagnitude();
   return;
  }
  Point midFromTop = from.getMidPoint(top);
  Point midFromTo = from.getMidPoint(to);
  Point midTopTo = top.getMidPoint(to);
  
  drawFractalLineSierpinsky(g,from, midFromTop, midFromTo,level-1);
  drawFractalLineSierpinsky(g,midFromTop, top, midTopTo, level-1);
  drawFractalLineSierpinsky(g,midFromTo, midTopTo, to, level-1);
 }

 /**
  * Draws the Levy C Curve http://en.wikipedia.org/wiki/Levy_C_curve
  * @param g
  * @param from start point
  * @param to end point
  * @param level number of recursive levels.
  */
 public void drawFractalLine(Graphics g, Point from, Point to, int level) {
  if (level == 0) {
   g.drawLine(from.getX(), from.getY(), to.getX(), to.getY());
   length += from.getVectorTo(to).getMagnitude();
   //   System.out.println(from + "  " + to);
   return;
  }
  Point mid = from.getMidPoint(to);
  Vector delta = from.getVectorTo(to);
  delta = delta.getPerpendicularRight();
  delta = delta.multiplyBy(0.5); //half its magnitude
  mid = mid.addVector(delta);
  drawFractalLine(g,from,mid,level-1);
  drawFractalLine(g,mid,to,level-1);
 }

 public enum Direction {Right,Left};

 /**
  * Draws the Dragon curve http://en.wikipedia.org/wiki/Dragon_curve
  * @param g
  * @param from
  * @param to
  * @param level
  * @param d
  */
 public void drawFractalLineDragon(Graphics g, Point from, Point to, int level, Direction d) {
  if (level == 0) {
   g.drawLine(from.getX(), from.getY(), to.getX(), to.getY());
   length += from.getVectorTo(to).getMagnitude();
   //   System.out.println(from + "  " + to);
   return;
  }
  Point mid = from.getMidPoint(to);
  Vector delta = from.getVectorTo(to);
  if (d == Direction.Right) {
   delta = delta.getPerpendicularRight();
  } else {
   delta = delta.getPerpendicularLeft();
  }
  delta = delta.multiplyBy(0.5); //half its magnitude
  mid = mid.addVector(delta);
  drawFractalLineDragon(g,from,mid,level-1,Direction.Right);
  drawFractalLineDragon(g,mid,to,level-1,Direction.Left);
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  JFrame frame = new JFrame("Fractal Curve");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  Fractal panel = new Fractal();

  frame.add(panel);
  frame.setSize(600,600);
  frame.setVisible(true);
  try {
   do {
    String userInput = JOptionPane.showInputDialog("How Many Levels?");
    panel.numLevels = Integer.parseInt(userInput);
    frame.repaint(); //don't forget to re-paint it !
   } while (true);
  } catch (NumberFormatException e) {
   //ignore it, just exit program
   System.exit(0);
  }
 }

}

Wednesday, November 17, 2010

Lab Test 3 - Review Question

Problem 5 - Page 788

And also write a test program which will keep on asking the user for a string input and check if its a palindrome or not, by using the recursive method you have developed. The program should stop if the user enters "quit". The test program should also write all the input strings and the results (if its palindrome or not) to a text file.

Chapter 12 Video: Generics

Below is the screencast for Chapter 12 which talks about generics. The chapter also covers dynamic data structures but we will not be focusing on those, so you can skip those sections if you want, or you can read them and be ready for CSCE 146.

CSCE 145: Chapter 12 from Jose Vidal on Vimeo.

Here are the slides from the textbook:

Thursday, November 11, 2010

Lab 21 -- Section 5 & 6 -- Tuesday November 16 -- Sorting using MergeSort

Sort a list of numbers using MergeSort(with recursion). Please work on this from before hand and you must get it to work by your deadline lab timings on Tuesday

Please Refer to the MergeSort Program given in Chapter 11 of the book
Cheers

PS: I was unable to make it to class today due to TOEFL preparations.

Lab 20 - Section 5 and 6

Problem 4 - Page 788