Re: Need basic help....
TheBigPJ wrote:
Ive got this far...
class q4 {
It is conventional to start classnames with a capital letter. e.g. Q4.
This avoids confusing other people who are reading youir code. "q4"
looks like a variable name or method name.
class Q4
char a;
public static void main(String[] args) {
I find it easier to read if you indent the contents of methods a further
four spaces. This is a Java convention.
q4 blah = new q4();
blah.newa();
blah.newa(); //must put command line here
I don't understand your comment.
From your homework description you need to instantiate a *separate*
object and initialise it with a specific value.
Q4 object2 = new Q4();
object2.newa('b');
Normally you allow for initialisation in the constructor. you can have
several constructors with different "signatures" (same name, different
number or types of argument)
Q4 object2 = new Q4('b');
blah.returna();
You are throwing away the returned value.
char retrievedChar = blah.returna();
There is a convention that "accessor" methods have names starting "get"
and that words within a variable name or method name are distinguished
by capitalizing the first letter of each word:
char retrievedChar = blah.getA();
Maybe you should use the result in some way?
System.out.println(retrievedChar);
or just
System.out.println(blah.getA());
However your lecturer said your object should provide a method that does
this. I'd create a printA() method.
}
public void newa() {
a = ' ';
}
public void newa(char a) {
this.a = a;
}
public char returna() {
See earlier notes about accessor names.
// Save to a file here
I'd have a separate saveToFile() method. Leave this until later.
return a;
}
}
But I don't think i am not on the right track :S
You're on the right track but could do some refactoring. I would proceed
in small steps and just try to satisfy the first one or two requirements
before adding more functionality.
You could be a bit more inventive with class and method names. If you've
already covered "inner classes" using one might help separate the
Question number from a meaningful name for the class that holds the
character.
class Q4 {
public static void main (String[] args) {
CharacterHolder firstCharacter = new CharacterHolder ();
firstCharacter.printValueToScreen();
CharacterHolder secondCharacter = new CharacterHolder ('b');
secondCharacter.printValueToScreen();
secondCharacter.printValueToFile("second character.txt");
}
class CharacterHolder {
char value;
public CharacterHolder() {
value = ' ';
}
public CharacterHolder(char newCharacter) {
value = newCharacter;
}
// etc etc
}
}
I find that using long meaningful names helps me understand and
structure my code better. Using short names can quickly lead to confusion.