Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS 214 Lab 7: Java Exercise CS 214 Lab 7: Java Exercise Begin by copying the program skeleton Average.java from the class directory into your new directory. Then use a text editor to open the file, and take a moment to study it, to see how much of our basic algorithm it implements. Java offers two different kinds of arrays (though they are declared and created in the same way): Arrays whose memory requirements are determined at compile-time, and Arrays whose memory requirements are determined at run-time. In this exercise, we will be examining only the compile-time determined arrays. Array Declarations A Java array can be defined using the form: ArrayDec ::= Type[] identifier Initializer Initializer ::= � | = { ExpressionList } | = new Type[ Expr ] | = null ExpressionList ::= � | Expression MoreExpressions MoreExpressions ::= � | , Expression MoreExpressions Using this information, define array1 as an array of four double values: 9.0, 8.0, 7.0 and 6.0. When that compiles correctly, use the information to define array0 as an array containing no values. Note that arrays are objects in Java, so our variables array0 and array1 are handles that store references to array objects. An array variable's value can thus be null, as indicated in the fourth Initializer production. Continue when both declarations compile correctly. Subprograms with Array Parameters To write method avg(), we must declare an array parameter, check its length: if its length is zero, return 0.0; otherwise compute the sum of the values in that array and return that sum divided by the number of values in the array. Since the method returns a single numeric value, we can return that value through the normal function-return mechanism. To sum the values in an array, we will need to implement our own sum() function. This function will need a local variable total in which to accumulate the sum of the array's values, and will use a for loop to iterate through the array's index values. Recall that the syntax of the Java for loop is just like that of C++: ForLoop ::= for (Expression; Expression; Expression) Statement An array parameter can be declared using syntax similar to that of a normal array object: ArrayParam ::= Type[ Expr ] identifier Expr ::= � | Expression If Expr is omitted, than an arbitrary array argument can be passed to that parameter. Using this information, we can implement the sum() function as follows: public static double sum(double[] theArray){ double total = 0.0; for (int i = 0; i < theArray.length; i++){ total += theArray[i]; } return total; } Using the preceding information, we can also define a stub for our avg() function: public static double avg(double anArray[]) { } To fill in the body of the function, we can use Java's if statement to check that the array is not null and that its length (which is stored in the array's length data field) is not zero. To sum the values in anArray, we can use the sum() method we just wrote. Drawing on these observations, we can complete our stub by writing: public static double avg(double anArray[]) { if (anArray == null || anArray.length <= 0) return 0.0; else return sum(anArray) / anArray.length; } With these functions defined, uncomment the lines in main() that invoke avg() and verify that it works correctly. Compile and test your code for correctness; then use script to create a file in which you cat your file, compile it, and execute it to show that it works correctly. That concludes the Java part of this lab. Calvin > CS > 214 > Labs > 07 > Java This page maintained by Joel Adams.