lectures


01-24-2014

posted Jan 24, 2014, 6:16 AM by Samuel Konstantinovich   [ updated Jan 24, 2014, 6:16 AM ]

IMPORTANT Fill out this form:
http://goo.gl/jUI3Aj

01-23-2014

posted Jan 22, 2014, 10:41 PM by Samuel Konstantinovich   [ updated Jan 22, 2014, 10:41 PM ]

Sample Assignment (The Observations are short, because everything worked really well.

01-21-2014

posted Jan 21, 2014, 8:44 AM by Samuel Konstantinovich   [ updated Jan 22, 2014, 5:09 AM ]

EDIT: Your results should be something along these lines...

For Array Size 100000

The builtin Sort Should be the fastest 
0.027 seconds on my really fast machine

selection Sort (should be approximately 2-4 times slower than insertion)
3.401 seconds on my really fast machine

insertion Sort (should be FASTER than selection) * My results in class were wrong, I had a bug in my sort! *
1.835 seconds on my really fast machine

bubble Sort (should be approximately 4-6 times slower than slection)
15.427 seconds on my really fast machine

radix Sort (Optimally** should be 4-8 times longer time than builtin...)
0.084 seconds on my really fast machine

**If you did MUCH better than selection/insertion with radix, that is fine.





Your goal is to run this bash script on your code, and generate some test results. 


Here is the batch script:
#!/bin/bash
echo Array Sizes:
types=(builtin selection insertion bubble radix)
for i in `seq 1 10`;
do
    let x=$i*10000
    echo $x
done

for n in `seq 0 4`;
do
    echo ${types[$n]} Sort
    for i in `seq 1 10`;
    do
        let x=$i*10000
        java -Xmx1g Tester ${types[$n]} 10000000 $x
    done
done



Here is your java wrapper class, you must add your sort functions into the appropriate methods.

import java.util.*;
public class Tester{
    public static void main(String[]args){
        try{
            String type = args[0];
            int max = Integer.parseInt(args[1]);
            int size = Integer.parseInt(args[2]);
            int[]L = makeRandomArray(size,max);

            long startTime = System.currentTimeMillis();
            sort(L,type,size+1);
            long stopTime = System.currentTimeMillis();
            long elapsedTime = stopTime - startTime;

            if(isSorted(L)){
                System.out.println(elapsedTime/1000.0);
            }else{
                System.out.println("Failed to sort");
            }
        }catch(Exception e){
            System.out.println("usage: Test <TYPE> <MAX_VALUE> <ARRAY_SIZE>\n");
            e.printStackTrace();
        }
    }

    public static int[] makeRandomArray(int size, int max){
        int[]ary = new int[size];
        for(int i=0;i<size;i++){
            ary[i]=(int)(max*Math.random());
        }
        return ary;
    }

    public static void sort(int[]L,String type, int max){
        if(type.equals("bubble")){
            Bubble(L);
        }else if(type.equals("radix")){
            radixsort(L);
        }else if(type.equals("insertion")){
            Insertion(L);
        }else if(type.equals("selection")){
            Selection(L);
        }else{
            Arrays.sort(L);
        }
    }

    public static boolean isSorted(int[]L){
        if(L==null || L.length<2){
            return true;
        }
        for(int i=0; i<L.length-1;i++)
            if(L[i]>L[i+1]){
                return false;
            }
        return true;
    }
    public static void Selection(int[]L){
    /*put in your code*/
    }

    public static void Insertion(int[]L){
    /*put in your code*/
    }

    public static void Bubble(int[]L){
    /*put in your code*/
    }

    public static void radixsort(int[]d){
    /*put in your code*/
    }

}



01-08-2014

posted Jan 8, 2014, 7:24 PM by Samuel Konstantinovich   [ updated Jan 8, 2014, 7:24 PM ]

Good practice for your final: 

2012 AP 1,3,4
http://apcentral.collegeboard.com/apc/public/repository/ap_frq_computerscience_12.pdf

Solutions can be found here:
http://apcentral.collegeboard.com/apc/public/exam/exam_information/2000.html


01-02-2014

posted Jan 2, 2014, 7:57 AM by Samuel Konstantinovich   [ updated Jan 2, 2014, 11:35 AM ]

Goal: A lab in 3 acts:
Complete Act 1 and Act 2 by tomorrow. 

Act I: Revisiting ROT13 and ROTx

Mr. K: "Write a Rotate command line program that you give a string, and a numerical offset. The program rotates the characters by offset places in the alphabet, z wraps around to a, and capitalization stays the same."

Annoying student: "What?"

Mr. K: "Can you rephrase the question?"

Annoying student: "Can we have some examples?"

Mr. K: "Sure!"

$java Rotate abz 1  
bca
$java Rotate aBz 3  
dEc
$Rotate "Hello World" 1  
Ifmmp Xpsme


Annoying student: "Is that all? We did that in intro term two..."

Mr. K:  "You will later improve the rotate command. If there is no number provided, your rotate command will decode an encoded string. This isn't as hard as you think."


Act II: Return of the Poet 

Mr. K:  "Write a program that reads a text file, and counts the frequency of each letter (ignore case)"

Annoying student: "That sounds useless"

Mr. K: "It is quite useful. You will use this program to analyze English text to give you an idea of which letters appear most frequently. You will take a large text and see which letters are most common. In any sufficiently long English text, the frequencies would be similar. This can help you in act III. We will use a body of text from project gutenberg  http://www.gutenberg.org "

Annoying student: "Will different books give different results?"

:::Something Shakespeare this way comes:::

Mr. K: "Absolutely, A list of chinese names will have a lot of L's and X's. ... We will all use MacBeth so we all get the same frequencies and can check if we are correct. Use the resulting frequencies in the next part."


Act III: Fall of the L337 encryption algorithm

Mr. K: "You can use the frequencies to determine how many times the letters were shifted over."

Annoying student: "Really?"

Mr. K: "Of course."

Annoying student: "How?"

Mr. K: "Why don't you figure it out?"

Annoying student: "Because I am lazy and want you to tell me answers to memorize so I don't have to think!"

Mr. K: "While I applaud your honesty, that doesn't change the fact that you will have to think about it more. Any questions?"

Annoying student: "Why isn't this play in iambic pentameter?"

Mr. K: "Because I am not a playwright and even if I were: I prefer iambic tetrameter. Now try to figure out a way to make this work"

12-20-2013 Prototype

posted Dec 21, 2013, 8:26 AM by Samuel Konstantinovich   [ updated Dec 21, 2013, 8:26 AM ]


12-18-2013 GUI Bits...

posted Dec 19, 2013, 8:17 AM by Samuel Konstantinovich   [ updated Dec 19, 2013, 8:26 AM ]

Demo 1:   No content... basic window

import javax.swing.*;
public class Gui1 extends JFrame {
    public Gui1() {
        this.setTitle("My first GUI");
        this.setSize(600,400);
        this.setLocation(100,100);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        Gui1 g = new Gui1();
        g.setVisible(true);
    }
}

Demo 2: 

import javax.swing.*;
import java.awt.*; //needed for pane

public class Gui2 extends JFrame {
    private Container pane;
    private JButton exitButton,b;
    private JLabel l;
    private JTextField text;
    private Container buttons;
    private JCheckBox box;

    public Gui2() {
this.setTitle("My first GUI");
this.setSize(600,400);
this.setLocation(100,100);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        
pane = this.getContentPane();
pane.setLayout(new FlowLayout());
//pane.setLayout(new GridLayout(4,0));

l = new JLabel("This is a label",null,JLabel.CENTER);
exitButton = new JButton("Exit");
b = new JButton("doSomething");
text = new JTextField(40);

/*make some buttons in a container to organize them*/
buttons = new Container();
buttons.setLayout(new FlowLayout());
//buttons.setLayout(new GridLayout(0,3));
buttons.add(exitButton);
buttons.add(b);
box = new JCheckBox("upcase");
buttons.add(box);

//Add things to your pane
pane.add(buttons);
pane.add(l);
pane.add(text);
    }

    public static void main(String[] args) {
Gui2 g = new Gui2();
g.setVisible(true);
    }

}

Version 3: ( For 12/19/2013 class)
import javax.swing.*;
import java.awt.*; //needed for pane

public class GuiDoStuff extends JFrame {
    private Container background,buttons;
    private JButton exitButton,doStuffButton;
    private JLabel L1,L2;
    private JTextField text;
    private JCheckBox box;

    public GuiDoStuff() {
setTitle("GUI that does stuff demo:");
setSize(600,400);
setLocation(100,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
        
background = this.getContentPane();
background.setLayout(new GridLayout(3,1));
L1 = new JLabel("Text to process:");
L2 = new JLabel("Results:");


exitButton = new JButton("Exit");
doStuffButton = new JButton("Process");

text = new JTextField(40);

/*make some buttons in a container to organize them*/
buttons = new Container();
buttons.setLayout(new GridLayout(1,3));
buttons.add(doStuffButton);
buttons.add(exitButton);

//Add things to your pane
background.add(buttons);

Container c = new Container();
c.setLayout(new FlowLayout() );
c.add(L1);
c.add(text);
background.add(c);
box = new JCheckBox("upcase");
c.add(box);
background.add(L2);
    }

    public static void main(String[] args) {
GuiDoStuff g = new GuiDoStuff();
g.setVisible(true);
    }

}






12-13-2013

posted Dec 13, 2013, 9:21 AM by Samuel Konstantinovich   [ updated Dec 13, 2013, 9:21 AM ]

Our goal is to make MyList iterable. 

1. Iterators cannot have primitive types, so we will use the Integer object to handle our int list. This is why our Iterable<> and Iterator<> has Integer inside them.

2. The modification to MyList is very small. We make one method, iterator() that returns an Iterator<Integer>. 




import java.util.*;
public class MyList implements Iterable<Integer>{
    private int [] data;
    private int size;

    public Iterator<Integer> iterator(){
        /*the iterator needs a copy of the object you are 
          iterating over, so we pass it a refernece to this*/
        return new MyListIterator(this);
    }
    public MyList(){
        data = new int[10];
        size=0;
    }
    /* the rest of MyList not shown*/
}


NOW YOU WRITE THE ITERATOR PART:


import java.util.*;
public class MyListIterator implements Iterator<Integer>{
    MyList data;
    int place;

    public MyListIterator(MyList L){
        data = L;
        place = 0;
    }

    public boolean hasNext(){
        /* to be implemented by you!*/
        return false;
    }

    public Integer next(){
        /* to be implemented by you!*/
        return null;
    }

    public void remove(){
        /*since we don't want to implement this...*/
        throw new UnsupportedOperationException();
    }
}

12-10-2013

posted Dec 10, 2013, 8:41 AM by Samuel Konstantinovich   [ updated Dec 10, 2013, 8:49 AM ]

Goal:
minimum:
wordsearch must take these parameters:
rows
cols
filename


bonus points:
wordsearch takes any of these sets of parameters:
rows 
cols 
filename
rows 
cols 
filename randomSeed
rows 
cols 
filename randomSeed cheatFlag

Examples:
java wordsearch 12 12 names.txt
java wordsearch 12 12 names.txt 1923
java wordsearch 12 12 names.txt 1923 1



Solutions to some word search problems:

import java.util.*;
public class WordGrid{

    private char[][]data;

    public void randomize(Random rand){
        for(int i = 0; i < data.length; i++){
            for(int j = 0; j < data[i].length; j++){
                if(data[i][j]=='_')
                    data[i][j]= (char)( 'a'+(int)(rand.nextDouble()*26) );
            }
        }
    }

   public boolean addWord(String word, int row, int col, int drow, int dcol){
        if(drow == 0 && dcol == 0){
            return false;
        }
        /*check if the word doesn't change letters*/
        try{

            for(int i = 0; i < word.length(); i++){
                char letter = data[row + i * drow][col + i * dcol];
                if(letter != '_' && letter != word.charAt(i)){
                    return false;
                }
            }
        }catch(ArrayIndexOutOfBoundsException e){
            return false;
        }
        /*write out the letters to the array*/
        for(int i = 0; i < word.length(); i++){
            data[row + i * drow][col + i * dcol] = word.charAt(i);
        }
        return true;
    }


...
...
}

12-06-2013

posted Dec 7, 2013, 12:12 PM by Samuel Konstantinovich   [ updated Dec 7, 2013, 12:12 PM ]

Goal: WordSearch

You should have working word searches by Monday:
a. Fill the board.
b. Print the words to find.
c. Use the args to make your program produce different sizes/configurations.
d. Can you reproduce the same board? Use a random seed to do this. The seed should come from the arguments, and produce the same random words, and locations.
e. Allow for a solution board to be printed. 

 

1-10 of 56