Write both sections in the same package. Part A. Write a program PartA and imple
ID: 3823025 • Letter: W
Question
Write both sections in the same package. Part A. Write a program PartA 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 (From this point on, use the variable length instead of a number to define the capacity of the array.) 2. Declare another array arr2, same length of arr1, without entering any integers. 3. Using a for loop print arri 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 while loop copy all elements of arr1 into arr2 in reverse order. 7. Using a do/while loop print arr1 displaying all elements on one line, separated by a Comma 8. Using a for loop, print arr2 displaying all element on one line, separate 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 PartB, that contains an array of five names and another array of five passwords (your choice). Use the following Names/Passwords in your string arrays (case sensitive) Bob jelly Jane clock Jill tree Sean computer Mel chip 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. 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 name/password does not match our database. Contact the administrator."Explanation / Answer
PART A:
ArrayDemo.java
public class ArrayDemo {
public static void main(String[] args) {
//Declaring and initializing array
int[] arr1={1,2,3,4,5,6,7,8,9};
//Calculating the size of the array
int length=arr1.length;
//Creating an array2 based on size of the array1
int[] arr2=new int[length];
//Displaying the elements in Array1
System.out.print("Array 1: ");
for(int i=0;i<length;i++)
{
System.out.print(arr1[i]);
if(i<length-1)
{
System.out.print(",");
}
}
//Displaying the elements in Array2
System.out.print(" Array 2: ");
int i=0;
while(i<length)
{
System.out.print(arr2[i]);
if(i<length-1)
{
System.out.print("-");
}
i++;
}
//populating the elements of array1 into array2 in reverse order
int k=0;
for(i=length-1;i>=0;i--)
{
arr2[k]=arr1[i];
k++;
}
//populating the elements of array1 into array2 in reverse order
i=0;
int m=length-1;
while(i<length)
{
arr2[i]=arr1[(m)--];
i++;
}
//Displaying the elements in Array1
System.out.print(" Array 1: ");
i=0;
do
{
System.out.print(arr1[i]);
if(i<length-1)
{
System.out.print(",");
}
i++;
}while(i<length);
//Displaying the elements in Array2
System.out.print(" Array 2: ");
for(i=0;i<length;i++)
{
System.out.print(arr2[i]);
if(i<length-1)
{
System.out.print("-");
}
}
}
}
______________________
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:
UserNamesPassword.java
import java.util.Scanner;
public class UserNamesPassword {
public static void main(String[] args) {
//Declaring variables
String uname,passwd;
//Creating an String arrays and initializing with values
String usernames[]={"Bob","Jane","Jill","Sean","Mel"};
String pwd[]={"jelly","clock","tree","computer","chip"};
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
int count=0;
boolean bool;
while(true)
{
//Getting the user name and pwd
System.out.print("Enter the user name :");
uname=sc.next();
System.out.print("Enter the user pwd :");
passwd=sc.next();
//calling the method to check the username and pwd is valid or not
bool=checkUserNamePwd(uname,passwd,usernames,pwd);
if(bool==false && count==0)
{
count++;
System.out.println("** Invalid.Try again **");
continue;
}
else if(bool==true)
{
System.out.println("Login Successful");
break;
}
else
{
System.out.println("Sorry your name and does not match our database. Contact the administrator");
break;
}
}
}
//This function will check whether the user name and pwd is valid or not
private static boolean checkUserNamePwd(String uname, String passwd,
String[] usernames, String[] pwd) {
boolean un=false,pw=false;
int i=0;
while(i<usernames.length)
{
if(uname.equals(usernames[i]))
{
un=true;
break;
}
else
{
i++;
}
}
i=0;
while(i<pwd.length)
{
if(passwd.equals(pwd[i]))
{
pw=true;
break;
}
else
{
i++;
}
}
if(un==true && pw==true)
return true;
else
return false;
}
}
output#1:
Enter the user name :Bob
Enter the user pwd :jel
** Invalid.Try again **
Enter the user name :Bob
Enter the user pwd :jely
Sorry your name and does not match our database.
Contact the administrator
_______________
Output#2:
Enter the user name :Bob
Enter the user pwd :jelly
Login Successful
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.