I'm trying to learn Java (albeit rather slowly). I'm in a computer science class at my highschool and I've written this little bit of code for a project. Basically it is a word guessing game where if you get a letter correct it reveals it. If you get a letter correct in the wrong spot it marks it with a +, and if the letter is not present at all it marks it with a *.I'm wondering if anyone can give me feedback as to how I can improve this code.Here is the assignment page if I didn't do an adequate job of explaining what the goal was:public class main {    private int length;    private String word;    private String result;    private String buffer;    public main(String aword) {        result = ;        word = aword;        length = word.length();        buffer = ;    }    public String guess(String aguess) {        for(int i = 0; i < length; i++) {            buffer = aguess.substring(i, i+1);            if(word.indexOf(buffer) == i)                result = result + buffer;            else if ((word.indexOf(buffer) != -1))                result = result + +;            if ((word.indexOf(buffer) == -1))                 result = result + *;        }        return result;    }}
Word Guessing Game
java;beginner;strings;homework
As was already brought out in the comment part of the requirement was to name your class HiddenWord. Granted you are in school and it is probably not going to be used much outside of your class room it is good practice to stick with the requirements...ESPECIALLY if you want to make this a career!Enough on that though. What I typically do with people who are new to a language is mention the wonders of unit testing, and writing tests and so forth. Seeing as I deleted Netbeans, and IntelliJ is taking a year to download I guess I'm forced to use Notepadd++ and hope that my syntax is close enough. What is Unit testing, and why should you care?Unit testing is a fast and reliable way (albeit tricky) to verify functionality of your code. The code is typically separated from production code so you don't have to include it in the finished product. There are different types of unit testing and different ways to go about doing the same thing. They all have the same goal in mind. Make sure your code works as expected Now as for why you should care about it the list could go on for some time. Some practical reasons are that it is a fast way to get feedback if you programmed something correctly (or incorrectly). It is a form of documentation (for others who may review your code).There are an exhaustive list of books, blogs, and videos on the internet about it, so I'll just stick with the issue at hand. I'm going to go out on a limb and assume that you verified the results with your eyes by typing in input. and while this is all well in good, it is comparatively slow. Consider the following TestNG test (there are also other test frameworks available such as JUnit, but I am unfamiliar with them)Just so you know I'll be using TestNG as my testframework, and this is what the entire test class looks like (without the tests)import org.testng.annotations.BeforeMethod;import org.testng.annotations.Test;import static org.testng.Assert.*;/** * Created by Robert on 5/10/2015. */public class HiddenWordTest {    @BeforeMethod    public void setUp() throws Exception {    }    @Test    public void testGetHint() throws Exception {    }}Now comes the hard/fun part. Writing a test that is simple to pass, but also is a baby step to completing your goal. Here is a easy one. The hard part is only write enough code to make this test pass, and no more!@Testpublic void testWhenLetterIsFoundGetHintReturnsLetter() throws Exception {    HiddenWord puzzle = new HiddenWord(A);    String hint = puzzle.getHint(A);    assertEquals(A, hint);}that test takes 0.387 seconds to run. What does it do? Well I create a new instance of HiddenWord with the hidden word being simply A. I then get the hint for a letter A. well.. according to your specifications that should return simply A. Next test. Wrong letter, and the secret word does not contain the specified character at all.@Testpublic void testWhenLetterIsNotFoundHintReturnsStar() {    HiddenWord puzzle = new HiddenWord(A);    String hint = puzzle.getHint(B);    assertEquals(hint, *);}So Those 2 tests take 0.35 seconds to run.. (Told you same time). The next test is a little harder, but is essentially the last step.@Testpublic void testWhenLetterIsFoundInWrongPlaceHintReturnsPlus(){    HiddenWord puzzle = new HiddenWord(AB);    String hint = puzzle.getHint(BB);    assertEquals(hint, +B);}I got that one to pass, and those 3 tests still only take 0.35 seconds to run. Since I'm confident now that my HiddenWord class is complete I run it against the table that is in your book.@Testpublic void TestFromBook(){    HiddenWord puzzle = new HiddenWord(HARPS);    assertEquals(puzzle.getHint(AAAAA), +A+++);    assertEquals(puzzle.getHint(HELLO), H****);    assertEquals(puzzle.getHint(HEART), H*++*);    assertEquals(puzzle.getHint(HARMS), HAR*S);    assertEquals(puzzle.getHint(HARPS), HARPS);}and for me, it was no big surprise that the test passed first time with no code changes. And what's more all 4 tests pass in 0.35 seconds.As for your code itself, java uses pascalCase (Where the first word of a multiword variable is lower case, and each word's first letter after is uppercase) for variable and field names. aword would be aWord.Since I was so heavy about speaking about time. How would your solution fair if the word to guess was HUGE. I mean.. REALLY REALLY BIG??? Say 1 million characters? As I'm typing this I put your solution under test. I create a 1 million character string, as a hidden word, then use your solution against another 1 million character word. (Its still running).. ok i canceled it after 166 seconds. I then replaced your solution with mine and ran it again. mine finished in about 0.2 seconds. To see your numbers with incremental times it is like thisString Length:20000 Time:169.289960 ms String Length:40000 Time:481.035566 ms String Length:80000 Time:1739.349658 ms exponentially getting bigger. For your purposes this is probably not going to be a problem, but it is something to consider when not only testing, but programming with data in general.