Implement in Java Goal: Write a list of people ordered so that no one appears in
ID: 3838406 • Letter: I
Question
Implement in Java
Goal: Write a list of people ordered so that no one appears in the list before anyone he or she is less smart than. Problem: Provide a RESTful service which accepts a s a POST of JSON a list relationships [A, B] such that A is smarter than B. Return in JSON the list sorted by smartness, with the smartest person first. Example input: (Einstein is smarter than Feynmann, Feynmann is smarter than Gell-Mann etc.) {inList :[ {smarter : [ Einstein, Feynmann ]}, {smarter : [ Feynmann, Gell-Mann ]}, {smarter : [ Gell-Mann Thorne]}, {smarter : [ Einstein, Lorentz ]}, {smarter : [ Lorentz, Planck ]}, {smarter : [ Hilbert, Noether ]}, {smarter : [ Poincare, Noether ]} ]} Example output: {outList: [ Einstein, feynmann, Gell-Mann, Thorne, lorentz, Planck, Hilbert, Poincare. Noether ]} Erroneous input (e.g. malformed JSON) should be handled gracefully. For reference see Cormen Ch. 22 Deliverables: Submit to the Blackboard by the due date: An HTTP URL to a RESTful service which must remain up and running 24/7 until grading is complete. Graders will invoke your service with a tool such as curl or Postman at a time of their choosing. AZIP file containing your source code, written in any language you choose.Explanation / Answer
package myProject;
import java.io.*;
//Class Smsrter definition
public class Smsrter
{
//Main method
public static void main(String ss[])
{
//Initializes input String list
String [][] inputList = { {"Einstein", "Feynmann"}, {"Feynmann", "Gell-Mann"}, {"Gell-Mann", "Thorne"},
{"Einstein", "Lorentz"}, {"Lorentz", "Planck"}, {"Hilbert", "Noether"}, {"Poincare", "Noether"}};
//Creates an output string list
String [] outputList = new String[12];
//Counter for the output list initializes to zero
int counter = 0;
//Flag to check the availability of the string
int flag;
//Stores the first smarter person name
outputList [counter++] = inputList[0][0];
//Loops till end of the list
for(int x = 1; x < inputList.length; x++)
{
//Initializes the flag to zero
flag = 0;
//Loops till end of output list size
for(int z = 0; z < counter-1; z++)
{
//Checks if the current smart person already available in the output list
if(outputList[z].equals(inputList[x][0]))
{
//Set the flag to 1
flag = 1;
//Come out of the loop
break;
}//End of if
}//End of inner for loop
//Checks if the flag is one the smarter person name is already in the list
if(flag == 1)
{
//Stores the previous less smarter person name in the output list and increase the counter for output list by one
outputList[counter++] = inputList[x-1][1];
flag = 0;
}
//If flag is zero
else
//Stores the current smarter person name in the output list and increase the counter for output list by one
outputList[counter++] = inputList[x][0];
}//End of outer for loop
//If the last less smarter person name is not available in the list then add it to output list
for(int z = 0; z < counter; z++)
{
//Checks if the current smart person already available in the output list
if(!(outputList[z].equals(inputList[inputList.length-1][1])))
{
outputList[counter] = inputList[inputList.length-1][1];
}//End of if
}//End of loop
System.out.print(" Final Result: ");
//Displays the smarter person list
//Loops till end of output list length
for(int x = 0; x < outputList.length; x++)
{
//Try block for num pointer exception
try
{
//Checks if the contents of output list is null then dont display
if(outputList[x].equals(null))
;//Do nothing statement
else
//If it is not null then display
System.out.print(outputList[x] + " ");
}//End of try block
//Catch
catch(Exception e)
{ }
}//End of for loop
}//End of main method
}//End of class
Output:
Final Result: Einstein Feynmann Gell-Mann Thorne Lorentz Hilbert Poincare Noether
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.