This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

How do I create a class in java?  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a) Create a class “Circle” that should include
• A private attribute “radius”
• A constructor that receives a parameter “radius”
• A method that returns the radius “getRadius()”
• A method that sets a new value on the radius setRadius()
• A method to return the area
• A method to return the circumference


I'm sorry for my bad indentation in the code below. I'm new to Java so I have obviously no idea what im doing. But I tried my best to do the assignment.
Would be awesome if anyone could look at my code and give some feedback on what I did wrong. Thanks.


 
Marshal
Posts: 61722
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Danielx Anderssonx wrote:. . . I'm sorry for my bad indentation in the code below.  . . .

No need to apologise to us; it is you who will suffer from inconsistent indentation.
Don't try writing out π; use this instead.

Actually, despite what you said, you got off to a good start. If you sorted out the indentation, the first 48% of your code would be correct ()
You will usually find that setXXX methods take a parameter and return nothing. And getXXX methods don't take a parameter and do return something. The getXXX methods use the fields of the current object and the setXXX methods alter the fields of the current object.

Many of us believe that setXXX() methods are not good object‑orientation (=OO); you can have your Circle change its circumference to -1234567890.0987654321 without your knowing anything about it A getXXX() method can also be less than ideal OO. But you have been told to write such methods, so you have to write them for full marks.
Remember, it is only the second just‑over‑half of your code needing rewriting, but you should review indentation for everything.
 
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I reformatted your code with the online tool found here: https://www.tutorialspoint.com/online_java_formatter.htm

Most modern IDEs like Eclipse and IntelliJ IDEA have a feature that reformats your code for your as well.


The comments you have in your code are a lot like somebody took a pad of Post-It notes and put labels like "My Shoes", "My Refrigerator", "My Car", "My Bed", "My Dog", "My Towel", etc. on everything they owned. Isn't a method named getCircumference() descriptive enough? I would think it is. Why would you need to put a comment there that basically says the same thing that the code does?

Your setRadius() method is also very unconventional, to say the least. Most setter methods are declared as void. That is, you don't really expect anything to come back when you call such a method because you just want it to *do* something instead of return something. Walk through the logic and talk it through. Listen to yourself and see if what you're saying makes sense. Here's how I would walk through that method:

Ok, so I tell the Circle that its radius is r. I create a new variable, newRadius, and set its value to whatever r is. Then I return the value of newRadius, which of course is just the same as the value of r. So, why did I even call the setRadius method? Kind of like I just spit into the wind, or threw a boomerang out and hit me in the head. What did I have to do all that for?
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compare what you wrote to this:

This code does not fulfill the requirements of your assignment but it's also arguably a better way to design a Circle object.
 
Campbell Ritchie
Marshal
Posts: 61722
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . A lot of the comments you have in your code are a lot like somebody taking a pad of Post-It notes and putting labels like "My Shoes" . . .

That is what PS told me off about here. What you start off with when furnishing a room is postits or similar for, “fridge goes here,” “microwave goes here,” “chair goes here,” “cooker goes here,” “table goes here,” etc. Then you put those items there, and later, as I said here, you remove them. OP has obviously reached the stage where they have completely served their purpose and he can remove the comments. Maybe we could do with some documentation comments, or shall we call that an advanced topic?
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thinking back to when I was a beginner and learning how to program, I know I wrote some comments that I would now consider redundant as well. My motivation back then was not the same as Piet's though and I don't entirely buy that argument. I would still argue that even a beginner should be able to see that they are writing a method like getCircumference without the little reminder that it is "A method that returns the circumference 'getCircumference()'." I don't know, maybe I'm expecting too much from people.

When I wrote redundant comments before, it was because that was what we were taught. One of my first programming languages was Pascal (I learned BASIC at the same time). This is how we were told to comment our code:

In BASIC, we had to use REM to comment code because we didn't have named subroutines:

Comments like these were reasonable and some redundancy was necessary when control structures contained many lines of code and it was difficult to easily discern the structure of the design/program. That was then though. Nowadays, we can write descriptive names with practically no limit to the length of the names we give other than what common sense tells us.

I could be wrong but I suspect the habit of commenting code redundantly in languages like Java is a carryover from older days. It's something some teachers probably never unlearned nor bothered to update their own style to adapt to modern industry practices. So they just keep passing on this "wisdom" from the old ages of doing things to the new crop of programmers. I think we who know better are obligated to at least remind newbies that norms are different now.
 
Master Rancher
Posts: 3001
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But then, there are beginners and there are beginners.

In the 90's I was teaching a course 'Introduction to Actuarial Mathematics'. The requirements to be allowed to follow this course were either a diploma from the High School with Maths (or better), or, since not too long, a diploma from a qua content much much lower rated course. As a consequence, I usually had two kinds of beginners: people graduated from university in Maths or Economy, and people who hardly (if at all) knew what the y^x button meant on their pocket calculator.
And in tests, I clearly noted the differences.

I sometimes see the same thing in this Beginners Forum. From the content of a post you can usually estimate with what kind of beginner you are dealing with. However, not always do we make this distinction in our replies.
 
Ranch Hand
Posts: 350
8
BSD Debian Open BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:T...I would still argue that even a beginner should be able to see that they are writing a method like getCircumference without the little reminder that it is "A method that returns the circumference 'getCircumference()'." I don't know, maybe I'm expecting too much from people.



No at all i agree with you getCircumference() is autoeplicative also for a beginner too no needs any additional comment(overuse of comments is a bad habit one can notice when code grows)  method's name communicates semantics, the intent

When I wrote redundant comments before, it was because that was what we were taught. One of my first programming languages was Pascal (I learned BASIC at the same time). This is how we were told to comment our code:


.....

Comments like these were reasonable and some redundancy was necessary when control structures contained many lines of code and it was difficult to easily discern the structure of the design/program. That was then though. Nowadays, we can write descriptive names with practically no limit to the length of the names we give other than what common sense tells us...



I still bear that style(code block comments) from back then but I'm aware  is almost redundant  mostly

PS: Great school for beginners here however congrats either
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose we could come to a compromise. I like Piet's attitude of being empathetic to beginners and sometimes we need to be cognizant that they may still need those training wheels.

How about we ask them like this:

@OP, do you think you really need comments like this?

What additional information are you getting from the comment that the code doesn't already give you? Also, what do you think happens when you decide to change the method name somehow? You'd have to change two things right? The code and the comment. Ideally, the code will speak for itself because you choose good names that express the intent clearly enough. That would make this kind of comment redundant, in which case you can delete it.


Is that a reasonable approach? I can see how the other approach we've used can come off as a little overbearing or stern.
 
Piet Souris
Master Rancher
Posts: 3001
105
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, looks very nice! And of course, we are all different, so a personal style is inevitable and welcome too. Keeps this site as interesting as it always is.
 
Harry Kar
Ranch Hand
Posts: 350
8
BSD Debian Open BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A starter do what his mentor do so if the mentor writes A the starter follow. But the starter don't know what is good and what is bad (s)he simply follow instead his mentor know. The responsibility of what and how teach is mentor's not student's
Now if the point is to teach good habits (regard comments) in my opinion the mentor have to explain the power of choice of good name identifiers and not use comments to comment what the identifier identify
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Yes, looks very nice! And of course, we are all different, so a personal style is inevitable and welcome too. Keeps this site as interesting as it always is.


Well then have a cow, since you're the one who inspired it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!