|
Spring Semester 2003 |
It will be constantly updated and adjusted in the meantime.
Please solve the following problems:
This was done in class on Tuesday, April 8.
It also appears in Lecture Notes Thirteen.
A solution for the Game of Nim appears in Lecture Notes Fourteen.
class ExpApproximator {
double term = 1, sum = 1;
int n = 0;
double x;
ExpApproximator(double x) { this.x = x; }
boolean keepGoing() {
if (Math.abs(term) < 0.00001) return false;
return true;
}
void calculate() {
n += 1;
term = term * x / n;
sum += term;
this.report();
}
void report() { System.out.println(sum); }
public static void main(String[] args) {
ConsoleReader c = new ConsoleReader(System.in);
System.out.print("What's the exponent? ");
double exp = c.readDouble();
ExpApproximator a = new ExpApproximator(exp);
while (a.keepGoing()) {
a.calculate();
}
a.report();
System.out.println("Proof: " + Math.pow(Math.E, exp));
}
}
Here's a solution worked out Monday night:
class Newton_Raphson {
public static void main(String[] args) {
ConsoleReader c = new ConsoleReader(System.in);
System.out.print("What's the number? ");
double a = c.readDouble();
System.out.print("What's the order of the root? ");
double n = c.readDouble();
double xOld = 1;
while (Math.abs(a - Math.pow(xOld, n)) > 0.01) {
xOld = xOld - (Math.pow(xOld, n) - a) /
(n * Math.pow(xOld, n - 1));
System.out.println(xOld);
}
System.out.println("Proof: " + Math.pow(xOld, n));
}
}
Check out the Wizard defined in these
notes.
Here's a simplified version of it:
class Wizard {
int countFactors(int number) {
int count = 0;
for (int i = 2; number > 1; i++) {
while (number % i == 0 ) {
count += 1;
System.out.println(i);
number = number / i;
}
}
return count;
}
public static void main(String[] args) {
Wizard a = new Wizard();
a.countFactors(Integer.parseInt(args[0]));
}
}
Here's a solution to this problem:
import java.applet.*;
import java.awt.*;
import javax.swing.JOptionPane;
/*
<applet code="One.class" width=300 height=300>
</applet>
*/
public class One extends Applet {
int number;
Circle[] circles;
public void init() {
this.number = Integer.parseInt(
JOptionPane.showInputDialog("How many? "));
this.circles = new Circle[number];
for (int i = 0; i < circles.length; i++)
circles[i] = new Circle(
(int)(Math.random() * 200 + 50),
(int)(Math.random() * 200 + 50),
(int)(Math.random() * 25 + 25),
new Color((float)Math.random(),
(float)Math.random(),
(float)Math.random()),
new Color((float)Math.random(),
(float)Math.random(),
(float)Math.random())
);
}
public void paint(Graphics g) {
for (int i = 0; i < circles.length; i++)
this.circles[i].show(g);
}
}
class Circle {
int x, y;
int radius;
Color color;
Color border;
Circle(int x, int y, int r, Color c, Color b) {
this.x = x; this.y = y;
this.radius = r;
this.color = c; this.border = b;
}
void show(Graphics g) {
g.setColor(color);
g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
g.setColor(border);
g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
}
}
Here's a solution to this problem:
import java.util.*;
class Average {
public static void main(String[] args) {
Calculator calc = new Calculator();
ConsoleReader c = new ConsoleReader(System.in);
System.out.print("Type the numbers: ");
String line = c.readLine();
calc.process(line);
}
}
class Calculator {
void process(String line) {
StringTokenizer st = new StringTokenizer(line);
double sum = 0, sumSq = 0, n = 0;
while (st.hasMoreTokens()) {
int x_i = Integer.parseInt(st.nextToken());
sum += x_i;
sumSq += x_i * x_i;
n += 1;
}
System.out.println(n + " numbers.");
System.out.println("Average is: " + sum / n);
System.out.println("Std. deviation: " +
Math.sqrt( (sumSq - sum * sum / n) / (n - 1)));
}
}
The basic for loop is given in Lecture Notes Thirteen.
for (int i = 3; i <= n; i++) {
fNew = fOld + fOlder;
fOlder = fOld;
fOld = fNew;
}
This also appears in Lecture Notes Thirteen.
This problem is listed here as problem 14.
A solution to it could be found in this document.
It's called Fourteen.java and both links are off Lecture Notes Ten.
This is Problem 10 in this set of programs.
Solutions (Ten.java) are posted
here
(off Lecture Notes Ten).
Here's a more general solution:
class GradeConverter {
String letter(double number) {
double[] values = { 0, 0.7, 1, 1.3, 1.7, 2, 2.3, 2.7, 3, 3.3, 3.7, 4};
String[] letters = {"F", "D-", "D", "D+", "C-", "C", "C+", "B-", "B", "B+", "A-","A"};
for (int i = 0; i < values.length; i++) {
if (values[i] > number) {
double average = (values[i] + values[i-1]) / 2;
if (number >= average) return letters[i];
else return letters[i-1];
}
}
return "I'm sorry!... Not a valid input.";
}
public static void main(String[] args) {
GradeConverter a = new GradeConverter();
for (int i = 0; i < 10; i++) {
double number = Math.random() * 4;
System.out.println(number + " = " + a.letter(number));
}
double number = 3.85;
System.out.println(number + " = " + a.letter(number));
}
}
But also see 5.7 below.
Both problems appear in here.
They are called Seven and Eight.
IntersectionApplet.java)
ColorApplet.java)
CarApplet.java)
That's reverse in Lab Three but here's more general solution(s):
class DigitExtractor {
int theNumber;
int index;
String theCopy;
DigitExtractor (int aNumber) {
this.theNumber = aNumber;
this.theCopy = this.theNumber + "";
this.index = this.theCopy.length();
}
int nextDigit() {
this.index -= 1;
return theCopy.charAt(this.index) - '0';
}
boolean hasMoreDigits() {
if (index > 0) return true;
else return false;
}
public static void main(String[] args) {
DigitExtractor d = new DigitExtractor(123456789);
while (d.hasMoreDigits()) {
System.out.println(d.nextDigit());
}
}
}
class DigitExtractorTwo {
int theNumber;
DigitExtractorTwo (int aNumber) {
this.theNumber = aNumber;
}
int nextDigit() {
int digit = this.theNumber % 10;
this.theNumber = this.theNumber / 10;
return digit;
}
boolean hasMoreDigits() {
if (this.theNumber > 0) return true;
else return false;
}
public static void main(String[] args) {
DigitExtractorTwo d = new DigitExtractorTwo(987654321);
while (d.hasMoreDigits()) {
System.out.println(d.nextDigit());
}
}
}
A similar approach is used in Lecture Notes Thirteen (Four.java).
A very similar problem is listed here with number 6.
A solution to this problem could be found in this document.
Both are links listed under Lab Notes Two (Six.java)
This, essentially, is your Lab Four.
Purse.java)
RootApproximator.java
Enough said.
Here's a solution:
class Tigger {
String x, y;
Tigger(int x, int y) {
this.x = x + "";
this.y = y + "";
}
void bounce() {
int a = calculate(x),
b = calculate(y);
this.x = a + "";
this.y = b + "";
}
int calculate(String a) {
int sum = 0;
for (int i = 0; i < a.length(); i++) {
sum += (a.charAt(i) - '0') * (a.charAt(i) - '0');
}
return sum;
}
String report() {
String
x = " " + this.x,
y = " " + this.y;
return
"Tigger just bounced to (" +
x.substring(x.length() - 3) + ", "
+ y.substring(y.length() - 3) + ")";
}
}
class One {
public static void main(String[] args) {
Player bonaparte, wellington;
bonaparte = new Player();
wellington = new Player();
System.out.println("Let the game begin!");
bonaparte.makeGuess();
wellington.makeGuess();
System.out.println("The guesses have been made: ");
System.out.println(" Bonaparte has chosen .... " + bonaparte.report());
System.out.println(" Wellington has chosen ... " + wellington.report());
if (bonaparte.strongerThan(wellington))
System.out.println("Bonaparte wins!");
else if (wellington.strongerThan(bonaparte))
System.out.println("Wellington wins!");
else System.out.println("It's a draw..."); }
}
class Player {
String guess;
String makeGuess() {
int value = (int) (Math.random() * 3);
if (value == 0) this.guess = "paper";
if (value == 1) this.guess = "rock";
if (value == 2) this.guess = "scissors";
return this.guess;
}
boolean strongerThan(Player other) {
if (this.guess.equals("paper") && other.guess.equals("rock" ) ||
this.guess.equals("rock" ) && other.guess.equals("scissors") ||
this.guess.equals("scissors") && other.guess.equals("paper"))
return true;
else return false;
}
String report() {
return guess;
}
}
class One {
public static void main(String[] args) {
Line a = new Line(new Point(0, 0), new Point (1, 1));
System.out.println(a.length());
}
}
class Line {
Point a, b;
Line(Point a, Point b) {
this.a = a;
this.b = b;
}
double length() {
return (this.a).distanceTo(this.b);
}
}
class Point {
double x, y;
Point(double x, double y) {
this.x = x; this.y = y;
}
double distanceTo(Point other) {
double
dX = this.x - other.x,
dY = this.y - other.y;
return Math.sqrt(dX * dX + dY * dY);
}
}
class Oracle {
ConsoleReader c = new ConsoleReader(System.in);
void takeCall() {
System.out.println("Oracle> Hi, ask me any question...");
String question = c.readLine();
System.out.println("Oracle> That's a tough one... any words of wisdom that would apply to this?");
this.advice = this.c.readLine();
System.out.println("Oracle> Nice. Back to your question I'd say: ");
System.out.println("***(" + this.answer + ")***");
this.answer = this.advice;
}
String answer = "The answer, my friend, is in the blowing of the wind.";
String advice = " Man gave names to all the animals, in the beginning... ";
}
class One {
public static void main(String[] args) {
Elevator e = new Elevator(20);
e.up(26);
e.down(14);
e.up(10);
e.down(30);
e.up(e.currentFloor() + 3);
}
}
class Elevator {
int floor;
Elevator(int floor) {
this.floor = floor;
}
void up(int to) {
if (this.floor >= to) {
System.out.println("Sorry, from floor " + this.floor +
" we can't go up to floor " + to);
} else {
System.out.println("Elevator going up (" +
this.floor +
" --> " + to + ")");
for (int i = this.floor; i <= to; i++) {
this.floor = i;
this.report();
}
System.out.println("Elevator now on floor: " + this.floor);
}
}
void down(int to) {
if (this.floor <= to) {
System.out.println("Sorry, from floor " + this.floor +
" we can't go down to floor " + to);
} else {
System.out.println("Elevator going down: (" +
this.floor +
" --> " + to + ")");
for (int i = this.floor; i >= to; i--) {
this.floor = i;
this.report();
}
System.out.println("Elevator now on floor: " + this.floor);
}
}
void report() {
System.out.println(" The elevator is now on floor " + this.floor);
}
int currentFloor() { return this.floor; }
}
Best of luck in preparing the exam!
Let me know if you need help.
I will be posting the text for each of these problems here soon.