0% found this document useful (0 votes)
2 views

Arrays

The document outlines a Java program that utilizes arrays to manage employee data, including names, contacts, genders, ages, and red flags. It includes methods for collecting feedback, displaying warnings, printing employee information, sorting arrays, and checking if arrays are sorted. The program allows user input for employee details and ensures data integrity through validation checks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Arrays

The document outlines a Java program that utilizes arrays to manage employee data, including names, contacts, genders, ages, and red flags. It includes methods for collecting feedback, displaying warnings, printing employee information, sorting arrays, and checking if arrays are sorted. The program allows user input for employee details and ensures data integrity through validation checks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Arrays – Ramirez

1.) Identify any task in your approved project that can be done using
arrays be it a 1D or 2D array of at least 10 elements. Create a
minimum of 2 arrays with different data types in the main method and
initialize the values of each element in the array by inputting it using
the Scanner class. Create a method for printing the elements of the
array and call it in method main to check its contents. (50 Points).

package activities;
import java.util.*;

public class Array {

public static String collectFeedback() {


Scanner scan = new Scanner(System.in);
System.out.print("What is your feedback to the company? ");
String feedback = scan.nextLine();
return feedback;
}

public static void showWarning() {


System.out.println("\
n========================================================================
=============================");
System.out.println("||\tPLEASE READ THE LIST OF WARNINGS AND
REMINDERS OF THE COMPANY ||\n||
||");
System.out.println("||\tSECURITY ALERT: Unauthorized access to
employee data is strictly prohibited. ||");
System.out.println("||\tDATA PRIVACY: Safeguard the personal
information of employees. ||");
System.out.println("||\tFEEDBACK: Your feedback is valuable to
us. ||");
System.out.println("||\tDATA ACCURACY CHECK: Always review and
verify the accuracy of employee data entered. ||");

System.out.println("=====================================================
================================================\n");
}

public static void showGratitude() {


System.out.println("Thank you for using the program, I hope you
enjoyed using it and is helpful to you!");
}

public static String enterRedFlag(int employeeNumber, Scanner


scanner) {
System.out.print("Please enter one red flag of employee
"+employeeNumber+": ");
return scanner.nextLine();
}

public static void printArray(String[] names, String[] contacts,


String[] genders, int[] ages, String[] redFlags) {
System.out.println("\nThe employees' information:");
for (int i = 0; i < names.length; i++) {
System.out.println("Employee " + (i + 1) + ":\n" +
"Name: " + names[i] + "\n" +
"Contact: " + contacts[i] + "\n" +
"Gender: " + genders[i] + "\n" +
"Age: " + ages[i] + "\n" +
"Redflag: " + redFlags[i] + "\n");
}
}

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);

showWarning();

System.out.print("Enter the number of employees: ");


int numEmployees = scan.nextInt();
scan.nextLine();

String[] names = new String[numEmployees];


String[] contacts = new String[numEmployees];
String[] genders = new String[numEmployees];
int[] ages = new int[numEmployees];
String[] redFlags = new String[numEmployees];

for (int i = 0; i < numEmployees; i++) {


System.out.print("Enter the name of employee "+(i+1)+": ");
names[i] = scan.nextLine();

System.out.print("Enter the contact number of employee "+


(i+1)+" [**** *** ****]: ");
contacts[i] = scan.nextLine();

System.out.print("Enter the gender of employee "+(i+1)+"


[Male/Female]: ");
genders[i] = scan.nextLine();

while (!genders[i].equalsIgnoreCase("male") && !


genders[i].equalsIgnoreCase("female")) {
System.out.println("Invalid gender input. Please enter
either 'Male' or 'Female'.");
System.out.print("Enter the gender of employee "+(i+1)+"
[Male/Female]: ");
genders[i] = scan.nextLine();
}

System.out.print("Enter the age of employee "+(i+1)+": ");


ages[i] = scan.nextInt();
scan.nextLine();

while (ages[i] < 18 || ages[i] > 60) {


System.out.println("Invalid age range. Employees must be
between 18 and 60 years old.");
System.out.print("Enter the age of employee "+(i+1)+":
");
ages[i] = scan.nextInt();
scan.nextLine();
}

redFlags[i] = enterRedFlag(i + 1, scan);


}

printArray(names, contacts, genders, ages, redFlags);

String feedback = collectFeedback();


System.out.println("\nUser's feedback: " + feedback + "\n");

showGratitude();
scan.close();
}
}
2.) Improve your code in item no.1 to include a method for searching
and sorting elements in the array. Also create another method to check
if the elements of the array are sorted or not. Use the 3 methods
mentioned inside the main method by calling it so that the user can
search a desired element in the array and display appropriate results.
(Note: Select only one type of array from item no.1 and make sure that
your program will sort the elements in the array if it is not sorted
before searching the desired element inputted by the user. But if the
elements are already sorted it will directly search that element.) (50
Points).

package activities;
import java.util.*;

public class Array_Sort_Search {

public static String collectFeedback() {


Scanner scan = new Scanner(System.in);
System.out.print("What is your feedback to the company? ");
String feedback = scan.nextLine();
return feedback;
}

public static void showWarning() {


System.out.println("\
n========================================================================
=============================");
System.out.println("||\tPLEASE READ THE LIST OF WARNINGS AND
REMINDERS OF THE COMPANY ||\n||
||");
System.out.println("||\tSECURITY ALERT: Unauthorized access to
employee data is strictly prohibited. ||");
System.out.println("||\tDATA PRIVACY: Safeguard the personal
information of employees. ||");
System.out.println("||\tFEEDBACK: Your feedback is valuable to
us. ||");
System.out.println("||\tDATA ACCURACY CHECK: Always review and
verify the accuracy of employee data entered. ||");

System.out.println("=====================================================
================================================\n");
}

public static void showGratitude() {


System.out.println("Thank you for using the program, I hope you
enjoyed using it and is helpful to you!");
}

public static String enterRedFlag(int employeeNumber, Scanner


scanner) {
System.out.print("Please enter one red flag of employee
"+employeeNumber+": ");
return scanner.nextLine();
}

public static void printArray(String[] names, String[] contacts,


String[] genders, int[] ages, String[] redFlags) {
System.out.println("\nThe employees' information:");
for (int i = 0; i < names.length; i++) {
System.out.println("Employee " + (i + 1) + ":\n" +
"Name: " + names[i] + "\n" +
"Contact: " + contacts[i] + "\n" +
"Gender: " + genders[i] + "\n" +
"Age: " + ages[i] + "\n" +
"Redflag: " + redFlags[i] + "\n");
}
}

public static void sortArrays(String[] names, String[] contacts,


String[] genders, int[] ages, String[] redFlags) {
// Sort names array
Arrays.sort(names);

// Sort contacts array


Arrays.sort(contacts);

// Sort genders array


Arrays.sort(genders);

// Sort ages array


Arrays.sort(ages);

// Sort redFlags array


Arrays.sort(redFlags);
}
public static boolean isSorted(int[] arr) {
for (int i = 1; i < arr.length; i++) {
if (arr[i] < arr[i - 1]) {
return false;
}
}
return true;
}

public static boolean isSorted(String[] arr) {


for (int i = 1; i < arr.length; i++) {
if (arr[i].compareTo(arr[i - 1]) < 0) {
return false;
}
}
return true;
}

public static boolean areAllArraysSorted(String[] names, String[]


contacts, String[] genders, int[] ages, String[] redFlags) {
return isSorted(names) && isSorted(contacts) && isSorted(genders)
&& isSorted(ages) && isSorted(redFlags);
}

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);

showWarning();

System.out.print("Enter the number of employees: ");


int numEmployees = scan.nextInt();
scan.nextLine();

String[] names = new String[numEmployees];


String[] contacts = new String[numEmployees];
String[] genders = new String[numEmployees];
int[] ages = new int[numEmployees];
String[] redFlags = new String[numEmployees];

for (int i = 0; i < numEmployees; i++) {


System.out.print("Enter the name of employee "+(i+1)+": ");
names[i] = scan.nextLine();

System.out.print("Enter the contact number of employee "+


(i+1)+" [**** *** ****]: ");
contacts[i] = scan.nextLine();

System.out.print("Enter the gender of employee "+(i+1)+"


[Male/Female]: ");
genders[i] = scan.nextLine();

while (!genders[i].equalsIgnoreCase("male") && !


genders[i].equalsIgnoreCase("female")) {
System.out.println("Invalid gender input. Please enter
either 'Male' or 'Female'.");
System.out.print("Enter the gender of employee "+(i+1)+"
[Male/Female]: ");
genders[i] = scan.nextLine();
}

System.out.print("Enter the age of employee "+(i+1)+": ");


ages[i] = scan.nextInt();
scan.nextLine();

while (ages[i] < 18 || ages[i] > 60) {


System.out.println("Invalid age range. Employees must be
between 18 and 60 years old.");
System.out.print("Enter the age of employee "+(i+1)+":
");
ages[i] = scan.nextInt();
scan.nextLine();
}

redFlags[i] = enterRedFlag(i + 1, scan);


}

// Sort all arrays


sortArrays(names, contacts, genders, ages, redFlags);

// Print sorted arrays


printArray(names, contacts, genders, ages, redFlags);

String feedback = collectFeedback();


System.out.println("\nUser's feedback: " + feedback + "\n");

// Check if all arrays are sorted


if (areAllArraysSorted(names, contacts, genders, ages, redFlags))
{
System.out.println("All arrays are sorted.");
} else {
System.out.println("Not all arrays are sorted.");
}

showGratitude();
scan.close();
}
}

You might also like