Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

The output code is isolated in a single class that takes a Counter<T> and a PrintStream, for instance System.out, but it could just as well be a file or a socket stream. Instead of manually adding spaces to smooth out and align the numbers, you can specify a minimum width specify a minimum width in Java format strings. For instance, %5s will pad the text to maintain a width of 5 spaces.

The output code is isolated in a single class that takes a Counter<T> and a PrintStream, for instance System.out, but it could just as well be a file or a socket stream. Instead of manually adding spaces to smooth out and align the numbers, you can specify a minimum width in Java format strings. For instance, %5s will pad the text to maintain a width of 5 spaces.

The output code is isolated in a single class that takes a Counter<T> and a PrintStream, for instance System.out, but it could just as well be a file or a socket stream. Instead of manually adding spaces to smooth out and align the numbers, you can specify a minimum width in Java format strings. For instance, %5s will pad the text to maintain a width of 5 spaces.

used diamond notation where possible
Source Link
Adam
  • 5.2k
  • 1
  • 30
  • 47
public abstract class RandomDataSource<T> {
 protected static final Random random = new Random();
 public abstract T nextElement();
 public Iterable<T> nextElements(int numberOfElements) {
 Collection<T> elements = new ArrayList<T>ArrayList<>(numberOfElements);
 for (int i = 0; i < numberOfElements; i++) {
 elements.add(nextElement());
 }
 return elements;
 }
}
public class Counter<T> implements Iterable<T> {
 private Map<T, Integer> map = new HashMap<T, Integer>HashMap<>();
 public void increment(T element) {
 int count = map.containsKey(element) ? map.get(element) : 0;
 map.put(element, count + 1);
 }
 public int getCount(T element) {
 return map.get(element);
 }
 public Iterable<T> getLargestElements() {
 int largest = Collections.max(map.values());
 Collection<T> keys = new ArrayList<>();
 for (T key : map.keySet()) {
 if (map.get(key) == largest) {
 keys.add(key);
 }
 }
 return keys;
 }
 @Override
 public Iterator<T> iterator() {
 return map.keySet().iterator();
 }
}
public abstract class RandomDataSource<T> {
 protected static final Random random = new Random();
 public abstract T nextElement();
 public Iterable<T> nextElements(int numberOfElements) {
 Collection<T> elements = new ArrayList<T>(numberOfElements);
 for (int i = 0; i < numberOfElements; i++) {
 elements.add(nextElement());
 }
 return elements;
 }
}
public class Counter<T> implements Iterable<T> {
 private Map<T, Integer> map = new HashMap<T, Integer>();
 public void increment(T element) {
 int count = map.containsKey(element) ? map.get(element) : 0;
 map.put(element, count + 1);
 }
 public int getCount(T element) {
 return map.get(element);
 }
 public Iterable<T> getLargestElements() {
 int largest = Collections.max(map.values());
 Collection<T> keys = new ArrayList<>();
 for (T key : map.keySet()) {
 if (map.get(key) == largest) {
 keys.add(key);
 }
 }
 return keys;
 }
 @Override
 public Iterator<T> iterator() {
 return map.keySet().iterator();
 }
}
public abstract class RandomDataSource<T> {
 protected static final Random random = new Random();
 public abstract T nextElement();
 public Iterable<T> nextElements(int numberOfElements) {
 Collection<T> elements = new ArrayList<>(numberOfElements);
 for (int i = 0; i < numberOfElements; i++) {
 elements.add(nextElement());
 }
 return elements;
 }
}
public class Counter<T> implements Iterable<T> {
 private Map<T, Integer> map = new HashMap<>();
 public void increment(T element) {
 int count = map.containsKey(element) ? map.get(element) : 0;
 map.put(element, count + 1);
 }
 public int getCount(T element) {
 return map.get(element);
 }
 public Iterable<T> getLargestElements() {
 int largest = Collections.max(map.values());
 Collection<T> keys = new ArrayList<>();
 for (T key : map.keySet()) {
 if (map.get(key) == largest) {
 keys.add(key);
 }
 }
 return keys;
 }
 @Override
 public Iterator<T> iterator() {
 return map.keySet().iterator();
 }
}
added 21 characters in body
Source Link
Adam
  • 5.2k
  • 1
  • 30
  • 47

##Single Responsibility Principle1 and Separation of Concerns2 A good way to start off is to count the number of responsibilities of your class hasPractical4_Assessed:

##Single Responsibility Principle1 and Separation of Concerns2 A good way to start off is to count the number of responsibilities your class has:

##Single Responsibility Principle1 and Separation of Concerns2 A good way to start off is to count the number of responsibilities of your class Practical4_Assessed:

added 35 characters in body
Source Link
Adam
  • 5.2k
  • 1
  • 30
  • 47
Loading
Source Link
Adam
  • 5.2k
  • 1
  • 30
  • 47
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /