Java

Archived from the original Sajha.com — preserved as posted, replies can no longer be added here.
Start a New Discussion
Archived Post

 Hello, Please help me with this Java assignment. Thank you in advance! :)   Exercise 1: Extending HashMap   Your goal is to extend the java defined HashMap class to add the ability to store multiple objects. To do this, you will edit MultiHashMap.java to both add new methods and override two methods from HashMap.   (1) You need to implement four new methods (see details in the MultiHashMap.java file) <!--[if !supportLists]-->a)  <!--[endif]-->getAll(Object key) <!--[if !supportLists]-->b)  <!--[endif]-->putAll(Object key, Collection values) <!--[if !supportLists]-->c)  <!--[endif]-->contains(Object key, Object value) <!--[if !supportLists]-->d)  <!--[endif]-->remove(Object key, Object value)   <!--[if !supportLists]-->(2)  <!--[endif]-->You need to override two methods in the HashMap class (means methods with the same signature and return type). <!--[if !supportLists]-->a)  <!--[endif]-->get <!--[if !supportLists]-->b)  <!--[endif]-->put   The external behavior of the get and put methods should be the same as in HashMap, but internally they should facilitate the ability to keep track of multiple associations. The only place where MultiHashMap should differ from HashMap externally is in its compliance with the MultiMap interface (it means that MultiHashMap should implement MultiMap).   <!--[if !supportLists]-->(3)  <!--[endif]-->To test your implementation, compile and run the file MultiHashMapTest.java.   Read the description for each method you are supposed to implement (inside the source code of MultiHashMap.java) carefully before you start! Here is an additional example to show the expected effects of these methods:   (NOTE: the format *-*-* stands for a collection of objects. Choose an appropriate class to maintain this collection)   Suppose testObj is an object of the MultiHashMap class, and it contains no key-value pairs initially.   <!--[if !supportLists]-->1)    <!--[endif]-->testObj.put(“A”, “red”) this method returns NULL and now testObj contains an entry: “A”  “red” <!--[if !supportLists]-->2)    <!--[endif]-->testObj.put(“A”, “blue”) this method returns the string “red” and now testObj contains an entry: “A”  “red”-“blue” <!--[if !supportLists]-->3)    <!--[endif]-->testObj.get(“A”) this method returns the string “blue” <!--[if !supportLists]-->4)    <!--[endif]-->testObj.getAll(“A”) this method returns “red”-“blue” <!--[if !supportLists]-->5)    <!--[endif]-->testObj.putAll(“A”, “green”-“black”-“grey”) now testObj contains an entry: “A”  “red”-“blue”-“green”-“black”-“grey” <!--[if !supportLists]-->6)    <!--[endif]-->testObj.get(“A”) this method returns the string “grey” <!--[if !supportLists]-->7)    <!--[endif]-->testObj.contains(“A”, “green”) this method returns TRUE <!--[if !supportLists]-->8)    <!--[endif]-->testObj.remove(“A”, “black”) this method returns TRUE and now testObj contains an entry: “A”  “red”-“blue”-“green”-“grey” HINT: <!--[if !supportLists]-->1)    <!--[endif]-->Multiple objects can be organized inside a SINGLE object which is a member of the java collection framework. <!--[if !supportLists]-->2)    <!--[endif]-->Iterator can NOT guarantee the order of the objects returned from a collection. Consider how to reserve the ordering of the objects associated with a key.     Exercise 2: Sorting made easy   The class java.util.Collections (do not confuse this with the interface java.util.Collection) provides many useful static methods for use with Collections, Lists, and Sets, such as sort, and binarySearch.   In this exercise, you will utilize the Collections.sort() method on a List of your own abstract data type. Normally, Collections.sort(List) takes as argument one List of Comparable elements and modifies the List so that the elements are sorted. We wish to sort a collection RatNum objects, but RatNum does not implement the Comparable interface, so we cannot use Collections.sort.   We could simply modify the RatNum.java code, but assume that option is not available (often you will not be able to decide to modify a code because other people depend on the current version). The alternative is to call Collections.sort (List, Comparator). The Comparator is a class you implement that only needs a single method, public static int compare (Object o1, Object o2), which returns a negative number if o1 > o2, 0 if o1 == o2, and a positive number if o1 < o2.   Now the Comparator defined inside RatNumSortingTest.java simply returns 0 for its compare() method, treating all RatNums as equal values in the sorting. So your task is to modify the compare() method of RatNumSortingTest.java so that it follows the appropriate specifications of a Comparator.

java_help · Apr 12, 2011 5:32 PM · 13,831 views

8 Replies

Can you paste the code of  file MultiHashMapTest.java.?

bange · Apr 13, 2011 11:12 AM

// Add javadoc on ur own, test with ur test file, test file would have been helpful if you've pasted it here/, yata herne fursad nai choina, SB ma chat gardai masta, let me know how it goes, you know where to find me import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import org.apache.commons.collections.MultiMap; public class MultiHashMap extends HashMap implements MultiMap {     private static final long serialVersionUID = 1L;     public Object getAll(Object key)     {         return super.get(key);     }     public void putAll(Object key, Collection values)     {          super.put(key, values);     }     public boolean contains(Object key, Object value)     {         List list = (ArrayList) super.get(key);         if (list == null && list.size() < 1)         {             return false;         }         else         {             return list.contains(value);         }     }     public Object get(Object key)     {         List list = (ArrayList) super.get(key);         if(list == null)         {             return null;         }         else         {             return list.get(list.size() - 1);         }     }     public String put(Object key, Object value)     {         List list =(ArrayList) super.get(key);         if (list == null)         {             list = new ArrayList();         }         list.add(value);         super.put(key, list);         if (list.size() == 1)         {             return null;         }         else         {             return (String)list.get(list.size() - 2);         }      }     public Object remove(Object key, Object item)     {         List list =(ArrayList) super.get(key);         if (list == null && list.size() < 1)         {             return false;         }         else         {             return list.remove(item);         }     } } Last edited: 13-Apr-11 12:48 PM

prankster · Apr 13, 2011 12:45 PM

Thanks a lot Prankster! I really appreciate it..here is the fileMultiHashMapTest.java import java.util.*; public class MultiHashMapTest {     /** Objects to be used as keys */     private static final Object o1 = new Object();     private static final Object o2 = new Object();     private static final Object o3 = new Object();     private static final Object o4 = new Object();     /** Strings to be used as values */     private static final String s1 = "one";     private static final String s2 = "two";     private static final String s3 = "three";     private static final String s4 = "four";     /** Collections to be used to test */     private static final Collection c12 = new Vector();     private static final Collection c12n = new Vector();     /** Main entry point to program */     public static void main(String[] args) {      setupCollections();      try {          testMHM();      } catch (Exception excn) {          excn.printStackTrace();      }     }     public static void setupCollections() {      c12.add(s1);      c12.add(s2);      c12n.addAll(c12);      c12n.add(null);     }     public static void testMHM() throws CheckFailedException {      MultiHashMap mhm = new MultiHashMap();      check("Assigning value to a key that was not in before", mhm.put(o1, s1) == null);      check("Verifying association caused by put", s1.equals(mhm.get(o1)));      check("Verifying return value when reassigning a key to a value", s1.equals(mhm.put(o1, s2)));      checkEqualCollection("Verifying getAll method", c12, mhm.getAll(o1));      mhm.putAll(o3, c12);      checkEqualCollection("Verifying putAll method", c12, mhm.getAll(o3));      mhm.put(o3, null);      checkEqualCollection("Verifying the ability to add null values with put method", c12n, mhm.getAll(o3));      check("Verifying the contains method works for null", mhm.contains(o3, null));      check("Verifying the contains method works", mhm.contains(o3, s1));      check("Verifying remove method return value", mhm.remove(o3, null));      check("Verifying remove method works", !mhm.contains(o3, null));      checkEqualCollection("Verifying HashMap remove method return value", (Collection) mhm.remove(o3), c12);      check("Verifying HashMap remove method works", mhm.get(o3) == null);     }         public static void check(String message, boolean result) throws CheckFailedException {      if (result == false)          throw new CheckFailedException(message + " failed");      else          System.out.println(message + " passed");     }     public static void checkEqualCollection(String message, Collection c1, Collection c2) throws CheckFailedException {      check(message, c1.containsAll(c2) && c2.containsAll(c1));     }     private static class CheckFailedException extends Exception {      public CheckFailedException() { super(); }      public CheckFailedException(String message) { super(message); }     } }

java_help · Apr 13, 2011 1:24 PM

Answer to second. import java.util.Comparator; class NameComparator implements Comparator {     public int compare(Object ratNum1, Object ratNum2)     {         //parameter are of type Object, so we have to downcast it to Employee objects         String ratNum1Val = ((RatNum)ratNum1).getValue();         String ratNum2Val = ((RatNum)ratNum1).getValue();         if(ratNum1Val > ratNum2Val)             return 1;         else if(ratNum1Val < ratNum2Val)             return -1;         else             return 0;     } } Not sure how yourRatNumSortingTest.java, but you can sort using this class as below Arrays.sort(arrayOfRatNumObjects, new RatNumComparator());     Last edited: 13-Apr-11 01:42 PM

prankster · Apr 13, 2011 1:41 PM

Please help me with this..Thank you in advance :) In this lab, you are required to program with Swing and File I/O.   Your application will be a Graphical User Interface (GUI) that looks similar to these: <!--[if !vml]--><!--[endif]--> <!--[if !vml]--><!--[endif]-->   This GUI contains several components: <!--[if !supportLists]-->·      <!--[endif]-->a JTextArea component, in which you can type text or display content of a file. <!--[if !supportLists]-->·      <!--[endif]-->a JMenuBar component, which contains two JMenu components <!--[if !supportLists]-->1)    <!--[endif]-->a JMenu component which is labeled "File". This menu contains three items: "Open", "Save As" and "Exit". <!--[if !supportLists]-->2)    <!--[endif]-->a JMenu component which is labeled "Style". This menu contains two sub-menus: the JMenu "font-type" and the JMenu "textAreaColor". The "font-type" menu contains three items: "Plain", "Bold" and "Italic". The "textAreaColor" menu contains two sub-menus: the JMenu "Foreground-Color" and the JMenu "Background-Color", each contains four items representing the color: “Pink”, “White”, “Blue” and “Black”.   You are required to achieve the following tasks with these components: <!--[if !supportLists]-->(1)  <!--[endif]-->If you click “File”-> “Open”, you should be able to open a file window as below. You can open a file by either double clicking the file name or typing in the file name and hitting the “open” button, just as you normally do. The content of the file you selected should be shown in the text area. Only the content of a plain text file is required to be shown correctly. If the file doesn’t exist (double clicking the file name won’t has this problem, but if you type into the file name, it is possible), show a message box to indicate such an error. If you click “Cancel”, this window will be closed and nothing happens. <!--[if !vml]--><!--[endif]-->             HINT: Design of this “open” window is NOT your job; it’s already done in the JFileChoose class. Go to http://java.sun.com/j2se/1.4.2... to check what functions and fields this class provides. What you need to do is figuring out which file has been selected and how to read its context into the text area. As to the message box, refer to the JOptionPane class. <!--[if !supportLists]-->(2)           <!--[endif]-->If you click “File”-> “Save As”, you should be able to open a file window as below. After you double click a file name or type in a file name and click “Save”, the text currently shown in the text area should be saved in that file. You are NOT required to handle a double confirmation if the file has existed, so be careful that you may override some useful files (just be careful by yourself, this lab doesn’t require you to do anything about it). If you click “Cancel”, this window will be closed and nothing happens. <!--[if !vml]--><!--[endif]-->                 HINT: Same as task (1), this window is also manipulated by the JFileChoose class. <!--[if !supportLists]-->(3)           <!--[endif]-->If you click “File”-> “Exit”, your text editor window will be closed and the program stops. <!--[if !supportLists]-->(4)           <!--[endif]-->You should be able to change the font style, the foreground color and the background color of the text area.

java_help · Apr 14, 2011 1:56 PM

Java_help Your 60% solution is in my thread. IT Solutions Center

Ayus · Apr 14, 2011 3:18 PM

Thanks Ayush...much appreciated!

java_help · Apr 14, 2011 3:24 PM

...and don't think that the professor's Turn It In report will miss sajha.com! Similarity Index ma sabai dekhai dincha!

kailoketa · Apr 14, 2011 4:56 PM

This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.

Start a New Discussion

You might be interested in...

Recent Classifieds View all
Upcoming Events View all
Service Providers View all