SRL Archive - Java Help and Tutorials

Constructor Confusion


Le Jingle

2012-11-15 22:31:02

I'm trying to make a char counter for a given text file in java.

I've a pretty good idea on how to accomplish this, however I'm stuck when a requirement is to have a constructor in the class named differently from the class name?

Ex.

public class LetterCounter {
public LetterInventory() {
//
}
}


Edit: I may have read the question wrong, as I've come across several sources saying you must have the same name as the class..

If I want to use letterCounter later on, as if I wanted to call such a line as:

LetterInventory inventory = new LetterInventory();
– > how would I go about this? I thought that I had to create a construct and store the values inside that construct?

Can I do this with methods (so I can use different names)?

Brandon

2012-11-16 00:17:46

What? Don't understand the question..

Simply don't call the constructor.. Objects in java are just references/pointers to a memory location. If you don't set that reference to a new instance, it does nothing and can be set later if the class is not static.



ClassName Variable = null;

//Do other stuff here...

Variable = new ClassNameConstructor(.....);

Le Jingle

2012-11-16 00:27:19

Here's the layout that may help
  • LetterInventory() – constructs the inventory with a maximum of 26 different letters.
  • void countOccurrences(Scanner file) – counts the frequency of each letter in a file
  • void displayTable() – displays the frequencies of each letter in the inventory
  • void resetInventory() – resets all of the counts to 0

The line that's given to us in main, that I bring into question is:
LetterInventory inventory = new LetterInventory();

The class is named, "LetterCounter" and I think that conflicts with line above?
Or can I create a method that delivers the same line as italicized above?

Brandon

2012-11-16 00:42:51

From: Le Jingle
Here's the layout that may help
  • LetterInventory() – constructs the inventory with a maximum of 26 different letters.
  • void countOccurrences(Scanner file) – counts the frequency of each letter in a file
  • void displayTable() – displays the frequencies of each letter in the inventory
  • void resetInventory() – resets all of the counts to 0

The line that's given to us in main, that I bring into question is:
LetterInventory inventory = new LetterInventory();

The class is named, "LetterCounter" and I think that conflicts with line above?
Or can I create a method that delivers the same line as italicized above?



You can create a method if you like OR if you know inheritance then:


import java.util.Scanner;

public class LetterCounter {
public LetterCounter() {

}

void countOccurrences(Scanner file) {

}


}

public class LetterInventory extends LetterCounter {

private char Letters[] = new char[26];

public LetterInventory() {
super();

for(char I = 'A', J = 0; I < 'Z'; ++I, ++J) {
Letters[J] = I;
}
}

public void displayTable() {

}

public void resetInventory() {

}
}

public static void main(String[] args) {
LetterCounter C = new LetterInventory();
}


It all depends..With the above, you can create a letterinventory through the counter class.

I'd say just stick to a method. Why is the classname different? Is it two classes? If it is then just do:


public class Counter {
private LetterInv = new LetterInv();

public Counter() {
}
}


IMHO, I think you're naming your classes wrong.
I think everything you have to do should be in a class called LetterInventory with a default constructor and the other stuff is just methods. Thus there should be no need for that LetterCounter class.

EX:


import java.util.Scanner;

public class LetterInventory {

private char Letters[] = new char[26];

public LetterInventory() {
super();

for(char I = 'A', J = 0; I < 'Z'; ++I, ++J) {
Letters[J] = I;
}
}

void countOccurrences(Scanner file) {

}

public void displayTable() {

}

public void resetInventory() {

}
}

Le Jingle

2012-11-18 15:42:57

Ah yes, thanks brandon!

I didn't know about inheritance, but I've got the basic idea of it, no idea on how I'd implement or remember it :p

HOWEVER: Our teacher named our code skeleton wrong, he named the class different from the constructors -.-
(he mentions that our class, in the instructions, should be LetterInventory, as the first constructor is named)

Thanks again!

Cheers,
Lj