Week 1 Meeting 2

The programming process:

Design – Analyzing the problem you are solving. 

·         Develop a comprehensive set of tests.

·         Decompose the problem into actors (nouns) and actions (verbs).

 

Code Development – Creating code to implement your design.

·         Create classes for the actors you identified during design.

·         Create methods for the actions you identified during design.

 

Compilation – Running the program that translates your Java instructions into machine code that the computer can execute; this program  is called the compiler

1.    After writing a small amount of code, compile it and find errors.

2.    Errors are much easier to identify if you compile small amounts at a time.

 

Execution – Running and testing your program.

·         Test your program by passing in the inputs for each test and checking the outputs against those you have calculated by hand.

·         When your program does not produce the correct output for one of your inputs, trace your code to find the source of the error.

·         Tracing is pretending you are the computer, reading your code, and doing by-hand exactly what the computer would do.

·         The debugger is a very useful tool for helping you trace your program.

 

Programming is an iterative process.  You first do a high-level design, then implement one method, then compile this class, then execute this method, then go back and do it again for the next method.

 

Here is a picture from the cannon powerpoint:

Preparing to run

Use Windows explorer (not Internet Explorer) to create a directory named cs256 on your P: drive.

Use the JGrasp Java Integrated Development Environment to write java code

·         Find the JGrasp program on the Start menu (Start | JGrasp)

·         Notice there are 3 prominent windows:

the ‘browse’ window

the code display window

the output window

Example: HelloWorld.java

A program that will display the message Hello World and read and display a String from the user.

public class HelloWorld {
  
public static void main(String[] args) {
      System.out.println(
"Hello World!");
   }
}

Edit

In JGrasp, choose menu item:

File | Open

Browse to P:

Create a folder cs256 if you do not already have one.  (R-click | new folder)

HelloWorld.java (in File Name: input box | create)

 

Compile
Next, click the compile button    compileButton

to translate your java code into byte codes that the java Virtual Machine can execute.

 

Notice the messages from the compiler in the Run I/O window at the bottom of the screen.

 

Run

Click the run button    runButton

 

Notice your message, Hello World!, appears in a box on the screen.

 

Debug

Click the debug button   

 

Notice your message, Hello World!, appears in a box on the screen.

Now click in the left column of the code view next to the System.out.println(“Hello World!”); line, which puts a breakpoint in your code, and run click the debug button again.

 

 

Variables

To do useful work, you often have to store values.

Values are stored in memory.

A particular memory location is named by a variable.

So, declaring a variable in your program associates a name with a particular memory location.

 

A variable declaration consists of 3 things:

1.    A type name – we have already seen Scanner and String and now we see double which indicates the variable will hold a number.

2.    A name you make up, which should have something to do with the purpose of the variable.  The following variable stores a person’s age, so it is named age

3.    A semicolon, which ends the variable declaration.

 

double age;

 

Reserves a spot in memory named age that can hold a numeric value.

 

Assignment

 

The statement that writes a value to a memory location is an assignment statement.  The assignment operator is =, which is not to be confused with what we call ‘equals’ in mathematics.  In this class, we pronounce = ‘gets’ to let us know a memory location is getting a value.

age = 5.5;

causes the value 5.5 to be written to the memory location named age.

 

To read a value from the user and store the value the user supplies in the variable named age that you declared above, type:

age = TextIO.getlnDouble();

 

The left side of an assignment statement must contain the name of a variable you have previously declared.

 

The right side may contain any expression.  In this case, we assume there is already a variable named scan holding a Scanner, which is able to read data from the user.

 

The value obtained by evaluating the right side is stored in the memory location named by the variable on the left side.

Expressions

A combination of literal values, variable names, and possibly values read from the user connected by operators.

Basic Arithmetic operators

+, -, *, /

Evaluation

* and / happen before + and

As in mathematics, () can be used to group operations.

 

After the entire expression on the right side of an assignment statement has been collapsed to its single value, this value is stored in the memory location named by the variable appearing on the left side of the assignment.

 

Here are example statements that add two numbers supplied by the user:

(Be careful – not all double quotes in these notes are real java double quotes and they can cause a compiler error.  Fix by retyping the double quote character in JGrasp.)

 

          System.out.print("Your double> ");

          // read a number from the user

          // and store this number in the variable named first

          // NOTE: you must type Enter at the end of the number

          first = TextIO.getlnDouble(); 

          System.out.print("Your double> ");

          second = TextIO.getlnDouble();      

          result = first + second;

 

Displaying a stored value

 

Call System.out.println() and supply a text message a + and the name of the variable that names a memory location, e.g.,

 

System.out.println("result is: " + result);

// NOTE: this + is concatenation, NOT addition, because the first argument, “result is”

// is a String, not a double

 

 

 Example: Demonstrates the ideas above

Save ../demos/W01N02.java to P:\cs256

Save ../demos/TextIO.java to P:\cs256 (must be in the same directory where you saved W01N02.java)

Edit

In JGrasp, choose menu item:

File | Open

Browse to P:\cs256\W01N02.java