It Object Exercise 2
It Object Exercise 2
It Object Exercise 2
class Exercise1
{
public static void main ( String[] args )
{
int[] val = {0, 1, 2, 3};
sum =
}
}
Complete the assignment statement so that it computes the sum of the numbers in
the array.
class Exercise2
{
public static void main ( String[] args )
{
int[] val = {13, -4, 82, 17};
int[] twice;
Complete the program so that a new array twice is constructed. Now copy values
from val to twice, but make the values in twice double what they are in val.
class Exercise3
{
public static void main ( String[] args )
{
int[] valA = { 13, -22, 82, 17};
int[] valB = {-12, 24, -79, -13};
int[] sum = { 0, 0, 0, 0};
Complete the program with four assignment statements so that each slot of sum
contains the sum of the corresponding slots in valA and valb. Ie., add slot zero of
valA to slot zero of valB and put the result in slot zero of sum, and so on.
class Exercise4
{
public static void main ( String[] args )
{
int[] valA = { 13, -22, 82, 17};
int[] valB = { 0, 0, 0, 0};
Complete the program with four assignment statements that put values into valB so
that the sum of corresponding slots in valA and valB is 25.
class Exercise5
{
public static void main ( String[] args )
{
int[] val = {0, 1, 2, 3};
int temp;
Complete the program so that the numbers in the array appear in reversed order.
You will need to use the variable temp to do this.
Complete the following program so that it computes the sum of all the elements of
the array, the sum of the even elements, and the sum of the odd elements. Assume
that all the numbers are zero or positive. Even integers are those for which N%2 is
zero.
import java.io.* ;
class ThreeSums
{
{
}
}
}
Complete the following program so that it computes and writes out the two largest
elements in the array.
import java.io.* ;
class TwoLargest
{
}
}
Complete the following program so that it reverses the order of the values in data,
then prints it out.
In the first version of the program there is only one array and its values are reversed
with some fairly tricky programming.
import java.io.* ;
class ReverserVersion1
{
}
}
Now write another program that uses two arrays. The first array data is not changed.
The second array result gets the elements of data in reversed order.
import java.io.* ;
class ReverserVersion2
{
}
}
An audio signal is sometimes stored as a list of int values. The values represent the
intensity of the signal at successive time intervals. Of course, in a program the signal
is represented with an array.
Often a small amount of noise is included in the signal. Noise is usually small,
momentary changes in the signal level. An example is the "static" that is heard in
addition to the signal in AM radio.
Smoothing a signal removes some of the noise and improves the perceptual quality
of the signal. This exercise is to smooth the values in an integer array.
Say that the original values are in the array "signal". Compute the smoothed array
by doing this: Each value smooth[N] is the average of three values: signal[N-1],
signal[N], and signal[N+1].
For the first element of smooth, average the first two elements of signal. For the last
element of smooth, average the last two elements of signal.
Use integer arithmetic for this so that the values in smooth are integers.
import java.io.* ;
class Smooth
{
}
}
In interpretting the results, remember that integer division discards the remainder. It
does not compute a rounded value. Here is a sample run of the program:
C:\>java Smooth
signal: 1 5 4 5 7 6 8 6 5 4 5 4
smooth: 3 3 4 5 6 7 6 6 5 4 4 4
C:\>
Say that you are interested in computing the average acid level of coffee as served
by coffee shops in your home town. You visit many coffee shops and dip your pH
meter into samples of coffee. You record your results in a text file such as the
following. The first line of the file gives the number of values that follow.
13
5.6
6.2
6.0
5.5
5.7
6.1
7.4
5.5
5.5
6.3
6.4
4.0
6.9
Create a text file containing the above or similar data. Now write a program that
reads the data into an array (use input redirection as explained in Chapter 22).
Compute the average of all the data. Now scan through the array to find the value
that is farthest (in either direction) from the average. Set this value to -1 to show
that it is not to be included. Compute and print the new average.
data[ 9 ] = 6.3
data[ 10 ] = 6.4
data[ 11 ] = 4.0
data[ 12 ] = 6.9
average: 5.930769230769231
most distant value: 4.0
new average: 5.6230769230769235
Complete the following program so that it computes the sum of all the elements of
the array. Write the program so that it works even if the dimensions of the rows and
columns are changed. In other words, use length rather than hard-coded numbers.)
import java.io.* ;
class ArraySum
{
}
}
}
}
Complete the following program so that it computes the sum of the elements in each
row.
import java.io.* ;
class RowSums
{
Modify the program so that it computes and prints the sum of each column of the
array.
Complete the following program so that it computes the maximum and minimun of
the elements in the array. Write the program so that it works even if the dimensions
of the rows and columns are changed. In other words, use length rather than hard-
coded numbers.)
import java.io.* ;
class ArrayMaxMin
{
{9, 1, 0, 2},
{0, 2, 6, 3, -1, -8} };
}
}
}
}
Modify the program so that it computes and prints the largest element in each row.
Write a program that reverses the order of the elements in each row of the matrix,
then prints out the resulting matrix.
A gray-level image is sometimes stored as a list of int values. The values represent
the intensity of light at discrete positions in the image.
An image may be smoothed by replacing each element with the average of the
element's neighboring elements.
Say that the original values are in the 2D array "image". Compute the smoothed
array by doing this: Each value smooth[r][c] is the average of nine values:
Assume that the image is rectangular, that is, all rows have the same number of
locations. Use integer arithmetic for this so that the values in smooth are integers.
import java.io.* ;
class Smooth
{
}
smooth[row][col] = sum/9;
}
}
}
The edges of the image are a problem because only some of the nine values that go
into the average exist. There are various ways to deal with this problem:
1. Easy (shown above): Leave all the edge locations in the smoothed image to
zero. Only inside locations get an averaged value from the image.
2. Harder: Copy values at edge locations directly to the smoothed image without
change.
3. Hard: For each location in the image, average together only those of the nine
values that exist. This calls for some fairly tricky if statements, or a tricky set
of for statements inside the outer two.
4. Alternate: Copy the original image into the center of an enlarged array that
has two more columns and two more rows than the original. Create another
enlarged array to hold a temporary smoothed version of the enlarged array.
Now, apply the Easy solution to the enlarged array with the other enlarged
array as the result. Next, copy the center of the result to the final smooth
image.
C:\>java ImageSmooth
Input:
0000 0 0 0 0 0 0 0 0
0000 0 0 0 0 0 0 0 0
0055 5 5 5 5 5 5 0 0
0055 5 5 5 5 5 5 0 0
0055 5 5 5 5 5 5 0 0
0055 5 5 5 5 5 5 0 0
0055 5 5 5 5 5 5 0 0
0055 5 5 5 5 5 5 0 0
0055 5 5 5 5 5 5 0 0
0055 5 5 5 5 5 5 0 0
0000 0 0 0 0 0 0 0 0
0000 0 0 0 0 0 0 0 0
Output:
00000 0 0 0 0 0 0 0
00111 1 1 1 1 1 0 0
01233 3 3 3 3 2 1 0
01355 5 5 5 5 3 1 0
01355 5 5 5 5 3 1 0
01355 5 5 5 5 3 1 0
01355 5 5 5 5 3 1 0
01355 5 5 5 5 3 1 0
01355 5 5 5 5 3 1 0
01233 3 3 3 3 2 1 0
00111 1 1 1 1 1 0 0
00000 0 0 0 0 0 0 0
C:\>
Once you have the program working with hard-coded data, modify it so that it reads
its data using input redirection (Chapter 22). Use your programming editor to create
some interesting input files, or create input files by doing the next exercise.
Further modify the program so that it writes out only the smoothed image, as text,
one integer per line. This output can be redirected to a text file and then used as
input for the image display program (see the second following exercise.)
This program uses no arrays. It is put here becase it is useful for use with some of
the other programs of this section.
Write a program that creates a 64 by 64 image as a text file. The image will consist
of eight bands of increasingly higher values. Think of the image as 64 rows of 64
integers each, but actually write out one integer per line. This is for the convenience
of the programs the input the image.
The image starts out with 8 rows of zero, so the program writes 8 times 64 zeros (as
character '0'). Next, the image has 8 rows of eight, so the program writes 8 times 64
eights (as character '8'). Next, the image has 8 rows of sixteen, and so on. Write one
value per line, without spaces or commas.
Use output redirection to send the output to a file. Use this file as input for you
image smoother (previous exercise), or for the image display program (next
exercise).
This is a very short program. The body consists of three lines: a double for loop with
a one line loop body.
It would be nice to display your images by some means other than printing out
integers. Ideally, your programs should read and write images using standard image
formats. But actual image formats, like giff, tiff, and jpeg are very complicated.
Instead, write a program that displays an image by using rows of characters to
represent brightness levels.
Write a program that reads in a file that contains one integer per line. Each integer
represents one location in the image. Assume that there are 64 rows and 64 columns
in the image. Assume that the integers are in the range 0 to 63.
For each integer, write out a single character depending on its value:
• 0 to 7: space
• 8 to 15: .
• 16 to 23: ,
• 24 to 31: -
• 32 to 39: +
• 40 to 47: o
• 48 to 55: O
• above 55: X
Don't put extra spaces between characters. Start a new line after each row of 128
characters. This is one place where the switch statement is convenient. To use it,
divide the input value by 8 to get the integer for the switch. Or you can use eight if-
else statements if you prefer.