1、Java Exercises JAVA练习题1.5 Input and OutputExercises 1Write a program MaxMin.java that reads in integers (as many as the user enters) from standard input and prints out the maximum and minimum alues.2Modify your program from the previous exercise to insist that the integers must be positive (by promp
2、ting the user to enter positive integers whenever the value entered is not positive).3Write a program Stats.java that takes an integer N from the command line, reads N double values from standard input, and prints their mean (average value) and standard deviation (square root of the sum of the squar
3、es of their differences from the average, divided by N - 1).4Extend your program from the previous exercise to create a filter that prints all the values that are further than 1.5 standard deviations from the mean.5Write a program LongestRun.java that reads in a sequence of integers and prints out b
4、oth the integer that appears in a longest consecutive run and the length of the run. For example, if the input is 1 2 2 1 5 1 1 7 7 7 7 1 1, then your program should print Longest run: 4 consecutive 7s.6Write a filter that reads in a sequence of integers and prints out the integers, removing repeate
5、d values that appear consecutively. For example, if the input is 1 2 2 1 5 1 1 7 7 7 7 1 1 1 1 1 1 1 1 1, your program should print out 1 2 1 5 1 7 1.7Write a program that takes a command-line argument N, reads in N-1 distinct integers between 1 and N, and determines the missing value.8Write a progr
6、am GeometricMean.java that reads in positive real numbers from standard input and prints out their geometric mean. The geometric meanof N positive numbers x1, x2, ., xN is (x1 * x2 * . * xN)1/N. Write a program HarmonicMean.java that reads in positive real numbers from standard input and prints out
7、their harmonic mean. The harmonic mean of N positive numbers x1, x2, ., xN is (1/x1 + 1/x2 + . + 1/xN) / (1 / N).Hint: for the geometric mean, consider taking logs to avoid overflow.9Suppose that the file input.txt contains the two strings F and F. What does the following command do (see Exercises 1
8、.2.35)? Here is the Java program Dragon.java.java Dragon input.txt | java Dragon | java Dragon 10Write a filter TenPerLine.java that takes a sequence of integers between 0 and 99 and prints 10 integers per line, with columns aligned. Then write a program RandomIntSeq that takes two command-line argu
9、ments M and N and outputs N random integers between 0 and M-1. Test your programs with the command java RandomIntSeq 200 100 | java TenPerLine.11Write a program WordCount.java that reads in text from standard input and prints out the number of words in the text. For the purpose of this exercise, a w
10、ord is a sequence of non-whitespace characters that is surrounded by whitespace.12Write a program that reads in lines from standard input with each line containing a name and two integers and then uses printf() to print a table with a column of the names, the integers, and the result of dividing the
11、 first by the second, accurate to three decimal places. You could use a program like this to tabulate batting averages for baseball players or grades for students.13Which of the following require saving all the values from standard input (in an array, say), and which could be implemented as a filter
12、 using only a fixed number of variables? For each, the input comes from standard input and consists of N real numbers between 0 and 1.Print the maximum and minimum numbers.Print the kth smallest value.Print the sum of the squares of the numbers.Print the average of the N numbers.Print the percentage
13、 of numbers greater than the average.Print the N numbers in increasing order.Print the N numbers in random order.14Write a program that prints a table of the monthly payments, remaining principal, and interest paid for a loan, taking three numbers as command-line arguments: the number of years, the
14、principal, and the interest rate (see Exercise 1.2.24).15Write a program Closest.java that takes three command-line arguments x, y, z, reads from standard input a sequence of point coordinates (xi, yi, zi), and prints the coordinates of the point closest to (x, y, z). Recall that the square of the d
15、istance between (x, y, z) and (xi, yi, zi) is (x - xi)2 + (y - yi)2 + (z - zi)2. For efficiency, do not use Math.sqrt() or Math.pow().16Given the positions and masses of a sequence of objects, write a progrm to compute their center-of-mass or centroid. The centroid is the average position of the N o
16、bjects, weighted by msass. If the positions and masses are given by (xi, yi, mi), then the centroid (x, y, m) is given by:m = m1 + m2 + . + mNx = (m1x1 + . + mnxN) / my = (m1y1 + . + mnyN) / mWrite a program Centroid.java that reads in a sequence of positions and masses (xi, yi, mi) from standard in
17、put and prints out their center of mass (x,y, m). Hint: model your program after Average.java.17Write a program SignalAnalyzer.java that reads in a sequence of real numbers between -1 and 1 and prints out their average magnitude, average power, and the number of zero crossings. The average magnitude
18、 is the average of the absolute values of the data values. The average power is the average of the squares of the data values. The number of zero crossings is the number of times a data value transitions from a strictly negative number to a strictly positive number, or vice versa. These three statis
19、tics are widely used to analyze digital signals.18Write a program CheckerBoard.java that takes a command-line argument N and plots an N-by-N checkerboard with red and black squares. Color the lower left square red.19Write a program that takes as command-line arguments an integer N and a double value
20、 p (between 0 and 1), plots N equally spaced points of size on the circumference of a circle, and then, with probability p for each pair of points, draws a gray line connecting them.20Write code to draw hearts, spades, clubs, and diamonds. To draw a heart, draw a diamond, then attach two semicircles
21、 to the upper left and upper right sides.21Write a program Rose.java that takes a command-line argument N and plots a rose with N petals (if N is odd) or 2N petals (if N is even) by plotting the polar coordinates (r, ) of the function r = sin(N ) for ranging from 0 to 2 radians. Below is the desired
22、 output for N = 4, 7, and 8.22Write a program Banner.java that takes a string s from the command line and display it in banner style on the screen, moving from left to right and wrapping back to the beginning of the string as the end is reached. Add a second command-line argument to control the spee
23、d.23Modify PlayThatTune.java to take additional command-line arguments that control the volume (multiply each sample value by the volume) and the tempo (multiply each notes duration by the tempo).24Write a program that takes the name of a .wav file and a playback rate r as command-line arguments and
24、 plays the file at the given rate. First, useStdAudio.read() to read the file into an array a. If r = 1, just play a; otherwise create a new array b of approximate size r times a.length. If r 1, populate b by interpolating from the original. Then play b.25Write programs that uses StdDraw to create e
25、ach of the following designs.2627Write a program Circles.java that draws filled circles of random size at random positions in the unit square, producing images like those below. Your program should take four command-line arguments: the number of circles, the probability that each circle is black, th
26、e minimum radius, and the maximum radius.Creative Exercises28Visualizing audio. Modify PlayThatTune.java to send the values played to standard drawing, so that you can watch the sound waves as they are played. You will have to experiment with plotting multiple curves in the drawing canvas to synchro
27、nize the sound and the picture.29Statistical polling. When collecting statistical data for certain political polls, it is very important to obtain an unbiased sample of registered voters. Assume that you have a file with N registered voters, one per line. Write a filter that prints out a random samp
28、le of size M (see Program 1.4.1).30Terrain analysis. Suppose that a terrain is represented by a two-dimensional grid of elevation values (in meters). A peak is a grid point whose four neighboring cells (left, right, up, and down) have strictly lower elevation values. Write a program Peaks.java that
29、reads a terrain from standard input and then computes and prints the number of peaks in the terrain.31Histogram. Suppose that the standard input stream is a sequence of double values. Write a program that takes an integer N and two double values l and r from the command line and uses StdDraw to plot
30、 a histogram of the count of the numbers in the standard input stream that fall in each of the N intervals defined by dividing (l , r) into N equal-sized intervals.32Spirographs. Write a program Spirograph.java that takes three command-line arguments R, r, and a and draws the resulting spirograph. A
31、 spirograph(technically, an epicycloid) is a curve formed by rolling a circle of radius r around a larger fixed circle or radius R. If the pen offset from the center of the rolling circle is (r+a), then the equation of the resulting curve at time t is given byx(t) = (R+r)*cos(t) - (r+a)*cos(R+r)/r)*
32、t)y(t) = (R+r)*sin(t) - (r+a)*sin(R+r)/r)*t)Such curves were popularized by a best-selling toy that contains discs with gear teeth on the edges and small holes that you could put a pen in to trace spirographs.For a dramatic 3d effect, draw a circular image, e.g., earth.gif instead of a dot, and show it rotating over time. Heres a picture of the resulting spirographwhen R = 180, r = 40, and a = 15.33Clock. Write a program Clock.java that displays an animation
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1