In Project ArrayLabs , create a new class named MyArrays Code the following, usi
ID: 3606433 • Letter: I
Question
In Project ArrayLabs, create a new class named MyArrays
Code the following, using the sample code in Lab 2A:
Create a string array named Bollywood
Add famous Bollywood actors (minimum 4)
Print each element using one statement per element
Use println for print
Print using a FOR loop and LENGTH COMMAND
Add an element
Search thru an array looking for an element, then print it’s location
Sort and print using FOR LOOP
Create an int array named Ages
Add ages of family and friends (minimum 5)
Print using a FOR LOOP
Sort and print using FOR LOOP
I need the code in java using eclipse.
I am sending you the sample code just for reference.Please don"t send me the sample code as answer. Its just for reference.
Explanation / Answer
package org.students;
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayOfStrings {
public static void main(String[] args) {
//Declaring variables
int age, count = 0;
String names[] = new String[4];
int ages[] = new int[5];
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
for (int i = 0; i < names.length; i++) {
System.out.print("Enter name#" + (i + 1) + ":");
names[i] = sc.nextLine();
}
//calling the methods
displayArray(names);
sortNames(names);
System.out.println("___ Displaying the Names after sorting ___ ");
//calling the methods
displayArray(names);
System.out.print("Enter the name you want to search :");
String searchName = sc.nextLine();
int location = searchNameInArray(names, searchName);
if (location != -1) {
System.out.println("'" + searchName + "' is found at index " + location);
} else {
System.out.println("'" + searchName + "' not found in the array");
}
for (int i = 0; i < ages.length; i++) {
System.out.print("Enter Age of Person#" + (i + 1) + ":");
ages[i] = sc.nextInt();
}
//calling the methods
displayAges(ages);
sortAges(ages);
System.out.println("___ Displaying the Ages after sorting ___ ");
displayAges(ages);
}
//This method will sort the Array elements based on age
private static void sortAges(int[] ages) {
//This Logic will Sort the Array of elements in Ascending order
int temp;
for (int i = 0; i < ages.length; i++) {
for (int j = i + 1; j < ages.length; j++) {
if (ages[i] > ages[j]) {
temp = ages[i];
ages[i] = ages[j];
ages[j] = temp;
}
}
}
}
//This method will display the Array elements
private static void displayAges(int[] ages) {
for (int i = 0; i < ages.length; i++) {
System.out.println(ages[i]);
}
}
//This method will sort the Array elements based on name
private static void sortNames(String[] names) {
//This Logic will Sort the Array of elements in Ascending order
String temp;
for (int i = 0; i < names.length; i++) {
for (int j = i + 1; j < names.length; j++) {
if (names[i].compareTo(names[j]) > 0) {
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
}
//This method will display the array elements
private static void displayArray(String[] names) {
System.out.println(" Displaying the Array Elements");
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
}
private static int searchNameInArray(String[] names, String searchName) {
for (int i = 0; i < names.length; i++) {
if (searchName.equalsIgnoreCase(names[i])) {
return i;
}
}
return -1;
}
}
_____________________
Output:
Enter name#1:Amitab Bachan
Enter name#2:Salman Khan
Enter name#3:Amir Khan
Enter name#4:Sharukh Khan
Displaying the Array Elements
Amitab Bachan
Salman Khan
Amir Khan
Sharukh Khan
___ Displaying the Names after sorting ___
Displaying the Array Elements
Amir Khan
Amitab Bachan
Salman Khan
Sharukh Khan
Enter the name you want to search :Salman Khan
'Salman Khan' is found at index 2
Enter Age of Person#1:34
Enter Age of Person#2:56
Enter Age of Person#3:23
Enter Age of Person#4:43
Enter Age of Person#5:67
34
56
23
43
67
___ Displaying the Ages after sorting ___
23
34
43
56
67
__________________Thank YOu
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.