Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 i 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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

AWS Certified Database Study Guide Specialty DBS-C01 Exam

Authors: Matheus Arrais, Rene Martinez Bravet, Leonardo Ciccone, Angie Nobre Cocharero, Erika Kurauchi, Hugo Rozestraten

1st Edition

1119778956, 978-1119778950

More Books

Students also viewed these Databases questions

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago