![]() |
![]() Fall Semester 2009 |
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