• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Deep Saxena
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Deep"

Please check your private messages.
[ June 12, 2008: Message edited by: Paul Sturrock ]
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 171
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!