This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Programing challenge Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Programing challenge" Watch "Programing challenge" New topic
Author

Programing challenge

Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
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

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1669
    
  11

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.


When you do things right, people won't be sure you've done anything at all.
Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
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

Joined: Mar 06, 2010
Posts: 44
So I could say circle shape and switch the bottom parts to shape.getcircumference for example? that's ok?
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1669
    
  11

Nick,

step by step..... what is the demo class supposed to do...... in your own words.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1669
    
  11

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

Joined: Mar 06, 2010
Posts: 44
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

Joined: Mar 06, 2010
Posts: 44
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1669
    
  11

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

Joined: Mar 06, 2010
Posts: 44
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1669
    
  11

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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1669
    
  11

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

Joined: Mar 06, 2010
Posts: 44



YAY!
Thanks again, you are my hero. I knew it was something stupid!
Another AH HA! moment thanks to java ranch.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1669
    
  11

sooo...... let's see the working product! We'll only need the demo class

Nick Harms
Ranch Hand

Joined: Mar 06, 2010
Posts: 44
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1669
    
  11

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
 
 
subject: Programing challenge