• Post Reply Bookmark Topic Watch Topic
  • New Topic

Fixing a provided class  RSS feed

 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was provided a class with lots of intentional mistakes.

The goal was to fix it.

I've nearly fixed most of the errors, aside from attempting to println the intArray.

Can anyone provide insight to remedy this issue from inspecting my code?

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. fields need to be private
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?
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Pawel.

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.
Unti323434tled.png
[Thumbnail for Unti323434tled.png]
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Winston
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, line 9 does not declare a constructor as it probably should. This is a method.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your original code is incorrectly indented, with the last } above each other. If you correct the indentation you will be able to see what is going on much more easily.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the reason you haven't been given answers is that you should be able to work them out for yourselves. If you have a method called getResult, you should know what it does and what it returns without having to be told.
 
Liutauras Vilda
Marshal
Posts: 4658
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the replies! They are indeed all useful to me.

I've updated the code, but it won't compile because it cannot find the main class.

So I should obviously insert somewhere, and it produces errors throughout the entire thing.

Advice for the rookie?

My current code:

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need a main method to be able to compile a class.
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.
 
Liutauras Vilda
Marshal
Posts: 4658
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Advice for the rookie?
I'd start over.

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)
2 methods

Try to build them from the given scratch.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cory Marcus wrote:. . . but it won't compile because it cannot find the main class.

So I should obviously insert somewhere, . . .
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.

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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!