Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Can anyone please help me urgently with the algorithm or code for this scenario. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Can anyone please help me urgently with the algorithm or code for this scenario." Watch "Can anyone please help me urgently with the algorithm or code for this scenario." New topic
Author

Can anyone please help me urgently with the algorithm or code for this scenario.

Deep Saxena
Greenhorn

Joined: Jun 12, 2008
Posts: 3
Write a java program using following details. You are free to make all necessary assumptions. All the assumptions made should be documented.

There are four scientists who need to be ordered according to their smartness (smartest to least smart). There is a text file "smart.txt" in the following format:

Satish Arun
Ramesh Suresh
Ramesh Satish
Suresh Satish

Each line has a pair of names, separated by an arbitrary number of white spaces (' '), where the person named by the first element of the pair is smarter than the person named by the second element of the pair. There is no limit to the number of such pairs listed in the file, but the listing would be sufficient to resolve the order of smartness of the four scientists.

Write a java program (ScientistResolver.java) that takes such a file and prints the list of all the distinct scientist names, without duplicates, one per line, ordered according to their smartness, as below.

Usage:
java ScientistResolver smart.txt

Result:
Ramesh
Suresh
Satish
Arun
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
First of all Ease Up

Secondly, which part are you having trouble with ?

If it's the reading of the file, then you need to look at a Java I/O tutorial.

Once you've read the file you need to parse each line. Look at the String class for some useful methods to do this.

As for the sorting, the simplest way would be to use a List and just move the scientists around in the List based on each line of the file.

I would give you some relevant links, but I can't connect to java.sun.com at the moment. That's a good place to find useful information.
[ June 12, 2008: Message edited by: Joanne Neal ]

Joanne
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

"Deep"

Please check your private messages.
[ June 12, 2008: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

As you are a beginner i would like to point out few links you should go through before asking questions in javaranch.

  • HowToAskQuestion
  • In particular,ShowSomeEffort


  • You can also go through this link,Java I/O to get started.
    Geoffrey Falk
    Ranch Hand

    Joined: Aug 17, 2001
    Posts: 171
    Define a comparator that returns the correct value for each pair of names. Then use Collections.sort() will sort the list for you.

    It assumes that the list can be well-ordered (There isn't a circularity).

    Geoffrey


    Sun Certified Programmer for the Java 2 Platform
     
    I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
     
    subject: Can anyone please help me urgently with the algorithm or code for this scenario.
     
    Similar Threads
    Help with priority queues in simulations. H/W
    help regarding java code for x-y routing algorithm
    Sun Cirtification
    BufferedReader...
    Need logic for this game.