Concepts

Here is an outline of some major ideas we've encountered in the course.

Topic Discussion Where to find
How many source files?

Simple programs can have just 1. This would be the case if you can do the whole thing from start to finish in a few minutes. All of your code can be in the main( ) function.

More interesting programs need more thought and organization. There will usually be one "Driver" with its main( ) function, plus another source file for each "class" we want to define.

8/28 slide 8
Structure of source file

In the broadest sense, a program should do input, calculations and then output. More generally, a Java source file typically has these elements:

  1. an introductory comment
  2. list of "import" classes (Scanner, FileInputStream, ...)
  3. declare the class ("public class Driver")
  4. In the case of Driver, we'll usually just have a main function. Otherwise, if we're defining a class, we have a lot more work to do: declare attributes and operations.
  5. Inside each function (or "method"), we write our executing statements. It's also helpful to have a comment introducing each function, to make it more readable.

8/28 slide 9
I/O

For most programs, we can think of 2 kinds of input: the keyboard and an input file. Fortunately, we can handle them similarly: we use the built-in Scanner class do the input. The Scanner class has functions to help us read numbers or Strings.

Output usually goes to the screen. But if you have a lot of output, it makes sense to write it to a file. Either way, we usually use the printf( ) function. This is a very convenient function to use because we can print a message, as well as the values of any of our variables. printf( ) can also format the output to make it look more attractive, including specifying how much space to leave for the output and how many decimal places of precision.

8/29 slides 2-5
variables

Variables hold the values we need to keep track of in a program. A variable has a name (called an identifier) as well as a type.

The first time you use a variable, you must declare it - this means you have to tell Java what type the variable is.

Java has 8 predefined "primitive" types. The most common ones we use are int, double, char and boolean. Besides the primitive types, there are class types, such as String, Scanner, plus any additional class types we want to define ourselves.

9/3 slides 4-6
types of statements

A computer program is simply a recipe: a list of statements for the computer to perform. Fortunately, there are only a few kinds of statements we need to become familiar with:

assignment statements
I/O statements
statements that make choices (such as if)
statements that allow us to loop (such as while)
statements that call functions, to delegate work to other parts of the program

9/3 slide 3
if statement

Used to make choices. The else part is optional. The general format is given below.

if (condition)
{
statements
}
else
{
statements
}

The "condition" needs to be some boolean expression, which is usually some comparison between values. If there is only one statement in the body, then the curly braces are optional.

The condition may ask more than one question, connected by && or || operators.

You can have several if/else if/else if conditions in a row, culminating in a final "else" case.

9/4 slides 2-3
9/5 slides 5-8
String class

This is a very useful data type. For example, be sure you understand how to do the following:

  • find a character at a particular position in the string
  • see where something is contained in the string
  • extract part of a string
  • compare two strings for equality and less/greater than.
  • concatenate strings, concatenate other values into a string
  • convert an integer to a string, and vice versa
9/8 slides 3-9
9/15 slide 2
while loop

A while loop is similar to an if statement, except that once we perform the body of the loop, we return to the top to check to see if the condition is still true. We only exit the loop once the condition is false.

There are 3 common mistakes when writing loops:

  1. Loop does not execute at all
  2. Infinite loop
  3. Loop is off by one iteration
9/11 slide 3
counting or adding with loop

One of the most common applications of a loop. When counting something, such as the number of times the letter E appears in a string, we'll need to have the following variables:

  • the string we want to check
  • a variable to keep track of the index (location) we are currently looking at in the string
  • how many E's we have found so far

The gist of the solution is as follows. Be sure to note where we increment the values of count and index.

count = 0;
index = 0;
while (index < s.length())
{
  if(s.charAt(index) == 'E')
    count = count + 1;
  
  index = index + 1;
}
9/11 slides 2, 4-7
9/12 slides 2-3
Error checking

One convenient application of a while loop is this: let's say you are doing interactive input from the user. You ask the user to enter some information (such as a password). You want to verify the password, and allow the user to retry if it's wrong. This is called error checking.

The easiest way to do this is to have a boolean variable, such as "needInput". Initially, this variable is true, because your program is hungry for input. The while loop will iterate as long as you still "needInput". Inside the loop, you can have an if-statement that checks the validity of the input, and set needInput to false if the input is correct.

9/15 slides 3-4
Using arrays

If your program uses an array, you'll need to do the following:

  1. Declare the array, and allocate space for it.
  2. Put values into the array. This usually involves reading values from a file.
  3. Do some kind of computation on the array. Again, this will need a loop.
  4. Possibly print out the contents of the array.

Common things we might want to with an array are: Finding some summary statistics, such as the sum, average, largest, smallest of all the cells. Another common operation is searching for some value in the array, similar to the idea of indexOf for strings.

For example, here is how you would find the largest element of an array. Initially assume the first element is the max. Then, ask each of the other elements if they are even larger. Each time you find a larger element, record the location. We'll need to have variables for:

  1. the array itself
  2. an index into the array
  3. the location or the value of the largest element. I think it's better to determine the location, rather than just the largest value. If you know the location, it's easy to find the value at that location. If you only knew the value of the largest element, this would not tell you where it is in the array.
int maxLocation = 0;
int i = 1;
while (i < a.length)
{
if (a[i] > a[maxLocation])
maxLocation = i;
}
9/17 slides 1-3
kinds of errors

There are 3 kinds of programming errors:

  1. Syntax errors
  2. Run-time errors
  3. Logic errors
9/17 slide 4
object-oriented design

This is where we carefully design our program so that it contains multiple source files (classes). One file will be our Driver, which contains our main( ) function. This file will give a high-level overview of what the program is doing.

The details of the implementation are left to the other file(s). This is where we define our classes. A class is simply a new data type, which has its own attributes and operations.

We approach a problem by seeing that it is the story about 1 or more "big nouns". This could be about an employee, a hockey team, a household appliance, a bank account, a player at a casino, a building, a product for sale, etc.

A class has attributes that we need to store. For example, what makes one employee unique or different from other employees?

Once you have decided on the attributes, you can write constructor(s) for your class. The purpose of a constructor is to tell Java how to initialize the attributes of a new object (varaiable) of this class (type).

After we have written the constructor(s), we write the other functions that our class needs.

It really helps to look at (and do) many examples to get the hang of O-O design.

9/22 slides 1-4
9/24 slides 2-4
function design

The purpose of a function is to encapsulate some computations. By doing so, we can avoid typing the same code over and over.

When designing a function, we need to consider:

  1. Do we need any additional information to get started? These will be our parameters. For example, to deposit money into a bank account, you would need to know the amount of the deposit. Note that the account would not need to be a parameter, because when you are inside the Account class, you already know all of your own attributes.
  2. Do the computations in this function change any of the class attribute values?
  3. Do we need to return a value to whoever is calling this function? In other words, is there some answer we need to report?
9/22 slides 5-7
9/24 slides 5-6
function call

A function call usually looks something like this:

horse.feed(sugarCube, 3);

In this case, "horse" is the name of some object, and "feed" is the name of a function defined in the class that horse belongs to. This function takes 2 parameters, meaning that this is information necessary for the feed( ) function to behave correctly.

Note that the way we wrote the function call above, there is no return value. This is a likely indication that the feed( ) function returns void. If feed( ) did return a value and we don't assign it to a value, we are simply ignoring it.

Here is an example of how we can store the return value from a function:

double weight = horse.weigh( );

In this case, the weigh( ) function returns a value, and we want to store it, rather than discard it. Note that when we call the weigh( ) function, it needs no parameters so the parentheses are empty.

9/24 slide 6
3 kinds of constructors   9/25
constants   9/25
returning a new object   9/26
toString( ) function   9/29 slide 4
array attribute   10/1 slides 3-5