• Post Reply Bookmark Topic Watch Topic
  • New Topic

A Beginner's Program - Comments?  RSS feed

 
Sanders Wellington
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'd appreciate any suggestions about this program. I wanted to create this as a segment for a test I'm going to give my high school students. This segment is used for calculating acreage. The length and width of property values are generated randomly to create an area between ~2 and ~10 acres. The answer is then randomly assigned to one of four buttons. The other buttons are then generated 10% progressively more or less (depending on the placement of the answer button).

I realize this is "junky code", however, I am just beginning and I would appreciate any helpful suggestions or comments.

Thanks - like a whole bunch,

MS
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"MaxwellSmart",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with invalid display names get deleted, often without warning

thanks,
Dave
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

I trust you'll take care of your display name as David requested, because we do take this policy seriously.

Now, your code... It works, so congratulations! Here are few thoughts.

First, depending on the resolution of a user's command prompt window, this output could wrap, so you might want to reconsider the formatting.

Second, the answerButton method has the greatest opportunity for streamlining. A switch/case statement is often more manageable than multiple if/else statements (especially since you're already "switching" on an int value).

Third, taking a step back for a bigger picture... This static code is really just one long procedure, with the methods simply "steps" in that procedure. I would try to think about this class as defining an object having state (variables) and behavior (methods). You could start with a more descriptive name, for example, AcreageModeler. Now, ask yourself what an instance of AcreageModeler should have (maybe properties like length, width, area, and acres), and what it should do (maybe things like generateModel, pickButton, adjustValues, printOutput).

(Note: Please avoid "magic numbers" like 43560. This should be defined as a constant with a descriptive name.)
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since no one else has mentioned it, there is a perfect place for an array (at least I'd stuff one in there) in favor of a few declarations in your post. An array can have the same value in different indexes to start, and have those values individually altered.

Nate
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nathan Leniz:
...there is a perfect place for an array...

Yeah, when you see things like val1, val2, val3... it's probably time to consider an array or collection.

I would also consider how to handle an arbitrary number of values (not just 4).
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sanders Wellington :
... I realize this is "junky code", ...


Doesn't look junky to me.

The advice about instance or static, array usage and switch/case are also what I see in the code. I enjoy writing code, and have spent time wrestling with some base issues.

The advice you are given is seasoned.
[ January 07, 2007: Message edited by: Nicholas Jordan ]
 
Sanders Wellington
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't look junky to me.
That makes my day!

I'm not able to program as much as I'd like to lately because today's the first day of class . . . going for a Master's degree MSCIT, S/E.

Yesterday I thought a nice "little" program would be to duplicate the game "21" (Black Jack?), so I began writing, and after a while I had a fairly significant program . . . everything worked and I was able to "follow" what I needed to do, then it started getting late (~2 a.m.) and I started relating to Hansel & Gretel - I call it the "lost without any crumbs" syndrome.

So, I went to bed and had significant annoyance leaving the unfinished program alone all day. I could do this stuff at my job if I could only figure out how to get a compiler to work with my Zodiac2. Any suggestions?

And . . . does this possibly indicate an addiction?

Danke,

Sanders
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sanders Wellington:
And . . . does this possibly indicate an addiction?


No, just a love of all things technology! I sometimes go to bed and try and solve problems in my head as i drift into sleep. Usually i endup getting back up and trying to tackle it again lol.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sanders Wellington:
That makes my day!

Thanks, I needed that.
I'm not able to program as much as I'd like to lately because today's the first day of class . . . going for a Master's degree MSCIT, S/E.

What is MSCIT, S/E - I tried to google it, returns do not provide a concise, definite choice field sufficient to disambiguate.
"21" (Black Jack?),

I do not gamble.
Yesterday I thought a nice "little" program would be to duplicate the game ..... so I began writing, and after a while I had a fairly significant program . . . everything worked and I was able to "follow" what I needed to do, then it started getting late (~2 a.m.) and I started relating to Hansel & Gretel - I call it the "lost without any crumbs" syndrome.

I have other metaphor(s) I use, mostly involving Hollywood grade renditions of some contemporary societal burdens.

So, I went to bed and had significant annoyance leaving the unfinished program alone all day. I could do this stuff at my job if I could only figure out how to get a compiler to work with my Zodiac2. Any suggestions?

Yes, see PM I sent you. I have quite a few things in place for you to do. First off is to give meaningful names to my classes for posing questions in channel. My mind works like a compiler, but the moderators prefer the Java style of naming things.
[ January 11, 2007: Message edited by: Nicholas Jordan ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!