JAVA: Matching system. The user enters their priority from 0 to 5 on four differ
ID: 3716205 • Letter: J
Question
JAVA: Matching system. The user enters their priority from 0 to 5 on four different questions. From a database of previous user responses, you find the one with the best match (smallest difference). For instance given the following database: ParticipantOne 3 4 2 5 ParticipantTwo 5 5 1 0 ParticipantThree 1 1 2 2 ParticipantFour 4 4 4 4 Participantfive 1 2 3 4 You would ask the user for their choices, for example 1 2 2 2 and would compare these against all the Participants and find the ParticipantThree is the best match.
Explanation / Answer
package javaapplication31;
import java.util.Scanner;
public class NearestNeibhour {
public static void main(String[] args) {
int resp[][],i,j,n,diff=0,diff1=1000,min=0,test[];
Scanner input=new Scanner(System.in);
System.out.print(" Eneter the number of participant:");
n=input.nextInt();
resp=new int[n][4];
test=new int[4];
System.out.print(" Eneter the responses of participant :");
for(i=0;i<n;i++)
{
System.out.print(" Enter the response of participant"+(i+1)+" ");
for(j=0;j<4;j++)
resp[i][j]=input.nextInt();
}
System.out.print("Eneter the responses to be find best match : ");
for(j=0;j<4;j++)
test[j]=input.nextInt();
input.close();
for(i=0;i<n;i++)
{
for(j=0;j<4;j++)
if(resp[i][j]>test[j])
diff+=resp[i][j]-test[j];
else
diff+=test[j]-resp[i][j];
if(diff<diff1)
{
diff1=diff;
min=i;
}
diff=0;
}
System.out.print(" Best Match is:");
for(j=0;j<4;j++)
System.out.print(resp[min][j]+" ");
}
}
Output:
run:
Eneter the number of participant:5
Eneter the responses of participant :
Enter the response of participant1
3 4 2 5
Enter the response of participant2
5 5 1 0
Enter the response of participant3
1 1 2 2
Enter the response of participant4
4 4 4 4
Enter the response of participant5
1 2 3 4
Eneter the responses to be find best match :
1 2 2 2
Best Match is:1 1 2 2 BUILD SUCCESSFUL (total time: 57 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.