Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default Can somebody help me in this assignment?

    I would really appreciate if anybody at least tell me only pseudocode even it would be enough only the logic part at least 3-5 line code how to start how to compare the numbers each other.

    This is the assignment.
    -----------------------
    Write a program SortCities, reading any number of cities with their corresponding postal numbers from a text file. You may assume that each line in the file contains one postal number (integer) and one city (string) and that they are separated by a semicolon (;). Create a class City, representing a city. The class City should implement the interface Comparable.

    Reading cities from file: C:\Temp\orter.dat
    Number of cities found: 7

    90325 Umeå
    35243 Växjö
    23642 Höllviken
    72211 Västerås
    51000 Jönköping
    75242 Uppsala
    96133 Boden

    The output should be in order list like below;

    23642 Höllviken
    35243 Växjö
    51000 Jönköping
    72211 Västerås
    75242 Uppsala
    90325 Umeå
    96133 Boden

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Simply follow your instructions. It says to create a class called City. Have you done that? what information do you think the class needs to store (ie instance variables)? What methods do you think the class will need?

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your goal is to sort by only zip code? Do you understand how to implement an interface? If you do, look up the comparable interface. Tip, ints and strings have built in methods to apply the method for you. Look up what comparable requires you to define. This is not too challenging, give it a show and post your code if you get stuck.

  4. #4
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default I need Help :s !!!

    I have created an class CITY and i have implemented with Comparable, what should i do now ???
    please anyone who can help me ? i would really appreciate

    public class CITY implements Comparable{

    /**
    * @param args
    */
    public static void main(String[] args)
    {
    File file = new File("cities.txt");

    FileInputStream fin = new FileInputStream(file);

    (here should be the tricky part please anyone can help me here?)


    }

    @Override
    public int compareTo(Object arg0) {
    // TODO Auto-generated method stub
    return 0;
    }

    }

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    1. Call your class City.

    2. Use Generics. Implement Comparable<City>

    3. Change the parameter to your compareTo method.

    4. Actually write the code in the compareTo method to compare the 2 City objects and return an appropriate value.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The code you supplied looks terrible. I don't mean to be rude, maybe you aren't showing everything, but it really is not a class. A class should have instance variables, getters, setters, and other methods to manipulate the data.

    When naming a class you usually want to use a capital first letter then lowercase the rest of the word, all words in one class follow this form
    example:
    Java Code:
    class City
    class CityStreet
    class CitiesState
    A full caps name is generally used for constants.
    Java Code:
    Math.PI
    BorderLayout.CENTER

    Don't even worry about the comparator now, The first step you should work on is getting a solid class built, and test it.

    To get the ball rolling Ill give you some ideas

    The class should have 2 instance variables, they can be String, int, double, float, etc, that's your choice.

    in the class you should override toString() which will return a string
    Java Code:
    public String toString()
    The code can be designed however you please.

    Next build a getter and setter for each instance variable.

    Also, creating some constructors is always helpful

    Finally, test everything in main. Create an instance of the object, apply each method one at a time printing what the method does as well.

    After this you are ready to implement the comparable.

  7. #7
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default some help in code !!!

    public class CITY implements Comparable<CITY>{

    int postalnumber;
    String city;

    public static void main(String[] args) {



    }


    public String getcity()
    {
    return city;
    }

    public int getpostalnumber()
    {
    return postalnumber;
    }

    @Override
    public int compareTo(CITY o) {
    // TODO Auto-generated method stub
    return 0;
    }

    }

    On the assignment it says to read from file the data and to order them from smallest to greatest zipcode and city should contain the file.
    I need also some code instead of theory.

    This is the assignment.
    -----------------------
    Write a program SortCities, reading any number of cities with their corresponding postal numbers from a text file. You may assume that each line in the file contains one postal number (integer) and one city (string) and that they are separated by a semicolon (. Create a class City, representing a city. The class City should implement the interface Comparable.

    Reading cities from file: C:\Temp\orter.dat
    Number of cities found: 7

    90325 Umeå
    35243 Växjö
    23642 Höllviken
    72211 Västerås
    51000 Jönköping
    75242 Uppsala
    96133 Boden

    The output should be in order list like below;

    23642 Höllviken
    35243 Växjö
    51000 Jönköping
    72211 Västerås
    75242 Uppsala
    90325 Umeå
    96133 Boden
    Last edited by lulzim; 02-23-2011 at 01:44 PM.

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,685
    Rep Power
    8

    Default

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The theory is whats important, if you copy from us the basics you will never be able to understand the more advanced concepts. I could create this class in about a minute, and so can you if you think about it. I gave you everything to make, you came back with instance variables, and getters.

    Try adding some setters and constructors, then TEST it in main.

    After you have all that you should google compareTo, or Comparable. You need to design the class correctly before reading it from the file. After you complete the main class structure you are 75% done. The rest simply comes down to implementing compareTo(Which is quite simple), and reading from a file, storing the items somewhere, and sorting them.

  10. #10
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default

    hehe,


    public class CITY implements Comparable<CITY>{

    int postalnumber;
    String city;

    public class CITY(int postalnumber, String city)
    {
    this.postalnumber=postalnumber;
    this.city=city;
    }


    public static void main(String[] args) {
    CITY c1= new CITY(250,"Vaxjo");
    CITY c2= new CITY(255,"Goteborg");
    CITY c3= new CITY(260,"Lund");

    }


    public String setcity(String city)
    {
    return this.city=city;
    }


    public String setpostalnumber(Int num)
    {
    return this.postalnumber=num;
    }



    public String getcity()
    {
    return city;
    }

    public int getpostalnumber()
    {
    return postalnumber;
    }

    @Override
    public int compareTo(CITY o) {
    return this.postalnumber - o.postalnumber ;
    }

    }

    What else should i do please?

  11. #11
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    The suggested way of completing the assignment is total overkill. Just throw the pairs into a java.util.TreeMap, with the location code as the key. Then iterate over the map entries and print out the goods. Done.

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Dont listen to the above poster. Stick with doing it this way to help you understand what is happening, if you dont understand the basics well the hard parts will be much more difficult. You are pretty much done, the method compareTo can take an item of type city. From there, google Integer.compareTo(). That can be used in your method

  13. #13
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default

    U people didnt get my point i think.
    The problem i should solve is to order numbers from the file that there are in line by line from file and not comparing objects.
    What i have to do is to read that file and to order lines by postal number of cities.
    Hope u got now the point

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Do you want it sorted by postal number?

    if it reads the following items from the list
    Java Code:
    12345 city1
    23456 city 2
    13456 city 3
    it should produces
    Java Code:
    12345 city1
    13456 city 3
    23456 city 2
    If thats what you wanted I understood your question and led you in the correct direction to solve it.

    It's important to understand the basics of designing a class before moving onto sorting.

    My goal was to push you towards correctly designing the class, and from there implementing the comparator.

    This new post confuses me, it seems like you are saying you are supposed to order by line number of the file, then you say by zip code.

    By the way, your final posted code seems nearly correct.
    Last edited by sunde887; 03-01-2011 at 02:52 AM.

  15. #15
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by lulzim View Post
    U people didnt get my point i think.
    The problem i should solve is to order numbers from the file that there are in line by line from file and not comparing objects.
    What i have to do is to read that file and to order lines by postal number of cities.
    Hope u got now the point
    BWAHAHAHAHAHA!

    Nice.

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by lulzim View Post
    U people didnt get my point i think.
    The problem i should solve is to order numbers from the file that there are in line by line from file and not comparing objects.
    What i have to do is to read that file and to order lines by postal number of cities.
    Hope u got now the point
    We understand just fine. It is you who doesn't understand. Each City object has a postcode. In the compareTo method you compare the postocde of the current City object to the postcode of the other City object that was passed as a parameter.

  17. #17
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    I feel like pointing out that is whole task can be done by a simple

    sort -k 1 orter.dat > output.dat

  18. #18
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Well gold star for you, we are so impressed.
    [/sarcasm]

    However this is an assignment to learn some simple Java skills not what is the most efficient way to achieve blah.

  19. #19
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Indeed. That is why earlier I was pushing him towards properly creating the class first. It may have been overkill, but it's such a basic concept if shouldn't take more than a few minutes. It is obvious that the op is a beginner and it will help him more to practice and solidify his understanding of the basics, rather than worrying about whats more efficient.

  20. #20
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default

    Ok if u think it takes only few minutes then, why u are not showing me i need actuallly the code most.
    And i dont i have to create any object, it would be simple enough just to read a file from main method and the numbers(postal code ) that is before city to order them in order way.
    This is very good example:

    if it reads the following items from the list
    Code:
    12345 city1
    23456 city 2
    13456 city 3
    it should produces
    Code:
    12345 city1
    13456 city 3
    23456 city 2

    Can u code this?

Page 1 of 2 12 LastLast

Similar Threads

  1. assignment help
    By esallender in forum New To Java
    Replies: 4
    Last Post: 10-25-2010, 12:10 PM
  2. help with an assignment!
    By Tek in forum New To Java
    Replies: 6
    Last Post: 10-24-2010, 11:32 PM
  3. Need help with assignment! please
    By runawaykinms in forum Java Applets
    Replies: 2
    Last Post: 10-06-2010, 09:58 AM
  4. GUI First Assignment-DUE 8/1/08
    By ljk8950 in forum AWT / Swing
    Replies: 2
    Last Post: 08-01-2008, 04:23 AM
  5. First GUI Assignment
    By ljk8950 in forum New To Java
    Replies: 1
    Last Post: 07-31-2008, 07:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •