In: Computer Science
Create a program that determines the name of the person with the highest or lowest number of votes. The data will be in two arrays. The first array is of type String and has the following names in it: Mike Muldune Justin Meiber Clark Kent Ana Karina The second array is of type integer and has the following numbers in it: 2324, 2425, 3344 and 2526. Ask the user to enter a 1 for highest and 2 for lowest. If the user enters a 1, print the name of the person with the highest number of votes and the number of votes received. It the user enters a 2, print the name of the person with the lowest number of votes and the number of votes received.
import java.util.Scanner;
public class HighestPerson {
public static void main(String[] args) {
String names[]= {"Mike
Muldune","JustinMelber","Clark Kent","Ana Karina"};
int votes[]=
{2324,2425,3344,2526};
Scanner sc = new
Scanner(System.in);
System.out.println("Press 1 for
highest votes 0 for lowest votes");
int ch=sc.nextInt();
if(ch==1) {
int
index=getHighest(votes);
System.out.println(names[index]+" got highest votes
"+votes[index]);
}
else {
int
index=getLowest(votes);
System.out.println(names[index]+" got lowest votes
"+votes[index]);
}
}
// returns the index of highest votes in votes
array
private static int getHighest(int[] arr) {
int m=0;
for(int i=1;i<arr.length;i++)
{
if(arr[m]<arr[i])
m=i;
}
return m;
}
// returns the index of lowest votes in votes
array
private static int getLowest(int[] arr) {
int m=0;
for(int i=1;i<arr.length;i++)
{
if(arr[m]>arr[i])
m=i;
}
return m;
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me