Reference no: EM13165515
Arrays & more loops practice
Project name:A170_E15_YourLastName_YourFirstName
Submit: Zip the folder and drop it in the Q drive.
PART A. Write a program ArraysAndLoops and implement the following in the main method:
1. Declare an array arr1 and initialize it to 1, 2, 3, 4, 5, 6, 7, 8, and 9
(For the rest of PART A, use the instance variable length instead of a number to define the capacity of
the array.)
2. Declare another array arr2, the same length as arr1, without giving it any initial values.
3. Using a for loop print arr1 displaying all elements on one line, separated by a comma (commas must
be placed only between numbers).
4. Using a while loop, print arr2 displaying all elements on one line, separated by a dash (dashes must be
placed only between numbers).
5. Using a for loop, copy all the values of arr1 into arr2 in reverse order.
6. Using a do/while loop, print arr1 displaying all elements on one line, separated by a comma.
7. Using a for loop, print arr2 displaying all elements on one line, separated by a dash.
Expected Output
Array 1: 1,2,3,4,5,6,7,8,9
Array 2: 0-0-0-0-0-0-0-0-0
Array 1: 1,2,3,4,5,6,7,8,9
Array 2: 9-8-7-6-5-4-3-2-1
PART B. Write a program, Passwords, that contains an array of five names and another array of five passwords
(your choice). Your program should ask the user to enter a name and a password. If the user enters the correct
name with its related password, grant access (e.g. display â??Access granted!â??). If the user enters the wrong
name/password (can be either or both), allow the user to re?try only once. If the user still cannot enter the
correct name/password, display the following: â??Sorry, your username/password does not match our database.
Contact the administrator.â??
PART C. Write a program, ArrayRange, that asks the user to input integers and that displays the difference
between the largest and the smallest. You should ask the user the number of integers s/he would like to enter
(this will allow you to set the length of the array). Allow the user to input all the numbers and then store them
in the array. Search the array for the largest number, then search for the smallest, and finally compute the
calculation. Display all the numbers given by the user, the max number, the min number, and the difference.
THIS IS MY PART C: I WAS WONDERING IF YOU COULD FIX IT I CANNOT FIND THE ERROR
import java.util.Scanner;
public class ArrayRange
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int[] array = new int[10];
int inputInteger = 0;
for (int i = 0; i < array.length; i++)
{
inputInteger = input.nextInt();
array[i] = inputInteger;
}
int maxNumber = array[0];
int minNumber = array[0];
for (int i = 0; i < array.length; i++)
{
if ( array[i] > maxNumber)
maxNumber = array[i];
}
for (int i = 0; i < array.length; i++)
{
if ( array[i] < minNumber)
minNumber = array[i];
}
for (int i = 0; i < array.length; i++)
System.out.print(array[i] + " ");
System.out.println();
System.out.println("max number in this array is " + maxNumber);
System.out.println("min number in this array is " + minNumber);
int difference = maxNumber - minNumber;
System.out.println("difference between the largest and the smallest is " + difference);
}
}