Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CSE114 Spring 2016
Lab Exercise 2 of Week 3
Chen-Wei Wang
Background: Arrays in Java
An array is a sequence of elements. Each element in an array is associated with an integer index. The
first element of an array has the index 0, the second has index 1, . . . , and the last element of an array
has the index value that is equal to the size (i.e., number of elements) of the array minus one.
Figure 1 illustrates an array of integers. The array has a fixed size of 10: its first element at index 0
is 940, its second element at index 1 is 880, . . . , and its last element at index 9 is 440. You can observe
that the minimum index value of the array in Figure 1 is 0 (zero), and its maximum index value (i.e., 9)
is its size (i.e, 10) minus one.
indices
High
scores
Figure 1: Example Array: An Integer Array of Fixed Size 10
For each array that you create in Java, both its name and type must be decided when you declare
it. Consider the Java code fragment below that creates an array of integers, called numbers, which
corresponds to the example array in Figure 1:
1 int[] numbers = new int[10];
2 numbers[0] = 940;
3 numbers[1] = 880;
4 numbers[2] = 830;
5 numbers[3] = 790;
6 numbers[4] = 750;
7 numbers[5] = 660;
8 numbers[6] = 650;
9 numbers[7] = 590;
10 numbers[8] = 510;
11 numbers[9] = 440;
12 int sizeOfNumbers = numbers.length;
13 System.out.println("Size of numbers is " + sizeOfNumbers);
In the above fragment of Java code, Line 1 declare a variable numbers, which is a place holder for
an array of (rather than a single) integer values. Notice the syntax of a pair of open and close square
brackets ([]): in Java, whenever you append [] to a known type (int, String, double, boolean, etc.),
it denotes a new data type that denotes an array of values. For example, int[] denotes a type of an
array of integers, double[] an array of double-precision floating numbers, boolean[] an array of true or
false values, String[] an array of string values, and so on.
Let us now revisit Line 1 again. The assignment source (i.e., right-hand side of the assignment operator
=) specifies that we create a new array of size 10. The syntax to learn here are the new keyword, and the
1
way you specify a fixed size for the array, by placing an integer value within the pair of square brackets
(e.g., int[10]).
Lines 2 to 11 in the above Java code assigns a new value to each of the ten elements in the array.
Each line is an assignment statement: the assignment source (the right-hand side) is an integer literal,
and the assignment target is a “slot” in the array (e.g., numbers[9]). To access an element in the array,
you need to supply two pieces of information:
1. the name of the array
2. the index of the element
In the case of the above Java code, since we already declare numbers as an integer array of size 10
(Line 1), we can now use its name and any valid index value (in between 0 and 9, inclusively) to access a
particular element. For example, we write numbers[2] to access the third element in numbers at index 2.
An important observation for you to make is that an array element may appear either as an assignment
source (i.e., right-hand side of =) or as an assignment target (i.e., left-hand side of =). For example:
1 numbers[0] = -1;
2 boolean isPositive = numbers[0] > 0;
3 System.out.println("First element in numbers is positive: " + isPositive);
Finally, Line 12 shows a useful function of the array for you to query about its size (i.e., maximum
number of elements that you can fill in). For example, in the case of array numbers in Figure 1, executing
numbers.length will return the integer value 10.
Background: Iterating through Arrays in Java
The idea about iterating through a Java array is easy: use a for loop and maintain a loop counter, such
that its value starts with 0 and goes up to and including its size (using the length function) minus one.
For example, the Java code below prints out all elements of numbers in Figure 1, from left to right:
1 for(int i = 0; i < a.length; i ++) {
2 System.out.println(a[i]);
3 }
Alternatively, you can write the equivalent as a while loop:
1 int i = 0;
2 while(i < a.length) {
3 System.out.println(a[i]);
4 i ++;
5 }
2
Problems
After studying the introduction material in the previous two sections, create a new Java project CSE114 S16 Week03 Lab2.
In that project, create a new class ArrayFacilities with the main method.
1. Prompt the user for an integer value:
How many numbers do you want to process?
Then your program should read in the next integer using the nextInt() function from Scanner.
For example, if the user enters 10, that means they will enter up to, but not more than, 10 numbers
for your program to process.
Once you get this upper bound from the user, use it to create an integer array of the corresponding
size. Refer to the background section for the syntax of creating a new array.
2. Now that the user already told you the maximum number of numbers they will give to your program
(from the previous step), repeatedly print the prompt message:
Enter an integer value:
Then your program should read in a new integer value. After reading each new integer value, ask
if the user would like to continue to enter a new value:
Would you like to continue? (true/false)
Now your program will expect the user to enter a boolean value, either true or false. Use the
nextBoolean() function from Scanner. If their answer is true, then you should check to see if
they already have entered too many numbers (according to the maximum value they specified in
step 1). For example, if they said they would only input up to 10 numbers, but they are now trying
to enter an 11th number, then your program must print an error message and terminate:
Error: You have entered too many numbers!
If they have not entered too many numbers so far, then you can store the new integer value into
the array. You will need to keep a counter in your program for the number of values that the user
has entered so far.
On the other hand, if their answer is false, then you should stop reading from them and continue
to the next step.
3. Now that you have maintained an array of integers, process it by generating three outputs:
3.1 Print out all elements in the array. For example:
You have entered 8 numbers:
1
2
3
-4
5
6
7
8
3.2 Print if the numbers entered contain are all positive. For example:
Your entered numbers are not all positive.
3.3 Print if the numbers entered contain at least one value that is larger than 5. For example:
Your entered numbers contain at least one number larger than 5.
For each of the above three outputs, write a separate loop statement to handle it.
3