Return to the Section 101 Homepage.
Discussion 2 (06-23)
Outline
* Languages Intro
* Lab 2 Review
* Language Design
* Groups
* Homework Help (Problem 3) / Array Resize / Vectors
Languages Intro
Intro
I brought in some books (The 1100 page C++ Primer, The 250 page C Programming Language, and one I wrote myself, the 1 page, large-type Complete Guide To Scheme) to demonstrate the range of syntactic complexity present in programming languages.
In addition to the syntax of a language, there is also a great deal to learn about the style, programming paradigm, and standard libraries associated with that language. Truly, learning a language is a difficult task and claiming to "know" a language is a bold statement.
I recommend practice (of material taught in classes and books) as the best way to really understand and remember a language.
So Whats The Good News?
1. Java is smaller and generally cleaner than C++.
2. You dont need all of the features of a language, or have to be a perfect and elegant programmer, to get some working code.
3. Many languages are similar; C, C++, and Java, for example, all share similarities.
Programming Paradigms
* Scheme is a functional language, meaning it doesnt use state (ie Variables).
* C is procedural, meaning programs are composed of interactions of procedures (Essentially just methods).
* C++ supports many paradigms, including procedural and Object Oriented.
* Java is Object Oriented.
Lab 2 Review
Here is an ASCIIgraphical representation of the three dimensional array we worked with yesterday in lab:
char[][][] ar; All arrays are references.
| ar is a reference.
|
\|/
----- ---------- ar[#][#] is a word
ar[#] is | |-->| | | ... and has type char[]
a line | | | | | ...
and has | | ----------
type ----- | |
char[][] | | | -------|
| | \|/ \|/
| | --------- ---------
----- |A|l|a|n| |T|u|r|...
| | --------- ---------
| . |
| . | ar[#][#][#] is a character
. and has type char
Language Design
I would like to look not only at what particular language features do, but why they are the way they are and how that affects us as programmers.
While Vs For
Here are two equivalent loops:
final int UPPER_BOUND = 10;
for (int i = 0; i < UPPER_BOUND; i ++)
{
// STUFF
}
final int UPPER_BOUND = 10;
int i = 0;
while (i < UPPER_BOUND)
{
// STUFF
i ++;
}
As we can see, the while loop can be used to exactly simulate the for loop. So why not get rid of the for loop and make the language a little smaller?
Notice how clear it is what the for loop is doing; You need only look at the "for" line. Further, just choosing the for loop indicates what sort of thing will be done inside the loop (ie A linear traversal from the specified lower bound to the specified upper bound).
Now notice how easily the declaration and increment of the loop variable (i) could get lost in the code in and surrounding the while loop. Also, it might be easy to forget the line that increments i, or accidentally delete it at some later point, leading to an infinite loop.
The for loop is present in most languages, incuding Java, becuase it helps the programmer. Its structure forces the programmer to deal with all of the administrative issues right up front. Its very existance indicates something about what is inside the loop to whomever may read the code.
Assignment Vs Equality
Whats wrong with this code fragment?
int a, b;
a = 7;
b = 3;
if (a = b)
{
//STUFF
}
Notice the condition of the
if statement. The programmer probably wanted it to read
a == b, but made a typo. Now the conditional does something unexpected.
This is a fairly common and hard to track down bug. Could language design help us? What if we used
eq instead of
== ?
As it turns out, it is actually compiler design that has solved this problem; Most compilers, including "javac", now offer warnings about this sort of conditional.
Groups
I asked everyone to work in groups of 3 or more to answer the following question (A modified version of Weiss 3.13):
A combination lock has the following basic properties: The 3 number combo is unknown outside of the lock; The lock can be opened with the combo; The combo can be changed, but only when the lock is open; The combination is set when the lock is created (Hint: Constructor); The lock starts locked.
Design a class that represents this lock. Include the open(), changeCombo(), and lock() methods. These methods should indicate if the operation was successful.
Here is my solution:
public class ComboLock
{
private boolean isOpen;
private int combo1, combo2, combo3;
public ComboLock(int startCombo1, int startCombo2, int startCombo3)
{
combo1 = startCombo1;
combo2 = startCombo2;
combo3 = startCombo3;
isOpen = false;
}
public boolean open(int candidate1, int candidate2, int candidate3)
{
if (isOpen)
{
return true;
}
if ((candidate1 == combo1) &&
(candidate2 == combo2) &&
(candidate3 == combo3))
{
isOpen = true;
return true;
}
else
{
return false;
}
}
public boolean changeCombo(int newCombo1, int newCombo2, int newCombo3)
{
if (isOpen)
{
combo1 = newCombo1;
combo2 = newCombo2;
combo3 = newCombo3;
return true;
}
else
{
return false;
}
}
public boolean lock()
{
isOpen = false;
return true;
}
}
Homework Help (Problem 3) / Array Resize / Vectors
Here is the pseudo-code for resizing an array (This is a generalization and modification of the code presented in Weiss Figure 2.6):
1. Figure out the size of the new array. If we are simulating a Vector, then the new aray should be twice as big as the old array.
2. Create the new array.
3. Copy the elements from the old array to the new array.
You can use a for loop or System.arraycopy() to copy the elements from one array to another.
Vector
A Vector is a data structure that provides dynamic array resizing (as described above). It begins with some initial capacity and doubles its internal array each time that capacity is exceeded. By doing so, it can gurantee some array-like run times (Covered later in the course), while offering dynamic resizing.
The Vector and array are some of the most fundamental data structures. They are used all the time in every type of programming. Java provides a Vector class which, not surprisingly, is named "Vector".
Return to the Section 101 Homepage.