• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Cylinder program  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am more frustrated than I have been in some time. I feel like I just may be too stupid  for Java. I am trying to create a cylinder program but I am failing from 360 degrees.  My main pinch point right now is that I am trying to use the Cylinder method to create new cylinder objects with specific radius and heights. Then I want to use the toString method to print out volume, area, height and radius. I have tried 200 different ways of accomplishing this but am not gaining any ground. If anyone could point me in the corretc direction I would be immensely grateful.  I have pasted my code below.




 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 7 to 10 in the code you gave shows a main method with no code in it. That means that when this program gets executed, it will do exactly nothing. You need to have code in there that will actually do something.
 
R L Miller
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the problem I am having. Anytime I try to put anything in lines 7-10 I get an error that I cannot use a non-static in a static . I don't understand what that means. It won't allow me to reference any of my Cylinder methods in lines 7-10 without an error. How to I work around that?
 
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you compile and execute the code?
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please go through this forum because somebody else has a similar assignment. I suspect they are doing the same course as you.
I think that particular error message is confusing. It tempts you to make the target static, but the correct solution is to move the current line out of a static method. The shouldn't say non‑static but instance. “Instance members can only be accessed from an instance context,” would be a far better error message. Your main method has an ideal length: one statement. Everything else sho‍uld move out of the main method into another method. More details in that link.
 
R L Miller
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I figured out what what incorrect so I wanted to follow up in case anyone else is struggling with the same issue. The problem was that I did not declare my Cylinder class as "static". Once I corrected that everything else fell into place. Amazing the suffering that one word caused me.  I have attached my updated code below to show my corrections. Thank you everyone for the help.


 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R L Miller wrote:. . . The problem was that I did not declare my Cylinder class as "static".  . . .
What a strange correction. Did you really want Cylinder to be a nested class?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The inner class does not need to be static if it is accessed from an instance of the class and not from the static main() method.  Change the code on line 6 and 7 by adding 4 lines as follows:
 
R L Miller
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie

Honestly, I don't know what I wanted. I am in an online Java course ( my first Java) and so there is not necessarily any teaching( TA is good at answering e-mails). We were basically given a book(not a very good one IMHO) and then assignments to churn out. We have a discussion board but are not able to paste in code so it is essentially useless to me. All I know is that my Assignment has to be formatted as LastName_A03Q1.Java and because of that my main class is named the same.
So in summation, I know absolutely nothing about Java. so little that I need a discussion forum to help me understand the people helping me in discussion forums....I feel like a toddler.
 
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seeing as how you have getArea() and getVolume() that calculate the values on the fly, you really don't need to have fields for 'area' and 'volume'.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are overusing the 'this' keyword. You only need to use it if there is a local variable with the same name as a field. Typical examples are constructors and setters. Example:

Not necessary here
 
Ranch Hand
Posts: 106
4
Java Netbeans IDE Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R L Miller wrote:@Campbell Ritchie

Honestly, I don't know what I wanted. I am in an online Java course ( my first Java) and so there is not necessarily any teaching( TA is good at answering e-mails). We were basically given a book(not a very good one IMHO) and then assignments to churn out. We have a discussion board but are not able to paste in code so it is essentially useless to me. All I know is that my Assignment has to be formatted as LastName_A03Q1.Java and because of that my main class is named the same.
So in summation, I know absolutely nothing about Java. so little that I need a discussion forum to help me understand the people helping me in discussion forums....I feel like a toddler.


Actually, you already know more than you think you do. Java is a huge language (lots to know) and it takes years to feel comfortable. This is what I have learned in the last two or three years I have been struggling with it. Books and advice are always helpful, but coding is the only way to learn. I had this idea that I would be part of a team at work and we would support each other. At my last job I worked in a vacuum, developed a java based application for a client, set up the DB and did all the design work and project management work. I wrote thousands of lines of code, delivered the project over due, but working. I still feel completely flustered. At the end of the project, a developer that I know finally gave me a thumbs up and said, "you know, you're a pretty good developer. You're stubborn and you don't give up and you're not afraid to fail." He still had nothing good to say about what I built. Learning is a commitment. If you can hang in there you'll be ahead of the crowd. Keep coding, and realize that in a few years you'll know even more than you give yourself credit for knowing, you'll be ready to take on any small to medium sized project and succeed. Find a project that you care about that you can solve with this language and start there. I look at your level of concern and your harsh self criticism and see in that the basis for a good developer. Don't let your own high expectations stop you at this early stage. Keep on coding.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!