27

Then I inevitably have students who abandon the good habits they had formed and want to make everything an Object, or declare everything as var. These are very different situations. As you observe, the use of var in C# is controversial, but using object as the type of all variables (and then needing to cast any time you want to actually use the variable)...


8

Introducing these sorts of shortcuts can cause exactly the problem that you've outlined. "Oh, goody!" says the misguided student, "Now I can stop worrying about the type!" There are a few principles that I directly teach, and repeat extensively in my instruction: There are two audiences for code, machines and people. We need to create code that is good ...


8

Luckily this is quite simple to make clear, even if you are steering clear of the IEEE standard. Honestly, even binary is not needed here. I would start by borrowing a page from Scheme, and say that we can't make blanket guarantees about the exactness of decimal numbers. For instance, when we try to express $2/3$ in decimal, and it doesn't work! All the ...


7

This might be an interesting way to do it, but here goes. Give your students a blank sheet of paper. Have them write out "3". Then have them write out $2^{10^{1000}}$. Most will probably stare at you blankly. A few clever/sassy ones will just write out the exponential notation. Tell them that just as they write out numbers a little differently depending on ...


6

Better yet, is there any way I can show the students such an explanation? (Perhaps an online, interactive demonstration of floating point precision etc. - I couldn't find one) Show them these bits of code: float one = 16777291; float two = one + 1; System.out.println(one == two); float one = 1.00000001f; float two = one + .00000001f; System.out.println(...


6

Why complicate matters with contrived analogies like hotel rooms and guests? It is much more straightforward to take the analogy of a simple paper "form" (which absolutely everyone in contact with the Western world has used and understands), and understand that there are spaces or "boxes" on the form which are referred to by names. The ...


6

If I learned anything reading Dale Carnagie, if you want to convince someone to do something, don't tell them "Do this because I want you to," tell them "Do this because if you don't, you're going to suffer for it and it's your loss." Speak in terms of their own self interest. You might want to tell them, "Now, you may want to use Objects for everything. ...


5

The use of an analogy is to provide a familiar jumping-off point to introduce something new. Since variables are unique features of computing systems, different from how anything else in the universe functions, we need some way to cross the gap. Let's look at how useful the analogy of hotel room and guest is, bearing in mind that most analogies do not ...


5

There are a number of issues to be addressed from clarity of the written code to efficiency of the compiled code. For beginners, I think the clarity issue is more important so I'll address that first. Before I begin, though, I'll also note that the inferred typing of variables in C# is still static (compile time), strong (types don't change) typing. That ...


5

Tell the students they have seven decimal digits of precision in a float, and that the decimal point is placed "somewhere" within those seven digits, depending on the magnitude of the number. Ask them to add 0.01 to 9,999,999 and express the result to seven digits. Ask what the answer would be if they performed the addition a thousand times. Now ask them ...


5

If you expect that many of the students will pursue software development careers, it would not be unreasonable to declare style standards for the course, and include it in their grading. When doing so, I strongly recommend explaining why particular rules are in place, because it moves disagreement into "arguing principles" instead of having "arbitrary rules"...


4

I'd maybe suggest an exercise of splitting these kinds of statements up into multiple variables and lines of code. So take this line: if ((n+"").length() == (((n+1)+"").length())) { And come up with this code: String nString = n + ""; int nLength = stringN.length(); int nPlusOne = n+1; String nPlusOneString = nPlusOne + ""; int nPlusOneLength = ...


4

I suspect that you handle such situations by informally building the parse tree of the expression in your mind. Maybe not all at once, but a bit at a time, perhaps. I'm not a cognition expert, of course, but it might help him if you teach him to do it explicitly on paper. He needs a table of operator precedence, of course and needs to be able to deal with ...


4

Using labels as a metaphor over boxes has merit and has been examined. This post looks at it from an early learner standpoint: https://www.felienne.com/archives/6063 There is a good post on this here that is more technical. https://hellokangning.github.io/post/variables-are-labels-not-boxes/ When looking at the strength of the metaphors to their "real ...


4

You can also use @Steve paper form idea for a second use, namely the importance of naming. I have often given students a paper form with boxes of different lengths, labeled A, B, C, etc. I then explain that their ability to fill in the form correctly (with information about themselves) will account for 50% of their final grade. Of course, they immediately ...


3

Carrano, Data Abstraction & Problem Solving with C++: Walls and Mirrors begins by introducing a simple ADT kind of like you're talking about, called a "Bag". But it is not used as a base class for later examples (e.g., trees, et. al.). From Sec. 1.5:


3

While uniform collections are the obvious example, you can also build or use a Pair class in which the types of the two components are given by generic arguments. If you don't want to build it, there is a built-in class with this property: $Pair<K,V>$ But building one like it is simple enough if that is your goal. Once you have your desired class, ...


3

If your students are mathematically inclined, you could simply think of the variable type as a constraint on the value contained inside of it. For example: int represents $x \in \mathbb{Z}$ (and depending on your type's size, $-2^n \leq x < 2^n$ for a signed integer and $0 \leq x < 2^n$ for an unsigned integer) The set of values that may be held by a ...


3

I think the student's problem is not with types, but first with the parsing of complex expressions (not to be confused with the tracing of it's evaluation). Mentally building the abstract syntax tree from the linear text representation. First rewrite remove the extra parentheses at right, and introduce a line break if ( (n+"").length() == ((n+1)+""...


2

Actually, it isn't quite so clear. If I run this in Java: // Simple computation with float and double // Increment by a third three times is not the same as incrementing by one // Float can actually be more accurate than Double in limited cases public static void main(String[] args) { float oneThirdF = 1.0f/3.0f; float wholeF = 0.0f; float ...


2

I highly recommend Database: Principles, Programming, and Performance, by O'Neill and O'Neill. Learned from that book for my first database course and loved it.


2

I don't think you can say any analogy is "right" or "wrong" across the board. Whether an analogy makes sense or not depends on the context of both the teacher and the student, and I don't think we can tell you whether any particular analogy is "right" for your context. I personally avoid analogies when I'm talking about using ...


2

I have to teach basic computer skills, applications and using online platforms in advance of programming this year. Since I will do some Spreadsheet work, I plan to initially use a cell to demonstrate an example of a variable... It has a (cell) reference (which can be changed to something with semantic meaning) It has a value (including Null/None/"...


2

I think it is a mistake to do this since it ties the "concept" of a variable, which is a language dependent item to an implementation on a certain kind of hardware. Yes, variables are often implemented in such a way, but the concept is independent of any implementation. A computer language creates a self-referential and self-consistent virtual ...


1

I worked as a data modeler at Siebel. One of the books that the Siebel data model is based upon is Data Model Patterns by David C. Hay. The book will not teach you how to build great data models. It is a collection of robust data models that are used in big systems used world wide. The one book that I have used (in teaching) that focusses solely on data ...


1

Then I inevitably have students who abandon the good habits they had formed and want to make everything an Object This can easily be shown to be a bad approach if you need a class specific property later on. object myStudent = new Student("John", "Doe"); string name = myStudent.Name; //doesn't work They'll probably use a hard cast, at which point the ...


1

There are a few reasons why this is bad, for example it could lead to code issues down the line where you need something from Horse that you don't have on Object, and it may also be a code smell. I'm going to suppose the code passes and runs fine, the architecture is fine, and just focus on the remaining issue: the issue generally is of course that it's not ...


1

Simple demonstration of "precision" using handheld calculators. Get a pair of calculators. One cheap model that has a limited number of digits in its display. Models can be found built in to clipboards and other "office" things. One decent model, not necessarily a good model, that has double the digits of the other calculator. If it has exactly double the ...


1

Many compilers will promote floats to doubles, do the calculation, and cast back. I see little or no reason to use floats unless one is dealing with huge matrices of floating-point numbers. I encourage my students to use doubles for most purposes.


1

You say “when he is finished, he no longer fully understands them.” There is a good practice to write self documenting code. This is done not with comments, but with well named entities (methods, classes, objects). This helps you to see what the code does (I do not mean how it does it, but in turn it will help you understand how it works). Therefore teach ...


Only top voted, non community-wiki answers of a minimum length are eligible