Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS170 Lab 9
    Single dimension arrays
Objectives of this lab:
• Write methods utilizing return values and input parameters
• Practice translating English specifications for programs into algorithms and code.
• Practice traversing single dimensional arrays
• Understand command line arguments and how they can be incorporated into a program
• Understand arrays as parameter variables and how methods modify arrays.
Lab preparation:
• Start a terminal application and prepare your lab9 directory.  Make a file named Lab9.java
Exercises: Command line arguments:
• This lab is designed to use command line arguments.  The program is designed to be run with 
an integer command line argument. Examples:
java Lab9 6
java Lab9 8
• The command line argument specifies how large an array to create in your main method.  In the 
first example above, the array should contain 6 elements, and in the second example, the array 
should contain 8.
• Modify the main method to use the command line argument correctly and create an array.
◦ Remember that all command line arguments are strings and are passed to the main method 
via the String array variable args.  
◦ You must convert the command line argument to an integer.  Review the 
Integer.parseInt function if you do not remember how to use it.
◦ This integer should then be used to make an array of the appropriate size.  
◦ You will use this array as an argument when you invoke the methods below.
Exercise 2:  Arrays and Methods
• In the file, you will write 2 methods which practice array manipulation:
◦ fillRandom
◦ minGap
• The fillRandom method
◦ This method should take an array as an argument.  The method should not return anything 
(in other words, its return type is void).  However, it should modify the parameter array.
◦ This function should fill the array with random numbers between 1-50 (inclusive). 
◦ You've written code to generate random numbers many times.  
◦ Traverse the parameter array, and assign each element a random number.  For example:
int rand = //generate a single random int between 1-50
a[index] = rand;
◦ Check your work!
▪ Start with a small array, 4 or 5 elements:
java Lab9 5
▪ In the main method, create the array from the CLA.
▪ Print out the array.  Remember the shortcut for printing arrays easily:
import java.util.Arrays;
...
S.O.P(Arrays.toString(arrayVariable));
▪ Invoke fillRandom with the array as an argument.
▪ Print the array again after the method returns and make sure it is filled with numbers 
between 1-50.
▪ Run/test your program multiple times with different sized arrays.
• The minGap method:
◦ The “gap” between two elements (values) in an array is defined as absolute value of the 2nd
value minus the first.
◦ You will write a method named minGap which returns the smallest gap found in an array. 
This array should take an integer array as a parameter.  It will return an integer which is the 
smallest gap found.  This method should not modify your input parameter array.
◦ Consider this array, a:
21 76 82 34 17 59
▪ Given this array above, the gaps would be:
• 55 (a[1] – a[0])
• 6 (a[2] – a[1])
• 48 (absolute value of a[3]-a[2])
• 17 (absolute value of a[4]-a[3])
• 42 (a[5] – a[4])
◦ The “minimum gap” of an array is the smallest gap between two adjacent elements.  So in 
the example array above, the minimum gap is 6 (82 – 76 or a[2]-a[1]).
◦ You can use the Math.abs method to calculate the absolute value.  This function takes an 
integer as a parameter and returns the absolute value of that number.
◦ Your function should return the minimum gap of the input parameter array.
• Turning in your work:
◦ Comment your code and make sure your style is correct.  
◦ Get credit for your work by either:
▪ Submitting your work to the Lab9 assignment on Blackboard.  Make sure you submit!  
Do not just save a draft.  Submit Lab9.java (not .class!).  You can submit as many times 
as you wish, but we only grade the last submission.
▪ Demo your working code to your TA.  Make sure both you and your TA sign the signin 
sheet.
• If you do not finish during your lab time, your submission is due to BB by 5pm on 
Saturday.  No late submissions are accepted for labs.