Results 1 to 3 of 3
  1. #1
    CrimaChronic is offline Member
    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Default SetsAndMaps.java

    I was looking for some help on one of my projects. I believe the first half is correct, but there are some complications in my second one. My professor is not giving me the help I need, any and all help will be much appreciated. It must be something small i'm missing.


    1. Write a static method sortAndRemoveDuplicates that accepts a list of integers as its parameter and
    rearranges the list's elements into sorted ascending order, as well as removing all duplicate values from
    the list. For example, the list {7, 4, -9, 4, 15, 8, 27, 7, 11, -5, 32, -9, -9} would become {-9, -5, 4, 7, 8,
    11, 15, 27, 32} after a call to your method. Use a Set as part of your solution.
    2. Write a static method is1to1 that accepts a Map<String, String> as its parameter and returns true if the
    two keys map to the same value. For example, {Darwin=206-9024, Hawking=123-4567, Newton=123-
    4567, Smith=949-0504} should return false, but {Darwin=206-9024, Hawking=555-1234, Newton=123-
    4567, Smith=949-0504} should return true. The empty map is considered 1-to-1 and returns true.


    My code:

    import java.util.*;
    public class SetsAndMaps {
    public static void main(String[] args) {

    // Set <Integer>object2 = new TreeSet <Integer>();

    ArrayList<Integer> object1 = new ArrayList<Integer>();

    object1.add(7);

    object1.add(4);

    object1.add(-9);

    object1.add(4);

    object1.add(15);

    object1.add(8);

    object1.add(27);

    object1.add(7);

    object1.add(11);

    object1.add(-5);

    object1.add(32);

    object1.add(-9);

    object1.add(-9);

    sortAndRemoveDuplicates(object1);

    System.out.println(object1);

    Set<Integer> object = sortAndRemoveDuplicates(object1);

    System.out.println(object);

    Map<String, String> people = new TreeMap<String, String>();

    people.put("Darwin", "206-9024");

    people.put("Hawking", "123-4567");

    people.put("Newton", "123-4567");

    people.put("Smith", "949-0504");

    is1to1(people);

    System.out.println(is1to1(people));

    }
    public static Set<Integer> sortAndRemoveDuplicates(
    ArrayList<Integer> object1) {
    Set<Integer> object = new TreeSet<Integer>(object1);
    return object;
    }
    public static boolean is1to1(Map<String, String> people) {
    Collection<String> names = people.values();
    Iterator<String> iter = names.iterator();
    while (iter.hasNext()) {
    String element = iter.next();
    while (iter.hasNext())
    {
    String nextElement = iter.next();
    if (element == nextElement || names.isEmpty() == true) {
    return true;
    } else {
    element = nextElement;
    }
    }
    }
    return false;
    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    14,795
    Rep Power
    20

    Default Re: SetsAndMaps.java

    Also posted at Java SetsAndMaps.java

    See the response there.

    Please edit your post, format the code properly and wrap it in code tags.
    http://www.java-forums.org/misc.php?do=bbcode#code
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    10,094
    Rep Power
    17

    Default Re: SetsAndMaps.java

    Also cross posted in the wrong section at SetsAndMaps.java HELP please :3 (Beginning Java forum at JavaRanch)

    Nothing to do with Applets. Moving to New to Java.

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

Posting Permissions

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