Domanda

I have to write this program for lab. I have to basically illustrate exactly what a hashmap is (keys and values) and the basic operations of declaration, .add(), .get(), and how to get the keys and values from the map. You will then apply this to the frequency histogram problem using the woodchucks.txt input file. I've done all this but I'm stuck on how to write my method that prints the histogram. Can someone please help me out?

import java.io.*;
import java.util.*;

public class Lab8
{
public static void main(String args[]) throws Exception
{
    BufferedReader infile = new BufferedReader(new FileReader(args[0]));
    HashMap<String,Integer> histogram = new HashMap<String,Integer>();
    String word;
    while ( (word = infile.ready()) != null )
    {
        if(histogram.get(word)==null)
            histogram.put(word,1);
        else
            histogram.put(word, histogram.get(word)+1);

    }   
             // YOUR CODE HERE

    infile.close();
    printHistogram( histogram );

} // END MAIN
// YOU FILL IN THIS METHOD
// READ PROBLEM SPECIFICATION TO SEE WHAT IS THE 80% vs 100% CREDIT SOLUTION

private static void printHistogram( HashMap<String,Integer> hm )
{

    // YOU CODE HERE
}
} // END LAB8 CLASS

Would I print the histogram like this?

for ( int i = 0; i < histogram.length; i++ )
         {
         output += "\n" + i + "\t" + histogram[ i ] + "\t";

         for ( int j = 1; j <= histogram[ i ]; j++ ) 
È stato utile?

Soluzione 2

I figured it out and this is the answer...

import java.io.*;
import java.util.*;

public class Lab8
{
public static void main(String args[]) throws Exception
{
    BufferedReader infile = new BufferedReader(new FileReader(args[0]));
    HashMap<String,Integer> histogram = new HashMap<String,Integer>();
    String word;
    while ((infile.ready()))
    {
        word = infile.readLine();
        if(histogram.get(word)== null) //if the word your currently on is not duplicated
        {
            histogram.put(word,1);
        }
        else
        {
            histogram.put(word, histogram.get(word)+1);
        }
    }   
             // YOUR CODE HERE

    infile.close();
    printHistogram( histogram );

} // END MAIN
// YOU FILL IN THIS METHOD
// READ PROBLEM SPECIFICATION TO SEE WHAT IS THE 80% vs 100% CREDIT SOLUTION

private static void printHistogram( HashMap<String,Integer> hm )
{
    List <String> keys = new ArrayList<String> (hm.keySet());   
    Collections.sort(keys);
    for (String key: keys) 
    {
        System.out.println(key + "\t"  + hm.get(key));

    }
} 
}// END LAB8 CLASS

Altri suggerimenti

So if i'll be doing this, i would start from creating two classes, one where i could hold word and number occurrences such as my HistogramItem

public class HistogramItem implements Comparable<HistogramItem> {
        @Override
        public String toString() {
            return "HistogramItem [word=" + word + ", occurence=" + occurence
                    + "]";
        }

        public int getOccurence() {
            return occurence;
        }

        public void updateOccurence() {
            this.occurence++;
        }

        public String getWord() {
            return word;
        }

        public HistogramItem(String word) {
            super();
            this.word = word;
        }

        private final String word;
        private int occurence = 0;

        @Override
        public int compareTo(HistogramItem o) {

            if (occurence == o.occurence) {
                return word.compareTo(o.word);
            }
            return o.occurence-occurence;
        }

    }

and other one which will be my actual histogram

public class Histogram {
        private Map<String, HistogramItem> map = new HashMap<>();
        private List<HistogramItem> list = new ArrayList<>();

        public void addWord(String word) {
            HistogramItem item = map.get(word);
            if (item == null) {
                item = new HistogramItem(word);
                map.put(word, item);
                list.add(item);
            }
            item.updateOccurence();
        }

        public List<HistogramItem> getList() {
            Collections.sort(list);
            return list;
        }

    }

i'm using two collections, hashmap, because search for entry is much quicker than in list, but list is much easier to sort, list can be created and sorted when requested

How this fits your original excercise? Simple

public static void main(String args[]) throws Exception
{
    BufferedReader infile = new BufferedReader(new FileReader(args[0]));
    Histogram histogram = new Histogram();
    String word;
    while ( (word = infile.ready()) != null )
    {
       histogram.addWord(w);

    }   
             // YOUR CODE HERE

    infile.close();

//   Below line prints histogram, can be placed in printHistogram method
for (HistogramItem item : histogram.getList()) {
            System.out.println(item.toString());
        }

} // END MAIN
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top