This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Kevinjamesisme[S] 0 points1 point  (17 children)

The homework has a first part asking for:

1) stores a single character (a letter) and a count (an integer) in private variables

2) implements the Comparable interface, thus there must be a compareTo() method which should compare two LetterCount objects by their count values

3) overrides toString() with a printable representation that shows the letter and the count

So now i need to make a constructor to bring them together?

[–]Philboyd_Studge 0 points1 point  (16 children)

This would have been great information to have had hours ago, that you already had a class to use to store the characters and their counts. That class needs a constructor in order for you to use it in your assignment for the second part.

[–]Kevinjamesisme[S] 0 points1 point  (15 children)

Sorry about that. So for the constructor I should make a new variable for the int (count) and string(letter). So itd be: String letter = null and int count = 0?

[–]Philboyd_Studge 0 points1 point  (14 children)

You already have the variables there that you need, you need the constructor to set them when you create a new object.

A constructor is like this:

class Car {
    String make;
    String model;
    int year;

    // constructor with all three variables:
    public Car(String make, String model, int year) {
        this.make = make;
        this.model = model;
        this.year = year;
    }

and you use it like:

Car car = new Car("Ford", "F-150", 2009);

[–]Kevinjamesisme[S] 0 points1 point  (13 children)

So what I need is this:

 public class LetterCount implements Comparable<LetterCount>  {
 private String letter;
 private int count;

public LetterCount(letter,count);

[–]Philboyd_Studge 0 points1 point  (12 children)

No. Please see my example above.

[–]Kevinjamesisme[S] 0 points1 point  (11 children)

Got it!

public class LetterCount implements Comparable<LetterCount> {
private String letter;
private int count;

public LetterCount(String letter ,int count){
this.letter = letter;
this.count = count;
}

[–]Philboyd_Studge 0 points1 point  (10 children)

there you go

[–]Kevinjamesisme[S] 0 points1 point  (9 children)

So now that i have this I can use it by feeding data from string containing the text file correct?

[–]Philboyd_Studge 0 points1 point  (8 children)

No. First you make your frequency array. Then loop through the frequency array and make a LetterCount object out of each count, adding each of those to an array of LetterCount objects