1.
Exercise
12 on page 497. Write the method priceIsRight
as specified and a main program that creates the array bids as
specified in the
problem, and calls the method with bids and 280, and prints
out the answer. Use
Java file PriceRight.java.
The
program consists of one static method named priceIsRight
and the main method. The method priceIsRight
has two
arguments, an int array of bids
and the correct price.
It has an int return value which
returns the winning
bid, for any given array of bids and correct price.
The
main method sets up the array of bids as specified and calls
the method as
shown in the problem. It picks up the return value in an int
variable, and then prints out results as shown below.
This
program takes no input from the user. It can be run by “java PriceRight” after being compiled with
“javac
PriceRight” or compiled inside Dr.
Java. It should
output the array and the winning bid, like this:
The
bids are: 200, 300, 250, 1, 950, 40
The
winning bid is 250.
2. Project 1 on page. 498. Call your program AddBigInts. Use a method for the addition. Note that a method can return an array as its return value.
The
program contains a static method named addBigInts
(or
whatever you want to call it) and the main method. The method
addBigInts has two arguments, each
an int
array to hold the digits of the number. It returns an int
array, or, if you prefer, has a third array argument for the
answer.
The
main method sets up two arrays of ints
from numbers
input from the user, say x[] and
y[].First ask the
user for the number of digits, then the digits themselves.
Then have main call
the method
like
this int[] sum = addBigInts(x,y),
or addBigInts(x, y, sum) if
you want to use an argument for the sum. Then the two numbers
and their sum is printed out, as
shown below for an example.
To
get single digits from the user, modify getGuess
and getInt from NumberGuess3, pg.
355, to insist on numbers
between 0 and 9. But get rid of “guess” in your code—these are
not guesses!
Thus
the program should have three static methods, addBigInts,
getDigit, and getInt, as
well as main.
The
program can be run by “java AddBigInts”
after being
compiled with “javac AddBigInts”
or compiled inside Dr. Java.
Welcome to the big
integer adder
How many digits?
4
First number:
Digit 3: 2
Digit 2: 3
Digit 1: 4
Digit 0: 5
Second number:
Digit 3: 1
Digit 2: 1
Digit 1: 1
Digit 0: 9
First: 2345
Second: 9119
Sum: 11464
Of course these are not large numbers, but this approach scales up. Note how the 9 + 5 step yields 14, which puts 4 in the answer and carries 1 to the next digit.
3. Consider PointMain.java on pp. 530-531. You can download it from the authors’ website (directory Point_5_fancy) using a link on the class web page. Modify PointMain to use JDK Points by adding “import java.awt.*” at the top of the program. Delete the lines that call method “distanceFrom Origin”. Modfiy PointMain to print out the two points like this, using their values, so that other points would also work.
Side Notes, not expected in output:
9
|
|
60
spaces between |s, 3 for each spot
8
|
|
7
|
|
6
|
|
5
|
|
4
|
|
3
|
o
|
p2
is at (4,3)
2
|
x
| p1
is at (7,2)
1
|
|
0
|
|
0 1
2 3 4 5 6 7 8
9 (2
spaces between digits, x axis)
Change the code to p1.translate(1,1);
p2.translate(-1,-1); and print it out again:
9
|
|
8
|
|
7
|
|
6
|
|
5
|
|
4
|
o
|
3
|
|
2
|
|
1
|
x
|
0
|
|
0 1
2 3 4 5 6 7 8 9
Note: these points are not plotted correctly--you figure out
where they should be.
Note:
it’s best to do the printing in a method that you call twice,
but not required
here. That’s the next problem.
4.
Generalize your code from
problem
3 to write method void printPoints(Point[]
points) that prints
up to 5 points on the same kind of text grid using symbols x,
o, +, *, and #,
which you set up in a String array so you can iterate through
them.
Rewrite PointMain into
Plotter.java for this.
If the array has more than 5 points, ignore the extra points.