Looking for some Feedback...

Hi. I'm new to coding in Java with a limited background in C++. Anyway, I just got my first Java program up and running and I'm looking for feedback regarding the structure of my program. I'm going from examples in my book and I need to learn Java before I transfer schools next term.Any feedback is much appreciated . *Disclaimer* My classes are taken from the book "Data Structures & Algorithms in Java".Thanks for your time.

//=================================

// Client Test Program

//=================================

package highscores;

import java.util.Scanner;

publicclass Main

{

public Main()

{

}

/**

* @param args the command line arguments

*/

publicstaticvoid main(String[] args)

{

Scores highscores =new Scores();

Scanner s =new Scanner(System.in);

int mainloop = 0;

while(mainloop == 0)

{

System.out.println("===HIGH SCORES===");

System.out.println("Press '1' to add an entry");

System.out.println("Press '2' to delete an entry");

System.out.println("Press '3' to display the high scores");

System.out.println("Press '4' to exit");

int choice = s.nextInt();

switch(choice)

{

case 1:

highscores.addInput();

break;

case 2:

highscores.removeInput();

break;

case 3:

highscores.displayScores();

break;

case 4:

++mainloop;

break;

default:

System.out.println("Error: not a valid menu option");

break;

}

}

}

}

package highscores;

publicclass GameEntry

{

protected String name;

protectedint score;

//costructor

public GameEntry(String n,int s)

{

name = n;

score = s;

}

//retrieves the name

public String getName()

{

return name;

}

//retrieves the score

publicint getScore()

{

return score;

}

//returns a string representation

public String toString()

{

return"(" + name +", " + score +")";

}

}

package highscores;

import java.util.Scanner;

publicclass Scores

{

publicstaticfinalint maxEntries = 10;

protectedint numEntries;

protected GameEntry[] entries;

//constructor

public Scores()

{

entries =new GameEntry[maxEntries];

numEntries = 0;

}

//accepts input from user for a new high score

publicvoid addInput()

{

Scanner s =new Scanner(System.in);

System.out.println("Enter your name: ");

String name = s.next();

System.out.println("Enter your high score: ");

int score = s.nextInt();

GameEntry newscore =new GameEntry(name, score);

add(newscore);

}

//adds a new score to the high score list

publicvoid add(GameEntry e)

{

int newScore = e.getScore();

if(numEntries == maxEntries)

{

if(newScore <= entries[numEntries-1].getScore())

return;

}

else

{

numEntries++;

int i = numEntries-1;

for(; (i>=1) && (newScore > entries[i-1].getScore()); i--)

entries[i] = entries[i-1];

entries[i] = e;

}

}

//accepts input from user to remove a high score

publicvoid removeInput()

{

Scanner s =new Scanner(System.in);

System.out.println("Enter the index of the score to remove: ");

int index = s.nextInt();

remove(index);

}

//removes and returns a high score at the specified index

public GameEntry remove(int i)throws IndexOutOfBoundsException

{

if((i<0) || (i >= numEntries))

thrownew IndexOutOfBoundsException("Invalid index: " + i);

GameEntry temp = entries[i];

for(int j = i; j < numEntries - 1; j++)

entries[j] = entries[j+1];

entries[numEntries - 1] =null;

numEntries--;

return temp;

}

//displays high score list

publicvoid displayScores()

{

String s ="[";

for(int i = 0; i < numEntries; i++)

{

if(i > 0)

{

s +=",";

}

s += entries[i];

}

System.out.println(s +"]");

}

}

[9090 byte] By [stevensantosgarciaa] at [2007-11-26 12:44:42]
# 1
> Hi. I'm new to coding in Java with a limited> background in C++. Anyway, I just got my first Java> program up and running I never knew that a beginner in java would start writing programs using Scanner and importing packages the very first program
qUesT_foR_knOwLeDgea at 2007-7-7 16:22:37 > top of Java-index,Java Essentials,New To Java...
# 2

This is odd. You're defeating one of the benefits of using a for loop here.

int i = numEntries-1;

for(; (i>=1) && (newScore > entries[i-1].getScore()); i--)

entries[i] = entries[i-1];

entries[i] = e;

Norweeda at 2007-7-7 16:22:37 > top of Java-index,Java Essentials,New To Java...
# 3
displayScores could be done in a single line:System.out.println(Arrays.asList(entries));where "Arrays" is "java.util.Arrays".
doremifasollatidoa at 2007-7-7 16:22:37 > top of Java-index,Java Essentials,New To Java...
# 4

> Any feedback is much appreciated .

Okay then. I won't hold back :-)

At a first rapid glance, you already exceed the average by not having everything inside one monster of a main method. You have paid some attention to readability, haven't you? Congrats.

Looking into it some more...

*) Bracing style violates the (meaning, Sun's) Java standard.

*) You have several superfluous comments. There's no point in documenting that a constructor is a constructor - and certainly not that it's a costructor ;-)

*) On the other hand, you do not have a class comment describing what the class' purpose is.

*) The way you break out of the user input loop is a bit weird; using a boolean instead of an int seems more logical. In fact why not just return in the '4' branch?

*) Your classes have protected fields for no reason. They ought to be private to start with. You can always relax the access later. Actually, fields should always always be private; if a subclass needs them, provide a protected accessor.

*) It's best to declare fields as "final" when they logically are.

*) You return a GameEntry from Scores.remove(int i). Why? You never use it.

*) Your adding/removing/keeping sorted bits are not so very nice. I don't like reading them even to see if they work correctly.

*) Which brings me to: you do not have any automated test code to check that all of the logic works correctly.

All in all, considering this is your first Java program ever, and especially if you do not have a lot of prior experience programming in general, I think it's really good and certainly exceeds the average.

Lokoa at 2007-7-7 16:22:37 > top of Java-index,Java Essentials,New To Java...
# 5
> *) Bracing style violates the (meaning, Sun's) Java> standard. Is that really an issue?. I know lots of people that use braces like that (including me). It's purely preference.
Norweeda at 2007-7-7 16:22:37 > top of Java-index,Java Essentials,New To Java...
# 6

> > *) Bracing style violates the (meaning, Sun's)

> Java

> > standard.

>

>

> Is that really an issue?.

No.

> It's purely preference.

Agreed.

When someone says "Any feedback is much appreciated" I take it to mean "read this and tell me anything that comes to your mind".

In general, when I do a code review, I do assume it is supposed to cover everything, including style.

Lokoa at 2007-7-7 16:22:37 > top of Java-index,Java Essentials,New To Java...
# 7

> When someone says "Any feedback is much appreciated"

> I take it to mean "read this and tell me anything

> that comes to your mind".

> In general, when I do a code review, I do assume it

> is supposed to cover everything, including style.

I don't think style matters at all as long as you convert it to project standards before you commit it.

I have 2 auto formats on my computer. One is my style and the other is the company style. A quick ctrl+shift+f and it's all ready to go. I say code any way you feel comfortable.

Norweeda at 2007-7-7 16:22:37 > top of Java-index,Java Essentials,New To Java...
# 8

> At a first rapid glance, you already exceed the

> average by not having everything inside one monster

> of a main method. You have paid some attention to

> readability, haven't you? Congrats.

Amen.

>

> Looking into it some more...

>

> *) Bracing style violates the (meaning, Sun's) Java

> standard.

But this is the way that God intended you to place braces. Pay no mind to this unwashed heathen.

> *) You have several superfluous comments. There's no

> point in documenting that a constructor is a

> constructor - and certainly not that it's a

> costructor ;-)

Agreed.

> *) On the other hand, you do not have a class comment

> describing what the class' purpose is.

Agreed.

> *) The way you break out of the user input loop is a

> bit weird; using a boolean instead of an int seems

> more logical. In fact why not just return in the '4'

> branch?

> *) Your classes have protected fields for no reason.

> They ought to be private to start with. You can

> always relax the access later. Actually, fields

> should always always be private; if a subclass needs

> them, provide a protected accessor.

Agreed.

> *) It's best to declare fields as "final" when they

> logically are.

As long as you know what that means.

Immutability is very good when possible. It's thread safe.

> *) You return a GameEntry from Scores.remove(int i).

> Why? You never use it.

> *) Your adding/removing/keeping sorted bits are not

> so very nice. I don't like reading them even to see

> if they work correctly.

> *) Which brings me to: you do not have any automated

> test code to check that all of the logic works

> correctly.

Amen. JUnit.

>

> All in all, considering this is your first Java

> program ever, and especially if you do not have a lot

> of prior experience programming in general, I think

> it's really good and certainly exceeds the average.

Far exceeds. Nice work.

%

duffymoa at 2007-7-7 16:22:37 > top of Java-index,Java Essentials,New To Java...
# 9
Thanks guys! I really appreciate all of the feedback. Seriously, you guys have made my day. Thanks for taking the time.
stevensantosgarciaa at 2007-7-7 16:22:37 > top of Java-index,Java Essentials,New To Java...