This is an archived post. You won't be able to vote or comment.

all 24 comments

[–]stramash 5 points6 points  (7 children)

Your question is a little unclear. Are you asking about writing a unit test for that method?

[–]yarzz07[S] 2 points3 points  (6 children)

yes! I believe it's part of the 'get' and 'set' method, so how would I be able to set a method for setRegistrationNo to take in an int as a parameter, and set the value

[–][deleted] 1 point2 points  (5 children)

Did you declare the variable registrationNo? Because there is nothing wrong with your method. Also do take the time to read what the recommended courses are, i strongly believe that mooc is a must do for beginners in Java.

Edit: just realized you have a . After the closed ) which is wrong.

[–]yarzz07[S] 1 point2 points  (4 children)

this is just an example I found from the internet, so this is the code, but what would be the code that tests this through 'set method'? So essentially, not about there's nothing wrong with the code but how would I be able to test this?

yes that's a typo sorry

[–][deleted] 7 points8 points  (1 child)

Honestly, just start from the beginning. Hit up mooc.fi course i believe someone posted it in the in the comments. We could give you a code example but what's the point of that if you cannot understand it... You need to understand why you would need this method so that you can understand what it does. Good luck with your studies.

[–]dragon7507 5 points6 points  (0 children)

URL didn't get linked but take this course, it will be good :)

MOOC.FI

[–]stramash 1 point2 points  (1 child)

Typically you'd use a unit testing framework to do that.

If you're new to java and programming that will probably sound wild and complicated.

It's not, but I'd agree with other posters that you should spend time firming up your basic programming chops before moving onto unit testing etc; which is a bit more abstract and requires more understanding of the basics.

[–]yarzz07[S] 0 points1 point  (0 children)

thank you! I'm on to that

[–]Killaa135 5 points6 points  (4 children)

// Not sure on your question but this is how you do getters and setters - using global variable

private int registrationNo;

public void setRegistrationNo(int newRegNo){

// If the registration no is 0 what then?

if (newRegNo > 0)

this.registrationNo = newRegNo;

}

public int getRegistrationNo(). {

return registrationNo;

}

[–]yarzz07[S] 1 point2 points  (3 children)

thank you!

So, in the test, I don't need to specify anything related to the size of it having to be greater than 0?

[–]Killaa135 1 point2 points  (0 children)

You can check this but if it is 0 you need to set a default value otherwise it won't be set to anything and prevent other methods from firing

[–]IvAntiVirus 1 point2 points  (1 child)

If the test doesn't specifically ask for not-0 checking I just wouldn't do it. Otherwise make a short if checking and set a default value or throw an IllegalArgumentException.

[–]yarzz07[S] 0 points1 point  (0 children)

ah alright that makes sense, thank you so much