![]() |
![]() Fall Semester 2009 |
Fri Dec 11
Mon Dec 7Notes 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:
You will notice the patterns are not completely specified.-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$
But that's easy to fix and the code above easily handles the text interface in Homework 3 problem 2.
Sat-Sun Dec 5-6import 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 04import 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 03The text (as you know) can be found here:
Use this link in the API to solve the second problem.Java All-In-One Desk Reference For Dummies, 2nd Edition by Doug Lowe and Barry Burd John Wiley & Sons © 2007 (914 pages) ISBN:9780470124512
Alternatively you can search (in the same online collection):
Chapter 5 - Working with CollectionsHerb Schildt's Java Programming Cookbook
Tue Dec 01
Tue Nov 24
Wed Nov 18Person marked with * does the typing.
Thiago Rachel* Alea* Victor Ron Danielle* Anthony* Kurt Adam* Michelle Brendan Will* Allen Munir* Jared Daniel*
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:
Send code to dgerman@indiana.edu at 12:19pm and watch development on class projector until 12:30pm.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>
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 18His copies of the programs you worked out then:
Tue Nov 17Solutions for: Homework One, Homework Two.
Some warm-up quizzes for the introductory track:
Fri Nov 13
Thu Nov 12class 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
Thu Oct 29
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 27Today'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-25We 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
one is the driver
client.One car
template.Engine engine,
two is the driver that drives the server extension of the client car.
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 23server.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:
You should see the content of the table as the program is accessed by various people:http://silo.cs.indiana.edu:8346/cgi-bin/1022/two
Notice thatmysql> 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>
one still works, as below, and the two share code but keep state in different places, at the same time.
Thu Oct 22
~/apache/cgi-bin/1022
template.py
client.py into the same folder.
one make it executable and access it from the web.
http://silo.cs.indiana.edu:xxxx/cgi-bin/yyyy/one
Now let's go to MySQL:
Locate the file that allows you to log in as regular user:cd /nobackup/dgerman/mysql-5.0.22/
Get in with the right password:-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$
At the mysql command prompt select your database:-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>
Then create the table:use awards;
Then exit.create table hwFive ( session_id char(8) primary key, message varchar(240), n1 int, n2 int, m1 int, m2 int, modified timestamp );
Tue Oct 20Notes wrote in class today: here.
Wed Oct 14
Tue Oct 13Notes that help with Homework Five: here.
Some other notes:
http://www.cs.indiana.edu/classes/a290-web/spr2008/tue0212.txt
http://www.cs.indiana.edu/classes/a348/spr2008/thu0214-setup.txt
http://www.cs.indiana.edu/classes/a348/spr2008/thu0214.txt
http://www.cs.indiana.edu/classes/a348/fall2005/notes/labSix.html
Fri Oct 09
Thu Oct 08Notes to be used in lab this week: or here.
Notes distributed in class today.
Tue Oct 06
Sat-Sun Oct 3-4Additional notes for help with Homework Two (Friday pm notes).
Fri Oct 02
Thu Oct 01
Tue Sep 29
Fri Sep 25Here's what we worked in lab today: notes.
Thu Sep 24Lecture notes for today can be found here.
Wed Sep 23
Tue Sep 22
Thu Sep 17
Tue Sep 15
Sat-Sun Sep 12-13
Fri Sep 11Here's how the lab today might go (notes from a similar exercise yesterday).
Thu Sep 10Transcript of notes from class today.
Links distributed in class on Tuesday are here.
Wed Sep 09Last semester's A201 website remains available to you.
Mon-Tue Sep 7-8
Thu Sep 03