This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!

Miles Davis

+ Follow
since May 08, 2015
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 Miles Davis

I need to create a program that converts a given amount of money to change. Here is the program that I came up with:

What I want to know is, is this the best implementation of such a program (for a beginner that is)? Given the limitations of floating-point arithmetic, is this the best way to handle converting a given sum of money to its equivalent change?
3 years ago

Campbell Ritchie wrote:The difference between the first and second methods in OP's post are that one creates an array with elements and the other an empty array. You can see that so much better with arrays of reference types than of primitives. If you have code like this (from this post):-If you print that out with methods of the Arrays class you get the obvious output:-What you have here is a declaration and array initialiser later. The normal form of an array initialiser is this:-
words = new String[]{"Most", "ranchers", "want", "to", "strangle", "Campbell"};
but if you have the declaration and initialisation on the same line you are allowed to miss part of it out:-
String[] words = {"Most", "ranchers", "want", "to", "strangle", "Campbell"};
So that would have the same effect on the words array, and the second and third versions of Miles Davis' code will have the same effect.

The first line however declares and initialises the same array but does not initalise the individual elements. In the case of an array of primitives, the default values might be usable; the int[] shown will be filled with zeroes. But in the case of an array of primitives, the default values are nulls (see the thread I got the earlier code from). It is potentially dangerous, and probably poor practice, to have nulls, but you can see what happens if you try a different initialisation of that words array.-I would say try to put known values into the array; if you don't know the values get them assigned as soon as you can. Avoid arrays full of nulls as far as possible. Avoid nulls as far as possible.

So essentially, the second and third ways of initializing an array are different in that the second is used only when the declaration is on the same line, and the third is used when the declaration is on a different line? If this is true, then should I just always use the third way since that is the more general case of the two?
3 years ago
I've seen multiple ways to initialize arrays in Java:

I understand that the first way is useful when you don't know the values you want in the array when you initialize it, and the second way is good when you do. However, I don't understand the difference between the second and the third way. Why should one be preferred over the other, and when should I use one rather than the other?
3 years ago
I learned in my Java textbook that everything in programming essentially consists of data and procedures. This makes some sense, because in an object, there is data (instance variables) and procedures that manipulate the data (methods). However, I am a little confused about what make up other constructs in Java. For example, an if statement: It is not data, so is it a type of procedure? Is a while-loop a procedure, since it's not data? What about an assignment? If these are not data or procedures, then what are they? How do they fit in to the foundations of computer science and programming? I am basically asking for an effective taxonomy of different programming constructs so that it all acts as a cohesive whole in my brain.
3 years ago

Stephan van Hulst wrote:The difference becomes very interesting when designing your own language.

A statement is like an instruction that your runtime performs. Programs consist of statements. Without statements, there is nothing to do.
An expression is a piece of code that can be 'evaluated', meaning it can be reduced to a value.

The two concepts are not related, or even similar. They may coincide with the same piece of code, but they do very different things.

You mentioned that 'x = 1' is a statement. It is not. 'x = 1' Is an expression that evaluates to 1, with the side effect of assigning the value 1 to x.
'x = 1;' is a statement (note the semi-colon at the end) that performs this assignment.

In Java, it's easy to distinguish statements. Statements end with a semi-colon. Statements can't be evaluated, they just do something.

Expressions are harder to distinguish: The following statement consists of 9 expressions:

If statements are so foundational to how a program operates (e.g. "without statements, there is nothing to do") then how to languages like LISP work? While Java has statements and expressions, LISP only has expressions. How is anything "done" in LISP if there are no statements?
3 years ago
In general, what is the difference between statements and expressions? This is a distinction which I have not fully distinguished yet. I know that typically an expressions is supposedly anything that returns a value, such as a literal, or a function. Statements are usually said to be commands to the interpreter, such as "print such and such" or "do . . . while". However, I don't get it. Wouldn't it make sense to say that print is an expression, since it is a function printing an input (input to output)? Also, people usually say that x = 1 is a statement, but couldn't this be considered an expression where the assignment operator is a function acting on two inputs and the output is that x refers to 1? Finally, couldn't a flow control structure such as if . . . else be considered a three-argument function in which one of the other arguments are returned based upon the truth value of the first input, making it an expression? If someone could help me clear this up, I would appreciate it.
3 years ago

Stephan van Hulst wrote:What is computation? Computation inherently involves transforming input to results. What transforms input to results? Functions. It doesn't matter if you want to call them functions, subroutines, lambdas, actors, categories or transitions; they all relate to the same concept.

That's a great way to put it, and precisely answers what I'm asking. However, what about the so-called "void" type of functions in, say, Java? These can take in inputs, but since they are type void, they return no outputs. Doesn't this stray from the notion of transforming input to results, and thus to computation?
3 years ago

Knute Snortum wrote:It is absolutely possible to have a programming language without the idea of functions. There are implementation of a Turing Machine that do not have functions. These languages are not terribly practical, which is why most programming languages have the idea of a function or method or subroutine.

I guess that is true. But when talking about computation in general, it always seems to relate to the concept of a function. For example, the Church-Turing thesis is stated as saying that there is no effective model of computing that can compute more mathematical functions than a Turing machine. Does this mean that when we talk about things being computable, we are really speaking about if a function can be effectively evaluated? Because when I read the literature, especially in the days of Turing, "function" seems to be the central object of computability. My central question, then, is the notion of computability intrinsically related to the evaluation of functions? If not then what is an example of computation that does not involved a function?
3 years ago
Although this question is not specifically Java related, it is a basic question that I need answering

This might come off as an overly pedantic question, but I am curious about how the mathematical idea of the function became so prevalent in today's programming languages. Although I have a very rudimentary knowledge of how machine code works, I know it's based on "1's and 0's." Given that, let's take Scheme for example. The language is based on Alonzo Church's lambda calculus, a language that describes the mathematical idea of a function in its purest form. Scheme is know to have implemented much of the lambda calculus as a foundation for the language. This begs the question: what does the concept of a mathematical function have to do with programming a machine (which is based on 1's and 0's)? Is it possible to program without the notion of a function? If so, how?
3 years ago
While this question does not have to do with Java in particular, it has to do with computing in general. The reason I'm asking here is because I never get replies when I ask this in other forums; everyone here, on the other hand, seems to be very helpful.

I am reading the first pages of Structure and Interpretation of Computer Programs by Abelson and Sussman, and they use the terms "process" and "procedure." Here is the quote from the book which confuses me:

"We are about to study the idea of a computational process. Computational processes are abstract beings that inhabit computers. As they evolve, processes manipulate other abstract things called data. The evolution of a process is directed by a pattern of rules called a program. People create programs to direct processes. In effect, we conjure the spirits of the computer with our spells. A computational process is indeed much like a sorcerer's idea of a spirit. It cannot be seen or touched. It is not composed of matter at all. However, it is very real. It can perform intellectual work. It can answer questions. It can affect the world by disbursing money at a bank or by controlling a robot arm in a factory. The programs we use to conjure processes are like a sorcerer's spells. They are carefully composed from symbolic expressions in arcane and esoteric programming languages that prescribe the tasks we want our processes to perform. A computational process, in a correctly working computer, executes programs precisely and accurately. Thus, like the sorcerer's apprentice, novice programmers must learn to understand and to anticipate the consequences of their conjuring. Even small errors (usually called bugs or glitches) in programs can have complex and unanticipated consequences."

As one can see, the terms "process" and "program" are thrown around a lot. I can't really get at what thee terms are exactly supposed to mean, and the relation between the two. I was thinking that maybe "process" refers to the general objective of a program, and a program is the actual code. However, this supposition isn't based on much. If someone could help me out with precisely defining these terms I would really appreciate it.

P.S. If this is an inappropriate sub-forum to post in then I'll gladly take it to another.
3 years ago
I have a question about how to construct an array in a constructor.

The following compiles:

However, the following does not compile.

Why isn't this valid? Is it possible to use the curly brackets for initializing an array in a constructor?
3 years ago
So far I've found that one method of edge detection in photos is to calculate the difference in the RGB signature from adjacent pixels and whether that difference crosses a certain threshol or not determines whether the pixel is at an edge. I need to to think of another method of edge detection, but am at a lost. Any ideas? They have to be fairly simple.
3 years ago

Knute Snortum wrote:I think you have the correct solution and the last element just goes away.

Alright, I don't see what else I could possibly put. Let's hope for the best.
3 years ago

Ashley Bye wrote:I assume itsSize is set elsewhere and is an accurate representation of the size of itsItem[]. Do you have to solve it in one line as the question seems to indicate? If so, I don't see how that is possible. If not, have you considered a temporary array?

Yes, it must be solved in one line...
3 years ago
I have the following question as it appears in its entirety:

Complete this method in the NumberList class that inserts the given value at the first location among the values stored in itsItem[0] through itsItem[itsSize-1].

I cannot tell whether this is a terribly formulated question or whether I am missing something. First of all, I'm not really sure what the question is asking me to do, but since I see that itsSize++, it's possible that it wants me to shift all of the entries in the array to the right so that given can be inserted into itsItem[0]. This would be itsItem[k - 1] = itsItem[k]. However, this would throw an index out of bounds exception. Also, I am not sure how itsSize would increase at all, since the length of a vanilla array in Java cannot change. What should I do?
3 years ago