• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Programing challenge

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is supposed to be a class with a demo. The demo gets the circle radius and sends it to the class to do work and return the circumference, area and diameter.
For some reason when I run the program (I get no errors when compiling) I get o's for my awnsers no matter what radius is used. I think its probably something stupid thing I overlooked, and i need a wiser person than I to help me out!
Much appreciated.

Circle class then demo
Circle Class


Demo

 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the demo class supposed to do, in normal person terms, step by step?

Edit: and never, EVER name your object reference the same as the class name. It's confusing and against most Java best practices.
 
Nick Harms
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My instructions are to write a program that demonstrates the circle class by asking the user for the circles radius creating a circle object and then displaying the circles area, diameter, and circumference.
 
Nick Harms
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I could say circle shape and switch the bottom parts to shape.getcircumference for example? that's ok?
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nick,

step by step..... what is the demo class supposed to do...... in your own words.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nick Harms wrote:So I could say circle shape and switch the bottom parts to shape.getcircumference for example? that's ok?



That would be preferable.

By saying


it looks like getRadius is/could be a static method.

The goal should be to make code more readable and less confusing..... not just so it "just works".
 
Nick Harms
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the demo demonstrates the class by using its methods, It will first ask the user for their radius and then send that to the class to be used in the methods to find the information. the methods return the desired information which you call by calling the object reference and the method name, such as circle.getDiameter.
 
Nick Harms
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There ya go!
I know this is probably a dumb question but i want to be sure that I dont need to change anything in the class after switching these right?

 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nick Harms wrote: It will first ask the user for their radius and then send that to the class to be used in the methods to find the information.



Well maybe you should program it to do that....
 
Nick Harms
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought that was what the object did? Do i simply need to move the object to after I ask them instead of before.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object reference names are for the reader/programmer, not the machine.

Katrina tells me frequently of someone who named their variables a, then aa, then aaa, then aaaa..... et cetera. The computer doesn't care. It will work. But how will you change/fix/debug it?

Being able to pick good reference/variable names is important.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nick Harms wrote:I thought that was what the object did? Do i simply need to move the object to after I ask them instead of before.



What happens if you try?

You could also do a more lazy instantiation and declare rad in the same line as setting it equal to nextDouble.

And while we're on the subject of naming things.... you might change rad to 'radius' . Letters are free and it adds to readability.
 
Nick Harms
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



YAY!
Thanks again, you are my hero. I knew it was something stupid!
Another AH HA! moment thanks to java ranch.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sooo...... let's see the working product! We'll only need the demo class

 
Nick Harms
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I know you could say double rad were it reads the input but how would you initialize it to 0 there?
Figured I might as well ask while Im here.

 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No need to initialize to zero....



This is also identical:


p.s. remember if you use 'Double' it's an object... 'double' is the primitive
 
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic