|
First Summer 2004 |
Fri Jun 18
Thu Jun 17
Congratulations to Jarred Braun for a clever solution to the magic square problem.
Reference
for Quiz Five and the final exam.
Final Exam in class today (1:10pm LH102).
Quiz Five is also due today.
Older exams that would be well worth reviewing:
Exam is multiple-choice, closed-book, in class and lasts 75 minutes.
Wed Jun 16
|
|
|
|
|
|
|
|
| Jingjun Sun | Will Odom | Xin Wang | Han Zeng | Ed Kwon | Riko Kwe | Amy Anderson | Jennifer Colgan |
Today in class we:
Tue Jun 15
Congratulations to Mayank Jhunjhunwala for two great ideas to be posted here soon.
The problems worked out in class yesterday:
import java.util.*;
class StdDev {
public static void main(String[] args) {
String numbers = "2 3 4";
StringTokenizer st = new StringTokenizer(numbers);
double[] x = new double[ st.countTokens() ];
int i = 0;
while (st.hasMoreTokens()) {
x[i] = Double.parseDouble(st.nextToken());
i = i + 1;
}
double ave = 0;
for (i = 0; i < x.length; i++) // stage one: calculate average
ave += x[i];
ave = ave / x.length;
double sum = 0;
for (i = 0; i < x.length; i++) // step two: squared deviations
sum += (x[i] - ave) * (x[i] - ave);
sum /= (x.length - 1);
double stdev = Math.sqrt(sum);
System.out.println("Formula one yields: " + stdev);
double ss = 0;
sum = 0;
for (i = 0; i < x.length; i++) { // only once
ss += x[i] * x[i]; // sum of squared elements
sum += x[i]; // simple sum of elements
}
stdev = Math.sqrt((ss - (sum * sum / x.length))/(x.length - 1));
System.out.println("Formula two gives: " + stdev);
}
}
Passing twice through a string of characters:
import java.util.*;
class One {
public static void main(String[] args) {
String a = "Once upon a time there was a string tokenizer.";
StringTokenizer st = new StringTokenizer(a);
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
st = new StringTokenizer(a);
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}
And the simplest way to solve the military time format problem:
class MilitaryTime {
public static void main(String[] args) {
String time2 = "0000",
time1 = "2359";
int t1 = Integer.parseInt(time1.substring(0, 2)) * 60 +
Integer.parseInt(time1.substring(2));
int t2 = Integer.parseInt(time2.substring(0, 2)) * 60 +
Integer.parseInt(time2.substring(2));
int d = (t2 - t1 + 24 * 60) % (24 * 60);
System.out.println(d/60 + " hrs. and " + d%60 + " mins.");
}
}
Mon Jun 14
Sun Jun 13
|
|
|
| Narine Yegian | Anthony Martin | Mr. Bean |
Narine was the engine behind the solution presented in class on Friday (Purse,
transfer, equals)
Congratulations to Anthony for getting accepted into NKS 2004 Summer School!
And (to the) Mr. Bean (in us,) for keeping our interest high during these hot summer days.
Sat Jun 12class Experiment {
public static void main(String[] args) {
Purse a = new Purse();
a.add(new Coin("quarter", 25));
a.add(new Coin("dime", 10));
a.add(new Coin("dime", 10));
System.out.println("The first contains: " + a);
Purse b = new Purse();
b.add(new Coin("dime", 10));
b.add(new Coin("quarter", 25));
b.add(new Coin("dime", 10));
System.out.println("The second one is: " + b);
/*****************************************************************
System.out.println(a + " and " + b + " ... are they the same?\n" +
"The answer is: " + a.equals(b));
*****************************************************************/
a.transfer(b);
System.out.println("The first one contains: " + a);
System.out.println("The second one now contains: " + b);
}
}
The Coin class:
class Coin {
Coin(String name, int value) {
this.name = name;
this.value = value;
}
String name;
int value;
boolean equals(Coin other) {
return this.value == other.value;
}
public String toString() {
return this.name;
}
}
The Purse class:
import java.util.*;
class Purse {
ArrayList contents;
Purse() {
this.contents = new ArrayList();
}
void add(Coin c) {
this.contents.add(c);
}
public String toString() {
String result = "[";
for (int i = 0; i < this.contents.size(); i++) {
result += this.contents.get(i)+ " ";
}
return result + "]";
}
boolean equals(Purse other) {
return this.in(other) && other.in(this);
}
boolean in(Purse other) {
for (int i = 0; i < this.contents.size(); i++) {
Coin coin = (Coin)(this.contents.get(i));
if (this.occurs(coin) != other.occurs(coin))
return false;
}
return true;
}
int occurs(Coin c) {
int sum = 0;
for (int i = 0; i < this.contents.size(); i++)
if (((Coin)this.contents.get(i)).equals(c))
sum += 1;
return sum;
}
void transfer(Purse other) {
while (! other.empty())
this.add((Coin)other.pop());
}
Object pop() {
return this.contents.remove(0);
}
boolean empty() {
return this.contents.size() == 0;
}
}
Fri Jun 11class Magic {
public static void main(String[] args) {
int n = 13;
int[][] a = new int[n][n];
int i = n - 1, j = n / 2;
for (int k = 1; k <= n * n; k++) {
a[i][j] = k;
if (a[(i+1)%n][(j+1)%n] > 0)
if (i == 0)
i = n - 1;
else
i = i - 1;
else {
i = (i + 1) % n;
j = (j + 1) % n;
}
System.out.println("------(" + k + ")---------------");
Magic.show(a);
}
// sum of lines:
for (int line = 0; line < a.length; line++) {
System.out.print("For line " + line + " the sum is: ");
int sum = 0;
for (int col = 0; col < a[line].length; col++) {
sum += a[line][col];
}
System.out.println(sum);
}
}
static void show(int[][] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
String number = " " + a[i][j];
System.out.print(number.substring(number.length() - 3));
}
System.out.println();
}
}
}
And here's Francesca's example as well.
class One {
public static void main(String[] args) {
int[] n1 = { 9, 9};
int[] n2 = { 0, 1};
int[] r = new int[n1.length];
for (int i =r.length - 1; i > 0; i--) {
r[i] += n1[i] + n2[i];
r[i-1] += r[i] / 10;
r[i] = r[i] % 10;
}
r[0] += n1[0] + n2[0];
for (int i = 0; i < r.length; i++) {
System.out.print(r[i]);
}
System.out.println();
}
}
Thu Jun 10
Francesca is going to provide a perspective on the last assignment today.
She will start from the following code:
class One {
public static void main(String[] args) {
int[] n1 = { 9, 9};
int[] n2 = { 0, 1};
int[] r = new int[n1.length];
// some code will be written here and discussed
for (int i = 0; i < r.length; i++) {
System.out.print(r[i]);
}
System.out.println();
}
}
Note the message of yesterday:
Date: Wed, 9 Jun 2004 15:23:44 -0500 (EST) From: Adrian German <dgerman@cs.indiana.edu> To: A201 Sum 2004 Distr. List <dgerman@indiana.edu> Subject: A201 Sum 2004 Practical Exam Today is Wed Jun 9. The lab tomorrow will offer a mock exam. You will receive a random problem and will have to turn it in. During the mock exam you will have access to everything you want or need, and the AIs can take questions and help. You can talk to others too. The real exam will be on Tue Jun 15 in labs (closed-book etc.) The class notes web page will be updated to include this info. Please let me know if you have any questions or need any help. The final exam is closed book, multiple-choice, Thu Jun 17, in class. During the wrap-up lab that day we could offer a make up for the Practical Exam to those who are interested. ... Adrian
Wed Jun 9
Tue Jun 8if statements.
class Two {
public static void main(String[] args) {
String[] months = {"January", "February", "March", "April", "June", "July", "August", "September", "October", "November", "December"};
System.out.println(months[Integer.parseInt(args[0]) - 1]);
}
}
This solution suggested by
Note the loophole.
Mon Jun 7Adam for this original solution to 3.16 (posted below).
![]()
![]()
Adam Weyhaupt Connie Gherna
Connie for her active and often successful pursuit of excellence.
Here's Adam's solution:
class One {
public static void main(String[] args) {
System.out.println(One.month(Integer.parseInt(args[0])));
}
static String month(int index) {
return
"January".substring (0, "January".length() * one(index)) +
"February".substring (0, "February".length() * two(index)) +
"March".substring (0, "March".length() * thr(index)) +
"April".substring (0, "April".length() * fou(index)) +
"May".substring (0, "May".length() * fiv(index)) +
"June".substring (0, "June".length() * six(index)) +
"July".substring (0, "July".length() * sev(index)) +
"August".substring (0, "August".length() * eig(index)) +
"September".substring(0, "September".length() * nin(index)) +
"October".substring (0, "October".length() * ten(index)) +
"November".substring (0, "November".length() * ele(index)) +
"December".substring (0, "December".length() * twe(index));
}
static int one(int i) {
return (i-2)*(i-3)*(i-4)*(i-5)*(i-6)*(i-7)*(i-8)*(i-9)*(i-10)*(i-11)*(i-12) / (-1 * -2 *-3 *-4 *-5 *-6 *-7 *-8 *-9 *-10 *-11 );
}
static int two(int i) {
return (i-1)*(i-3)*(i-4)*(i-5)*(i-6)*(i-7)*(i-8)*(i-9)*(i-10)*(i-11)*(i-12) / ( 1 * -1 *-2 *-3 *-4 *-5 *-6 *-7 *-8 * -9 *-10 );
}
static int thr(int i) {
return (i-1)*(i-2)*(i-4)*(i-5)*(i-6)*(i-7)*(i-8)*(i-9)*(i-10)*(i-11)*(i-12) / ( 2 * 1 *-1 *-2 *-3 *-4 *-5 *-6 *-7 * -8 * -9 ) ;
}
static int fou(int i) {
return (i-1)*(i-2)*(i-3)*(i-5)*(i-6)*(i-7)*(i-8)*(i-9)*(i-10)*(i-11)*(i-12) / ( 3 * 2 * 1 *-1 *-2 *-3 *-4 *-5 *-6 * -7 * -8 ) ;
}
static int fiv(int i) {
return (i-1)*(i-2)*(i-3)*(i-4)*(i-6)*(i-7)*(i-8)*(i-9)*(i-10)*(i-11)*(i-12) / ( 4 * 3 * 2 * 1 *-1 *-2 *-3 *-4 *-5 * -6 * -7 );
}
static int six(int i) {
return (i-1)*(i-2)*(i-3)*(i-4)*(i-5)*(i-7)*(i-8)*(i-9)*(i-10)*(i-11)*(i-12) / ( 5 * 4 * 3 * 2 * 1 *-1 *-2 *-3 *-4 * -5 * -6 );
}
static int sev(int i) {
return (i-1)*(i-2)*(i-3)*(i-4)*(i-5)*(i-6)*(i-8)*(i-9)*(i-10)*(i-11)*(i-12) / ( 6 * 5 * 4 * 3 * 2 * 1 *-1 *-2 *-3 * -4 * -5 );
}
static int eig(int i) {
return (i-1)*(i-2)*(i-3)*(i-4)*(i-5)*(i-6)*(i-7)*(i-9)*(i-10)*(i-11)*(i-12) / ( 7 * 6 * 5 * 4 * 3 * 2 * 1 *-1 *-2 * -3 * -4 );
}
static int nin(int i) {
return (i-1)*(i-2)*(i-3)*(i-4)*(i-5)*(i-6)*(i-7)*(i-8)*(i-10)*(i-11)*(i-12) / ( 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 *-1 * -2 * -3 );
}
static int ten(int i) {
return (i-1)*(i-2)*(i-3)*(i-4)*(i-5)*(i-6)*(i-7)*(i-8)*(i- 9)*(i-11)*(i-12) / ( 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 * -1 * -2 );
}
static int ele(int i) {
return (i-1)*(i-2)*(i-3)*(i-4)*(i-5)*(i-6)*(i-7)*(i-8)*(i- 9)*(i-10)*(i-12) / (10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 * -1 );
}
static int twe(int i) {
return (i-1)*(i-2)*(i-3)*(i-4)*(i-5)*(i-6)*(i-7)*(i-8)*(i- 9)*(i-10)*(i-11) / (11 * 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 );
}
}
Sun Jun 6/*
<applet code="Two.class" width=400 height=400>
</applet>
*/
import java.awt.*;
import java.applet.*;
public class Two extends Applet {
int count = 0;
public void paint(Graphics g) {
this.count += 1;
System.out.println("Paint called: " + this.count);
}
}
Here's the sorting program:
class One {
public static void main(String[] args) {
System.out.print("I am being called to help with sorting: ");
One.show(args);
boolean sorted = true;
for (int i = 0; i < args.length - 1; i++) {
if (Integer.parseInt(args[i]) > Integer.parseInt(args[i+1])) {
sorted = false;
String temp = args[i];
args[i] = args[i+1];
args[i+1] = temp;
}
}
if (sorted)
One.show(args);
else
One.main(args);
}
static void show(String[] x) {
for (int i = 0; i < x.length; i++)
System.out.print(x[i] + " ");
System.out.println();
}
}
Sat Jun 5
Fri Jun 4
| Chapter 2: | P2.7, | P2.9, | P2.10, | P2.11, | P2.12, | P2.13, | P2.14, | P2.15. | ||
| Chapter 3: | P3.11, | P3.14, | P3.16
(no ifs),
| P3.17. | ||||||
| Chapter 5: | P5.2, | P5.8, | P5.9, | P5.12. | ||||||
| Chapter 6: | P6.2, | P6.3, | P6.4, | P6.5, | P6.7, | P6.10, | P6.11, | P6.12, | P6.16, | P6.17. |
| Chapter 13: | P13.10, | P13.2, | P13.3, | P13.4, | P13.5, | P13.6, | P13.19. |
That is, 33 problems from the book.
Thu Jun 3Running letter grades are as of this time and they're correct.
I am grading the quizzes and won't be coming through labs today.
Instead, I will be coming through labs next Tuesday.
Wed Jun 2Date: Tue, 1 Jun 2004 15:30:11 -0500 (EST) From: Adrian German <dgerman@cs.indiana.edu> To: A201 Sum 2004 Distr. List <dgerman@indiana.edu> Subject: A201 Midterm Exam (Wed 1:10pm LH102) The midterm exam is tomorrow in class at 1:10pm. The exam is multiple-choice and contains 45-50 questions. The best preparation for the exam are the multiple-choice exercises in OnCourse. Also the old exams listed on "What's New?" page. Today in class we went over an older Midterm Exam from Summer 2001 and we justified the answers leaving a few questions as an exercise but mostly covering all of the questions listed in it. The Tigger problem could be turned in as late as Thursday June 3 at the end of the day (11:59pm). But for the quiz due tomorrow I would strongly encourage everybody to make an effort and turn it in. It's easy (5+5=10 questions from Chapter 5 and 6) and amounts to a review of the posted answers so it can only do you good, for the exam. Exam is multiple-choice so bring a No. 2 pencil to class for scantrons. If you have any questions and/or concerns please let me know. The easiest way would be to reply to this message. ... Adrian
Tue Jun 1So we will take a look at this midterm exam (from three summers ago).
Mon May 31A bit of humor before and during the exam week(s):
(Reading assignment for this joke: Chapter 6).
And here's the sorting routine of Friday:
class One {
public static void main(String[] args) {
int[] a = new int[args.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(args[i]);
}
One.show(a);
One.sort(a);
One.show(a);
}
static void show(int[] a) {
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}
static void sort(int[] a) {
boolean sorted;
do {
sorted = true;
for (int i = 0; i < a.length - 1; i++)
if (a[i] > a[i+1]) {
sorted = false;
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
System.out.println("I am sorting...");
} while (! sorted);
}
}
Sat-Sun May 29-30import java.util.*;
class Processing {
public static void main(String[] args) {
ConsoleReader c = new ConsoleReader(System.in);
String line;
do {
System.out.print("Echo> ");
line = c.readLine();
System.out.println(no_change(line)); // no change
System.out.println(sdrawkcab_gnihtyreve(line)); // everything backwards it says
System.out.println(tsuj_esrever_eht_stnetnoc_fo_sdrow(line)); // just reverse the contents of words (but not the order of words)
System.out.println(words_of_order_the_reverse_just(line)); // just reverse the order of words (but not the words themselves)
} while (! line.equals("bye"));
}
static String sdrawkcab_gnihtyreve(String line) { // second definition in line
String result = "";
for (int i = line.length() - 1; i >= 0; i--) {
result = result + line.charAt(i);
}
return result;
}
static String words_of_order_the_reverse_just(String line) { // last definition
String result = "";
StringTokenizer stapler = new StringTokenizer(line);
while (stapler.hasMoreTokens()) {
String token = stapler.nextToken();
result = token + " " + result;
}
return result;
}
static String tsuj_esrever_eht_stnetnoc_fo_sdrow(String line) { // this by some coincidence is the third
String result = "";
StringTokenizer stapler = new StringTokenizer(line);
while (stapler.hasMoreTokens()) {
String token = stapler.nextToken();
String changedToken = "";
for (int i = 0; i < token.length(); i++) {
changedToken = token.charAt(i) + changedToken;
}
result = result + changedToken + " ";
}
return result;
}
static String no_change(String line) { // that's the first, the simplest, last one to mention
return line;
}
}
Fri May 28
Thu May 27
|
|
| Hye Won Ko | Kyung Tae Ha |
for suggesting this exercise today during office hours:
Wed May 26equals from class String.
boolean equals(String other) {
if (this.length() != other.length())
return false;
for (int i = 0; i < this.length(); i++)
if (this.charAt(i) != other.charAt(i))
return false;
return true;
}
The basic elements of Lab Eight (overlap(s) method from two perspectives).
class Circle {
int radius;
Point center;
boolean overlaps(Circle other) {
return this.radius + other.radius > this.center.distanceTo(other.center);
}
}
class LabEight {
static boolean overlap(Circle one, Circle two) {
return one.overlaps(two);
}
}
Many thanks to
for suggesting the
![]()
Sunil Carspecken
"y" problem we discussed in class today.
The "y" problem was defined as:
why does"y" == "y"evaluate totrue(most always)but
"yes".substring(0, 1) == "y"evaluates tofalse(always)
Tue May 25
|
|
|
|
| Jim Gilbert | Amy Anderson | Jennifer Colgan | Connie Gherna |
Jim and Amy for their participation in the lecture (mostly Tue May 25).
Jennifer for the minute paper of Tue May 25.
Connie for all her labs and assignments thus far.
Tue May 25// this part was simulating a char extractor
class One {
String str;
One(String str) {
this.str = str;
}
boolean hasMoreChars() {
if (this.str.length() > 0)
return true;
else
return false;
}
String nextChar() {
String first = this.str.substring(0, 1);
String rest = this.str.substring(1);
this.str = rest;
return first;
}
}
The other was just a demo of StringTokenizer usage:
import java.util.*;
class Two {
public static void main(String[] args) {
String staples = "I am running out of time again!";
StringTokenizer stapler = new StringTokenizer(staples);
while (stapler.hasMoreTokens()) {
String token = stapler.nextToken();
System.out.println("(" + token + ")");
}
}
}
Mon May 24
Last name: Bean. First name: Mr.
Sat-Sun May 22-23Written exams (midterm and final) are closed-book, and multiple-choice (or short answer).
More quizzes to help you practice for the two exams will be posted here and in OnCourse.
The Practical Exam will be in lab, closed-book as well. (Details to be posted soon.)
A list of problems you should review for it will be posted here in a day or two.
I hope your weekend is coming along fine.
Fri May 21class Root {
public static void main(String[] args) {
ConsoleReader c = new ConsoleReader(System.in);
System.out.print("Give me the number: ");
double n = c.readDouble();
System.out.print("What's the initial guess: ");
double a = c.readDouble();
while (Math.abs(a * a - n) > 0.0000000001) {
System.out.print("Working on " + a + "improving it to ");
a = (a + n / a) / 2;
System.out.println(a);
}
System.out.println(a + " squared is " + a * a + " almost " + n);
}
}
Slides
we might use today. Here's the URL to Heron's formula from MathWorld.
Thu May 20Date: Thu, 20 May 2004 17:44:18 -0500 (EST) From: Adrian German <dgerman@cs.indiana.edu> To: A201/A597/I210 Sum 2004 Distr. List <dgerman@indiana.edu> Subject: A201 week 2 update Dear A201 Friends, Just a reminder that the First Quiz is due tomorrow in class. You need to answer in writing the five most important questions of those listed for review at the end of Chapter 3. Five (most important, and the reason why these would be the most important) would be enough---although if you want to answer more that would be good too. If you need help or have any questions please let me know. ... Adrian
Wed May 19frilled.cs.indiana.edu%ls -ld Fraction.java
-rw------- 1 dgerman faculty 668 May 19 15:18 Fraction.java
frilled.cs.indiana.edu%cat Fraction.java
class Fraction {
int num;
int den;
Fraction(int num, int den) {
this.num = num;
this.den = den;
}
String show() {
return this.num + "/" + this.den;
}
Fraction sub(Fraction theOtherOne) {
int resultingNum =
this.num * theOtherOne.den -
this.den * theOtherOne.num;
int resultingDen =
this.den * theOtherOne.den;
return new Fraction(resultingNum, resultingDen);
}
public static void main(String[] args) {
Fraction a = new Fraction(1, 2);
Fraction b = new Fraction(3, 2);
Fraction bMinusA = b.sub(a);
System.out.println(b.show() + " - " + a.show() + " = " + bMinusA.show());
}
}
frilled.cs.indiana.edu%javac Fraction.java
frilled.cs.indiana.edu%java Fraction
3/2 - 1/2 = 4/4
frilled.cs.indiana.edu%
Hopefully this will help you with Point.
Tue May 18"What's Due?" page updated as well, contains reading assignments.
In class today we solve problems:
Fifteen)
if statements (see Lecture Ten for diagrams)
Tomorrow we will likely go through all the questions of Lab Five.
Mon May 17
| Mira Stoilova | MF 2:30-3:30pm (LH201 suite) |
| Sid Stamm | T 4:40-5:40pm and W noon-1pm (LH201 suite) |
| Adrian German | MTRF 10:30-11:30am (LH201D) and
MTW 5:30-6:30pm (LH016, basement). |
Sun May 16Minor adjustments, editing to be finished by tomorrow afternoon.
Sat May 15
|
|
|
| Narine Yegian | Anthony Martin | Jonathan Zwi |
Narine came up with a quick solution for the second problem of
Assignment Two.
Anthony has solved two of the puzzles from the
first lecture in great style.
And Jonathan has asked very pertinent
questions on the syntax and semantics of programming languages.
Fri May 14I will be available today 5:30-6:30pm in Lindley 016 (basement).
Thu May 13Assignment Two posted.
Wed May 12
Tue May 11