![]() |
![]() Spring Semester 2002 |
Sat May 4
Tue Apr 30
Mon Apr 29
Fri Apr 26
Thu Apr 25
Here's the code for the game.
Here are some notes that build towards it:
Here's to get us started on I/O and basic networking.
Tue Apr 23
Sun Apr 21
(1 + 1) * 2 + 2 * ( 1 + 1 )
*
")
and when that happens we need to propagate this delay throughout. So, in
our case, we only need to take care of pluses and minuses, which should
not be evaluated if there are stars and slashes waiting to be processed
(they bind stronger to the numbers than their lower precedence counterparts).
So the code more or less becomes (only an if
statement changes):
static boolean reducePlusesAndMinuses(Vector expr) { boolean done = true; for (int i = 0; i < expr.size(); i++) { if (expr.elementAt(i).equals("+") || expr.elementAt(i).equals("-")) { if ( // value to its left isValue((String)expr.elementAt(i - 1)) && // value to its right isValue((String)expr.elementAt(i + 1)) && // and not a higher priority operator to wait on ! ( ( // value on left not at end of vector (i - 2) >= 0 && // higher precedence operator waiting there ( ((String)expr.elementAt(i - 2)).equals("*") || // note there are two such operators ((String)expr.elementAt(i - 2)).equals("/") ) ) // this can happen on the left || // or (it can happen on the right) ( // value on right not at end of vector (i + 2) < expr.size() && // higher precedence operator waiting there ( ((String)expr.elementAt(i + 2)).equals("*") || // note there are two such operators ((String)expr.elementAt(i + 2)).equals("/") ) ) ) ) { Integer a = new Integer((String)expr.elementAt(i - 1)); Integer b = new Integer((String)expr.elementAt(i + 1)); // ... reduce the plus signYou should include this change in your programs.
Fri Apr 19
Second Midterm MAKE-UP EXAM will likely be held in
Confirmation to be posted here soon.Swain West 119 on
Tue Apr 23 7:15pm-9:15pm.
Thu Apr 18
Now the question is: why is this interesting; and if it is, then what?(2+4)*2+3*(3+7)
Wed Apr 17
import java.io.*; import java.util.*; class Eval { /* this Hashtable must be shared between two static methods (main, which creates it and process, which is using it for lookup,) so it must be global enough */ static Hashtable env = new Hashtable(); // here's the main method getting away with a try/catch public static void main(String[] args) throws IOException { //this is why we need to acknowledge the IOException BufferedReader console = new BufferedReader (new InputStreamReader(System.in)); //checked after the loop, must be visible enough String line; do { //prompt System.out.print("Eval> "); //... and read line = console.readLine(); //stop if you have to if (line.equals("bye")) break; //get ready to take things apart StringTokenizer st = new StringTokenizer(line); //prepare the storage for the resulting tokens Vector v = new Vector(); //and now just do it while (st.hasMoreTokens()) { v.addElement(st.nextToken()); } //so now the tokens are in a Vector, v //could be an assignment statement if (v.size() > 2 && v.elementAt(1).equals("=")) { //in which case you take the name at the front String name = (String)(v.elementAt(0)); //... and keep it in the variable called 'name' //then remove the string and the equal sign v.removeElementAt(1); v.removeElementAt(0); //the Vector is shrinking as you remove elements so, //either remove backwards or twice at index 0 (zero)! //now process the remaining expression and store the // value in the Hashtable in the entry for the name // you found in position 0 in the original Vector env.put(name, Eval.process(v)); } else {// if it's not an assignment statement, // ... then you can start evaluating it Eval.process(v); }//end of conditional processing // getting ready to report the result System.out.print("Done: "); Eval.show(v); //the Vector is shown here } while (true);//loop if you have to (exit with break!) }// end of main // straghtforward procedure for showing a Vector static void show(Vector v) { for (int i = 0; i < v.size(); i++) { System.out.print(" " + v.elementAt(i)); } System.out.println(); } // this is the heart of the evaluator static String process (Vector v) {// receives the Vector as a param // wishful thinking (see do/while test below) boolean finished = true; // first replace identifiers by their values for (int i = 0; i < v.size(); i++) { // get the String that's there (position i) // this could be anything: +, -, 123, a, * String element = (String) (v.elementAt(i)); // look it up in env (nevermind how it got there) Object value = env.get(element); // if you find it, there's a value associated with if (value != null) { // replace it (index i) by the value it has v.setElementAt((String)value, i); } }// all identifiers have been replaced // we're ready for the real processing do { // remove parens and update your agenda finished = Eval.removeParens(v); // all three methods used here return true when // they could not find anything to work on within // the expression, and they return false if they // could change it in at least one aspect; therefore // your hope of being finished is true only when none // of the workers find anything to work on... finished = finished && Eval.removeStars(v); finished = finished && Eval.removePluses(v); // report the current status of the Vector // from within the process of changing it System.out.print("Processing: "); Eval.show(v); } while (! finished); // if not finished keep going... // return the first element in the resulting Vector return (String) (v.elementAt(0)); } static boolean removeParens(Vector v) { // empty procedure, reports true return true; // this means it 'thinks' it's done... } static boolean removeStars(Vector v) { // same here, you need to provide these methods... return true; } static boolean removePluses(Vector v) { // the only one that works boolean done = true;// assume nothing will have to be done // look everywhere in the Vector for (int i = 0; i < v.size(); i++) { // if you find a plus sign if (v.elementAt(i).equals("+")) { done = false; // there's work to be done // expect two numbers on its sides int n = Integer.parseInt((String) (v.elementAt(i - 1))), m = Integer.parseInt((String) (v.elementAt(i + 1))); // store the sum where the first operand was v.setElementAt((n + m) + "", i - 1); // carefully remove the plus and the second operand v.removeElementAt(i + 1); v.removeElementAt(i); // don't skip the next token i -= 1; // the Vector has shrunk so it's in front // of you, and the for loop will increment i by default }// end of determining if you're looking at a '+' sign }// end of the for loop return done; // report whether we're done or not }// end of the removePluses method }// end of story
Tue Apr 16
Mon Apr 15
Wed Apr 17
so we can
talk about double buffering in class first.
Thu Apr 11
Grades for the Second Midterm have been posted this morning.
Tue Apr 9
Lecture notes for the week posted.
Thu Apr 4
Lecture, lab notes for today posted.
Wed Apr 3
The test is WEDNESDAY APRIL 3 (THREE), in RH100, between 7-9pm.
Tue Apr 2
Mon Apr 1
Sat-Sun Mar 30-31
Fri Mar 29
Note that correct answers are included.
Thu Mar 28
Tue Mar 26
Thu Mar 21
The average of practical grades was 80.82, with a standard deviation of 14.79.
Wed Mar 20
import java.io.*; public class PracticalTest { public static void main(String[] args) { ConsoleReader c = new ConsoleReader(System.in); System.out.print("Enter the card notation: "); String hand = c.readLine(); String two = card(hand); suit(two); } public static String card(String card) { String one = card.substring(0,1); String two = card.substring(1,2); if (one.equalsIgnoreCase("A")) System.out.print("Ace of "); else if (one.equals("2")) System.out.print("Two of "); else if (one.equals("3")) System.out.print("Three of "); else if (one.equals("4")) System.out.print("Four of "); else if (one.equals("5")) System.out.print("Five of "); else if (one.equals("6")) System.out.print("Six of "); else if (one.equals("7")) System.out.print("Seven of "); else if (one.equals("8")) System.out.print("Eight of "); else if (one.equals("9")) System.out.print("Nine of "); else if (one.equals("1")) { if (two.equals("0")) { System.out.print("Ten of "); two = card.substring(2, 3); } } else if (one.equalsIgnoreCase("J")) System.out.print("Jack of "); else if (one.equalsIgnoreCase("Q")) System.out.print("Queen of "); else if (one.equalsIgnoreCase("K")) System.out.print("King of "); else System.out.print("Unknown denomination of "); return two; } public static void suit (String two){ if (two.equalsIgnoreCase("D")) System.out.println("diamonds."); else if (two.equalsIgnoreCase("H")) System.out.println("hearts."); else if (two.equalsIgnoreCase("S")) System.out.println("spades."); else if (two.equalsIgnoreCase("C")) System.out.println("clubs."); else System.out.println("unknown colour."); } }This is based on the solution posted but it's using methods, as in Lab 8.
Tue Mar 19
Mon Mar 18
Sat-Sun Mar 9-17
Fri Mar 8
Lab Notes Nine and Lecture Notes Nineteen (for after the break) already posted.
Here's the code we developed in class yesterday.
class One { public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); int[] b; System.out.print("Size> "); int n = console.readInt(); b = new int[n]; Cookbook.show(b); Cookbook.squares(b); Cookbook.show(b); b = Cookbook.insert(b, 3, 13); Cookbook.show(b); } } class Cookbook { static void show(int[] a) { for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); } static void squares(int[] a) { for (int i = 0; i <= a.length - 1; i++) { a[i] = (i + 1) * (i + 1); } } static int[] insert(int[] x, int ind, int val) { int[] temp = new int[x.length + 1]; for (int i = 0; i < ind; i++) temp[i] = x[i]; temp[ind] = val; for (int i = ind; i < x.length; i++) temp[i + 1] = x[i]; return temp; } }
Thu Mar 7
I have received a few messages asking me about this week's lab. This week we have labs as usual, except I am aware this week is a bit different, so let me explain what the difference is.
If you can't make it to the lab this week you will have to make it up the week after the break. The notes will be posted soon, in class you will be asked about the problems on the practical a week ago. That's basically it about the lab this week.
I am still working on the grades for the practical. I hope to have them posted at the end of the week at the latest. I have two more sections to grade. I'll try to post the grades for the makeup as well before the week is over.
Wed Mar 6
Tue Mar 5
Mon Mar 4
Sun Mar 3
Sat Mar 2
Fri Mar 1
Thu Feb 28
In class we will continue to solve problems. Please keep up though
with the posted notes. You have now seen
most of Chapter Seven, and the notes for today summarize all known aspects.
You have also seen recursion and the notes end with a neat example
of using recursion to solve a difficult problem. You also should be in command
of all of Chapter Three and the notes posted for the lecture two days ago go
through the development of a Fraction
type that should be
instructive.Fraction
s are like BigInteger
s
(i.e., precise and neat).
Wed Feb 27
The review session is in Rawles Hall at 7pm, we will solve problems.
Tue Feb 26
For problems from chapter 6 check the notes, and list of problems posted.
(Links to chapter 5 problems are in here, as you probably already know).
Mon Feb 25
Lecture notes for tomorrow posted.
Sun Feb 24
Wed Feb 27 7pm Practical ExamReview
Wed Mar 6 7-9pm Midterm One Makeup Exam
Sat Feb 23
Also a reminder: documentation about all of the Java classes that will be used this semester available under Useful Links (click on Hierarchy). Sun's Java tutorial is available here.
Fri Feb 22
StringTokenizer
can be found here.
Thu Feb 21
Wed Feb 20
Tue Feb 19
Mon Feb 18
Sun Feb 17
Sat Feb 16
Fri Feb 15
Here's the distribution of (raw) scores:
The average was 34.92, the standard deviation was 8.72.50 *** 49 ****** 48 ******* 47 **** 46 ******** 45 ****** 44 ******* 43 **** 42 ***** 41 ************* 40 ********* 39 ********* 38 ******** 37 ****** 36 ******** 35 ************ 34 ******* 33 ***** 32 **** 31 ******* 30 **** 29 ****** 28 ******* 27 ******** 26 ***** 25 ******** 24 ***** 23 **** 22 ******* 21 ***** 20 **** 19 ** 18 * 14 **
Thu Feb 14
Wed Feb 13
Please bring a pencil with you for the scantrons.
Exam consists of about 52 multiple-choice questions.
Good luck and see you tonight.
Tue Feb 12
Second, when you approach the Practice Quizzes and the Sample Midterm please approach them as Learning Tools, not as instruments of testing. Use them to learn something new, not to measure how much you know. You should be happy if you cannot solve any of the problems by yourself at first, but are able to understand why the right answer is as indicated. This means that you have learned something in the process, that will help you on the exam.
Remember that the exam on Wednesday is open-book, open-notes, but individual work.
Mon Feb 11
Sun Feb 10
Skip questions 31, 32, 38-42 (inclusive), 46, and 47.
All the others make for a very good study material for the exam on Wednesday.
Please note that correct answers are clearly indicated, for your convenience.
Also, don't forget your exam on Wednesday is open-book, and open-notes.
Sat Feb 9
Fri Feb 8
Thu Feb 7
(This justification developed with Omar Khan during office hours).
Say we need random numbers between a
, and b
,
where a
is less than b
. Let y
be
such a random number. Then we can calculate how far into the segment
y
is, as a percentage.
Sincedouble p = (y - a) / (b - a); // [1]
y
is random that means p
can be anywhere
between 0
and 1
.
Well, such numbers can be generated with Math.random()
.
So, let's turn [1]
on its head, to obtain y
as a
function of p
.
Then, we have:
Now replacey = p * (b - a) + a;
p
by Math.random()
:
Same as stretching followed by a translation. End of story. Thanks, Omar.y = (b - a) * Math.random() + a;
Wed Feb 6
Please note the unusual date and time and procedure for turning in Lab Assignment Five.
Tue Feb 5
Lab Notes for this week posted, contain Lab Assignment Five.
Mon Feb 4
1. The intersection
method in class Rectangle
does
not return a valid (that is, meaningful) answer when the Rectangle
s
don't overlap. (This is what the book says too, but one needs to read even the
text of the problem with care!). So what do we do when they don't overlap and
how do we know that in advance? The solution is to check if they overlap
before you
calculate
the intersection (which you should do only if they overlap!).
2. The epsilon variable is needed in Fourteen
of the
Problem Set One (with solutions) so we don't divide by 0 (zero). But
when x is negative and much smaller than epsilon the result
is incorrect. What do we do? (This is problem P2.18 page 99 in the book). The
trick is to think of the problem in physical terms: if you're really using a
car and if in your calculations you use an epsilon that is small
enough, e.g. about 0.1in (or smaller, like 0.001 of the car's length) then
Other than this "negative micron" case, the formula is perfect.
But does
anyone have a formula that covers even this case with accuracy?
Sat-Sun Feb 2-3
Fri Feb 1
boolean
type (through run-time
tests) has potentially the greatest influence on the program's course of
action. We've seen I/O with ConsoleReader
and some simple,
yet non-trivial programs. Don't forget the warmups and problems posted
along with solutions!
Thu Jan 31
Wed Jan 30
Tue Jan 29
Mon Jan 28
boolean
primitive type (pp. 207-210)
char
primitive type (pp. 731-733)
By the end of the week we should have covered all of Chapter Three.
Sun Jan 27
Sat Jan 26
Fri Jan 25
Also, please
let me know if you appear listed in the wrong section. If you are
listed in the wrong section please send me an e-mail message so I can make
the correction, and wait for a confoirmation from me before submitting your
homework assignment.
Thu Jan 24
boolean
s.
Wed Jan 23
Tue Jan 22
Mon Jan 21
Date: Mon, 21 Jan 2002 13:16:45 -0500 (EST) From: Adrian GermanTo: Undisclosed recipients: ; Subject: A201/A597/I210 Update First of all if you receive this message in error please let me know also so I can update my e-mail distribution list. Dear A201/A597/I210 friends, I went to the bookstore and bought a new book that comes with a CD. On the CD there's Sun's Java 2 SDK. I just tried installing it and it only took me a few minutes. Those who still can't get Emacs or Sun's JDK installed from the web are welcome to borrow this CD from me and use it to install SDK. (SDK is the new name for JDK 1.3, so it's basically the same and with it you will be able to run javac and java from the command line). I also wanted to review the reading assignments from the book: a) tomorrow in class we will do mostly 2.5, 2.6, 2.7, and solve problem P2.19 (Problem 15 in the set posted with Lab Two Notes) You would also do well to review some of the warmups (that have also been posted with Lab Notes Two, with solutions). This last set (of warmups) can be found in the book on pp. 92-94. c) on Thursday we plan to introduce the boolean type as part of our discussion on expressions, types, and operators. We'll talk about this again when we study chapter 5, and I will post a set of warmups and programming problems at that time, also. b) to review what has been already covered, one should focus on sections 1.7-12, 2.1-3 (including Common Error 2.1, Quality Tip 2.1, Advanced Topic 2.1-4, Common Error 2.2 and Prod. Hint 2.1) d) next week we start Chapter 3, user-defined classes. I posted lecture notes Seven already, which correspond to sections 3.1-4 from the book. Hope this helps. If you have questions, or if you need any help please let me know. ... Adrian
Sat-Sun Jan 19-20
Fri Jan 18
The Lab Assignment is due next lab (Thu-Fri) as indicated on the What's Due? page.
Thu Jan 17
(Please note we use dgerman-I210 for the name of the class account).
I am also going to assemble a list of A201 tutors.
Here's an interesting problem courtesy of Wendy Clendening:
This does not work. Why? (The answer will be provided in class).public class Math { public static void main(String[] args) { int a = 5; int b = 7; System.out.println(Math.max(a, b)); } }
Wed Jan 16
jdk1.3
(also known as Java 2). If you have
this installed you can use Notepad, javac
, and
java
to develop programs.
Alternatively, you can
download
Emacs. (Here's a reference card for Emacs, to help you use it more effectively).
Remember that no editor (or any Java IDE for that matter) is Java. What we
mean by Java is essentially encapsulated in two programs: javac
and java
. The rest is ancillary material related to development.
But Emacs
is the superior editor!
Tue Jan 15
Please check the "What's Due?" page frequently also.
Mon Jan 14
One is trying to describe the nature of programming as being a field in which (like any other science) solving problems (and enjoying it) is the way to go.
The other one is trying to put into a broader perspective the rigour (or precision) with which you will be confronted in this class, and in other classes, throughout your college years.
With your permission, I hope you find them useful.
Sun Jan 13
The single most significant set of notes for this semester will be
Sum 2001.
Sat Jan 12
Fri Jan 11
Here's the link to Sun's website, as well.
Thu Jan 10
Lecture notes for this week, next, lab notes for today, posted.
Wed Jan 9