Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
COS 126 Programming Assignment: Conditionals and Loops COS 126 Conditionals, Loops, Arrays Programming Assignment The goal of this assignment is to write five short Java programs to gain practice with loops, conditionals, and arrays. The goal of this assignment is to write five short Java programs to gain practice with loops and conditionals. Bits. Write a program Bits.java that takes an integer command-line argument N and uses a while loop to compute the number of times you need to divide N by 2 until it is strictly less than 1. Print out the error message "Illegal input" if N is negative. % java Bits 0 % java Bits 8 0 4 % java Bits 1 % java Bits 16 1 5 % java Bits 2 % java Bits 1000 2 10 % java Bits 4 % java Bits -23 3 Illegal input Remark: This computes the number of bits in the binary representation of N, which also equals 1 + floor(log2 N) when N is positive. This quantity arises in information theory and the analysis of algorithms. Boolean and integer variables. Write a program Ordered.java that reads in three integer command-line arguments, x, y, and z. Define a boolean variable isOrdered whose value is true if the three values are either in strictly ascending order (x < y < z) or in strictly descending order (x > y > z), and false otherwise. Print out the variable isOrdered using System.out.println(isOrdered). % java Ordered 10 17 49 true % java Ordered 49 17 10 true % java Ordered 10 49 17 false Type conversion and conditionals.   Several different formats are used to represent color. For example, the primary format for LCD displays, digital cameras, and web pages, known as the RGB format, specifies the level of red (R), green (G), and blue (B) on an integer scale from 0 to 255. The primary format for publishing books and magazines, known as the CMYK format, specifies the level of cyan (C), magenta (M), yellow (Y), and black (K) on a real scale from 0.0 to 1.0. Write a program RGBtoCMYK.java that converts RGB to CMYK. Read three integers red, green, and blue from the command line, and print the input red, green, and blue, then print the equivalent CMYK values using these formulas: Hint. Math.max(x, y) returns the maximum of x and y. % java RGBtoCMYK 75 0 130 // indigo red = 75 green = 0 blue = 130 cyan = 0.423076923076923 magenta = 1.0 yellow = 0.0 black = 0.4901960784313726 If all three red, green, and blue values are 0, the resulting color is black. Here is what your program should do: % java RGBtoCMYK 0 0 0 // black red = 0 green = 0 blue = 0 cyan = 0.0 magenta = 0.0 yellow = 0.0 black = 1.0 Integer division and remainder. Write a program, Time.java, that reads in one non-negative integer command-line argument that represents the number of minutes that have elapsed since 12:00pm. Create a string whose value is "am" or "pm". The program then outputs a start time of 12:00pm and a finish time of the start time added to the elapsed time. % java Time 50 Start time: 12:00pm End time: 12:50pm % java Time 100 Start time: 12:00pm End time: 1:40pm % java Time 720 Start time: 12:00pm End time: 12:00am Checkerboard. Write a program Checkerboard.java that takes an integer command-line argument N, and uses two nested for loops to print an N-by-N "checkerboard" pattern like the one below: a total of N2 asterisks, where each row has 2N characters (alternating between asterisks and spaces). % java Checkerboard 4 % java Checkerboard 5 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * A drunkard's walk. A drunkard begins walking aimlessly, starting at a lamp post. At each time step, the drunkard forgets where he or she is, and takes one step at random, either north, east, south, or west, with probability 25%. How far will the drunkard be from the lamp post after N steps? Write a program RandomWalker.java that takes an integer command-line argument N and simulates the motion of a random walker for N steps. After each step, print the location of the random walker, treating the lamp post as the origin (0, 0). Also, print the square of the final distance from the origin. % java RandomWalker 10 % java RandomWalker 20 (0, -1) (0, 1) (0, 0) (-1, 1) (0, 1) (-1, 2) (0, 2) (0, 2) (-1, 2) (1, 2) (-2, 2) (1, 3) (-2, 1) (0, 3) (-1, 1) (-1, 3) (-2, 1) (-2, 3) (-3, 1) (-3, 3) squared distance = 10 (-3, 2) (-4, 2) (-4, 1) (-3, 1) (-3, 0) (-4, 0) (-4, -1) (-3, -1) (-3, -2) (-3, -3) squared distance = 18 Write a program RandomWalkers.java that takes two integer command-line arguments N and T. In each of T independent experiments, simulate a random walk of N steps and compute the squared distance. Output the mean squared distance (the average of the T squared distances). % java RandomWalkers 100 10000 % java RandomWalkers 400 2000 mean squared distance = 101.446 mean squared distance = 383.12 % java RandomWalkers 100 10000 % java RandomWalkers 800 5000 mean squared distance = 99.1674 mean squared distance = 811.8264 % java RandomWalkers 200 1000 % java RandomWalkers 1600 100000 mean squared distance = 195.75 mean squared distance = 1600.13064 As N increases, we expect the random walker to end up farther and farther away from the origin. But how much farther? Use RandomWalkers to formulate a hypothesis as to how the mean squared distance grows as a function of N. Use T = 100,000 trials to get a sufficiently accurate estimate. Remark: this process is a discrete version of a natural phenomenon known as Brownian motion. It serves as a scientific model for an astonishing range of physical processes from the dispersion of ink flowing in water, to the formation of polymer chains in chemistry, to cascades of neurons firing in the brain. Dice and the Gaussian distribution. Write a program TenDice.java that takes an integer command-line argument N, and rolls 10 fair six-sided dice, N times. Use an array to tabulate the number of times each possible total (between 10 and 60) occurs. Then print out a text histogram of the results, as illustrated below. % java TenDice 1000 10: 11: 12: 13: 14: 15: 16: 17: 18: * 19: **** 20: 21: *** 22: ****** 23: ******** 24: **************** 25: ************* 26: ********** 27: ********************************* 28: **************************************** 29: ********************************* 30: *************************************************** 31: ***************************************************************** 32: ******************************************************** 33: ************************************************************************************** 34: *********************************************************** 35: ********************************************************************* 36: *********************************************************************************** 37: ************************************************************** 38: ***************************************************************** 39: *************************************** 40: ***************************************************** 41: ************************************ 42: **************************** 43: ************************ 44: ************************ 45: ********* 46: *********** 47: ******* 48: *** 49: ** 50: 51: 52: * 53: 54: 55: 56: 57: 58: 59: 60: Remark: the central limit theorem, a key result in probability and statistics, asserts that the shape of the resulting histogram tends to the ubiquitous bell curve (Gaussian distribution) if the number of dice and rolls is large. Program style and format. Now that your program is working, go back and look at the program itself. Is your header complete? Did you comment appropriately? Did you use descriptive variable names? Did you avoid unexplained, hard-wired constants? Are there any redundant conditions? Follow the guidelines in the Reviewing your Programs section of the Checklist. Submission. Submit the files Bits.java, Ordered.java, RGBtoCMYK.java, Time.java, RandomWalker.java, RandomWalkers.java, TenDice.java, and a fully completed copy of the readme.txt file for this week. Challenge for the bored. Implement Time.java with no if statements!