Examine the following program: class Exercise1 { public static void main (String[] args ) { int[] val = {0, 1, 2, 3}; sum = System.out.println(
Examine the following program: class Exercise1
{
public static void main (String[] args )
{
int[] val = {0, 1, 2, 3}; sum =
System.out.println( "Sum of all numbers = " + sum );
}
}
Complete the assignment statement so that it computes the sum of the numbers in the array.
Exercise 2 --- Two Arrays
Examine the following program: class Exercise2
{
public static void main ( String[] args )
{
int[] val = {13, -4, 82, 17};
int[] twice;
System.out.println( "Original Array: "
+ val[0] + " " + val[1] + " " + val[2] + " " + val[3] );
// Construct an array object for twice.
// Put values in twice that are twice the
// corresponding values in val.
System.out.println( "New Array: "
+ twice[0] + " " + twice[1] + " " + twice[2] + " " + twice[3] );
}
}
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.
Exercise 3 --- Three Arrays
Examine the following program: 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};
// Add values from corresponding slots of valA and valB
// and put the result in the corresponding slot of sum.
System.out.println( "sum: "
+ sum[0] + " " + sum[1] + " " + sum[2] + " " + sum[3] );
}
}
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.
Exercise 4 --- Same Sum
Examine the following program: class Exercise4
{
public static void main ( String[] args )
{
int[]valA = { 13, -22, 82,17};
int[]valB ={0, 0, 0, 0};
// Put values into valB so that the sum of the values
// in corresponding slots of valA and valB is 25.
System.out.println( "valA: "
+ valA[0] + " " + valA[1] + " " + valA[2] + " " + valA[3] );
System.out.println( "valB: "
+ valB[0] + " " + valB[1] + " " + valB[2] + " " + valB[3] );
System.out.println( "sum: "
+ (valA[0]+valB[0]) + " " + (valA[1]+valB[1]) + " "
+ (valA[2]+valB[2]) + " " + (valA[3]+valB[3]) );
}
}
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.
Exercise 5 --- Reverse Order
Examine the following program: class Exercise5
{
public static void main ( String[] args )
{
int[] val = {0, 1, 2, 3}; int temp;
System.out.println( "Original Array: "
+ val[0] + " " + val[1] + " " + val[2] + " " + val[3] );
// reverse the order of the numbers in the array
System.out.println( "Reversed Array: "
+ val[0] + " " + val[1] + " " + val[2] + " " + val[3] );
}
}
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.
Exercise 6 --- Sum of Even, Odd, and All Elements
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
{
public static void main ( String[] args ) throws IOException
{
int[] data = {3, 2, 5, 7, 9, 12, 97, 24, 54};
// declare and initialize three sums
// compute the sums
for ( int index=0; index < data.length; index++)
{
}
// write out the three sums System.out.println();
}
}
Exercise 7 --- Two Largest Elements
Complete the following program so that it computes and writes out the two largest elements in the array.
import java.io.* ; class TwoLargest
{
public static void main ( String[] args ) throws IOException
{
int[] data = {3, 1, 5, 7, 4, 12, -3, 8, -2};
// declare and initialize variables for the two largest
// compute the two largest
for ( int index= ; index < data.length; index++)
{
}
// write out the two largest System.out.println();
}
}
Exercise 8 --- Reversal of Elements
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.* ; classReverserVersion1
{
public static void main ( String[] args ) throws IOException
{
int[] data = {1,2,3,4,5,6,7,8,9,10,11,12,13,14};
// reverse the data
for ( int j=0; j < be careful here; j++)
{
}
// write out the new data
for ( int j=0; j < data.length; j++)
{
}
}
}
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.* ; classReverserVersion2
{
public static void main ( String[] args ) throws IOException
{
int[] data = {1,2,3,4,5,6,7,8,9,10,11,12,13,14};
int[] result =
// copy the data in reversed order to result for ( int j=0; j < be careful here; j++)
{
}
// write out the result
for ( int j=0; j < result.length; j++)
{
}
}
}
Exercise 9 --- Signal Smoother
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
{
public static void main ( String[] args ) throws IOException
{
int[] signal = {5, 5, 4, 5, 6, 6, 7, 6, 5, 4, 1, 4};
int[] smooth
// compute the smoothed value for each
// slot of the array smooth smooth[0] =
smooth[ signal.length-1 ] = for ()
{
}
// write out the input
for ( int j=0; j < smooth.length; j++)
{
}
// write out the result
for ( int j=0; j < smooth.length; j++)
{
}
}
}
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:>
Click hereto go back to the mainmenu.
Exercise 10 --- Data Tweeker(long)
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
Unfortunately, your pH meter is known to sometimes produce false readings. So you decide to disregard the reading that is most distant from the average.
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.
Here is a run of the program:
C:>java CoffeeAverage
data[ 1 ] =6.2
data[ 2 ] =6.0
data[ 3 ] =5.5
data[ 4 ] =5.7
data[ 5 ] =6.1
data[ 6 ] =7.4
data[ 7 ] =5.5
data[ 8 ] =5.5
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
Exercise 11 --- Sum of All Array Elements
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
{
public static void main ( String[] args ) throws IOException
{
int[] data = { {3, 2, 5},
{1, 4, 4, 8, 13},
{9, 1, 0, 2},
{0, 2, 6, 3, -1, -8} };
// declare the sum
// compute the sum
for ( int row=0; row < data.length; row++)
{
for ( int col=0; col < ???; col++)
{
}
}
// write out the sum System.out.println();
}
}
Exercise 12 --- Sum of Each Row
Complete the following program so that it computes the sum of the elements in each row.
import java.io.* ;
class RowSums
{
public static void main ( String[] args ) throws IOException
{
int[] data = { {3, 2, 5},
{1, 4, 4, 8, 13},
{9, 1, 0, 2},
{0, 2, 6, 3, -1, -8} };
// declare the sum
// compute the sums for each row
for ( int row=0; row < data.length; row++)
{
// initialize the sum
// compute the sum for this row for ( int col=0; col < ???; col++)
{
}
// write the sum for this row System.out.println();
}
}
}
Exercise 13 --- Sum of Each Column
Modify the program so that it computes and prints the sum of each column of the array.
Exercise 14 --- Maximum and Minimum Elements
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
{
public static void main ( String[] args ) throws IOException
{
int[] data = { {3, 2, 5},
{1, 4, 4, 8, 13},
{9, 1, 0, 2},
{0, 2, 6, 3, -1, -8} };
// declare the max and the min
// compute the sum
for ( int row=0; row < data.length; row++)
{
for ( int col=0; col < ???; col++)
{
}
}
// write out the results System.out.println();
}
}
Exercise 15 --- Largest Elements
Modify the program so that it computes and prints the largest element in each row.
Exercise 16 --- Reversal of Elements 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.
Exercise 17 --- Image Smoother (long)
A gray-level imageis 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:
image[r-1][c-1], image[r-1][c ], image[r-1][c+1],
image[r ][c-1], image[r ][c ], image[r][c+1],
image[r+1][c-1], image[r+1][c ], image[r+1][c+1].
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
{
public static void main ( String[] args ) throws IOException
{
int[][] image = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0}};
// assume a rectangular image
int[][] smooth = new int[ image.length ][ image[0].length ];
// Compute the smoothed valuefor
// non-edge locations in theimage.
for ( int row=1; row
{
for ( int col=1; col
{
}
smooth[row][col] = sum/9;
}
// write out the input
// write out the result
}
}
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:
- Easy (shown above): Leave all the edge locations in the smoothed imageto zero. Only inside locations get an averaged value from theimage.
- Harder: Copy values at edge locations directly to the smoothed imagewithout change.
- Hard: For each location in the image, average together only those of thenine values that exist. This calls for some fairly tricky if statements, or a tricky set of for statements inside the outertwo.
- 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 enlargedarray. 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.
Here is a sample run of the hard solution:
C:>java ImageSmooth Input:
0 0 0 0 0 0 0 0 0 0 00
0 0 0 0 0 0 0 0 0 0 00
0 0 5 5 5 5 5 5 5 5 00
0 0 5 5 5 5 5 5 5 5 00
0 0 5 5 5 5 5 5 5 5 00
0 0 5 5 5 5 5 5 5 5 00
0 0 5 5 5 5 5 5 5 5 00
0 0 5 5 5 5 5 5 5 5 00
0 0 5 5 5 5 5 5 5 5 00
0 0 5 5 5 5 5 5 5 5 00
0 0 0 0 0 0 0 0 0 0 00
0 0 0 0 0 0 0 0 0 0 00
Output:
0 0 0 0 0 0 0 0 0 0 00
0 0 1 1 1 1 1 1 1 1 00
0 1 2 3 3 3 3 3 3 2 10
0 1 3 5 5 5 5 5 5 3 10
0 1 3 5 5 5 5 5 5 3 10
0 1 3 5 5 5 5 5 5 3 10
0 1 3 5 5 5 5 5 5 3 10
0 1 3 5 5 5 5 5 5 3 10
0 1 3 5 5 5 5 5 5 3 10
0 1 2 3 3 3 3 3 3 2 10
0 0 1 1 1 1 1 1 1 1 00
0 0 0 0 0 0 0 0 0 0 00
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 tocreate some interesting input files, or create input files by doing the nextexercise.
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.)
Exercise 18 --- Image Creator (short)
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. Writeone value per line, without spaces orcommas.
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 loopwith a one line loop body.
Exercise 19 --- Image Display (medium length)
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 jpegare 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.
Step by Step Solution
3.43 Rating (156 Votes )
There are 3 Steps involved in it
Step: 1
Lets go through each exercise one by one and complete them Exercise 1 class Exercise1 public static void mainString args int val 0 1 2 3 int sum 0 for int num val sum num SystemoutprintlnSum of all nu...See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started