2. variable names should begin with lower case
3. line 10 is wrong (I'll let you figure out why, ask if you don't see it)
4. for loop should be in a method (also indentation is wrong)
5. the for loop prints the entire array three times. Is it what you need?
6. printValue and getValue are private methods that are not used anywhere
Do you have a documentation for this class? What it is supposed to do?
Just wrapping my mind around these issues. Java is still relatively new to me.
That's a large portion of the problem. The course which has provided me with this code to fix hasn't actually told me what it's meant to do. At all.
The other students in my class have raised questions, but the person in charge seems to think we don't need more information.
Please see the original code attached.
Paweł Baczyński wrote:4. for loop should be in a method
@Cory: Actually, ALL logical code - ie, anything that is not a declaration - must be in either a method, a constructor, or an initializer block.
And since the latter are extremely rare (and actually worth avoiding if you can), you can shorten that to "All logical code must be in either a method or a constructor" for now.
Cory Marcus wrote:I was provided a class with lots of intentional mistakes.
Cory Marcus wrote:I've nearly fixed most of the errors
Those two are different. If you're talking about mistakes - lots of them.
If about errors (compilation errors) - actually just a few.
And in your code you posted the error is that "for" loop is not in a method - and that is it, the rest is fine.
You better ask your instructor, what they exactly expecting from you.
I would create a separate class, put main method there. Inside main method I would create an instance of ClassWithErrors and test if it works correctly.
You put the for loop inside a private constructor. This is wrong. Put in into some method. I'd go for public void printValues().
The for method should print elements from the array.
Your fields are still public. They should be private.
Your methods are private. They should be public.
You need to fix a declaration at line 8 so it is a public constructor, not private method.
I'd start over.
Advice for the rookie?
Read again what task says:
1. Write the erring section of code and explain what the error is.
It suggests, that understanding about error could be different. If you assume, that:
is meant to be constructor, then there is an error, because "void" shouldn't be there. But you have to explain that.
If you assume and state that you want to treat it as a method, that is perfectly fine, method could be named the same as a class name, but it is a bad practice. So better assume it as a constructor.
About "main" method you're likely mistaken. It is not needed there. "main" method is to execute your class, but it is perfectly fine to have a class without "main" method.
So, this class has:
2 member variables
2 constructors (overloaded)
Try to build them from the given scratch.
That is not correct. You can happily compile code without a main method, you simply can't execute it. That is something different. Nowhere in the question does it say to execute that code, so I suggest you shouldn't add a main method.
Cory Marcus wrote:. . . but it won't compile because it cannot find the main class.
So I should obviously insert somewhere, . . .
And to add to what Paweł has already told you, what on earth does line 22 mean? Where does the private constructor in line 20 come from? You are supposed to find the mistakes in the original code, not write something completely different.