Fall Semester 2009


Fri Dec 11
Lab notes from today's lab.

Mon Dec 7
Homework Four and Five posted.

Notes for Tue and Thu (helping with these two assignments) also posted.

Tonight the notes will be augmented with even more specific steps, to be posted Tue morning.

Thanks to Adam Mendelevitz for bringing up regular expressions tonight during office hours:

import java.util.regex.*;
import java.util.*; 

public class One {
  public static void main( String args[] ){
    Scanner s = new Scanner(System.in); 
    String line; 

    Pattern circleMatch = Pattern.compile( "Circle radius ([0123456789]+) location (-{0,1}\\d+) (-{0,1}\\d+)" );

    Pattern rectangleMatch = Pattern.compile( "Rectangle size (\\d+) (\\d+) location (-{0,1}\\d+) (-{0,1}\\d+)" );

    System.out.print("Enter>"); 
    while (s.hasNextLine()) {
      line = s.nextLine(); 
      if (line.equals("done")) break;
      System.out.println(line); 
      Matcher circle = circleMatch.matcher( line );
      Matcher rectangle = rectangleMatch.matcher( line );
      if (circle.find()) {
        System.out.println("I see a Circle, radius " + 
                           circle.group(1) + " at (" + circle.group(2) + ", " + circle.group(3) + ")"); 
      } else if (rectangle.find()) {
        System.out.println("I see a Rectangle."); 
      } else { 
        System.out.println("I don't see what you're saying."); 
      } 
      System.out.print("Enter>"); 
    }     
  }
}
Here's this program in action:
-bash-3.2$ java One
Enter>Circle radius 2 location 1 2
Circle radius 2 location 1 2
I see a Circle, radius 2 at (1, 2)
Enter>Rectangle size -1 3 location 2 10
Rectangle size -1 3 location 2 10
I don't see what you're saying.
Enter>Rectangle 12 4 location -1 -2
Rectangle 12 4 location -1 -2
I don't see what you're saying.
Enter>Circle radius 12 location -1 -1
Circle radius 12 location -1 -1
I see a Circle, radius 12 at (-1, -1)
Enter>done
-bash-3.2$ 
You will notice the patterns are not completely specified.

But that's easy to fix and the code above easily handles the text interface in Homework 3 problem 2.

Sat-Sun Dec 5-6
Thanks to Daniel Hively for bringing this up during office hours:
import java.util.*; 

abstract class Shape implements Comparable<Shape> {
  abstract double area(); 
  public int compareTo(Shape other) {
    if (this.area() > other.area()) return 1; 
    if (this.area() < other.area()) return -1;
    else return 0; 
  } 
}

class Circle extends Shape {
  double area() { return 3.4; } 
  public String toString() { return "Circle area: " + this.area(); }
}

class Rectangle extends Shape {
  double area() { return 2.7; } 
  public String toString() { return "Rectangle area: " + this.area(); } 
}

class One {
  public static void main(String[] args) {
    ArrayList<Shape> a = new ArrayList<Shape>(); 
    a.add(new Circle()); 
    a.add(new Rectangle()); 
    a.add(new Circle()); 
    a.add(new Rectangle()); 
    System.out.println(a); 
    Collections.sort(a); 
    System.out.println(a); 
  } 
}

Fri Dec 04
The code for the first problem yesterday:
import java.util.*; 

class Four {
  public static void main(String[] args) {
    int[] a = new int[args.length]; 
    for (int i = 0; i < args.length; i++) 
      a[i] = Integer.parseInt(args[i]); 
    System.out.println(Arrays.toString(a)); 
    Arrays.sort(a); 
    System.out.println(Arrays.toString(a)); 
  }  
}
The second problem:
import java.util.*;

class Five {
  public static void main(String[] args) {
    ArrayList a = new ArrayList<Student>(); 
    a.add(new Student("Angie"   , 3.1)); 
    a.add(new Student("Amy"   , 3.1)); 
    a.add(new Student("Leslie", 3.4)); 
    a.add(new Student("Matt"  , 2.3)); 
    a.add(new Student("John"  , 1.7)); 
    a.add(new Student("Jamie" , 2.7)); 
    System.out.println(a);     
    Collections.sort(a); 
    System.out.println(a);     
  } 
} 

class Student implements Comparable<Student> {
  String name; 
  double gpa; 
  Student(String name, double gpa) {
    this.gpa = gpa; 
    this.name = name; 
  } 
  public String toString() {
    return this.name + ": " + this.gpa;
  } 
  public int compareTo(Student other) {
    if (this.gpa  > other.gpa) return  1;
    if (this.gpa  < other.gpa) return -1;
    else return this.name.compareTo(other.name);
  }

}

Thu Dec 03
Use Chapter 2 in Book 4 in the text to solve the first problem in Homework Three.

The text (as you know) can be found here:

Java All-In-One Desk Reference For Dummies, 2nd Edition 	
by Doug Lowe and Barry Burd 
John Wiley & Sons © 2007 (914 pages) 
ISBN:9780470124512
Use this link in the API to solve the second problem.

Alternatively you can search (in the same online collection):

Chapter 5 - Working with Collections

Herb Schildt's Java Programming Cookbook

Tue Dec 01
Notes from today's class.

Tue Nov 24
Notes from today's class.

Wed Nov 18
For the group exercise the teams are:
Thiago Rachel*
Alea* Victor
Ron Danielle*
Anthony* Kurt
Adam* Michelle
Brendan Will*
Allen Munir*
Jared Daniel*
Person marked with * does the typing.

Problem is: implement a class of objects called Game that asks addition questions of you.

A Game object knows the player's name, grades the player's answer and keeps track of the score.

Here's how one can use such a class:

class Example {
  public static void main(String[] args) {
    Game a, b, c, d; 
    a = new Game("Adrian"); 
    a.report(); 
    a.ask(); 
    a.report();
    b = new Game("Alea"); 
    b.report(); 
    b.ask(); 
    b.report(); 
    a.ask(); 
    a.report();
    a.reset(); 
    a.report(); 
    
  } 
}
The code above produces the following output:
C:\Users\dgerman\Desktop>java Example
Game for Adrian created.
Adrian you have 0 correct answers out of 0 so far.
Adrian, what is 30 + 39? 69
That's right.
Adrian you have 1 correct answers out of 1 so far.
Game for Alea created.
Alea you have 0 correct answers out of 0 so far.
Alea, what is -10 + 49? 23
No, the right answer was: 39
Alea you have 0 correct answers out of 1 so far.
Adrian, what is 9 + 1? 10
That's right.
Adrian you have 2 correct answers out of 2 so far.
Game for Adrian has been reset now.
Adrian you have 0 correct answers out of 0 so far.

C:\Users\dgerman\Desktop>
Send code to dgerman@indiana.edu at 12:19pm and watch development on class projector until 12:30pm.

Solution goes through this stages:

class Game {
  String name;
  Game(String name) {
     this.name = name; 
     System.out.println("Game for " + this.name + " has been created."); 
  }
  void ask() {
    System.out.println("I am Game method ask in " + this.name + "'s game/object"); 
  }
  void reset() {
    System.out.println("I am Game method reset in " + this.name + "'s game/object");     
  }
  void report() {
    System.out.println("I am Game method report in " + this.name + "'s game/object");     
  }
}

class Exercise {
  public static void main(String[] args) {
    Game a, b, c, d; 
    a = new Game("Adrian"); 
    a.report(); 
    a.ask(); 
    a.report();
    b = new Game("Alea"); 
    b.report(); 
    b.ask(); 
    b.report(); 
    a.ask(); 
    a.report();
    a.reset(); 
    a.report(); 
  } 
}
That was stage one, now the final stage:
import java.util.*; 

class Game {
  int right, wrong, n1, n2;
  Scanner sc;  
  String name; 
  Game(String name) {
    this.name = name; 
    this.sc = new Scanner(System.in); 
    System.out.println("Game for " + this.name + " created."); 
  } 
  void ask() {
    this.n1 = (int) (Math.random() * 100 - 50); 
    this.n2 = (int) (Math.random() * 100 - 50); 
    System.out.print(this.name + ", what is " + this.n1 + " + " + this.n2 + "? ");
    int answer = this.sc.nextInt(); 
    if (answer == this.n1 + this.n2) {
      System.out.println("That's right."); 
      this.right += 1; 
    } else {
      System.out.println("No, the right answer was: " + (n1 + n2)); 
      this.wrong += 1; 
    } 
  } 
  void report() {
    System.out.println(this.name + " you have " + this.right + " correct answers out of " + (this.right + this.wrong) + " so far."); 
  } 
  void reset() {
    this.wrong = 0; 
    this.right = 0;
    System.out.println("Game for " + this.name + " has been reset now."); 
  } 
}

Wed Nov 18
Dann's notes from lab last week: here.

His copies of the programs you worked out then:

Tue Nov 17
Notes developed in class today.

Solutions for: Homework One, Homework Two.

Some warm-up quizzes for the introductory track:

  1. Batch One
  2. Batch Two
  3. Batch Three
  4. Batch Four
  5. Batch Five
  6. Batch Six
  7. Batch Seven (try this if you think you know for loops)
  8. Batch Eight
  9. Batch Nine
  10. Batch Ten

  11. An older exam

Fri Nov 13
Notes from class yesterday.

Thu Nov 12
Program developed in class today:
class Point{

  int x, y; 

  public Point(int x, int y) {
    this.x = x;
    this.y = y;
  }

  public static double distanceBetween(Point a, Point b) {
    double distance; 
    distance = (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y);
    return Math.sqrt(distance); 
  }

  public double distanceTo(Point a) {
    double distance, dx, dy; 
    dx = this.x - a.x; 
    dy = this.y - a.y; 
    distance = Math.sqrt(dx * dx + dy * dy); 
    return distance;
    
  }

  public static void main(String[] args){
    Point obama = new Point(0, 5);
    Point mccain = new Point(3, 8);
    System.out.println(obama.distanceTo(mccain));
    System.out.println(mccain.distanceTo(obama));
    System.out.println(Point.distanceBetween(obama, mccain));
  }
    

}

Script we used to interactively write this in class step by step.

Thu Nov 05
Notes developed in class today.

Thu Oct 29
These are the notes as we wrote them in class today.

Date: Thu, 29 Oct 2009 08:43:09 -0400 (EDT)
From: Adrian German 
To: ...
Subject: reading assignment


If you're reading this you're in A290/A590 Java or A202/A598 and you will
be studying Java over the next six weeks, or you are in A348/A548 and you
will also need to review Java over the same interval of time.

http://www.cs.indiana.edu/~dgerman/fall2009/students.html contains links
to all the student blogs, for feedback. The list has been updated today.

Office hour appointments can be scheduled here (list of available times is
updated weekly) http://silo.cs.indiana.edu:8346/cgi-bin/fall2009/schedule

http://www.libraries.iub.edu/scripts/countResources.php?resourceId=59555
has the book we will use as a reference: it's called Java for Dummies (no
offense) 9 books in 1 by Lowe and Burd.

For this week please look through Book I and II.

http://www.cs.indiana.edu/classes/a202-dger/sum2009/code/0727.html has the
code examples in the book. We're going to go through these in class today.

Please let me know if you have questions and/or if you need any help.

I will write a bit more in the evening.

Sincerely,
Adrian German

--
Tue Oct 27
Today we start the second part of the course.

Today's lecture will discuss object oriented programming.

We start with a collection of examples in Python, Javscript and Java.

You can refer to

http://www.libraries.iub.edu/scripts/countResources.php?resourceId=59555
There's a book there by Doug Lowe and Barry Burd we will refer to.

Also a book by Thomas Petchel, the last three weeks of this class.

Sat-Sun Oct 24-25
Take a look at this image to understand how the new approach works:
We start from this collection of files: an engine inside a car driven by a person.

-bash-3.2$ ls -l
total 40
-rw-r--r-- 1 dgerman faculty  720 Oct 22 11:41 template.py
-rw-r--r-- 1 dgerman faculty 2469 Oct 22 11:46 client.py
-rwx------ 1 dgerman faculty  152 Oct 22 13:16 one
-bash-3.2$
one is the car, with wheels, that can be driven on land.

Call one and it will drive on land. It will work.

Now here is the "boat":

-bash-3.2$ ls -l
total 40
-rw-r--r-- 1 dgerman faculty  720 Oct 22 11:41 template.py
-rw-r--r-- 1 dgerman faculty 2469 Oct 22 11:46 client.py
-rwx------ 1 dgerman faculty  152 Oct 22 13:16 one
-rw-r--r-- 1 dgerman faculty 1795 Oct 22 13:35 server.py
-rwx------ 1 dgerman faculty   75 Oct 22 13:17 two
-bash-3.2$ 
two is the boat.

If

server is only adding the propellers to the car.

In server the wheels are not used. (We expected that, we knew that).

But

Fri Oct 23
Now copy server.py into the same folder (see below).

Also copy two and make it executable.

two works with the database and provides the server side version of one.

Just make sure that there are non references to my username, port, server in the file. Mine can be accessed from here:

http://silo.cs.indiana.edu:8346/cgi-bin/1022/two
You should see the content of the table as the program is accessed by various people:
mysql> select * from hwFive;
+------------+---------------------------------+------+------+------+------+---------------------+
| session_id | message                         | n1   | n2   | m1   | m2   | modified            |
+------------+---------------------------------+------+------+------+------+---------------------+
| 58974425   | Very good.Score now: 2 out of 4 |   26 |   23 |    2 |    4 | 2009-10-22 13:36:24 | 
| 11848907   | Welcome                         |   17 |  -18 |    0 |    0 | 2009-10-22 13:36:42 | 
| 24997385   | Very good.Score now: 1 out of 1 |  -37 |   48 |    1 |    1 | 2009-10-22 13:37:40 | 
+------------+---------------------------------+------+------+------+------+---------------------+
3 rows in set (0.00 sec)

mysql> 
Notice that one still works, as below, and the two share code but keep state in different places, at the same time.

Thu Oct 22

  1. Go to (create it if necessary) ~/apache/cgi-bin/1022
  2. Copy in that folder template.py
  3. Now copy client.py into the same folder.
  4. Finally copy one make it executable and access it from the web.
  5. To access use URL http://silo.cs.indiana.edu:xxxx/cgi-bin/yyyy/one

Now let's go to MySQL:

cd /nobackup/dgerman/mysql-5.0.22/
Locate the file that allows you to log in as regular user:
-bash-3.2$ ls -ld connect_as_lbird 
-rwx------ 1 dgerman faculty 105 Oct 13 12:26 connect_as_lbird

-bash-3.2$ cat connect_as_lbird 
mysql --socket=/nobackup/dgerman/mysql/mysql.sock \
  --port=8974 --host=silo.cs.indiana.edu -u lbird -p
-bash-3.2$ 
Get in with the right password:
-bash-3.2$ ./connect_as_lbird 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.22-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>
At the mysql command prompt select your database:
use awards;
Then create the table:
create table hwFive (
  session_id char(8) primary key,
  message varchar(240),
  n1 int,
  n2 int,
  m1 int,
  m2 int,
  modified   timestamp
);
Then exit.

Tue Oct 20
Notes to discuss briefly today: here

Notes wrote in class today: here.

Wed Oct 14
Notes of what we did in class yesterday.

Tue Oct 13
Notes for today's class: here.

Notes that help with Homework Five: here.

Some other notes:

Fri Oct 09
Things to do to start the lab today: here.

Thu Oct 08
Programs developed in class today: one, two.

Notes to be used in lab this week: or here.

Notes distributed in class today.

Tue Oct 06
Notes from class earlier today.

Sat-Sun Oct 3-4
Office hours appointments for next week can be scheduled here.

Additional notes for help with Homework Two (Friday pm notes).

Fri Oct 02
Updated notes for today's lab.

Thu Oct 01
Here are the notes we wrote in class today.

Tue Sep 29
Notes for today's class are here.

Fri Sep 25
Sample code for a card game (not a real model).

Here's what we worked in lab today: notes.

Thu Sep 24
Steps developed in class can be found here.

Lecture notes for today can be found here.

Wed Sep 23
Office hours for this week can be scheduled here.

Tue Sep 22
Notes for today: here.

Thu Sep 17
Simple, instructive tutorial on CGI/Python.

Tue Sep 15
Check the Class Notes page for updates.

Sat-Sun Sep 12-13
Here's the link to available times for office hours, please use it to make appointments.

Fri Sep 11
Actual notes from the lab today in real time.

Here's how the lab today might go (notes from a similar exercise yesterday).

Thu Sep 10
Alternate lab times can be found here.

Transcript of notes from class today.

Links distributed in class on Tuesday are here.

Wed Sep 09
Website for this semester instantiated.

Last semester's A201 website remains available to you.

Mon-Tue Sep 7-8
Individual student blogs updated.

Thu Sep 03
List of students and ports this semester.


Updated by Adrian German for A348/A548