The design recipe will evolve during the course of this term, in line with the different phases that are being considered.
During the first module Computation, we will be constructing Java programs using the following template:
/** * Define the package within which you are working. The package should include * your CCC unix id, so we can uniquely identify the code. */ package heineman.oct24; /** * What is the specific concept being defined here. The name of the * class reflects the concept being defined. * * @author AUTHOR-ID */ public class Template { /** Define main function, the entry point to a Java program. */ public static void main (String []args) { // The BODY of the function. The computation appears here, between the { ... } // INPUT
// PROCESSING
// OUTPUT } } |
This template embodies the traditional division of computations into an INPUT, PROCESSING, and OUTPUT phases. For the purposes of this first module in the course, all Java computations are defined within a single main method as a series of Java statements.
Some small programs do not require any user input from the keyboard. These programs consist solely of defining variables to represent values over which computations are made.
Over time, one realizes the benefit of being able to run a program multiple times, with different responses from the user. So there needs to be a standard way for the program to "read an int value typed on the keyboard" or "read in a String of characters until the user presses enter".
When you store information in a primitive type (int, boolean, or double, for example), programs can perform simple computations over these values. As we will see, most of the operations that you would find on a simple calculator are available to you. Those functions found only on scientific calculators (i.e., x^y, Sin-1 x) will be handled differently.
You can define new variables at any time (so long as they have a unique name) and the computation proceeds in order of the defined statements.
Once the computation has defined all required values, they are output to the console by printing them out, either one by one, or as a group, using the functionality provided by System.out.print() and System.out.println().
You can choose the appropriate representation for your information. For starters, you can use variables defined as primitive types. By Oct-31 you will start to use arrays for aggregating information.
Variable Definition | int x; |
Array Definition | double []ar; |
As computations proceed, you have a variety of statements at your disposal
Variable Assignment | x = 10; |
Variable Definition & Assignment | double pi = 3.14159; int [] daysOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
Conditional | if (boolean-guard) { true-StatementBlock } else { false-StatementBlock} |
Looping | while (boolean-guard) { whileBlock } for (initialization; |
In the following statement, the value of y (on the LEFT hand side) is modified while x (the variable on the RIGHT hand side) remains unchanged. Note, you are not making the logical statement that the values are the same!
y = x; |
To increment a variable, use the following template, which may look odd at first if you view it mathematically:
y = y + 1; |
This simply says that you set y to be the value of y's current value plus one. Note that Java has an additional operator that offers the equivalent capability, the postIncrement operator, viz.:
y++; |
To compare values, use the standard operators (<=, <, >, >=).
Thus, one commonly writes
if (x < 10) { // code only executes if guard is true } |
To compare equality, we must use something other than the assignment operator '='. For historical reasons, Java uses the operator '==' (pronounced "equals-equals") as the proper way to compare values. To compare inequality, use the operator '!=' (pronounced "not-equals").
You can construct an array of type T by using the template
// template to create array: T []varName= new
T[desiredCount]; int []myAr = new int[10]; |
Once an array is created, you can change individual values via indexing:
myAr[3] = x; |
note that x is read in the above fragment, and
unchanged. The variable myAr still points to the same array, but its
4th member (REMEMBER! Count from zero for indexing) has been changed to be
the value of x.
Given a structured array, you will likely need to perform some operation
over all elements of the array (i.e., double a value, see if the element
matches a target value being sought). The standard template for processing
an array is:
int i = 0; while (i < ar.length) { // process the ith element ... ar[i] ... // advance! i = i + 1; } |