CS 1063, Fall 2005
|
This page will provide extra examples and other information not given in the lectures. Some of these examples use features of Java not yet covered in the course.
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Type an integer and then 'Return' ---> ");
int quantity = in.nextInt();
System.out.println("You typed: " + quantity);
}
}
In case this program doesn't work or you are not using Java 1.5, you can use the following alternative ways to input numbers in Java: Java Input without 1.5.
This lab is perhaps a little confusing. Here are some hints:
You need to study the directions for this lab carefully. The lab also requires some use of strings, as well as if-else statements. Here are some hints, and a preliminary design that we worked out in class on Friday:
String s1 = "Hello"; // number chars 0, 1, 2, 3, 4
System.out.println(s1.length()); // prints 5
You must not use ==, but instead you must use one of the methods equals or compareTo. So to compare strings s1 and s2 for equality:
if (s1.equals(s2)) ... // check if equal
if (s2.equals(s1)) ... // same thing
if (s1.equals(s2) == true) ... // same thing
if (s1.compareTo(s2) == 0) ... // need the == 0
if (s2.compareTo(s1) == 0) ... // same thing
To check if strings s1 and s2 are NOT equal:
if (!s1.equals(s2)) ... // check if equal
if (!s2.equals(s1)) ... // same thing
if (s1.equals(s2) == false) ... // same thing
if (s1.equals(s2) != true) ... // same thing
if (s1.compareTo(s2) != 0) ... // need the != 0
if (s2.compareTo(s1) != 0) ... // same thing
if (!(s2.compareTo(s1) == 0)) ... // same thing
String s2 = "abcdefg"; // number chars 0, 1, 2, 3, 4, 5, 6
System.out.println(s2.length()); // prints 7
System.out.println(s2.substring(3, 6)); // prints: def
System.out.println(s2.substring(1)); // prints: bcdefg (drops first char)
System.out.println(s2.substring(2)); // prints: cdefg (drops first 2 chars)
System.out.println(s2.substring(3)); // prints: defg (drops first 3 chars)
Then try the same 4 items again, except use setPosition() 3 times with "A", "B", "C", so that this time you can check that the lock did open.
The basis for this lab is a certain "magic formula" that computes square roots. (The formula comes from calculus, but it's plausible without knowing calculus, and you can just use it without understanding it.)
Here's the idea: we start with a guess for the root, and then we produce a newer, better guess, using the old guess. Call the old and new guesses for the root rold and rnew. Then here is a special case of the magic formula, just for the square root of two:
rnew = rold/2.0 + 1.0/rold;
Now suppose you execute the magic formula twice, starting with rold = 1 as the initial guess. (Try it!). The result is the same after the second try, because rold is still the same. What we need is to make the new guess be the old guess and try again. So we generate a sequence of statements like the following:
rold = 1.0;
rnew = rold/2.0 + 1.0/rold;
System.out.println("rnew: " + rnew); // prints: rnew: 1.5
rold = rnew;
rnew = rold/2.0 + 1.0/rold;
System.out.println("rnew: " + rnew); // prints: rnew: 1.4166666666666665
rold = rnew;
rnew = rold/2.0 + 1.0/rold;
System.out.println("rnew: " + rnew); // prints: rnew: 1.4142156862745097
rold = rnew;
rnew = rold/2.0 + 1.0/rold;
System.out.println("rnew: " + rnew); // prints: rnew: 1.4142135623746899
rold = rnew;
rnew = rold/2.0 + 1.0/rold;
System.out.println("rnew: " + rnew); // prints: rnew: 1.414213562373095
The digits in red above inside the comments are correct, so we end up with a very accurate value for the square root of 2. The code above keeps repeating the same three statements over and over, so it almost cries out to be put into a loop. You keep executing as long as the new guess squared and two are not "too close", using some measure such as 0.00001.
There is quite a bit more to the lab, but this gives you an idea how it is working.
Be careful with the basic calculation, illustrated in the lab write up with:
5/15 * 150000 == 50000
If you were just working with integers, you could write this as:
5 * 150000 / 15 == 50000
since otherwise, 5/15 would be 0. Alternatively, you could do the calculation as all doubles and then cast to an int.
OriginalCost: 200000, FinalValue: 50000, YearsDepreciated: 5 Year Depreciation totalDepreciated depreciatedValue 1 50000 50000 150000 2 40000 90000 110000 3 30000 120000 80000 4 20000 140000 60000 5 10000 150000 50000
Here is another possible chart with original cost of 50000, final value of 10000, and depreciated over 4 years.
OriginalCost: 50000, FinalValue: 10000, YearsDepreciated: 4 Year Depreciation totalDepreciated depreciatedValue 1 16000 16000 34000 2 12000 28000 22000 3 8000 36000 14000 4 4000 40000 10000
Here is another possible chart with original cost of 60000, final value of 0, and depreciated over 5 years.
OriginalCost: 60000, FinalValue: 0, YearsDepreciated: 5 Year Depreciation totalDepreciated depreciatedValue 1 20000 20000 40000 2 16000 36000 24000 3 12000 48000 12000 4 8000 56000 4000 5 4000 60000 0
Here is another possible chart with original cost of 1000000, final value of 200000, and depreciated over 10 years. In this example, why do you think that the final depreciated value is 200005, instead of exactly 200000.
OriginalCost: 1000000, FinalValue: 200000, YearsDepreciated: 10 Year Depreciation totalDepreciated depreciatedValue 1 145454 145454 854546 2 130909 276363 723637 3 116363 392726 607274 4 101818 494544 505456 5 87272 581816 418184 6 72727 654543 345457 7 58181 712724 287276 8 43636 756360 243640 9 29090 785450 214550 10 14545 799995 200005
Activity 2 in the Week 15 lectures uses static methods, and your methods in the Series class of Project 3 should also be static.
We often use static methods when only one instance of the method is needed. (There is no need for more than one sqrt method.)
private static double fact(double x){
double result = 1;
for(int k = 2; k <= x; k++){
result = result * k;
}
return result;
}This can be declared private because it is only used inside Series. It must be declared static because it is used without creating an instance of the Series class.