|
Spring Semester 2005
|
A201/A597 LAB ASSIGNMENT TWO
Please work out the answers to the following questions:
dm = m * ((Math.sqrt(1 + v / c) / Math.sqrt(1 - v / c)) - 1); volume = Math.PI * r * r * h; volume = 4 * Math.PI * Math.pow(r, 3) / 3;
x1 = (-b - Math.sqrt(b * b - 4 * a * c)) / 2 * a; x2 = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 * a;
Would the same example work correctly if you used floating-point?
Give an example of a floating-point roundoff error. Would the same example work correctly if you used integers?
When using integers, you would of course need to switch to a smaller unit, such as cents instead of dollars or milliliters instead of liters.
n be an integer and x
a floating-point number. Explain the difference between
andn = (int)x;
For what values ofn = (int)Math.round(x);
x do they give the same result?
For what values of x do they give different results?
What happens if x is negative?
public class WarmUpSix {
public static void main(String[] args) {
System.out.print("This program adds two numbers.),
x = 5;
int y = 3.5;
System.out.print("The sum of " + x + " and " + y " is: ");
System.out.println(x + y)
}
}
public class WarmUpSeven {
public static void main(String[] args) {
ConsoleReader console = new ConsoleReader(System.in);
int total = 1;
System.out.println("Please enter a number:");
int x1 = Integer.parseInt(console.readLine());
total = total + x1;
System.out.println("Please enter another number:");
int x2 = Integer.parseInt(console.readLine());
total = total + x1;
double average = total / 2;
System.out.println("The average of two numbers is "
+ average);
}
}
2, 2.0, "2", and "2.0".
andx = 2; y = x + x;
s = "2"; t = s + s;
Should you always initialize every variable with zero?
Explain the advantages and disadvantages of such a strategy.
x is an int and
s is a String)
Integer.parseInt("" + x) is the same as x
"" + Integer.parseInt(s) is the same as s
s.substring(0, s.length()) is the same as s
How do you remove the first character? The last character?
That is, if n is 23456, how do you find out
2 and 6?
Do not convert the number to a string.
Hint: %, Math.log
final variable?
Can you define a final variable without supplying its value?
What are the values of the following expressions?double x = 2.5; double y = -1.5; int m = 18; int n = 4; String s = "Hello"; String t = "World";
x + n * y - (x + n) * y
m / n + m % n
5 * x - n / 5
Math.sqrt(Math.sqrt(n))
(int)Math.round(x)
(int)Math.round(x) + (int)Math.round(y)
s + t
s + n
1 - (1 - (1 - (1 - (1 - n))))
s.substring(1, 3)
s.length() + t.length()
Math.random().
Then find a way to produce random integers in the interval [a, b].
(a and b are given by the user, and a < b).
public class One
{ public static void main(String[] args)
{ Rectangle a = new Rectangle(5, 10, 20, 30);
System.out.println(a);
a.translate(15, 25);
System.out.println(a);
}
}
Put this in a file called One.java, compile and run. As the notes say, your program won't compile. What's missing?
Fix the program, compile and run it.
Your output should look like this:
Did you obtain the same output?frilled.cs.indiana.edu%java One java.awt.Rectangle[x=5,y=10,width=20,height=30] java.awt.Rectangle[x=20,y=35,width=20,height=30] frilled.cs.indiana.edu%
import java.awt.Rectangle;
public class Two
{ public static void main(String[] args)
{ Rectangle a = new Rectangle(5, 10, 20, 30);
Rectangle b = a;
a.translate(10, 10);
b.translate(5, 5);
System.out.println(a);
}
}
Place it into a file called Two.java, compile and run it. I obtain the following output:
What output do you obtain?frilled.cs.indiana.edu%java Two java.awt.Rectangle[x=25,y=30,width=20,height=30] frilled.cs.indiana.edu%
Now suppose that instead of printing a at the end we print b.
What would the output of the program be then, and why?
Same question(s) for this program:
public class Two {
public static void main(String[] args) {
int a, b;
a = 3;
b = a;
a = a + 10;
b = b + 5;
System.out.println(a);
}
}
public class Five {
public static void main(String[] args) {
String greeting = "Hello, Bill!";
greeting.toLowerCase();
System.out.println(greeting);
}
}
Please try to deduce the answer first. Then run the program to double check your answer.
You can only modify one line only.HELLO, BILL!
public class Seven {
public static void main(String[] args) {
int one = 2;
int two = 3;
int two = two + one;
System.out.println(two);
}
}
public class Eight {
public static void main(String[] args) {
int n;
int m;
n = 2;
m = m + 2;
System.out.println(m);
}
}
|
(Giving change) Implement a program that directs
a cashier how to give change. The program has two inputs:
Hint:
| |
|
Here's a sample run with your program: frilled.cs.indiana.edu%java Six Type the amount due then press enter. 3.72 Type the amount received then press enter. 5 Give 1.28 in change as follows: 5 quarters 0 dimes 0 nickels 3 cents frilled.cs.indiana.edu%java Six Type the amount due then press enter. 0.08 Type the amount received then press enter. 0.5 Give 0.42 in change as follows: 1 quarters 1 dimes 1 nickels 2 cents frilled.cs.indiana.edu% |
Next time be ready to discuss the answers to all these questions.
Thu Jan 20 08:16:57 EST 2005