CSC 1302 Principles of Computer Science II Lab 5: Array Practice

$30.00

Category: You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (5 votes)

Purpose:
In java, arrays are used to store multiple values on a single variable, instead of declaring
separate variables for each value. In this assignment, you will practice how to use arrays with
parameters in Java, and the program should prompt the user for the number of elements in the
array and then the elements of the array.
Task1:
Write a Java program that asks the user to enter an array of integers in the main method. The
program should prompt the user for the number of elements in the array and then the elements
of the array. The program should then call a method named minGap that accepts the array
entered by the user as a parameter and returns the minimum ‘gap’ between adjacent values in
the array. The main method should then print the value returned by the method. The gap
between two adjacent values in an array is defined as the difference in value between the first
element and the second element.
For example, suppose the user entered the following array of integers
{1, 3, 6, 5, 12}
The first gap is 2 (3 – 1), the second gap is 3 (6 – 3), the third gap is 1 (6 – 5) and the fourth gap
is 5 (12 – 6). Thus, the call of minGap(array) should return 1 because that is the smallest gap in
the array. If you are passed an array with fewer than 2 elements, you should return 0.
Expected Output of Task 1:
Optional Template for Task1:
//Name: Chengzong Peng
//PantherID:000-000-000
//Due Date:
//=======================================
import java.util.Scanner;
public class Lab_04 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(“Enter an array of integers “);
System.out.println(“How many elements: “);
int length = ;
int [] arr = ;
System.out.println(“What are the elements: “);
for(){
}
System.out.println(minGap(arr));
}
public static int minGap(int[] array) {
}
else{
for(){
}
}
}
return min;
}
}
Criteria:
1. Upload all of the .java and the .class files to the CSc1302 dropbox on http://
icollege.gsu.edu.
2. Your assignment will be graded based on the following criteria: (a) Are your programs
runnable without errors? (b) Do your programs complete the tasks with specified outputs?
(c) Do you follow the specified rules to define your methods and programs? (d) Do you
provide necessary comments include the programmer information, date, title of the
program and brief description of the program.
3. Please comment the important lines in the .java file as shown in the template. The
important lines including but not limited to i) variables, ii) for-loop, iii) while-loop, iv)
if-else statement, iv) methods. Please use your own words to describe what is your
purpose to write this line. A .java file without comment will be graded under a 40%
penalty.
4. Make sure that both the .java and .class files are named and uploaded to icollege
correctly. If any special package is used in the program, be sure to upload the package
too. Should you use any other subdirectory (whatsoever) your program would not be
graded, and you will receive a 0 (zero).
5. No copying allowed. If it is found that students copy from each other, all of these
programs will get 0.