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;
public class Main
{
public Main()
{
}
/**
* @param args the command line arguments
*/
public static void 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;
public class GameEntry
{
protected String name;
protected int score;
//costructor
public GameEntry(String n, int s)
{
name = n;
score = s;
}
//retrieves the name
public String getName()
{
return name;
}
//retrieves the score
public int getScore()
{
return score;
}
//returns a string representation
public String toString()
{
return "(" + name + ", " + score + ")";
}
}
package highscores;
import java.util.Scanner;
public class Scores
{
public static final int maxEntries = 10;
protected int numEntries;
protected GameEntry[] entries;
//constructor
public Scores()
{
entries = new GameEntry[maxEntries];
numEntries = 0;
}
//accepts input from user for a new high score
public void 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
public void 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
public void 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))
throw new IndexOutOfBoundsException("Invalid index: " + i);
GameEntry temp = entries;
for(int j = i; j < numEntries - 1; j++)
entries[j] = entries[j+1];
entries[numEntries - 1] = null;
numEntries--;
return temp;
}
//displays high score list
public void displayScores()
{
String s = "[";
for(int i = 0; i < numEntries; i++)
{
if(i > 0)
{
s += ",";
}
s += entries[i];
}
System.out.println(s + "]");
}
}