Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

- In the main class, declare and initialize two arrays as follows: Long[] salari

ID: 3877400 • Letter: #

Question

- In the main class, declare and initialize two arrays as follows:
Long[] salaries = {24000, 34000, 40000, 21000, 18000};
String[] profession = {"nurse", "teacher", "Architect", "Bus Driver", "Postman"};
- Write program code to count the number of salaries in the lower range of tax, i.e. salaries
below 20K.
- Write program code to find a print-out the Profession with the highest Salary.
5. Encode the functionality in ‘4.’ above in a separate method that would calculate the
highest value for any arbitrary array passed to it (of type long).


/* source file that contains the main function that initializes a string
List and calls a method (findMax()) of another class (MaxStr)
to find the longest string in the array list
*/

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListEx
{
public static void main(String[] args)
{
// Create a dynamic list of Strings (ArrayList<String>) and intialise it
ArrayList<String> s = new ArrayList<String>();
s.add("potato");
s.add("cucumber");
s.add("onion");
s.add("cabbage");
s.add("mint");

//iterate through the List and print its String elements
Iterator<String> itr = s.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}

// create a sort object
MaxStr myMaxStr = new MaxStr();
// call the findMax function to find the longest String
String longestStr = myMaxStr.findMax(s);
// print the mximum
System.out.println("The longest String is: " + longestStr);

}
}

Explanation / Answer

class Main {
public static void main(String[] args) {
  
// Declaring variables
int[] salaries = {24000, 34000, 40000, 21000, 18000};
String[] profession = {"nurse", "teacher", "Architect", "Bus Driver", "Postman"};
  
// to count the number of salaries in the lower range of tax, i.e. salaries below 20K.
  
int count = 0;
for(int i=0; i<salaries.length; i++)
{
// checking if the salary is lessthan 20k
if(salaries[i] < 20000)
count++;
}
// printing output
System.out.println("Count of salaries below 20K: "+count);
  
// the Profession with the highest Salary.
int max = 0;
for(int i=1; i< salaries.length; i++)
{
if(salaries[max] < salaries[i])
max = i;
}
System.out.println("The Profession with the highest Salary is "+profession[max]);
}
}

/*OUTPUT
Count of salaries below 20K: 1
The Profession with the highest Salary is Architect
*/