Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!

Kenneth Albertson

Ranch Hand
+ Follow
since Sep 18, 2005
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Kenneth Albertson

It's a Scanner. This will do the job if the user types in exactly what is expected:And this will do it even if they put extra junk on the inputs:
14 years ago

Originally posted by mike cool:
ok guys i solve to sum to int arrays without using BigInteger
here is the code
pls tell me if there is any thing wrong

Come on, mike, what happened to testing? What answer does this code give for 15 + 6 ? It looks to me that it will return 75. Is that correct?

This code will only work correctly when array1 and array2 are both the same length. It will also fail if they are both 40 elements long, and there is a "remainder" (that is a misleading variable name) of 1. But one of your earlier posts said that "the 2 arrays have different sizes 30 and 40".
[ October 22, 2005: Message edited by: Kym Thompson ]
14 years ago
People who teach beginning programming often ask their students to answer the question "Is X part of the operating system". Twenty five years ago (before wimp interfaces?) it was probably a reasonable question, which helped students understand what an OS is and does. But today I don't see the value in leading students to believe that this question actually has a meaningful answer.

The first major problem with this question is that, at any point in history, there are some functions provided by all operating systems, and some functions only provided by some. Such as a wimp interface, or networking support, or particular accessories, or even built-in java support. So the question should be, "Is X part of the Y OS, version Z".

The other major problem is that, even for a particular OS, the question does not always have un uncontested answer. After all, fur has been flying for 10 years now because Bill Gates suggested that a web browser should be part of the OS.
[ October 21, 2005: Message edited by: Kym Thompson ]
14 years ago
It's good to work things out for yourself, and you learn a lot that way, but you shouldn't have to re-invent the wheel every day. Several people have given good suggestions, but you have not understood them, so it is time for some clearer instructions.

The structure of the program that you are trying to write is a very basic and important pattern that you will use over and over again. Your teacher should have shown you an example. If he or she didn't, they aren't doing a very good job. If they did, but you didn't study it, then you aren't doing a very good job.

Here is the code that you need to get your command menu working. Remember that this is very basic, and the program will crash and burn if you enter anything other than an integer. But it will do for a beginner.What should you do next? Study this code carefully. Make sure that you understand every line of it. Run it over and over again, with different inputs, and different sequences of inputs. Make small changes to it, and run it again, to confirm that the changes have the effect that you expect.

ONLY once you are completely sure that you understand what this code does, and how it works, should you start to replace the "Responding to option" blocks. Do these ONE AT A TIME. Make sure that each one works correctly before you start the next one.

This is a very important way of working that should also have been demonstrated to you (same comments as before). You SHOULD NOT write your programs all at once. You should always start with the smallest and simplest possible version of what you want to do (like the control menu), and make sure that is working correctly before continuing. Then you add a little more, and test it again. If it doesn't work, you know that the error must be in the 4 lines that you just added.

Now you try this code and tell us where you get to.
14 years ago
Indexes effectively transfer work from output time to input time, so the answer to your question is yes, the indexes will slow down inserts. It is always a trade-off, and the ideal solution depends on your application. If there are lots of inserts, and they they have to be lightning fast, you might be better of with no indexes and slow queries, but that would obviously be an unusual situation. In most situations the inserts with indexes will be ok.

There are even situations where it is best to speed up inserts by minimizing the number of indexes, but come reporting time you build a bunch of indexes, run your reports, and then drop the indexes again.

And these comments are not specific to MySQL.
Start with a simple natural language description of what you want to do, perhaps like this:

I'll read a command, and
while the command isn't a request to stop,
I'll respond to it,
and then read another one
and so on ...
14 years ago
For the benefit of those who care about such historical trivia ...

From Ken Arnold, James Gosling & David Holmes, 2000, The Java Programming Language, 3rd ed, Addison-Wesley, pp 184-5:

Falling through to the next case can be useful in some circumstances. But in most cases a break should come after the code that a case label selects. Good coding style suggests that you always use some form of FALLTHROUGH comment to document an intentional fall-through.

... You should terminate the last group of statements in a switch with a break, return or throw, as you would a group of statements in an earlier case. Doing so reduces the likelihood of accidentally falling through the bottom of what used to be the last part of the switch when a new case is added.

And from Brian Kernighan & Dennis Ritchie, 1988, The C Programming Language, 2nd ed, Prentice Hall, p 59:

Falling through cases is a mixed blessing. On the positive side, it allows several cases to be attached to a single action ... But it also implies that normally each case must end with a break to prevent falling through to the next. Falling through from one case to another is not robust, being prone to disintegration when the program is modified. With the exception of multiple labels for a single computation, fall-throughs should be used sparingly, and commented.

As a matter of good form, put a break after the last case ... even though its logically unnecessary. Some day when another case gets added at the end, this bit of defensive programming will save you.

It's a while since that book last came down off the shelf! For any youngsters who have read this far, and don't know why Dennis Ritchie's opinions about the C switch statement should be of interest, he designed and first implemented C, after which he and Ken Thompson (no relation) used it to create an operating system that came to be called unix.
14 years ago
Please read How To Ask Questions On Java Ranch, especially Carefully Choose One Forum. Three forums at once is too many, especially for this question.
14 years ago

Originally posted by Sarah isme:
how do you know which value to give to things and stuff like this, nowhere seems to explain this!

The algorithm is a general search strategy which can be applied to different games. But the state values depend on the game, and are independent of the algorithm. You need to study the game, rather than the search algorithm. Your end result has two parts: search algorithm + state valuation algorithm.
14 years ago
First, the "pHal is not resolved" error occurs because pHal is created within class Main, and so is not visible within class sleepingState. If you wanted a sleepingState object to be able to change the state of a Hal object, you would have to pass a reference to a Hal object (ie the variable pHal) to the sleepingState object.

More generally, though, why is sleepingState a class? All it does is (attempt to) change the state of a Hal instance. Shouldn't this be done by a method on the Hal class?
14 years ago
Hi there, Chris. Still working on this problem? A number of people made helpful suggestions when you asked the same question a couple of days ago. If you still don't know where to start, perhaps you need to go right back to the beginning. Tell us what classes you think that you need, and then the two fundamental things that you need to know about each class:
  • What do they need to know, ie fields, and
  • What do they need to do, ie methods.
  • Central to this problem is the design of a data structure to hold the menu items. The previous discussion suggested an array or a list. I suggest that you use an array, it will be an easier approach for a beginner.

    I also suggest that you do the problem in two parts. Start by ignoring the requirement to avoid duplicate entries, because that is the hardest part. Get everything else working, and by then you should have enough understanding of what you are doing to tackle the removal of duplicate entries as well.

    BTW, a quick refresher of How To Ask Questions On Java Ranch might be useful as well.
    [ October 13, 2005: Message edited by: Kym Thompson ]
    14 years ago
    What results does this code produce for the test cases that I suggested? Especially the last one!

    And please read this explanation of how to use code tages. You can't expect anyone to read code if it is not formatted.
    14 years ago
    Michael has given you some more help on the logic needed for the nextAccountNumber method. The first thing that you will need is a test harness. You could use something like this:The next step is for you to start coding up the AccountNumberMaker class and nextAccountNumber() method. Let us know how you go.
    14 years ago
    What about a fifth approach? Use just one function:

    14 years ago
    The discussion has been about introducing identifiers into boolean expressions, but I wonder if that is actually the best approach here. If you start with this:why not begin by simplifying it to this:Then reduce (fooValue > 10) && (fooValue > 80) to just (fooValue > 80), which gives:For greater clarity, I would be tempted to lay it out like this (omitting one set of parentheses that, IMHO, add nothing):Now you could introduce some constants, as Stan suggests.

    So I am not disagreeing with what has been said about introducing identifiers, but I don't believe that it would be the best approach in this particular case. The meaning of the last expression above is, to me, extremely clear.
    14 years ago