<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1502916310418593789</id><updated>2011-10-20T19:21:35.659-04:00</updated><category term='while'/><category term='for'/><category term='shell'/><category term='loops'/><category term='arrays'/><category term='else'/><category term='random'/><category term='command prompt'/><category term='input'/><category term='oop'/><category term='methods'/><category term='bash'/><category term='data'/><category term='if'/><category term='variables'/><category term='switch'/><category term='case'/><title type='text'>Andrew's Leap 2007!</title><subtitle type='html'>Beginner Programming Track</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>27</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-4364966808382341661</id><published>2007-07-30T08:38:00.000-04:00</published><updated>2007-07-30T08:42:51.744-04:00</updated><title type='text'>Final Projects!</title><content type='html'>For the next two weeks you will be working on final projects. Your groups may not be more than 4 people, and even then the project better be absolutely fabulous. Today we will see what kind of ideas you guys came up with and discuss how doable they are.&lt;br /&gt;And then you will code. &lt;br /&gt;&lt;br /&gt;Since all of your code is original, you need to take your time to come up with a good design!! This means write things out with pen and paper, draw them, etc &lt;i&gt;before&lt;/i&gt; you begin to code. Also, this means that Sean and I will not be able to help you much, since we're not even that sure of what it is you're doing - but you are, hopefully. &lt;i&gt;You have to learn to troubleshoot your own code, and, most of all, not give up! Ever!&lt;/i&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-4364966808382341661?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/4364966808382341661/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=4364966808382341661' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/4364966808382341661'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/4364966808382341661'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/07/final-projects.html' title='Final Projects!'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-3029857359906465395</id><published>2007-07-26T22:00:00.000-04:00</published><updated>2007-07-26T09:06:08.132-04:00</updated><title type='text'>Snake Game</title><content type='html'>&lt;pre&gt;&lt;b&gt;Block.java&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;import javax.swing.JLabel;&lt;br /&gt;&lt;br /&gt;public class Block extends JLabel {&lt;br /&gt;&lt;br /&gt;    public Block() {&lt;br /&gt;        super("");&lt;br /&gt;        mode = 0;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void setMode(String mode) {&lt;br /&gt;        if(mode.equals("tail")) {&lt;br /&gt;            setText("o");&lt;br /&gt;            this.mode = 1;&lt;br /&gt;        } else&lt;br /&gt;        if(mode.equals("head")) {&lt;br /&gt;            setText("O");&lt;br /&gt;            this.mode = 2;&lt;br /&gt;        } else&lt;br /&gt;        if(mode.equals("pill")) {&lt;br /&gt;            setText(":)");&lt;br /&gt;            this.mode = 3;&lt;br /&gt;        } else&lt;br /&gt;        if(mode.equals("kill")) {&lt;br /&gt;            setText("X");&lt;br /&gt;            this.mode = 4;&lt;br /&gt;        } else {&lt;br /&gt;            setText(" ");&lt;br /&gt;            this.mode = 0;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public boolean occupied() {&lt;br /&gt;        return mode == 1 || mode == 2;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public int scoreOffset() {&lt;br /&gt;        if(mode == 3)&lt;br /&gt;            return 10;&lt;br /&gt;        return mode != 4 ? 0 : -5;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private int mode;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Coord.java&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;public class Coord {&lt;br /&gt;&lt;br /&gt;    public Coord(int row, int col) {&lt;br /&gt;        this.row = row;&lt;br /&gt;        this.col = col;&lt;br /&gt;        check();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void increment(int direction[]) {&lt;br /&gt;        row += direction[0];&lt;br /&gt;        col += direction[1];&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private void check() {&lt;br /&gt;        for(; maxRow &lt;= row; row--);&lt;br /&gt;        for(; maxCol &lt;= col; col--);&lt;br /&gt;        for(; 0 &gt; row; row++);&lt;br /&gt;        for(; 0 &gt; col; col++);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public String toString() {&lt;br /&gt;        return row + ", " + col;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public boolean valid() {&lt;br /&gt;        return col &gt;= 0 &amp;&amp; col &lt; maxCol &amp;&amp; row &gt;= 0 &amp;&amp; row &lt; maxRow;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public boolean equals(Coord other) {&lt;br /&gt;        return row == other.row &amp;&amp; col == other.col;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public static int maxRow;&lt;br /&gt;    public static int maxCol;&lt;br /&gt;    public int row;&lt;br /&gt;    public int col;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;b&gt;GUI.java&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;import java.awt.Container;&lt;br /&gt;import java.awt.GridLayout;&lt;br /&gt;import java.awt.event.KeyEvent;&lt;br /&gt;import java.awt.event.KeyListener;&lt;br /&gt;import java.util.LinkedList;&lt;br /&gt;import java.util.Random;&lt;br /&gt;import javax.swing.JApplet;&lt;br /&gt;&lt;br /&gt;public class GUI extends JApplet&lt;br /&gt;    implements Runnable, KeyListener {&lt;br /&gt;&lt;br /&gt;    public GUI() {&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void init() {&lt;br /&gt;        mousePost = new int[2];&lt;br /&gt;        mousePost[0] = 0;&lt;br /&gt;        mousePost[1] = 0;&lt;br /&gt;        direction = new int[2];&lt;br /&gt;        direction[0] = -1;&lt;br /&gt;        direction[1] = 0;&lt;br /&gt;        Coord.maxCol = 20;&lt;br /&gt;        Coord.maxRow = 20;&lt;br /&gt;        map = new Block[20][20];&lt;br /&gt;        paused = 1;&lt;br /&gt;        dead = 0;&lt;br /&gt;        newGame();&lt;br /&gt;        addKeyListener(this);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void start() {&lt;br /&gt;        Container c = getContentPane();&lt;br /&gt;        c.setLayout(new GridLayout(map.length, map[0].length));&lt;br /&gt;        for(int i = 0; i &lt; map.length; i++) {&lt;br /&gt;            for(int d = 0; d &lt; map[0].length; d++)&lt;br /&gt;                c.add(map[i][d]);&lt;br /&gt;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        if(updateThread == null) {&lt;br /&gt;            updateThread = new Thread(this);&lt;br /&gt;            updateThread.start();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void run() {&lt;br /&gt;        while(updateThread != null)  {&lt;br /&gt;            if(paused == 0 &amp;&amp; dead == 0) {&lt;br /&gt;                if(mode == 0) {&lt;br /&gt;                    Random rand = new Random();&lt;br /&gt;                    Coord oldHead = (Coord)snake.getFirst();&lt;br /&gt;                    Coord newHead = new Coord(oldHead.row, oldHead.col);&lt;br /&gt;                    newHead.increment(direction);&lt;br /&gt;                    for(; !newHead.valid() || map[newHead.row][newHead.col].occupied(); newHead.increment(direction)) {&lt;br /&gt;                        int pos = rand.nextInt(2);&lt;br /&gt;                        direction[pos] = rand.nextInt(3) - 1;&lt;br /&gt;                        direction[(pos + 1) % 2] = 0;&lt;br /&gt;                        if(direction[pos] == 0)&lt;br /&gt;                            for(; direction[(pos + 1) % 2] == 0; direction[(pos + 1) % 2] = rand.nextInt(3) - 1);&lt;br /&gt;                        newHead = new Coord(oldHead.row, oldHead.col);&lt;br /&gt;                    }&lt;br /&gt;&lt;br /&gt;                }&lt;br /&gt;                move();&lt;br /&gt;                plantKill();&lt;br /&gt;            }&lt;br /&gt;            try {&lt;br /&gt;                Thread.sleep(150L);&lt;br /&gt;            }&lt;br /&gt;            catch(InterruptedException e) {&lt;br /&gt;                return;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void stop() {&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void destroy() {&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public boolean move() {&lt;br /&gt;        Coord oldHead = (Coord)snake.getFirst();&lt;br /&gt;        Coord newHead = new Coord(oldHead.row, oldHead.col);&lt;br /&gt;        newHead.increment(direction);&lt;br /&gt;        if(!newHead.valid() || map[newHead.row][newHead.col].occupied()) {&lt;br /&gt;            paused = 1;&lt;br /&gt;            dead = 1;&lt;br /&gt;            return false;&lt;br /&gt;        }&lt;br /&gt;        int before = bonus;&lt;br /&gt;        bonus += map[newHead.row][newHead.col].scoreOffset();&lt;br /&gt;        if(bonus &gt; before)&lt;br /&gt;            plantPill();&lt;br /&gt;        snake.addFirst(newHead);&lt;br /&gt;        map[newHead.row][newHead.col].setMode("head");&lt;br /&gt;        map[oldHead.row][oldHead.col].setMode("tail");&lt;br /&gt;        if(bonus &lt;= 0) {&lt;br /&gt;            Coord temp = (Coord)snake.getLast();&lt;br /&gt;            map[temp.row][temp.col].setMode("blank");&lt;br /&gt;            snake.removeLast();&lt;br /&gt;            if(bonus &lt; 0) {&lt;br /&gt;                temp = (Coord)snake.getLast();&lt;br /&gt;                map[temp.row][temp.col].setMode("blank");&lt;br /&gt;                snake.removeLast();&lt;br /&gt;                bonus++;&lt;br /&gt;            }&lt;br /&gt;        } else {&lt;br /&gt;            bonus--;&lt;br /&gt;        }&lt;br /&gt;        return true;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void plantKill() {&lt;br /&gt;        Random rand = new Random();&lt;br /&gt;        int i = rand.nextInt(map.length);&lt;br /&gt;        int d = rand.nextInt(map[0].length);&lt;br /&gt;        int count;&lt;br /&gt;        for(count = 0; map[i][d].occupied() &amp;&amp; count &lt; 200; count++) {&lt;br /&gt;            i = rand.nextInt(map.length);&lt;br /&gt;            d = rand.nextInt(map[0].length);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        if(count == 200)&lt;br /&gt;            return;&lt;br /&gt;        int die = rand.nextInt(50);&lt;br /&gt;        if(die == 1)&lt;br /&gt;            map[i][d].setMode("kill");&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void plantPill() {&lt;br /&gt;        Random rand = new Random();&lt;br /&gt;        int i = rand.nextInt(map.length);&lt;br /&gt;        int d = rand.nextInt(map[0].length);&lt;br /&gt;        int count;&lt;br /&gt;        for(count = 0; map[i][d].occupied() &amp;&amp; count &lt; 200; count++) {&lt;br /&gt;            i = rand.nextInt(map.length);&lt;br /&gt;            d = rand.nextInt(map[0].length);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        if(count == 200) {&lt;br /&gt;            return;&lt;br /&gt;        } else {&lt;br /&gt;            map[i][d].setMode("pill");&lt;br /&gt;            return;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void newGame() {&lt;br /&gt;        direction[0] = -1;&lt;br /&gt;        direction[1] = 0;&lt;br /&gt;        mousePost[0] = 0;&lt;br /&gt;        mousePost[1] = 0;&lt;br /&gt;        for(int c = 0; c &lt; map.length; c++) {&lt;br /&gt;            for(int d = 0; d &lt; map[0].length; d++)&lt;br /&gt;                if(map[c][d] == null)&lt;br /&gt;                    map[c][d] = new Block();&lt;br /&gt;                else&lt;br /&gt;                    map[c][d].setMode("");&lt;br /&gt;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        bonus = 0;&lt;br /&gt;        mode = 1;&lt;br /&gt;        paused = 1;&lt;br /&gt;        dead = 0;&lt;br /&gt;        snake = new LinkedList();&lt;br /&gt;        snake.add(new Coord(17, 10));&lt;br /&gt;        snake.add(new Coord(18, 10));&lt;br /&gt;        snake.add(new Coord(19, 10));&lt;br /&gt;        snake.add(new Coord(20, 10));&lt;br /&gt;        plantPill();&lt;br /&gt;        move();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void keyTyped(KeyEvent keyevent) {&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void keyPressed(KeyEvent e) {&lt;br /&gt;        mode = 1;&lt;br /&gt;        char c = e.getKeyChar();&lt;br /&gt;        if(c == 'a') {&lt;br /&gt;            paused = 0;&lt;br /&gt;            direction[0] = 0;&lt;br /&gt;            direction[1] = -1;&lt;br /&gt;        } else&lt;br /&gt;        if(c == 'w') {&lt;br /&gt;            paused = 0;&lt;br /&gt;            direction[0] = -1;&lt;br /&gt;            direction[1] = 0;&lt;br /&gt;        } else&lt;br /&gt;        if(c == 'd') {&lt;br /&gt;            paused = 0;&lt;br /&gt;            direction[0] = 0;&lt;br /&gt;            direction[1] = 1;&lt;br /&gt;        } else&lt;br /&gt;        if(c == 's') {&lt;br /&gt;            paused = 0;&lt;br /&gt;            direction[0] = 1;&lt;br /&gt;            direction[1] = 0;&lt;br /&gt;        } else&lt;br /&gt;        if(c == 'p')&lt;br /&gt;            paused = (paused + 1) % 2;&lt;br /&gt;        else&lt;br /&gt;        if(c == 'n')&lt;br /&gt;            newGame();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void keyReleased(KeyEvent keyevent) {&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    LinkedList snake;&lt;br /&gt;    Block map[][];&lt;br /&gt;    int mode;&lt;br /&gt;    int paused;&lt;br /&gt;    int dead;&lt;br /&gt;    int bonus;&lt;br /&gt;    int direction[];&lt;br /&gt;    int mousePost[];&lt;br /&gt;    private Thread updateThread;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Once you copy-paste the code into Eclipse appropriately, Run the GUI class as an applet. The other two classes are not for running! they are just data type creations, basically.&lt;br /&gt;Make sure to look through the code and figure out what's going on. Ask me any questions that arise..&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-3029857359906465395?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/3029857359906465395/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=3029857359906465395' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/3029857359906465395'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/3029857359906465395'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/07/snake-game.html' title='Snake Game'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-5767601259845926618</id><published>2007-07-24T10:37:00.000-04:00</published><updated>2007-07-24T10:46:02.793-04:00</updated><title type='text'>Runnable</title><content type='html'>&lt;pre&gt;import java.awt.*;&lt;br /&gt;import javax.swing.*;&lt;br /&gt;import java.awt.event.*;&lt;br /&gt;import java.util.*;&lt;br /&gt;&lt;br /&gt;public class HelloWorld extends JApplet implements MouseListener,Runnable&lt;br /&gt;{&lt;br /&gt;    Thread updateThread;&lt;br /&gt;    int x,y;&lt;br /&gt;    Random rand;&lt;br /&gt;    public void init(){&lt;br /&gt;        rand = new Random();&lt;br /&gt;        x=0;&lt;br /&gt;        y=0;&lt;br /&gt;    }&lt;br /&gt;    public void start(){&lt;br /&gt;        addMouseListener(this);&lt;br /&gt;        if (updateThread == null) {&lt;br /&gt;            updateThread = new Thread(this, "threadname");&lt;br /&gt;            updateThread.start();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    public void run() {&lt;br /&gt;        Thread myThread = Thread.currentThread();&lt;br /&gt;        while (updateThread == myThread) {&lt;br /&gt;            if(rand.nextInt(2)==0){&lt;br /&gt;                    x+=3;&lt;br /&gt;                }&lt;br /&gt;                else{&lt;br /&gt;                    y+=3;&lt;br /&gt;                }&lt;br /&gt;                repaint();&lt;br /&gt;        try {&lt;br /&gt;            Thread.sleep(50);&lt;br /&gt;        } catch (InterruptedException e){  }&lt;br /&gt;    }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void stop(){&lt;br /&gt;        updateThread=null;&lt;br /&gt;    }&lt;br /&gt;    public void destroy(){}&lt;br /&gt;    public void paint(Graphics g){&lt;br /&gt;        g.fillRect(x,y,2,2);&lt;br /&gt;    }&lt;br /&gt;    public void mouseClicked(MouseEvent e){&lt;br /&gt;        x = e.getX();&lt;br /&gt;        y = e.getY();&lt;br /&gt;        repaint();&lt;br /&gt;    }&lt;br /&gt;    public void mouseReleased(MouseEvent e){&lt;br /&gt;    }&lt;br /&gt;    public void mousePressed(MouseEvent e){&lt;br /&gt;    }&lt;br /&gt;    public void mouseExited(MouseEvent e){&lt;br /&gt;    }&lt;br /&gt;    public void mouseEntered(MouseEvent e){&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;}&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-5767601259845926618?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/5767601259845926618/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=5767601259845926618' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/5767601259845926618'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/5767601259845926618'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/07/runnable.html' title='Runnable'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-8466664028433311928</id><published>2007-07-24T10:00:00.000-04:00</published><updated>2007-07-24T10:37:05.450-04:00</updated><title type='text'>Applets</title><content type='html'>Hokay, so, applets are way cool.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Life-cycle&lt;/b&gt;&lt;br /&gt;Your command-line programs have that main method that always gets run - a pretty simple "life cycle." Applets have four such special methods:&lt;br /&gt;&lt;code&gt;public void init(){}&lt;br /&gt;public void start(){}&lt;br /&gt;public void stop(){}&lt;br /&gt;public void destroy(){}&lt;/code&gt;&lt;br /&gt;Init and destroy mark the birth and death of your applet. Start gets called each time the applet becomes active, and stop each time it becomes inactive.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Imports&lt;/b&gt;&lt;br /&gt;You will need to import these packages for every applet you make:&lt;br /&gt;&lt;code&gt;import java.awt.*;&lt;br /&gt;import javax.swing.*;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Paint method&lt;/b&gt;&lt;br /&gt;&lt;code&gt;public void paint(Graphics g){}&lt;/code&gt;&lt;br /&gt;In this method, call the graphics element g to make marks on your applet. For example:&lt;br /&gt;&lt;code&gt;g.drawString("Hello, World!", 15,25);&lt;/code&gt;&lt;br /&gt;You should check out the &lt;a href=http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html&gt;Graphics class in the API&lt;/a&gt; for a full listing of the methods you can use.&lt;br /&gt;To update the applet, you can call &lt;B&gt;repaint();&lt;/b&gt; pretty much anywhere in the code.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;MouseListener&lt;/b&gt;&lt;br /&gt;&lt;code&gt;import java.awt.event.*;&lt;/code&gt;&lt;br /&gt;Then in your Applet header, make sure to add &lt;code&gt;implements MouseListener&lt;/code&gt;&lt;br /&gt;You will need to override these methods:&lt;br /&gt;&lt;code&gt;public void mouseClicked(MouseEvent e){}&lt;br /&gt;public void mouseEntered(MouseEvent e){}&lt;br /&gt;public void mouseExited(MouseEvent e){}&lt;br /&gt;public void mousePressed(MouseEvent e){}&lt;br /&gt;public void mouseReleased(MouseEvent e){}&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-8466664028433311928?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/8466664028433311928/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=8466664028433311928' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/8466664028433311928'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/8466664028433311928'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/07/applets.html' title='Applets'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-7287229441643480106</id><published>2007-07-20T11:30:00.000-04:00</published><updated>2007-07-21T19:00:49.235-04:00</updated><title type='text'>It's Alive (Again)!!</title><content type='html'>The blog will be updated on a regular basis for the last week of lecture. Check back posts for problems and stuff. Talk to me about them if you have issues.&lt;br /&gt;&lt;br /&gt;Now, next week, we will be learning about graphics, truly a magical beast. By the end of the week, you will have coded &lt;a href=http://withcolors.phpnet.us/Scripts/snake5/GUI.html target=_blank&gt;this Snake game&lt;/a&gt;. It does lots of cool stuff.&lt;br /&gt;&lt;br /&gt;Since we're on to the &lt;i&gt;really&lt;/i&gt; good stuff, where you can pretty much can, and should, code anything you want, you should get your home computer set up for this, if you haven't already. Get BlueJ, and Java 1.4.2. That version of Java does NOT have Scanner (talk to me about this), but the applets that you generate with it will run literally &lt;i&gt;anywhere&lt;/i&gt; (while higher versions of Java have a few kinks to them, especially if you're compiling with BlueJ, don't ask me why because I don't know, but it's caused me anough worry to be wary of it). If you have any questions/concerns, that's what I'm here for.&lt;br /&gt;&lt;br /&gt;The point of this: you really, really need your IDE set up one of them days.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-7287229441643480106?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/7287229441643480106/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=7287229441643480106' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/7287229441643480106'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/7287229441643480106'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/07/its-alive-again.html' title='It&apos;s Alive (Again)!!'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-3017369123222994860</id><published>2007-07-20T10:00:00.000-04:00</published><updated>2007-07-21T18:53:01.866-04:00</updated><title type='text'>LinkedList</title><content type='html'>The coolest thing about arrays is that you can access any element inside them immediately just by calling arrayname[a][b]... But what if you had, for example, a list of ages representing students in a class:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;1. [17] [15] [16] [19] [15] [15] [16]&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Now, imagine the seventeen-year-old wants to drop out. So what do you do? You create a new empty array with 1 less element,&lt;br /&gt;&lt;br /&gt;&lt;code&gt;2. [  ] [  ] [  ] [  ] [  ] [  ]&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;and start filling it with the existing elements, omitting 17&lt;br /&gt;&lt;br /&gt;&lt;code&gt;3. [15] [  ] [  ] [  ] [  ] [  ]&lt;br /&gt;4. [15] [16] [  ] [  ] [  ] [  ]&lt;br /&gt;5. [15] [16] [19] [  ] [  ] [  ]&lt;br /&gt;6. [15] [16] [19] [15] [  ] [  ]&lt;br /&gt;7. [15] [16] [19] [15] [15] [  ]&lt;br /&gt;8. [15] [16] [19] [15] [15] [16]&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;That's seven operations to do. Moreover, the number of necessary operations increases as the size of the starting array increases. If we're talking about a class of seven people, than that's ok; but if we have a "class" of a few thousand, to use an array would be very, very inefficient.&lt;br /&gt;And that's why there are lists. These are different from arrays in that they're not multi-dimensional tables, but strings that tie together independent variables:&lt;pre&gt;array: [..] [..] [..]&lt;br /&gt;list:   .. - .. - ..&lt;/pre&gt;The point is, its easier to break a connecting string than to reassign values of a table. Back to the class example, now with a list:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;1. 17-15-16-19-15-15-16&lt;br /&gt;2. 17 15-16-19-15-15-16&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;That's a single operation! And it doesnt matter how large the list is.&lt;br /&gt;&lt;br /&gt;To isolate a single element in a list, though, you need to flip through all the previous ones with an iterator. An iterator is an object that allows you to walk through a list. Imagine a cursor. It only moves one element over at a time, and it can move either backward or forward, with no knowledge of the non-neighboring elements. So, in the class example, say you wanted to go to the position the nineteen year old was it. In an array, you would simply call ages[3], but in a list, you'd use an iterator (denoted by a |):&lt;br /&gt;&lt;br /&gt;&lt;code&gt;1. |17-15-16-19-15-15-16&lt;br /&gt;2. 17-|15-16-19-15-15-16&lt;br /&gt;3. 17-15-|16-19-15-15-16&lt;br /&gt;4. 17-15-16-|19-15-15-16&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;So, if you know the position you want to look at, you can do it in a single operation in an array, but many operations (the number of which does depend on the size) in a list. Therefore, your program design choice of data structures should depend on what actions you want done efficiently.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Programming Challenge:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;In order to understand lists, you should be able to code a list from scratch. That's why we had you do your own LinkedList.java and ListNode.java. Now, the list we did in class was one-directional:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;[ ]-&gt;[ ]-&gt;[ ]-&gt;etc&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;For extra fun, create a bi-directional list that you do not have to rewind! You would have two cursors, one for adding, and one for iteration:&lt;prE&gt;+-&gt;[ ]&lt;-&gt;[ ]&lt;-&gt;[ ]&lt;-&gt;[ ]&lt;-+&lt;br /&gt;|                         |&lt;br /&gt;+-------------------------+&lt;/pre&gt;&lt;br /&gt;So, you start with one element, you add to the right and shift over. So, the adder cursor will always be at the historicaly "last" item. The last item always points to the beginning. Note that you should be able to insert - ie, add at an arbitrary position - too! Have fun.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-3017369123222994860?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/3017369123222994860/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=3017369123222994860' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/3017369123222994860'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/3017369123222994860'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/07/linkedlist.html' title='LinkedList'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-7924528010918783449</id><published>2007-07-19T10:00:00.000-04:00</published><updated>2007-07-21T18:45:57.529-04:00</updated><title type='text'>Trees &amp; Recursion</title><content type='html'>Trees and recursion go together, which is why we do them one after another.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Recursion&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Besides loops, there is another way of dealing with repeating events: Recursion. Recursion is when you have a method, and that method calls itself, thereby reiterating. This is a little strange and confusing to code at first, but later it becomes a very instinctive thing. For example, consider (conceptually) a hallway with doors:&lt;pre&gt;+---+&lt;br /&gt;|   |&lt;br /&gt;[   ]&lt;br /&gt;|   |&lt;br /&gt;[   ]&lt;br /&gt;+- -+&lt;/pre&gt;Each square bracket is a door. you don't know what is behind the door. All you know if that there can be one of three things: (1) an empty room, (b) a room with treasure in it, and (c) another hallway that has doors with the same properties. Your tast is to create a method that looks for the treasure. (You know that there is exactly one room with treasure in it and that hallways aren't infinite). How would you look for it? Here's a sample algorithm:&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Check each door in the hallway consecutively:&lt;br /&gt;&lt;li&gt;If the door leads to an empty room, move on to the next one.&lt;br /&gt;&lt;li&gt;If the door leads to the room with the treasure, end the search and declare victory.&lt;br /&gt;&lt;li&gt;If it is a hallway, check every door in that hallway using the same algorithm. When done, and no treasure has been found, go back into the previous hallway and move on to the next door. &lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;When you have an idea of how to go about solving a problem, coding it is relatively easy. First off, the above algorithm assumes that there exists a (relatively) independent piece of code that can be called with various parameters. The parameters? The position of the door. So, more closely to Java-speak, the above would look like this:&lt;pre&gt; public String seekTreasure(String door){&lt;br /&gt;1   if(isEmptyRoom(door)){&lt;br /&gt;2     return "";&lt;br /&gt;      }&lt;br /&gt;3   else if(isHallway(door)){&lt;br /&gt;4     String[] doors = getDoors(door);&lt;br /&gt;5     for(int count=0; count&amp;lt;doors.length; count++){&lt;br /&gt;6         if(seekTreasure(doors[count])!=""){&lt;br /&gt;7           return doors[count];&lt;br /&gt;          }&lt;br /&gt;        }&lt;br /&gt;     }&lt;br /&gt;8  else{&lt;br /&gt;9    return door;&lt;br /&gt;    }&lt;br /&gt;   }&lt;/pre&gt;Line-by-line explanation:&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Assume that there is a method boolean isEmptyRoom(String). (When I say "assume", I mean that for an outline such as this where no restrictions on assumption were placed, it is acceptable to use these methods as long as you define what they do without actually writing out the code, although you *would* have to write them out if you were actually coding this problem.) This method is defined as a method that takes in a String representation of a door and returns true if the door leads to an empty room and false if not. This if statement basically tests if this particular door leads to an empty room...&lt;br /&gt;&lt;li&gt;...if it does, return an empty String.&lt;br /&gt;&lt;li&gt;The method boolean isHallway(String) is also assumed existant. It takes a String representation of a door and tests whether it opens to a hallway. Note: this else is redundant. You can just have an if there. Because of the return statement in the if statements, they are mutually exclusive.&lt;br /&gt;&lt;li&gt;Assume the existence of String[] getDoors(String) which takes in the string representation of a door and returns a String array of the represantations of the doors within this new hallway.&lt;br /&gt;&lt;li&gt;A for loop (See: Loops) that iterates through the array of doors.&lt;br /&gt;&lt;li&gt;Runs the method seekTreasure on each door in the hallway. This is the step where the actual recursion shows up. If the door returns anything other than an empty String...&lt;br /&gt;&lt;li&gt;... the method returns the representation for that particular door.&lt;br /&gt;&lt;li&gt;Another redundant else. Generally, I wouldn't use the classic if-else construction in a practice program, but I do recommend doing so for any large project you do because it makes code reading and understanding later on just that much easier.&lt;br /&gt;&lt;li&gt;Basically, the previous if's rule out all possibilities except the actual treasure. So, if it gets this far, the room must contain the treasure, and so the method returns the representation for this door. &lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;There are two reasons I introduced three non-existent methods in the code:&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;It's important to learn to read and write code little bit my little bit. Approaching everything at the same time can be overwhelming and ultimately useless. Assuming the existence of many things, however, narrows down the immediate problem and allows you to focus on specific details as opposed to the big picture all at once. However, this also requires some planning.&lt;br /&gt;&lt;li&gt;Actually, recursion works best with problems based on Trees, so avoiding using Trees would be unnecessarily ugly. Looking for a challenge? Read the chapter on Trees, and code this problem from start to finish! (Note: In the seekTreasure method, take in a Tree and assume it's given. You will not need any other methods, but you can make them for simplicity's sake. Create a main test method which actually creates a sample Tree, which you can hard-code to make sure everything works right, and then run the method on.) &lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;b&gt;Trees&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Trees are incredibly awesome. When you understand trees, you will not be able to remember why you didn't know and love trees before ^__^&lt;br /&gt;A graph is defined as a set of nodes connected to one another. This is an example of a valid graph:&lt;pre&gt;o---o&lt;br /&gt;|   |&lt;br /&gt;o---o&lt;/pre&gt;A graph is defined such that from any one node there is a valid path to any other node.&lt;br /&gt;A tree is a special kind of graph:&lt;pre&gt;   o&lt;br /&gt;  / \&lt;br /&gt; o   o&lt;br /&gt;    / \&lt;br /&gt;   o   o&lt;br /&gt;    \&lt;br /&gt;    o&lt;/pre&gt;A tree is defined such that each node has 0 to n children nodes, but only 1 parent node, unless it is the root (the topmost node), in which case it has no parent at all. In this chapter, I will discuss primarily binary seach trees, which have many very cool properties (and are all over the AP test), and are defined such that each node has either 2 children or no children at all. Nodes that have no children are called leaf nodes.&lt;pre&gt;     o&lt;br /&gt;    / \&lt;br /&gt;   o   o&lt;br /&gt;  / \&lt;br /&gt; o   o&lt;br /&gt;    / \&lt;br /&gt;   o   o&lt;/pre&gt;There is another requirement to binary search trees: the right child must always be bigger than the node, and the left child must always be smaller. (By bigger and smaller, I mean in comparison of values. Each node is has integer, or some sort of comparable value.) For example, compare these two trees and determine which is a valid binary search tree and which isn't:&lt;pre&gt;(a)    10      (b)     10&lt;br /&gt;      /  \            /  \&lt;br /&gt;     8   20          5   20&lt;br /&gt;    / \             / \&lt;br /&gt;   4   9           4   8&lt;br /&gt;      / \             / \&lt;br /&gt;     7  11           7   9&lt;/pre&gt;(b) is the valid tree. (a) is invalid because:&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;while 7 smaller than 9 and satisfies the rule for the node 9, it is not bigger than node 8, and therefore breaks the rule!&lt;br /&gt;&lt;li&gt;the same thing with 11: it works for nodes 9 and 8, but it must be smaller than 10 by definition, and is not. &lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;b&gt;Lesson to take away with you!&lt;/b&gt;&lt;br /&gt;The reason Trees and Recursion are so connected is that Trees posess a very unique quality: each node of a tree can be treated as a tree in itself. This property might not seem like much, but it is integral to dealing with trees!&lt;br /&gt;One of the most common operations to a tree is traversal - going through the tree and seeing what all the values are (and outputting them, performing mathematical operations on them, etc).&lt;br /&gt;There are three types of tree traversal:&lt;br /&gt;&lt;uL&gt;&lt;br /&gt;&lt;li&gt;preorder: traverse left child, traverse right child, read value of node&lt;br /&gt;&lt;li&gt;inorder: traverse left child, read value of node, traverse right child&lt;br /&gt;&lt;li&gt;postorder: read value of node, traverse left child, traverse right child &lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;Note how it reads, "traverse ... child" NOT "read value of ... child"! That is because if the child has children of its own, you must deal with them, too. So, what you have is a process the begs to use recursion:&lt;br /&gt;(the folowing is pseudocode for a valid traversal algorithm)&lt;pre&gt;method traverseInorder(TreeNode node){&lt;br /&gt;  if( hasLeftChild )&lt;br /&gt;    traverseInorder( getLeftChild )&lt;br /&gt;  output( value )&lt;br /&gt;  if( hasRightChild )&lt;br /&gt;    traverseInorder( getRightChild )&lt;br /&gt;  }&lt;/pre&gt;&lt;br /&gt;Now, there are many different types of traversal - that is because they all have neat properties you can use in applications. Inorder traversal will generate a sorted list of all the elements in O(n) time, which, considering how bulky sorting algorithms generally get, is pretty cool. Post- and preorder have their own specific applications. One of these deals with the following problem:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Programming Challenge:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Take as input a preorder mathematical expression (assume all input to be valid), calculate the result, and output both the inorder expression and the result. The input expression may only contain positive integers and basic integer operators - so +, -, * only! (add % to that list if you feel very energetic). For example: [input ] + 3 - * 4 5 6 [output] 3 + ( ( 4 * 5 ) - 6) = 17&lt;br /&gt;&lt;br /&gt;This is basically is a simplified expression evaluator. It may not lok much like a tree problem, but that depends on how you approach it! A good way (and yes, this counts as a hint!) is to imagine the expression as a tree where nodes are operators except leaf nodes, which are integers:&lt;pre&gt;   +&lt;br /&gt;  / \&lt;br /&gt; 3  -&lt;br /&gt;   / \&lt;br /&gt;  *  6&lt;br /&gt; / \&lt;br /&gt;4  5&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Another programming challenge...&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Do this using Stacks. You can use google/wikipedia powers to look up an algorithm for "Polish notation" calculation. Best of luck!!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-7924528010918783449?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/7924528010918783449/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=7924528010918783449' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/7924528010918783449'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/7924528010918783449'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/07/trees-recursion.html' title='Trees &amp; Recursion'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-5132244518311470353</id><published>2007-07-17T10:00:00.000-04:00</published><updated>2007-07-18T09:07:15.802-04:00</updated><title type='text'>Stacks &amp; Queues</title><content type='html'>Both are data structures that build upon the ArrayList datastructure. They have these key methods:&lt;br /&gt;&lt;br /&gt;public String pop() (to remove and return an element)&lt;br /&gt;public String peek() (to return, but not remove, an element)&lt;br /&gt;public String push() (to add an element)&lt;br /&gt;&lt;br /&gt;Both remove the first element in the ArrayList that they use in pop() and access the first element in peek(). In push(), however, a difference arises: in Stacks, an element is added to he front, and in Queues to the back. So, if you stored and printed the same thing from both, one would actually reverse it:&lt;br /&gt;&lt;br /&gt;input: Mary had a little lamb&lt;br /&gt;-stack: [lamb] [little] [a] [had] [Mary]&lt;br /&gt;-queue: [mary] [had] [a] [little] [lamb]&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-5132244518311470353?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/5132244518311470353/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=5132244518311470353' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/5132244518311470353'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/5132244518311470353'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/07/stacks-queues.html' title='Stacks &amp; Queues'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-1286046546670900655</id><published>2007-07-16T10:00:00.000-04:00</published><updated>2007-07-17T10:31:42.515-04:00</updated><title type='text'>Monday: Hashing, Maps, and Really Hard Problems</title><content type='html'>We learned about the wonders of hash first.&lt;br /&gt;This is a way to make a numeric code for a string and then assign it to a spot in a simple array.&lt;br /&gt;&lt;br /&gt;Then we learned about Maps. The great advantage of arrays and ArrayLists is that they use &lt;b&gt;indeces&lt;/b&gt; to name their elements, so that you can easily access using such things like for loops. The great &lt;i&gt;dis&lt;/i&gt;advantage of arrays and ArrayLists is that they use indeces and not &lt;b&gt;keys&lt;/b&gt;. Keys are not numbers, but user-defined objects. Maps use keys to call its elements.&lt;br /&gt;&lt;br /&gt;So, suppose you wanted to create a data structure that linked name and phone number. One way would be to use two arrays, one with phone numbers and the other with names:&lt;pre&gt;[ 555 5555 ] [ 555 6789 ] [ 555 1234 ] [ 555 1969 ]&lt;br /&gt;      0            1            2            3&lt;br /&gt;&lt;br /&gt;[ bob g.   ] [ some guy ] [ big bird ] [ mjackson ]&lt;br /&gt;      0            1            2            3&lt;/pre&gt;&lt;br /&gt;This way, every entry has an index from 0 to 3 that corresponds to some piece of data is two distinct places. Now, using a Map would allow you to do this:&lt;pre&gt;[ 555 5555 ] [ 555 6789 ] [ 555 1234 ] [ 555 1969 ]&lt;br /&gt; "bob g."     "some guy"   "big bird"   "mjackson"&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Then there were two problems that I threw at you.&lt;br /&gt;&lt;br /&gt;1. The Anagrams problem, which uses Maps. I sent the code for this to your guest account - be sure to check it out.&lt;br /&gt;&lt;br /&gt;2. Numbers.java problem - consider this the challenge problem!&lt;br /&gt;Firs, read in a number of any length up to 5 digits without leading 0's. Then, rearrange this number to be the largest number possible with these digits (without leading 0's!) So, for example:&lt;br /&gt;input: 12543&lt;br /&gt;output: 54321&lt;br /&gt;input: 69&lt;br /&gt;output: 96 (NOT 00096!)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-1286046546670900655?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/1286046546670900655/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=1286046546670900655' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/1286046546670900655'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/1286046546670900655'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/07/monday-hashing-maps-and-really-hard.html' title='Monday: Hashing, Maps, and Really Hard Problems'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-8805043147264212614</id><published>2007-07-13T09:22:00.000-04:00</published><updated>2007-07-13T09:30:09.546-04:00</updated><title type='text'>ArrayList and The Amazing Radix Sort</title><content type='html'>Today, instead of trying to write code, you will tyr to analyze it. Since, usually, the internet is at your disposal and all resources are generally fair game, you need to be able to use them. Even if you can't come up with code, it's a good idea to be able to discern what code means:&lt;br /&gt;&lt;prE&gt;import java.util.ArrayList;&lt;br /&gt;import java.util.Random;&lt;br /&gt;&lt;br /&gt;public class Radix{&lt;br /&gt;  public static void main ( String args[] ){&lt;br /&gt;&lt;br /&gt;    ArrayList[] buckets = new ArrayList[10];&lt;br /&gt;    for(int c=0; c&amp;lt;buckets.length; c++){&lt;br /&gt;      buckets[c] = new ArrayList();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    Random rand = new Random();&lt;br /&gt;    int[] arr = new int[100];&lt;br /&gt;    for(int c=0; c&amp;lt;arr.length; c++){&lt;br /&gt;      arr[c] = rand.nextInt(1000);&lt;br /&gt;    }&lt;br /&gt;    // RADIX SORT&lt;br /&gt;    for(int c=0; c&amp;lt;4; c++){&lt;br /&gt;      for(int d=0; d&amp;lt;arr.length; d++){&lt;br /&gt;        buckets[ (arr[d]/((int)Math.pow(10,c)))%10 ].add(new Integer(arr[d]));&lt;br /&gt;      }&lt;br /&gt;      int b = 0;&lt;br /&gt;      for(int d=0; d&amp;lt;arr.length; d++){&lt;br /&gt;        if(buckets[b].isEmpty()){  &lt;br /&gt;          b++; d--;&lt;br /&gt;          continue;&lt;br /&gt;        }&lt;br /&gt;        arr[d] = ((Integer)buckets[b].remove(0)).intValue();&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;       &lt;br /&gt;    System.out.println(((char)27)+"[34;47m");&lt;br /&gt;    for(int c=0; c&amp;lt;arr.length; c++){&lt;br /&gt;      System.out.print(arr[c]+" ");&lt;br /&gt;    }&lt;br /&gt;    System.out.println(((char)27)+"[0m");&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;For this, you can use (and will use) the API for &lt;a href=http://java.sun.com/j2se/1.3/docs/api/java/lang/Integer.html&gt;Integer&lt;/a&gt; and &lt;a href=http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html&gt;ArrayList&lt;/A&gt;. &lt;br /&gt;&lt;br /&gt;Your task is to figure out how the Radix sort actually works (notice that no comparisons are being made - interesting, isn't it?) just from looking an dplaying with the code above.&lt;br /&gt;Good luck!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-8805043147264212614?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/8805043147264212614/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=8805043147264212614' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/8805043147264212614'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/8805043147264212614'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/07/arraylist-and-amazing-radix-sort.html' title='ArrayList and The Amazing Radix Sort'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-8689238155389933815</id><published>2007-07-12T10:00:00.000-04:00</published><updated>2007-07-12T09:48:13.129-04:00</updated><title type='text'>More Algorithms: Selection &amp; Insertion Sort</title><content type='html'>Since Bubble Sort is pretty inefficient, let's look at soem other, more effiient sorting algorithms.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Selection Sort&lt;/b&gt;&lt;br /&gt;is generally much better than bubble sort.&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Go through the array from 0 to n-2 using the counter i.&lt;br /&gt;&lt;li&gt;Each time, assume that the element at i is the smallest. Save this index (not the value itself!) as min. Then, iterate through the array from i+1 to n-1, changing min to reflect the position of the smallest element.&lt;br /&gt;&lt;li&gt;If, after the search for another min is completed, and if i no longer equals min, swap the elements at i and min.&lt;br /&gt;&lt;/ol&gt;&lt;pre&gt;import java.util.*;&lt;br /&gt;public class Selection{&lt;br /&gt;  public static void main(String args[]){&lt;br /&gt;    Random rand = new Random();&lt;br /&gt;    int[] arr = new int[rand.nextInt(90)+10];&lt;br /&gt;    for(int c=0; c&amp;lt;arr.length; c++){&lt;br /&gt;      arr[c] = rand.nextInt(100);&lt;br /&gt;    }&lt;br /&gt;    int i, j, min;&lt;br /&gt;    for(i=0; i&amp;lt;arr.length-1; i++){&lt;br /&gt;      min=i;&lt;br /&gt;      for(j=i+1; j&amp;lt;arr.length; j++){&lt;br /&gt;        if(arr[j]&amp;lt;arr[min]){&lt;br /&gt;          min=j;&lt;br /&gt;        }&lt;br /&gt;      }&lt;br /&gt;      if(min==i) continue;&lt;br /&gt;      int temp = arr[min];&lt;br /&gt;      arr[min]=arr[i];&lt;br /&gt;      arr[i]=temp;&lt;br /&gt;    }&lt;br /&gt;    for(int c=0; c&amp;lt;arr.length; c++){&lt;br /&gt;      System.out.println(arr[c]);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Insertion Sort&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Insertion sort, which very similar to Selection Sort, has several differing points, and works like this:&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Go through the array of size n (not from 0 to n-1, but from 1 to n-1), using the counter i. Make sure to store the value of that element.&lt;br /&gt;&lt;li&gt;In each iteration, assume that the element at i is the greatest in the array. Use a while loop to push it around: the while runs as long at the element at i is less than the element right before it.&lt;br /&gt;&lt;/oL&gt;But now, instead of the code, something &lt;b&gt;completely different&lt;/b&gt;! The following code is bad. it has several syntax &lt;i&gt;and&lt;/I&gt; logic errors. Your task is to find them!&lt;br /&gt;&lt;pre&gt;import java.util.*;&lt;br /&gt;&lt;br /&gt;public class Insertion{&lt;br /&gt;  public static void main(String args[]){&lt;br /&gt;&lt;br /&gt;    //generate the array&lt;br /&gt;    Random rand = new Random()&lt;br /&gt;    int[] arr = new int[rand.nextInt];&lt;br /&gt;    for(int c=0; c&amp;lt;arr.length; c++){&lt;br /&gt;      arr[c] = rand.nextInt(100);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    //sort the array&lt;br /&gt;    int i, j, index;&lt;br /&gt;    for(i=1; i&amp;lt;arr.length; i++);&lt;br /&gt;      index=arr[i];&lt;br /&gt;      j=i;&lt;br /&gt;      while( (j&amp;gt;0) &amp;&amp; (arr[j-1]&gt;index) ){&lt;br /&gt;        arr[i]=arr[j-1];&lt;br /&gt;        j=j-1;&lt;br /&gt;      }&lt;br /&gt;      arr[j]=index;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    //print it out&lt;br /&gt;    for(int c=0; c&amp;lt;=arr.length; c++){&lt;br /&gt;      System.out.println(arr[c]);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-8689238155389933815?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/8689238155389933815/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=8689238155389933815' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/8689238155389933815'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/8689238155389933815'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/07/more-algorithms-selection-insertion.html' title='More Algorithms: Selection &amp; Insertion Sort'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-49010405423822990</id><published>2007-07-11T10:00:00.000-04:00</published><updated>2007-07-12T09:20:18.102-04:00</updated><title type='text'>Algorithms: Bubble Sort &amp; Binary Search</title><content type='html'>Today, we started with different kinds of sorting algorithms. We worked with ints, but the algorithm can be applied to any comparable data type.&lt;br /&gt;&lt;br /&gt;First, there is &lt;b&gt;bubble sort&lt;/b&gt;.&lt;br /&gt;This traverses an array of size n, each time flipping two numbers that are not in order, ie, when the number that goes first is greater than the number that follows it. At best, when the array is already sorted, the program traverses the array in such manner only once, and then exists, since no flips have been made and the array is apparently ordered. At worst, when every single pair of items in the array is not in order, the program traverses the array n-1 times, bringing every biggest number to the back and then returning to the rest. (-1 because the last number is in the right spot already - think pancakes!)&lt;br /&gt;This algorithm, then, takes, in the worst-case, n&lt;sup&gt;2&lt;/sup&gt; iterations, which is pretty slow. There are more efficient ways to sort, which we will get to by the end of the week.&lt;br /&gt;Here is my code for this particular sort:&lt;pre&gt;import java.util.*;&lt;br /&gt;public class Bubble{&lt;br /&gt;  public static void main(String args[]){&lt;br /&gt;    Random rand = new Random();&lt;br /&gt;    int[] arr = new int[rand.nextInt(90)+10];&lt;br /&gt;    for(int c=0; c&amp;lt;arr.length; c++){&lt;br /&gt;      arr[c] = rand.nextInt(100);&lt;br /&gt;    }&lt;br /&gt;    int n = 1;&lt;br /&gt;    while(n!=0){&lt;br /&gt;      n=0;&lt;br /&gt;      for(int c=0; c&amp;lt;arr.length-1; c++){&lt;br /&gt;        if(arr[c]&amp;gt;arr[c+1]){&lt;br /&gt;          n=1;&lt;br /&gt;          int temp = arr[c];&lt;br /&gt;          arr[c]=arr[c+1];&lt;br /&gt;          arr[c+1]=temp;&lt;br /&gt;        }&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;    for(int c=0; c&amp;lt;arr.length; c++){&lt;br /&gt;      System.out.println(arr[c]);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Next, we did &lt;b&gt;Binary Search&lt;/b&gt;. This is a fairly efficient searching algorithm that has only one requirement - that the elements in the array where it searches be ordered. So, see, there are uses for sorting!&lt;br /&gt;Binary search works by changing limits. Suppose you have an array (that you cannot see at once):&lt;br /&gt;&lt;code&gt;[_ _ _ _ _ _ _ _ _ _] (n=10)&lt;/code&gt;&lt;br /&gt;Now, we wnt to find which element the number 8 is stored in. We know it's in there. But where?! So, binary search: look at the middle element (middle=(upper bound+lower bound)/2; upper bound is, at first, n-1, and lower bound is 0), and compare.&lt;br /&gt;&lt;code&gt;[_ _ _ _ 5 _ _ _ _ _]&lt;/code&gt;&lt;br /&gt;Since 5 is &lt;i&gt;less&lt;/i&gt; than 8, we move the &lt;i&gt;lower bound&lt;/i&gt; of the search:&lt;br /&gt;&lt;code&gt;&amp;nbsp;_ _ _ _ 5[_ _ _ _ _]&lt;/code&gt;&lt;br /&gt;Now, the lower bound &lt;i&gt;is&lt;/i&gt; the middle! In the next iteration, look at the new middle - it was calculaed the same, but with different value for the lower bound:&lt;br /&gt;&lt;code&gt;&amp;nbsp;_ _ _ _ 5[_ _ 9 _ _]&lt;/code&gt;&lt;br /&gt;9 is &lt;i&gt;greater&lt;/i&gt; than 8, so move the upper bound and recalculate the middle value again:&lt;br /&gt;&lt;code&gt;&amp;nbsp;_ _ _ _ 5[_ _]9 _ _&lt;/code&gt;&lt;br /&gt;Keep doing this until the element at the middle is the value you are looking for!&lt;br /&gt;&lt;code&gt;&amp;nbsp;_ _ _ _ 5[_ 8]9 _ _&lt;/code&gt;&lt;br /&gt;There it is, Yaaaaay!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-49010405423822990?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/49010405423822990/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=49010405423822990' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/49010405423822990'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/49010405423822990'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/07/algorithms-bubble-sort-binary-search.html' title='Algorithms: Bubble Sort &amp; Binary Search'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-3862141186967801978</id><published>2007-07-10T15:53:00.000-04:00</published><updated>2007-07-10T20:13:07.700-04:00</updated><title type='text'>Tuesday: Problem Day!!</title><content type='html'>The main problem was HMRO - "Help the Military Recruitment Office"&lt;br /&gt;You created Person.java and HMRO.java. &lt;br /&gt;&lt;br /&gt;If you were done...&lt;br /&gt;&lt;br /&gt;1. Read in a string containing only letters or digits and print out "YES" if it is a palindrome (same backwards as forwards) and "NO" otherwise.&lt;br /&gt;&lt;br /&gt;2. Read in a double d and an integer k and print out the kth decimal digit of d. Sample input: 3.14159 2 Sample output: 4&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-3862141186967801978?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/3862141186967801978/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=3862141186967801978' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/3862141186967801978'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/3862141186967801978'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/07/tuesday-problem-day.html' title='Tuesday: Problem Day!!'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-907005036557170638</id><published>2007-07-09T10:00:00.000-04:00</published><updated>2007-07-10T10:30:40.145-04:00</updated><title type='text'>Fun with Strings</title><content type='html'>&lt;pre&gt;import java.util.Scanner;&lt;br /&gt;&lt;br /&gt;public class Str{&lt;br /&gt;&lt;br /&gt;    public static void main(String args[]){&lt;br /&gt;        &lt;br /&gt;        Scanner scan = new Scanner(System.in);&lt;br /&gt;        System.out.print("Type a word: ");&lt;br /&gt;        String test = scan.next();&lt;br /&gt;        &lt;br /&gt;        //print out some stats about this word&lt;br /&gt;        &lt;br /&gt;        System.out.println( "- it is "+test.length()+" letters long" );&lt;br /&gt;        System.out.println( "- the first two letters are "+test.substring(0,2));&lt;br /&gt;        System.out.println( "- the last letter is "+test.charAt(test.length()-1));&lt;br /&gt;        &lt;br /&gt;        int index = test.indexOf('a');&lt;br /&gt;        if (index==-1){&lt;br /&gt;            System.out.println("- the letter a does not occur in this word");&lt;br /&gt;        }&lt;br /&gt;        else{&lt;br /&gt;            System.out.println("- the letter a occurs at "+index);&lt;br /&gt;        }   &lt;br /&gt;    } &lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;A String is, essentially, an array of characters. It is defined in the Java class, String. You can use the API to check out the different methods in this class &lt;a href=http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html target=_blank&gt;right here&lt;/A&gt;. To use these methods, you must create a String object:&lt;br /&gt;&lt;code&gt;String s = "";&lt;/code&gt;&lt;br /&gt;And then call the methods on it. For example, s.length() would return an int for the length of the string. The list of methods, and their return-types, is in the API.&lt;br /&gt;&lt;br /&gt;This is the practice problem we did in class: Read in the user's name, then pass that string into a messUp(String original) method. YYour task was to write this method. The method itself creates a blank string, then iterates a loop as many times as there are letters in the original string, each time adding onto the new string a letter at a random position in the original string.&lt;br /&gt;&lt;pre&gt;import java.util.Scanner;&lt;br /&gt;import java.util.Random;&lt;br /&gt;public class Names{&lt;br /&gt;    public static void main( String args[] ){&lt;br /&gt;        Scanner scan = new Scanner(System.in);&lt;br /&gt;        System.out.print("What is your name? ");&lt;br /&gt;        String usrName = scan.next();&lt;br /&gt;        System.out.println("Hi, "+usrName.toLowerCase());&lt;br /&gt;        System.out.println("Mine's "+messUp(usrName.toLowerCase()));&lt;br /&gt;    }&lt;br /&gt;    &lt;i&gt;public static String messUp(String original){&lt;br /&gt;        String newStr="";&lt;br /&gt;        Random rand = new Random();&lt;br /&gt;        for(int c=0; c&amp;gt;original.length(); c++){&lt;br /&gt;            newStr+=original.charAt(rand.nextInt(original.length()));&lt;br /&gt;        }&lt;br /&gt;        return newStr;&lt;br /&gt;    }&lt;/i&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-907005036557170638?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/907005036557170638/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=907005036557170638' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/907005036557170638'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/907005036557170638'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/07/fun-with-strings.html' title='Fun with Strings'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-507532458995938019</id><published>2007-07-03T10:00:00.000-04:00</published><updated>2007-07-04T23:34:39.495-04:00</updated><title type='text'>Trees on Fire</title><content type='html'>First, here is the Tree class used throughout this excersize:&lt;br /&gt;&lt;pre&gt;public class Tree{&lt;br /&gt;&lt;br /&gt;  boolean burning;&lt;br /&gt;&lt;br /&gt;  public Tree(){&lt;br /&gt;    burning = false;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;  public void setOnFire(){&lt;br /&gt;    burning = true;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;  public boolean isOnFire(){&lt;br /&gt;    return burning;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;  }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;And the infamous TreesOnFire.java...&lt;br /&gt;&lt;pre&gt;public class TreesOnFire{&lt;br /&gt;&lt;br /&gt;  public static void main( String args[] ){&lt;br /&gt;    // create the 2d array of tree's - a forest!&lt;br /&gt;    Tree[][] forest = new Tree[25][25];&lt;br /&gt;    for( int c=0; c&amp;lt;forest.length; c++){&lt;br /&gt;      for( int d=0; d&amp;lt;forest[c].length; d++){&lt;br /&gt;         forest[c][d] = new Tree();&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // set a tree on fire.&lt;br /&gt;    forest[10][13].setOnFire();&lt;br /&gt;&lt;br /&gt;    // now run two for-loops and set neighboring trees on fire!!&lt;br /&gt;    for( int c=0; c&amp;lt;forest.length; c++){&lt;br /&gt;      for( int d=0; d&amp;lt;forest.length; d++){&lt;br /&gt;        for( int i = -1; i&amp;lt;=1; i++){&lt;br /&gt;          for( int j = -1; j&amp;lt;=1; j++){&lt;br /&gt;            //check if coordinate is VALID.&lt;br /&gt;            if( (c+i)7lt;0 || (c+i)&amp;gt;=forest.length ) continue;&lt;br /&gt;            if( (d+j)&amp;lt;0 || (d+j)&amp;gt;=forest[c].length ) continue;&lt;br /&gt;            if (forest[c+i][d+j].isOnFire()==true){&lt;br /&gt;              forest[c][d].setOnFire();&lt;br /&gt;            }&lt;br /&gt;          }&lt;br /&gt;        }&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;This should compile and run properly, but it will not print anything out just yet!&lt;br /&gt;There are some key concepts that are very important in all of this. First, there is this segment:&lt;prE&gt;        for( int i = -1; i&amp;lt;=1; i++){&lt;br /&gt;          for( int j = -1; j&amp;lt;=1; j++){&lt;br /&gt;            //check if coordinate is VALID.&lt;br /&gt;...&lt;/pre&gt;&lt;br /&gt;Image all the combinations of i and j that the for loops go through:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;i=-1 j=-1&lt;br /&gt;i=-1 j=0&lt;br /&gt;i=-1 j=1&lt;br /&gt;&lt;br /&gt;i=0 j=-1&lt;br /&gt;i=0 j=0&lt;br /&gt;i=0 j=1&lt;br /&gt;&lt;br /&gt;i=1 j=-1&lt;br /&gt;i=1 j=0&lt;br /&gt;i=1 j=1&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;"i" describes the &lt;b&gt;offset&lt;/b&gt; within &lt;b&gt;rows&lt;/b&gt;, and "j" the offset within &lt;b&gt;columns&lt;/b&gt; up/down or right/left of the coordinate [c][d] within our array. By adding the i and j values to the c and d coordinates, we actually traverse not only the element at [c][d], but also all of its neighbors!&lt;pre&gt;&lt;br /&gt;  |  j=-1 |  j=0  |  j=1  |       inside array&lt;br /&gt;--+-------+-------+-------+--       &lt;i&gt;forest&lt;/i&gt;...  &lt;br /&gt;i |       |       |       | &lt;br /&gt;= | -1,-1 |  -1,0 |  -1,1 |                 c-1,d-1   c-1,d    c-1,d+1&lt;br /&gt;-1|       |       |       |&lt;br /&gt;--+-------+-------+-------+--&lt;br /&gt;i |       |       |       |&lt;br /&gt;= |  0,-1 |  &lt;b&gt;0,0&lt;/b&gt;  |  0,1  |      ==&gt;         c,d-1     &lt;b&gt;c,d&lt;/b&gt;      c,d+1&lt;br /&gt;0 |       |       |       |&lt;br /&gt;--+-------+-------+-------+--&lt;br /&gt;i |       |       |       |&lt;br /&gt;= |  1,-1 |  1,0  |  1,1  |                  c+1,d-1  c-1,d     c-1,d+1&lt;br /&gt;1 |       |       |       |&lt;br /&gt;--+-------+-------+-------+--&lt;br /&gt;  |       |       |       |&lt;/pre&gt;&lt;br /&gt;We need this loop to check out whether the neighboring trees and see if they're on fire.&lt;br /&gt;now, suppose c=0 and d=0. Obviously, c-1,d-1, c-1,d, c-1,d+1, c,d-1, and c+1,d-1 do not exist in the array and would cause the progream to have an array out of bounds runtime exception. This is where this little thing comes in:&lt;prE&gt;&lt;br /&gt;            if( (c+i)7lt;0 || (c+i)&amp;gt;=forest.length ) continue;&lt;br /&gt;            if( (d+j)&amp;lt;0 || (d+j)&amp;gt;=forest[c].length ) continue;&lt;/pre&gt;&lt;br /&gt;This is the foolproof that our checker will never go out of bounds of the array. It says that if adding either i to c or j to d causes a number smaller than 0 or bigger than the largest number the array can handle, the loop must "continue;"&lt;br /&gt;All continue, as opposed to break, means is that the loopimmediately goes on to the next step without bothering to do any of the stuff in it for the current step. So, if one block has bad ccordinates, the loop will simply ignore it, and go on to the next block! Very cute.&lt;br /&gt;&lt;br /&gt;While generating good code is pretty hard, it is important that you recognize what code does! If you do not understand &lt;i&gt;any&lt;/i&gt; part of Tree.java or TreesOnFire.java, please ask either of your lovely TAs about it.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-507532458995938019?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/507532458995938019/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=507532458995938019' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/507532458995938019'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/507532458995938019'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/07/trees-on-fire.html' title='Trees on Fire'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-7943096700830649996</id><published>2007-07-02T10:00:00.000-04:00</published><updated>2007-07-01T22:47:39.136-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='oop'/><title type='text'>Object-Oriented Programming</title><content type='html'>(Note: This is, conceptually, one of the most important (if not &lt;i&gt;the&lt;/i&gt; most important..) distinctions that make Java Java. It's undoubtedly a pretty difficult distinction, so it will take longer than a day to really grasp and appreciate. What follows is, basically, a monster crash-course in OOP with lots of new words and concepts. Don't let them intimidate you ^_^)&lt;br /&gt;&lt;br /&gt;Suppose you have a letter. You don't know which letter it is, it's simply a hypothetical letter. It can be defined as one of 26 symbols. It can be either capitalized or non-capitalized. In Java, you would treat this letter as an object with two parameters - a boolean (something that can either be true or false) value for capitalization and an int (a whole number) value for the place it has in the alphabet. There can be many different letters, but they will all have that basic structure, and will only vary in parameter values. An object in Java is defined in a &lt;b&gt;class&lt;/b&gt;. This is what a Letter (it is cutomary to start class names with a capital letter) class would look like:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class Letter{&lt;br /&gt;  boolean capitalized;&lt;br /&gt;  int placement;&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;That describes a generic letter. That is what a class is - a guideline for something that isn't yet defined. But an &lt;b&gt;object&lt;/b&gt; is specific and defined. &lt;i&gt;Think of a class as a blank form that you can make many copies of, and an object as a single filled-out form that is different from all the rest.&lt;/I&gt; To create an object using a class, you need to create an &lt;b&gt;instance&lt;/B&gt; of that class. In order to do that, the class must have a &lt;b&gt;constructor method&lt;/b&gt;. This is a grouping of code that does the job of actually filling out the metaphorical form that is a class. Here is what the Letter constructor would look like in the context of the Letter class:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class Letter{&lt;br /&gt;  boolean capitalized;&lt;br /&gt;  int placement;&lt;br /&gt;  public Letter(boolean caps, int place){&lt;br /&gt;    capitalized = caps;&lt;br /&gt;    placement = place;&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;i&gt;(Note that the name of the constructor must be identical to the name of the class.)&lt;/i&gt; The constructor method would take two values - a boolean and an int - and assign them to this letter so as to define it. boolean caps and int place are both parameters that are passed down to the class. The class doesn't need to worry about where they come from; it should be content with assuming that they will be supplied and that they will be valid. However, what if you want to have a, say, default constructor? What if whoever was filling out the form left a few fields blank?&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Letter{&lt;br /&gt;  boolean capitalized;&lt;br /&gt;  int placement;&lt;br /&gt;  public Letter(boolean caps, int place){&lt;br /&gt;    capitalized = caps;&lt;br /&gt;    placement = place;&lt;br /&gt;    }&lt;br /&gt;  public Letter(){&lt;br /&gt;    capitalized = false;&lt;br /&gt;    placement = 0;&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;What will happen is that you can call either Letter() or Letter(some bool,some number) and the class will know which method to go into.&lt;br /&gt;&lt;br /&gt;Now the class has two constructors! When this happens (when there is more than one method in a class with the same name), it is called &lt;b&gt;overloading&lt;/b&gt;. It's completely valid, as long as the methods take in different kinds of parameters. For example, in this Letter class, there are two constructors - Letter(boolean,int) and Letter(). We could also add a Letter(int,int) or Letter(boolean), etc.., but another Letter() would not be allowed and the program would not compile.&lt;br /&gt;&lt;br /&gt;Having now established a Letter, we can move on to a Word. This is where the object-oriented bit should really hit you. Just think about it: a word is just a collection of letters. So, we can define Word using Letter. Like so:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Word{&lt;br /&gt;  Letter[] letters;&lt;br /&gt;  public Word(int length){&lt;br /&gt;    letters = new Letter[length];&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;(Whenever you see these around - [] - you should think immediately - arrays! Arrays can be any type. So far, you've only seen int[], but you can have pretty much anything in an array.)&lt;br /&gt;&lt;br /&gt;What we have is a Word that is defined as a collection of Letter objects of known size. This way, you can form Sentence or Text objects. This heirarchial way of coding is very, very important. In non-object-oriented programming languages, this effect would be achieved by have many methods within the same class. However, an object-oriented approach simplifies things and makes them readily accessible.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;non-object-oriented: Must open a book in order to see a letter&lt;br /&gt;class Book&lt;br /&gt;  method Page&lt;br /&gt;    // call to method Paragraph&lt;br /&gt;  method Paragraph&lt;br /&gt;    // call to method Sentence&lt;br /&gt;  method Sentence&lt;br /&gt;    // call to method Word&lt;br /&gt;  method Word&lt;br /&gt;    // call to method Letter&lt;br /&gt;  method Letter&lt;br /&gt;    // code&lt;br /&gt;&lt;br /&gt;object-oriented: Can access any of the classes&lt;br /&gt;class Book, calls Page&lt;br /&gt;class Page, calls Paragraph&lt;br /&gt;class Paragraph, calls Sentence&lt;br /&gt;class Sentence, calls Word&lt;br /&gt;class Word, calls Letter&lt;br /&gt;class Letter&lt;br /&gt;&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-7943096700830649996?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/7943096700830649996/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=7943096700830649996' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/7943096700830649996'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/7943096700830649996'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/07/object-oriented-programming.html' title='Object-Oriented Programming'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-7018554946467953001</id><published>2007-07-02T09:30:00.000-04:00</published><updated>2007-07-02T09:22:44.400-04:00</updated><title type='text'>Week 1 Solutions</title><content type='html'>These are the solutions to the first three problem sets.&lt;br /&gt;&lt;br /&gt;&lt;a href=http://andrewsleap.blogspot.com/2007/06/problem-set-1.html&gt;Problem Set 1&lt;/a&gt;&lt;pre&gt;public class Triangle1{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public static void main(String args[]){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double x1 = 6;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double x2 = 3;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double y2 = 4;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double perimeter = x1 + pythag(x2,y2) + pythag(x1-x2,y2);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println(perimeter);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public static double pythag(double a, double b){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return Math.sqrt(Math.pow(a,2)+Math.pow(b,2));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;public class Triangle2{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public static void main(String args[]){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double x1 = 6;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double x2 = 3;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double y2 = 4;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double perimeter = x1 + pythag(x2,y2) + pythag(x1-x2,y2);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int n = 4;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double perimN = perimeter/Math.pow(2,n-1);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println(perimeter);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public static double pythag(double a, double b){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return Math.sqrt(Math.pow(a,2)+Math.pow(b,2));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href=http://andrewsleap.blogspot.com/2007/06/problem-set-2.html&gt;Problem Set 2&lt;/a&gt;&lt;pre&gt;import java.util.*;&lt;br /&gt;public class Phone{&lt;br /&gt;  public static void main(String args[]){&lt;br /&gt;    Scanner scan = new Scanner(System.in);&lt;br /&gt;    String input = scan.next();&lt;br /&gt;    char letter = input.charAt(0);&lt;br /&gt;    switch( 'e' ){&lt;br /&gt;      case 'a': case 'b': case 'c':&lt;br /&gt;        System.out.println("2"); break;&lt;br /&gt;      case 'd': case 'e': case 'f':&lt;br /&gt;        System.out.println("3"); break;&lt;br /&gt;      case 'g': case 'h': case 'i':&lt;br /&gt;        System.out.println("4"); break;&lt;br /&gt;      case 'j': case 'k': case 'l':&lt;br /&gt;        System.out.println("5"); break;&lt;br /&gt;      case 'm': case 'n': case 'o':&lt;br /&gt;        System.out.println("6"); break;&lt;br /&gt;      case 'p': case 'q': case 'r': case 's':&lt;br /&gt;        System.out.println("7"); break;&lt;br /&gt;      case 't': case 'u': case 'v':&lt;br /&gt;        System.out.println("8"); break;&lt;br /&gt;      case 'w': case 'x': case 'y': case 'z':&lt;br /&gt;        System.out.println("9"); break;&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;import java.util.*;&lt;br /&gt;public class Roads{&lt;br /&gt;  public static void main(String args[]){&lt;br /&gt;    Random rand = new Random();&lt;br /&gt;    int die1 = rand.nextInt(4);&lt;br /&gt;    if (die1==0){&lt;br /&gt;      System.out.print("Left: you walk forever ");&lt;br /&gt;      System.out.println("in a haunted forest");&lt;br /&gt;      }&lt;br /&gt;    else if(die1==1){&lt;br /&gt;      System.out.print("Straight, then ");&lt;br /&gt;      int die2 = rand.nextInt(2);&lt;br /&gt;      if (die2==0){&lt;br /&gt;        System.out.print("left: you will be ");&lt;br /&gt;        System.out.println("forever thirsty :(");&lt;br /&gt;        }&lt;br /&gt;      else{&lt;br /&gt;        System.out.print("right: you will be ");&lt;br /&gt;        System.out.println("forever hungry :x");&lt;br /&gt;        }&lt;br /&gt;      }&lt;br /&gt;    else if (die1==2){&lt;br /&gt;      System.out.print("Right: you will never turn ");&lt;br /&gt;      System.out.println("left again. Ever.");&lt;br /&gt;      }&lt;br /&gt;    else{&lt;br /&gt;      System.out.print("You turned back to seek ");&lt;br /&gt;      System.out.println("your fortune elsewhere.");&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;import java.util.*;&lt;br /&gt;public class Challenge{&lt;br /&gt;  public static void main( String args[] ){&lt;br /&gt;    Random rand = new Random();&lt;br /&gt;    int die1 = rand.nextInt(6) + 1;&lt;br /&gt;    int die2 = rand.nextInt(6) + 1;&lt;br /&gt;    // ifelse block 1&lt;br /&gt;    if(die1+die2==7){&lt;br /&gt;      System.out.print("+50 ");&lt;br /&gt;      }&lt;br /&gt;    else if(die1==die2){&lt;br /&gt;      System.out.print("+35 ");&lt;br /&gt;      }&lt;br /&gt;    // the following ifs are not part of a block!&lt;br /&gt;    if(die1+3==die2 || die2+3==die1){&lt;br /&gt;      System.out.print("+27 ");&lt;br /&gt;      }&lt;br /&gt;    if((die1==1 || die2==1) &amp;&amp; die1!=die2){&lt;br /&gt;      System.out.print("-15 ");&lt;br /&gt;      }&lt;br /&gt;    if(die1==5){&lt;br /&gt;      System.out.print("+5 ");&lt;br /&gt;      }&lt;br /&gt;    if(die2==5){&lt;br /&gt;      System.out.print("+5 ");&lt;br /&gt;      }&lt;br /&gt;    if(die1==3){&lt;br /&gt;      System.out.print("+3 ");&lt;br /&gt;      }&lt;br /&gt;    if(die2==3){&lt;br /&gt;      System.out.print("+3 ");&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href=http://andrewsleap.blogspot.com/2007/06/problem-set-3.html&gt;Problem Set 3&lt;/a&gt;&lt;pre&gt;import java.util.*;&lt;br /&gt;public class FrequencyMap&lt;br /&gt;{&lt;br /&gt; public static void main(String args[]){&lt;br /&gt;     Random rand = new Random();&lt;br /&gt;     int[][] map = new int[20][20];&lt;br /&gt;     for(int c=0; c&lt;400; c++){&lt;br /&gt;        map[rand.nextInt(20)][rand.nextInt(20)]++;&lt;br /&gt;        }&lt;br /&gt;     for(int c=0; c&lt;20; c++){&lt;br /&gt;         for(int d=0; d&lt;20; d++){&lt;br /&gt;             System.out.print(map[c][d]+" ");&lt;br /&gt;            }&lt;br /&gt;            System.out.println("");&lt;br /&gt;        }&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;import java.util.*;&lt;br /&gt;public class Guesser&lt;br /&gt;{&lt;br /&gt;  public static void main(String args[]){&lt;br /&gt;    Random rand = new Random();&lt;br /&gt;    Scanner scan = new Scanner(System.in);&lt;br /&gt;    int yours = 1;&lt;br /&gt;    int userWins = 0;&lt;br /&gt;    int total = 0;&lt;br /&gt;    while(yours!=0){&lt;br /&gt;      System.out.print("Number between 1 and 100: ");&lt;br /&gt;      yours = scan.nextInt();&lt;br /&gt;      int mine = (rand.nextInt(100)+1);&lt;br /&gt;      if(yours&gt;mine){&lt;br /&gt;        System.out.println("You win!");&lt;br /&gt;        usrWins++;&lt;br /&gt;        }&lt;br /&gt;      else if (yours==mine){&lt;br /&gt;        System.out.println("It's a tie o_O");&lt;br /&gt;        }&lt;br /&gt;      else{&lt;br /&gt;        System.out.println("I win!");&lt;br /&gt;        }&lt;br /&gt;      total++;&lt;br /&gt;      }&lt;br /&gt;    System.out.println("You've choses to quit the game :(");&lt;br /&gt;    System.out.println("Your score: "+usrWins+"/"+total);&lt;br /&gt;    }&lt;br /&gt;  }&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-7018554946467953001?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/7018554946467953001/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=7018554946467953001' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/7018554946467953001'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/7018554946467953001'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/07/week-1-solutions.html' title='Week 1 Solutions'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-2352387410413755543</id><published>2007-06-29T10:00:00.000-04:00</published><updated>2007-06-29T10:15:58.049-04:00</updated><title type='text'>Friday: Problem day!</title><content type='html'>Today, we will focus on coding some algorithms!&lt;br /&gt;&lt;br /&gt;We also learn a new operator, %:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;5 % 2 = 1&lt;br /&gt;1256788 % 10 = 8&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;This operator gets the remainder from the division of the two numbers :)&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Sieve of Eratosthenes&lt;/b&gt; &lt;a href=http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes&gt;(animation)&lt;/a&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Write a list of numbers from 2 to the largest number you want to test for primality. Call this List A. (This is the list of squares on the left-hand-side of the picture.)&lt;br /&gt;&lt;li&gt;Write the number 2, the first prime number, in another list for primes found. Call this List B. (This is the list on the right-hand-side of the picture.)&lt;br /&gt;&lt;li&gt;Strike off 2 and all multiples of 2 from List A.&lt;br /&gt;&lt;li&gt;The first remaining number in the list is a prime number. Write this number into List B.&lt;br /&gt;&lt;li&gt;Strike off this number and all multiples of this number from List A. The crossing-off of multiples can be started at the square of the number, as lower multiples have already been crossed out in previous steps.&lt;br /&gt;&lt;li&gt;Repeat steps 4 through 6 until no more numbers are left in List A.&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;b&gt;1:&lt;/b&gt; Read in a positive integer n1 followed by a positive integer n2, where n2 &gt; n1.  Print out the number of prime numbers in the range [n1,n2].  Note:  your program must run in under 1 second when n1=0 and n2 = 1 million.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Base-n&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;2:&lt;/b&gt; First, write a program that converts a binary sequence into decimal. For example:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;input: 11010&lt;br /&gt;16(1)+8(1)+4(0)+2(1)+1(0) = 26&lt;br /&gt;output: 26&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Use % to separate the digits. For example, if I'm working with integers and I want to isolate the hundredth (in bold) digit in 34&lt;b&gt;5&lt;/b&gt;79, I divide by 100, and then % by ten:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;int var = 34579;&lt;br /&gt;var = var / 100; // this makes var 345&lt;br /&gt;var = var % 10; // and this makes var 5&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;b&gt;3:&lt;/b&gt; When you finish with that, write a program that converts decimal into binary using repeated division by 2:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;input: 26&lt;br /&gt;26/2 =&gt; 0 (this is the remainder of the operation, and also the first digit of the resulting binary number)&lt;br /&gt;13/2 =&gt; 10 (write the new remainder in front of the result up to this point)&lt;br /&gt;6/2&amp;nbsp;&amp;nbsp;=&gt; 010&lt;br /&gt;3/2&amp;nbsp;&amp;nbsp;=&gt; 1010&lt;br /&gt;1/2&amp;nbsp;&amp;nbsp;=&gt; 11010&lt;br /&gt;output: 11010&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;b&gt;4:&lt;/b&gt; And when the binary-decimal and back converter is finished, write a program that converts from any base 2&lt;=n&lt;=10 to any base 2&lt;=m&lt;=10. It might be helpful to create methods int toDecimal(int var) and int toN(int var, int n) and convert all bases to decimal before converting them to something else.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;5:&lt;/b&gt; For a particular challenge, modify the last problem  to accomodate 2&lt;=n&lt;=36 and 2&lt;=m&lt;=36 by using an array of both numebrs and letters.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-2352387410413755543?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/2352387410413755543/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=2352387410413755543' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/2352387410413755543'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/2352387410413755543'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/06/friday-problem-day.html' title='Friday: Problem day!'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-4905963730657708895</id><published>2007-06-28T10:00:00.000-04:00</published><updated>2007-06-28T09:57:17.346-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='while'/><category scheme='http://www.blogger.com/atom/ns#' term='loops'/><category scheme='http://www.blogger.com/atom/ns#' term='for'/><category scheme='http://www.blogger.com/atom/ns#' term='arrays'/><title type='text'>Arrays &amp; Loops</title><content type='html'>&lt;b&gt;Arrays&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Arrays are basically collections of variables of the same kind that store stuff and allow easy access. For example, you could have a list of integers that would be represented in an array like so:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;int[] vals = new int[4];&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;By setting the type to something[], you basically create a table with one dimension - or a list. Likewise, if you declare a something[][], you create a table with two dimensions. You can create as many dimensions as you want.&lt;br /&gt;You when you've got your dimensions straight, think about size. How many values do you want each dimension to hold? If you've got a list, just do a new something[a], a being the number of elements. When you've got two dimensions, think something[a][b], when a is the number of elements in a list and b is the number of elements inside each one of those previous elements. You can treat these 2-d arrays like coordinate planes or matrices.&lt;br /&gt;&lt;br /&gt;Note that while there are x elements, there is no x-th element, but there is a 0-th element, which makes a total of x elements:&lt;br /&gt;&lt;code&gt;[element 0] [element 1] [element 2] ... [element x-1]&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The real advantage of arrays is revealed in repeating events, such as loops, where you can say, "OK, I want this range of items in this array to be incremented by x" and such in only a few lines, irrelevant of the length of the array! Loops are very important in using arrays, so here they are.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Loops&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;There are two kinds of loops - the while loop and the for loop. Here's example code using each of them to solve the same problem - to increment all integers in the 1-dimentional array arr by 2:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//while loop&lt;br /&gt;int counter = 0;&lt;br /&gt;while(counter&amp;gt;arr.length){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;arr[count]=arr[count]+2;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;counter++;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;// for loop&lt;br /&gt;for(int counter=0; counter&amp;gt;arr.length; counter++){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;arr[count]=arr[count]+2;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The while is pretty straightforward. It takes any bounds, in this case a "as long as this counter is smaller than the size of the array". It runs as long as those bounds are true. Note that counter is incremented by one each time the loop iterates. When you ++ something, its the same as saying, something=something+1. Same with --.&lt;br /&gt;&lt;br /&gt;The for loop need a little more explanation. The counter is a variable that exists ONLY in the bounds of the for loop, because it is created in the loop itself. This is not required, and you can declare it before, but it's generally less messy to declare counters within loops. The first argument does this. It created the counter, and sets it to 0. The second argument gives the condition. This can be simple, like above, or a condition monster (see Logic article for explanation of conditions). The third argument is all the things that are done each time the loop iterates. In this case, incrementing the counter. Also, it looks neater. It is generally more useful and easier to use for looks for array-related tasks. You can have one loop inside another, like so, too:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// double for loop; increment every element in a 2D array&lt;br /&gt;for(int row=0; row&amp;gt;arr.length; row++){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(int col=0; col&amp;gt;arr[0].length;col++){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;arr[row][col]=arr[row][col]+2;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-4905963730657708895?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/4905963730657708895/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=4905963730657708895' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/4905963730657708895'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/4905963730657708895'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/06/arrays-loops.html' title='Arrays &amp; Loops'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-8828236796577053154</id><published>2007-06-28T09:30:00.000-04:00</published><updated>2007-06-28T10:18:57.029-04:00</updated><title type='text'>Problem Set 3</title><content type='html'>&lt;b&gt;Frequency Map&lt;/b&gt; &lt;a href=http://andrewsleap.blogspot.com/search/label/arrays&gt;Arrays&lt;/a&gt;, &lt;a href=http://andrewsleap.blogspot.com/search/label/loops&gt;Loops&lt;/a&gt;, &lt;a href=http://andrewsleap.blogspot.com/search/label/random&gt;Random&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Create a 20x20 integer array with all initial values at 0. Now, drop 400 "bombs" on the array. This is how bombs behave: the location of their destination within the array is random. When a bomb hits a spot in the array, it increments that value by 1. After all the bombs have been dropped, print out the array (make it look neat). For example, a possible result would look like this:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;% java FrequencyMap&lt;br /&gt;2 2 3 1 1 0 0 0 0 0 0 0 0 2 0 1 0 2 1 1 &lt;br /&gt;0 1 1 0 0 0 0 0 1 0 0 1 0 1 3 0 0 1 0 0 &lt;br /&gt;1 0 1 1 1 2 0 2 1 0 0 1 1 1 2 1 2 1 0 0 &lt;br /&gt;0 2 0 2 2 0 2 1 1 2 2 0 1 0 1 3 1 3 0 0 &lt;br /&gt;1 0 1 4 1 0 0 0 1 0 0 2 0 0 4 1 0 0 1 1 &lt;br /&gt;1 2 1 0 1 1 1 0 0 1 1 1 1 0 2 0 0 1 0 0 &lt;br /&gt;6 2 1 1 0 2 2 1 0 2 0 0 0 2 2 1 1 2 0 3 &lt;br /&gt;0 0 0 0 2 2 0 1 1 1 2 0 0 0 2 2 1 1 1 1 &lt;br /&gt;0 1 0 1 0 1 1 0 4 1 3 0 2 0 0 0 1 0 1 3 &lt;br /&gt;0 1 1 1 2 2 1 0 4 2 3 1 1 3 0 1 3 0 1 1 &lt;br /&gt;1 1 1 2 3 3 1 2 1 0 1 1 0 0 2 0 0 2 0 1 &lt;br /&gt;0 2 1 1 2 0 1 1 0 0 2 0 1 0 0 1 1 0 1 1 &lt;br /&gt;2 2 0 3 0 0 2 1 0 2 0 1 0 1 1 1 3 0 1 2 &lt;br /&gt;2 2 0 2 1 2 0 1 2 2 0 1 1 1 1 3 0 0 1 0 &lt;br /&gt;1 2 3 0 1 1 0 3 0 2 2 1 1 0 0 1 0 0 1 3 &lt;br /&gt;1 0 1 1 0 2 0 0 1 2 2 1 2 0 2 1 1 4 1 1 &lt;br /&gt;1 1 1 0 1 0 1 1 3 0 4 2 0 2 1 1 1 0 2 4 &lt;br /&gt;1 0 1 0 1 3 0 0 0 0 1 1 0 0 1 0 0 1 0 1 &lt;br /&gt;2 1 0 1 1 2 1 2 3 1 0 1 1 1 2 0 1 0 1 0 &lt;br /&gt;2 0 4 2 1 1 1 2 1 0 3 2 3 2 0 0 0 1 2 0 &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Number Guesser&lt;/b&gt; &lt;a href=http://andrewsleap.blogspot.com/search/label/input&gt;User input&lt;/a&gt;, &lt;a href=http://andrewsleap.blogspot.com/search/label/random&gt;Random&lt;/a&gt;, &lt;a href=http://andrewsleap.blogspot.com/search/label/loops&gt;Loops&lt;/a&gt; &lt;br /&gt;&lt;br /&gt;Modify the existing number-guesser program (is which the user types in a number the computer guesses a number, and whichever is higher wins) so that it runs continuously until the user types 0. Also add a "global score" counter: have one variable increment each time the game is player, and another increment each time the user wins. In the end, after the user has typed 0, out put the total score as wins/games. Possible output:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Type a number: 45&lt;br /&gt;My guess: 46&lt;br /&gt;I win!&lt;br /&gt;Type a number: 3&lt;br /&gt;My guess: 1&lt;br /&gt;You win!&lt;br /&gt;Type a number: 67&lt;br /&gt;My guess: 67&lt;br /&gt;It's a tie o_O&lt;br /&gt;Type a number: 45&lt;br /&gt;My guess: 36&lt;br /&gt;You win!&lt;br /&gt;Type a number: 0&lt;br /&gt;You've chosen to quit the game :(&lt;br /&gt;Your score: 2 / 4&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Challenge: Cipher&lt;/b&gt; &lt;a href=http://andrewsleap.blogspot.com/search/label/input&gt;User Input&lt;/a&gt;, &lt;a href=http://andrewsleap.blogspot.com/search/label/if&gt;If-else&lt;/a&gt;, &lt;a href=http://andrewsleap.blogspot.com/search/label/arrays&gt;Arrays&lt;/a&gt;, &lt;a href=http://andrewsleap.blogspot.com/search/label/loops&gt;Loops&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Prompt for 10 single-digit int inputs. Then, use a wraparound cipher to "encode" it. This means that every digit is incremented by one, unless that digit is 9, it which case it becomes 0. You will use two loops, one for getting+processing info, and another for printing it out in the end. You only need one integer array.&lt;br /&gt;Sample program output:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;% java Cipher&lt;br /&gt;Please type in ONLY single digits!&lt;br /&gt;0: 1&lt;br /&gt;1: 2&lt;br /&gt;2: 3&lt;br /&gt;3: 4&lt;br /&gt;4: 5&lt;br /&gt;5: 3&lt;br /&gt;6: 7&lt;br /&gt;7: 8&lt;br /&gt;8: 0&lt;br /&gt;9: 9&lt;br /&gt;Encoded number:&lt;br /&gt;2345648910&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;After everyone will have at least attempted to solve the problems, I will post the answers.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-8828236796577053154?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/8828236796577053154/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=8828236796577053154' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/8828236796577053154'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/8828236796577053154'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/06/problem-set-3.html' title='Problem Set 3'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-7338744549708598191</id><published>2007-06-27T10:30:00.000-04:00</published><updated>2007-06-27T10:00:10.729-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='data'/><category scheme='http://www.blogger.com/atom/ns#' term='random'/><category scheme='http://www.blogger.com/atom/ns#' term='input'/><title type='text'>Data: User Input &amp; Random</title><content type='html'>&lt;b&gt;User Input&lt;/b&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import java.util.Scanner;&lt;br /&gt;public class HelloWorld{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public static void main(String args[]){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.print("What is your name? ");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Scanner scan = new Scanner(System.in);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String name = scan.nextLine();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println("Hello, "+name+"!");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;To get user input, we will be using Scanner, so you absolutely must import it, or the compiler won't recognize it and give lots of errors.&lt;br /&gt;Scanner is a little buggy, though.&lt;ul&gt;&lt;br /&gt;&lt;li&gt;The .nextLine() immediately following a .nextInt() will return an empty string. Therefore, after you read in an integer and before you read in a string, just do a .nextLine() without assigning the result to anything.&lt;br /&gt;&lt;li&gt;If .nextInt() (or any other non-string-getter) is called and the user types in a non-integer, you get lots of exceptions. Try-catching them doesn't work. So, if you wanna foolproof your program, use the following method in your program instead of .nextInt():&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public static int getInt(){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Scanner scan = new Scanner(System.in);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;while(true){ // run forever until the user types in a number&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try{ // try to parse the typed-in strign as an integer:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return Integer.parseInt(scan.nextLine());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch( Exception e ){ // if fail, aks user again:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println("Please type in a number.");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Don't worry if you don't understand it yet: you will tomorrow. Just, whenever you need to get a number, do not do scan.nextInt(), instead, use getInt().&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Random&lt;/b&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import java.util.Random; // you need this line for all rpograms involving the Random object!&lt;br /&gt;public class RandomTest{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public static void main(String args[]){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Random rand = new Random();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println("Random number: "+rand.nextInt());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println("Random number from 0 to 9: "+rand.nextInt(10));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println("Random double: "+rand.nextDouble());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println("Random double from 0 to 1: "+rand.nextDouble(1));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-7338744549708598191?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/7338744549708598191/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=7338744549708598191' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/7338744549708598191'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/7338744549708598191'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/06/data-user-input-random.html' title='Data: User Input &amp; Random'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-9052504797788585726</id><published>2007-06-27T10:00:00.001-04:00</published><updated>2007-06-27T10:12:08.293-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='case'/><category scheme='http://www.blogger.com/atom/ns#' term='switch'/><category scheme='http://www.blogger.com/atom/ns#' term='if'/><category scheme='http://www.blogger.com/atom/ns#' term='else'/><title type='text'>If &amp; Switch</title><content type='html'>&lt;b&gt;If-Else&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Most problems require handling some sort of conditions and doing different things in different cases. If-statements in Java do this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;if (condition){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;action A&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;else{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;default action&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;If there are more than two cases, you can use (as many as you need)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;else if(another condition){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;action B&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;between the original if and the ending else. Here's an example:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;if (there is rain){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;use an umbrella&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;else if (there is a trustworthy account of oncoming rain){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;bring an umbrella just in case&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;else{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;leave the umbrella alone&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The &lt;b&gt;action&lt;/B&gt; portion of the if statement can be any &amp;lt;working&amp;gt; piece of code.&lt;br /&gt;&lt;br /&gt;The &lt;b&gt;condition&lt;/b&gt; is a statement that is a &lt;b&gt;boolean&lt;/b&gt;, ie, it is either true or false. If it is true, the action is performed. If not, the next condition is tested, or, if there are no more conditions, the default action is performed. It typically uses the following operators:&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;==&lt;/b&gt; Ex.: A==B "is A equal to B?"&lt;br /&gt;&lt;li&gt;&lt;b&gt;!=&lt;/b&gt; Ex.: A!=B "is A NOT equal to B?"&lt;br /&gt;&lt;li&gt;&lt;b&gt;&amp;gt;&lt;/b&gt; Ex.: A&amp;gt;B "is A larger than B?"&lt;br /&gt;&lt;li&gt;&lt;b&gt;&amp;gt;=&lt;/b&gt; Ex.: A&amp;gt;=B "is A larger than or equal to B?"&lt;br /&gt;&lt;li&gt;&lt;b&gt;&amp;lt;&lt;/b&gt; Ex.: A&amp;lt;B "is A smaller than B?"&lt;br /&gt;&lt;li&gt;&lt;b&gt;&amp;lt;=&lt;/b&gt; Ex.: A&amp;lt;=B "is A smaller than or equal to B?" &lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;For example, a valid piece of code would be:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//assume var is some random-generated integer between 0 and 9, inclusively&lt;br /&gt;if(var&lt;5){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println("too small!");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;else{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println("too BIG!");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;But what if you wanted to put in more constraints? Here's an example:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//assume var1 and var2 are two independent random-generated integers&lt;br /&gt;//&amp;nbsp;&amp;nbsp;&amp;nbsp;between 0 and 9, inclusively&lt;br /&gt;if(var1&lt;5 &amp;&amp; var2&lt;5){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;//if both are less than 5:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println("too small!");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;else if(var1&lt;5 || var2&lt;5){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;// both can't be less than 5, because if they were, the first if&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;// would have been true; the order of the&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;// if's really, really matters&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;// basically if one is smaller than 5 and the other isn't.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println("just right ^.^");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;else{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println("too BIG!");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;the || is part of the operators that you can use to tie several constraints together into a single condition:&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;||&lt;/b&gt; or (inexclusive - if at least one is true, it returns true)&lt;br /&gt;&lt;li&gt;&lt;b&gt;&amp;&amp;&lt;/b&gt; and (returns true if both are ture) &lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;So, you can have condition monsters such as:&lt;br /&gt;&lt;codE&gt;&lt;br /&gt;((var1&lt;var2 &amp;&amp; var3!=7) || ((var3&gt;var2 &amp;&amp; var3&gt;var1) || var3==0))&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;In case you're wondering what that returns, here's the English version (note: it's just a random collection of conditions. it has no universal meaning. assume, for the purposes of this "demonstration," that var1 var2 and var3 are random integer variables.) This statement is true if:&lt;ul&gt;&lt;br /&gt;&lt;li&gt;var1 is smaller than var2 at the same time as var3 is anything other than 7, OR&lt;br /&gt;&lt;li&gt;var3 is equal to 0, OR&lt;br /&gt;&lt;li&gt;var3 is larger than both var1 and var2 &lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;b&gt;Switch&lt;/B&gt;&lt;br /&gt;&lt;br /&gt;Besides the if statement, you can have a switch. While in an if statement, you set certain bounds. Switches are different because they only analyze a single variable in the whole switch (while the if can set constraints or as many variables as you can type) and they don't set bounds, they set cases, such as:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;switch(var1){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;case 1: action A; break;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;case 2: action B;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;case 3: action C; break;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;default: default action; break;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;First off, the variable it analyzes is noted in the beginning of the switch. This variable can only be either int or char. Note the presence of "break;". This tells the switch to abort as soon as the action is complete. The case for 2, for example, is missing a break. This means that when var1=2, BOTH action B and action C is run because the switch doesn't abort until it sees the break; in case 3. Switches can be very neat in some cases, and just plain unusable in others.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-9052504797788585726?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/9052504797788585726/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=9052504797788585726' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/9052504797788585726'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/9052504797788585726'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/06/if-switch.html' title='If &amp; Switch'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-6497874987130824918</id><published>2007-06-27T09:30:00.000-04:00</published><updated>2007-06-27T10:11:21.452-04:00</updated><title type='text'>Problem Set 2</title><content type='html'>Topics: &lt;a href=http://andrewsleap.blogspot.com/search/label/input&gt;user input&lt;/A&gt;, &lt;a href=http://andrewsleap.blogspot.com/search/label/random&gt;random&lt;/a&gt;, &lt;a href=http://andrewsleap.blogspot.com/search/label/if&gt;if-else&lt;/a&gt;, &lt;a href=http://andrewsleap.blogspot.com/search/label/switch&gt;switch&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;1:&lt;/b&gt; Write a program, using a switch, that takes as input a lowercase letter char and prints out the integer value of that character as it would be on a phone. So, 'a', 'b', and 'c' would yield 2, etc.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;2:&lt;/b&gt; Write a program, using either if-else or switch, that uses random-generated integers as input. Basically, write this in Java:&lt;br /&gt;&lt;code&gt;you're standing before three roads.&lt;br /&gt;if you go left, you will walk forever in some sort of haunted forest.&lt;br /&gt;if you go straight ahead, you will encounter two more possible roads. of these, if you go left, you will always be thirsty, and, if you go right, you will always be hungry.&lt;br /&gt;if you go right, you will never be able to make a left turn again. ever.&lt;br /&gt;you can choose not to go by wither of the roads and turn back, seeking your fortune elsewhere.&lt;/code&gt;&lt;br /&gt;You can use either two random-generated int's, one 0 to 3 and the other 0 to 1, OR one random-generated int that ranges from 0 to 4. Make sure to include (even if not exactly verbatim) every portion of the narration in proper places.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Challenge!&lt;/b&gt; Write a program, using if-else construction, that calculates the score in a specific game involving two 6-sided dice. The dice numbers should be randomly generated, then printed out along with the resulting score.&lt;br /&gt;&lt;code&gt;The rules of the game: (1) if the two dice add up to 7, you get 50 points; (2) if the two dice are both he same number, you get 35 points; (3) if one of the dice is exactly 3 bigger than the other (eg, 4 and 1) you get 27 points; (4) if exactly one if the dice is 1, you lose 15 points; (5) you get 5 points for every 5 that appears and 3 points for every 3.&lt;/code&gt;&lt;br /&gt;This program is fairly challenging and will require several if-else blocks!&lt;br /&gt;Note: the highest possible score is 50+27+5 and is the result of rolling 2 and 5. The lowest is -15 and is the result of rolling a 1 and anything that isn't 1 or 4. Use this as test input to check your program for errors.&lt;br /&gt;Good luck :)!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-6497874987130824918?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/6497874987130824918/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=6497874987130824918' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/6497874987130824918'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/6497874987130824918'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/06/problem-set-2.html' title='Problem Set 2'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-1515642541470216151</id><published>2007-06-26T11:00:00.000-04:00</published><updated>2007-06-26T13:13:44.961-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='bash'/><category scheme='http://www.blogger.com/atom/ns#' term='shell'/><category scheme='http://www.blogger.com/atom/ns#' term='command prompt'/><title type='text'>Bash Shell</title><content type='html'>&lt;span style="font-size:180%;"&gt;Intro&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;You guys have been spoiled with pretty pictures and using a mouse to govern things. Well, for the first few weeks we will be working with the command line(as in we have to type everything).  We will be working with the bash shell.  A shell is a program that interprets what you type on the screen and tells the OS what you want to do.  Different shells have different looks and "feels."  The commands may also vary slightly in how you type them in and how they will display things.  To return to the bash shell, or if you are unsure of what shell you are in, just type &lt;span style="font-weight: bold;"&gt;bash&lt;/span&gt; then strike the enter/return key.&lt;br /&gt;&lt;span style="font-size:180%;"&gt;&lt;br /&gt;The Screen&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;So the first thing we have to go over is what that text on the left hand side means... you should see your guest login and some symbols.  Don't worry you haven't broken the computer...yet.   All this tells you is that you are in your home directory.  Your home directory is where all of your files are stored.  This is important because you will probably need to refer to or copy code from previous lessons. This is also where you TYPE your commands.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:180%;"&gt;Doin' Somtin' Useful&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;Making Directories (Folders)&lt;/span&gt;&lt;br /&gt;Now lets start organizing our home folder. we can make a directory to put all of the code for our first program.  Lets say &lt;span style="font-weight: bold;"&gt;first&lt;/span&gt; will be the name of the directory&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;.  The command for making a directory is &lt;span style="font-weight: bold;"&gt;mkdir&lt;/span&gt; or in more human words make directory.  Now, to make our directory we type &lt;span style="font-weight: bold;"&gt;mkdir first&lt;/span&gt;.  This can make any number of directories you want with whatever name you want.&lt;br /&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;Changing Directories (Folders)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;So, now that we made the directory, how do we get into it?  Well, traveling though directories in command line is nothing like looking at things in windows on your Windows or Mac computer.  It is like walking through a garden while looking though a straw.  You cannot see the whole thing at once, but instead you have to walk every path and get close to each flower, one at a time, in order to see them....  In command line, you can really only be working in one directory at a time. All the commands you enter are for that directory ONLY.  To change to a new directory we use the command  &lt;span style="font-weight: bold;"&gt;cd&lt;/span&gt;.  So, to get into our new directory we type &lt;span style="font-weight: bold;"&gt;cd first&lt;/span&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;What is in the directory?&lt;br /&gt;&lt;/span&gt;To find out what is in the current directory you are in you type the command &lt;span style="font-weight: bold;"&gt;ls&lt;/span&gt;.  That is it.  It will list all the files and directories in that directory.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:180%;"&gt;Making Files ( with pico )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;There are a host of text editors to use in the command line.  We will be using pico for the sheer fact that all the commands are listed on the bottom.  To make a new file go to the directory you want your file in and then type &lt;span style="font-weight: bold;"&gt;pico first.java&lt;/span&gt;.  This will make a file called, first.java in the directory.  DON'T FORGET THE  .java !!! Now, jump to &lt;a href="http://andrewsleap.blogspot.com/2007/06/hello-world-variables-functions.html"&gt;Katie's post&lt;/a&gt; about your first program.  Go to Compiling Java to learn how to make it do something.&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-1515642541470216151?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/1515642541470216151/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=1515642541470216151' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/1515642541470216151'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/1515642541470216151'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/06/bash-shell.html' title='Bash Shell'/><author><name>curdog</name><uri>http://www.blogger.com/profile/06977395643080333803</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-5396311878397926340</id><published>2007-06-26T10:30:00.000-04:00</published><updated>2007-06-26T13:14:45.486-04:00</updated><title type='text'>Compiling Java</title><content type='html'>&lt;span style="font-size:180%;"&gt;Intro&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;Java is an interpreted language, which means that is does not directly execute on your computer's processor or use system specific methods.  Though, you still have to compile the source code into java byte code. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:180%;"&gt;&lt;br /&gt;Making Stuff Work( hopefully )&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;Compiling&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;Now, to compile your program we will use the  &lt;span style="font-weight: bold;"&gt;javac&lt;/span&gt; command, short for java compiler.  So to complile your first program you would type &lt;span style="font-weight: bold;"&gt;javac first.java&lt;/span&gt;.  If all goes well, there should be no hoopla on your screen.  If there is a problem, the compiler will tell you the line number and maybe what it is.  Take all errors with a grain of salt, however, the compiler does not always get the right error.  Occasionally, your error will be several lines above the error is lists.  The only way you can lear to recognize this is by writing lots of programs...&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;Runing the Program&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;Now, that we have the program compiled, we have to invoke the java interpreter to read the .class file that the compiler made.  To do this, we simply type &lt;span style="font-weight: bold;"&gt;java first&lt;/span&gt;.  DO NOT ADD .class to the end of the class that is to be run.  Again, the java interpreter will tell you if anything goes wrong during runtime.&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-5396311878397926340?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/5396311878397926340/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=5396311878397926340' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/5396311878397926340'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/5396311878397926340'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/06/compiling-java.html' title='Compiling Java'/><author><name>curdog</name><uri>http://www.blogger.com/profile/06977395643080333803</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-8117290812861134700</id><published>2007-06-26T10:00:00.000-04:00</published><updated>2007-06-27T10:23:09.750-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='methods'/><category scheme='http://www.blogger.com/atom/ns#' term='variables'/><title type='text'>Hello, World! Variables &amp; Methods</title><content type='html'>Welcome to the steaming-hot goodness of Java!!!&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Sample Program&lt;/b&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class First{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public static void main ( String args[] ) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println("Hello, world!");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//some variables....&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int x = 42;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double y = 3.14;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;char c = 'q';&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String str = "Hello, world!";&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//...that you can print out&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println( str+":"+c+" ~ "+smapleMethod(x,y) );&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public static int sampleMethod( int a, int b) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//a &lt;b&gt;method&lt;/B&gt; contains a collection of&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//procedures that constitute a single task.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//for example, this method adds together two integers, a and b&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return a+b;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Java Conventions&lt;/b&gt;&lt;ul&gt;&lt;li&gt;Every &lt;b&gt;variable and method name&lt;/b&gt; starts with a lowercase letter. If it consists of more than one word, each word after the first is capitalised: variableOne, varOneOfMany, justVariable, etc&lt;br /&gt;&lt;li&gt;Every &lt;b&gt;constant&lt;/B&gt; is written in ALL CAPS.&lt;br /&gt;&lt;li&gt;&lt;b&gt;Class names&lt;/B&gt; follow the same rules and variable and method names, but start with a capital letter: ClassOne, MyFirstProgram, etc&lt;br /&gt;&lt;li&gt;Make sure to indent your code! See how neat the Sample program looks? &lt;b&gt;Indenting makes code readable.&lt;/b&gt;&lt;br /&gt;&lt;li&gt;As you code, you should &lt;b&gt;comment on what your code is and why it's that way&lt;/b&gt;. Otherwise, hours later, you'll have no idea why you did something and then spend the next n hours trying to figure it out. It's just not worth it to not comment. Use either &lt;b&gt;//&lt;/B&gt; or &lt;b&gt;/* stuff! */&lt;/b&gt; to comment. // comments the line out, and /* */ comments out a block of text regardless of line breaks.&lt;br /&gt;&lt;/uL&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href=http://andrewsleap.blogspot.com/2007/06/problem-set-1.html&gt;Problem Set 1 is now over here!&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-8117290812861134700?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/8117290812861134700/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=8117290812861134700' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/8117290812861134700'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/8117290812861134700'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/06/hello-world-variables-functions.html' title='Hello, World! Variables &amp; Methods'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1502916310418593789.post-7394363501462790503</id><published>2007-06-26T09:30:00.001-04:00</published><updated>2007-06-27T10:21:26.859-04:00</updated><title type='text'>Problem Set 1</title><content type='html'>Topics: &lt;a href=http://andrewsleap.blogspot.com/2007/06/hello-world-variables-functions.html&gt;the basics&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Declare three doubles, x1, x2, and y2.&lt;br /&gt;The doubles represent triangle T1 which has vertices at (0,0), (x1,0), and (x2,y2).&lt;br /&gt;&lt;b&gt;1:&lt;/b&gt; Calculate and print out the perimeter of T1.&lt;br /&gt;&lt;br /&gt;Using the set up from the previous problem, try this one:&lt;br /&gt;Declare an integer, n.&lt;br /&gt;Now, suppose triangle T2 is formed by connecting the midpoints of the sides of triangle T1, and in general, triangle Tk+1 is formed by connecting the midpoints of the sides of triangle Tk.&lt;br /&gt;&lt;b&gt;2:&lt;/b&gt; Calculate and print out the perimeter of triangle Tn.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1502916310418593789-7394363501462790503?l=andrewsleap.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewsleap.blogspot.com/feeds/7394363501462790503/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1502916310418593789&amp;postID=7394363501462790503' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/7394363501462790503'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1502916310418593789/posts/default/7394363501462790503'/><link rel='alternate' type='text/html' href='http://andrewsleap.blogspot.com/2007/06/problem-set-1.html' title='Problem Set 1'/><author><name>kattack</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
