Writing Centers
Readings
TA Lab hours at SigSam
Monday at 7:00 and
Wednesday at 4:50.
My office hour is Wednesday at 4:30 in SF2304.
CS Department Selling VAJava.
The CD contains the "official" version of Visual Age, not the downloadable one. It's the same that is sold in the bookstore for (I think) $100 or so, but you don't get the paper manual or the MindQ tutorial that comes with the boxed version. The difference btwn the download and the boxed version is apparently quite minimal, but students without modem access or who can't.
how many people would be willing to pay $10 for the CD (incl. tax).
The reproducer needs a minimum order of 20, and it takes about 5 days from
when we place the order, so if we ordered next Thur. afternoon after you've
made your head count, they should be avail the following Thur (June 4).
The head count needn't be exact, but we don't want too many extra since
we likely won't be able to unload them.
Program Elements - Introduction
Primitive Data Types
A data type is defined by a set of values and the operators you can perform on them
Each value stored in memory is associated with a particular data type
The Java language has several predefined types, called primitive data types
The following reserved words represent eight different primitive types:
byte, short, int, long, float, double, boolean, char
Integers
There are four separate integer primitive data types They differ by the amount of memory used to store them
Type Storage Min Value Max Value byte 8 bits -128 127 short 16 bits -32,768 32,767 int 32 bits -2,147,483,648 2,147,483,647 long 64 bits < -9 x 10 18 > 9 x 10 18
Floating Point
There are two floating point types:
The float type stores 7 significant digits
The double type stores 15 significant digits
Approximate Approximate Type Storage Min Value Max Value float 32 bits -3.4 x 10 38 3.4 x 10 38 double 64 bits -1.7 x 10 308 1.7 x 10 308
Characters
A char value stores a single character from the Unicode character set
It is an ordered list of characters
It uses sixteen bits per character,
allowing for 65,536 unique characters
It is an international character set, containing symbols and characters
from many world languages
Characters
The ASCII character set is still the basis for many other programming languages
ASCII is a subset of Unicode, including:
uppercase letters A, B, C, ... lowercase letters a, b, c, ... punctuation period, semi-colon, ... digits 0 , 1, 2, ... special symbols \&, |, \\, ... control characters carriage return, tab, ...
Boolean
A boolean value represents a true or false condition
They can also be used to represent any two states, such as a light bulb being on or off
The reserved words true and false are the only valid values
for a boolean type
Wrappers
For each primitive data type there is a corresponding wrapper class. For example:
Primitive Type Wrapper Class int Integer double Double char Character boolean BooleanWrapper classes are useful in situations where you need an object instead of a primitive type
They also provide some useful methods
Variables
A variable is an identifier that represents a
location in memory that holds a particular type of data
Variables must be declared before they can be used
The syntax of a variable declaration is:
data-type variable-name;
For example:
int total;
int total, count, sum;
int total = 0, count = 20;
float unit_price = 57.25;
See Piano_Keys.java
The expression can be a single value or a more complicated calculation
Constants
A constant is similar to a variable except that they keep the same value
throughout their
existence
They are specified using the reserved word final in the declaration
For example:
final double PI = 3.14159;
final int numberOfStudents = 25;
Why use Constants?
they prevent inadvertent errors because their value cannot change
they make code more readable by giving meaning to a value
they facilitate change because the value is only specified in one place
Input and Output
Java I/O is based on input streams and output streams
There are three predefined standard streams:
The print and println methods write to standard output
Stream Purpose Default Device System.in reading input keyboard System.out writing output monitor System.err writing errors monitor
Input and Output
The Java API allows you to create many kinds of streams to perform various kinds of I/O
To read character strings, we will convert the System.in stream to another kind of stream using:
DataInputStream stdin = new DataInputStream(System.in);This declaration creates a new stream called stdin
We will discuss object creation in more detail later
Escape Sequences
See Echo.java
An escape sequence is a special sequence of characters preceded by a backslash (\)
Escape Sequence Meaning \t tab \n new line \" double quote \' single quote \\ backslash
Buffers
As you type, the characters are stored in an input buffer
When you press enter, the program begins processing the data
Similarly, output information is temporarily stored in an output buffer
The output buffer can be explicitly flushed(sent to the screen) using the flush method
See Python.java
Numeric Input
Converting a string that holds an integer into the integer value can be done with a method in the Integer wrapper class:
value = Integer.parseInt (my_string);value can be read and converted in one line:
num = Integer.parseInt (stdin.readLine());See Addition.java and Addition2.java
Expressions
An expression is a combination of operators and operands
The arithmetic operators include addition (+), subtraction (-), multiplication (*), and division (/)
Operands can be literal values, variables, or other sources of data
The programmer determines what is done with the result of an expression
(stored, printed, etc.)
Division
If the operands of the / operator are both integers, the result is an integer (the fractional part is truncated)
If one or more operands to the / operator are floating point values, the result is a floating point value
The remainder operator (%) returns the integer remainder after dividing the first operand by the second
The operands to the % operator must be integers
See Division.java
The remainder result takes the sign of the numerator
Division
Expression Result
17 / 5 3
17.0 / 5 3.4
17 / 5.0 3.4
9 / 12 0
9.0 / 12.0 0.75
6 % 2 0
14 % 5 4
-14 % 5 -4
Operator Precedence
The order in which operands are evaluated in an expression is determined by a well-defined precedence hierarchy
Operators at the same level of precedence are evaluated according to their associativity (right to left or left to right)
Parentheses can be used to force precedence
Appendix D contains a complete operator precedence chart for all Java
operators
Operator Precedence
Multiplication, division, and remainder have a higher precedence than addition and subtraction
Both groups associate left to right
Expression: 5 + 12 / 5 - 10 % 3 Order of evaluation: (3) (1) (4) (2) Result: 6
Operator Precedence
Expression Result
2 + 3 * 4 / 2 8
3 * 13 + 2 41
(3 * 13) + 2 41
3 * (1 3 + 2) 45
(5 * (4 - 1)) / 2 7
Operator Precedence
boolean test8 = 6*4 < 3*8; /* False. 24 is not less than 24 */
The if Statement
if (condition)
statement;
false
(condition) ------.
| |
true | |
| |
V |
statement; |
| |
|<-----------'
|
V
Boolean Expressions
The condition of an if statement must evaluate to a true or false result
Java has several equality and relational operators:
Operator Meaning
== equal to
!= not equal to
< less than
<= less than or equal to
> greater than
>= greater than or equal to
See Temperature.java
Block Statements
Several statements can be grouped together into a block statement
Blocks are delimited by braces
block statement can be used wherever a statement is called for in the Java syntax
See Temperature2.java
The if-else Statement
if (condition) statement1; else statement2;
false
(condition) ------.
| |
true | |
V V
statement1; statement2;
| |
|<-----------'
|
V
Nested if Statements
The body of an if statement or else clause can be another if statement
These are called nested if statements
See Football_Choice.java
Note: an else clause is matched to the last unmatched if
(no matter what the indentation implies)
The while Statement
while (condition)
statement;
false
,---->(condition) ------.
| | |
| true | |
| | |
| V |
| statement; |
| | |
`----------' |
,----------'
|
V
Zero or more times
Infinite Loops
The body of a while loop must eventually make the condition false
If not, it is an infinite loop, which will execute until the user interrupts the program
This is a common type of logical error
- always double check that your loops will terminate normally
See Forever.java
Program Development
The creation of software involves four basic activities:
establishing the requirements
creating a design
implementing the code
testing the implementation
The development process is much more
involved that this, but these basic steps are a good starting point
Program Development
Requirements ------.
|
V
English
|
Design -> |
V
Pseudo code
|
Implementation -> |
V
Java code
Testing
3.16
/* Pseudo code:
read an integer value, num
if num < 2 print error and return
sum = 2 + 4 + 6 + ... + the largest number less
than or equal to num
print sum;
/*
import java.io.*;
public class T { // need to find a better class name than T.
public static void main (String[] a) throws IOException {
int num = 100;
if ( num < 2 )
System.out.println(" Error " + num + " < 2");
else {
int sum = 0;
for (int i = 2;i <= num;i = i + 2) sum += i;
System.out.println("Sum = " + sum);
}
}
}
Requirements
Requirements specify the tasks a program must accomplish (what to do, not how to do it)
They often address the user interface
An initial set of requirements are often provided, but usually must be critiqued, modified, and expanded
It is often difficult to establish detailed, unambiguous, complete requirements
Careful attention to the requirements can save significant time and
money in the overall project
Design
algorithm, which is a step-by-step process for solving a problem
The design specifies the algorithms and data needed
In object-oriented development, it establishes the classes, objects, and methods that are required
The details of a method may be expressed in pseudocode, which
is code-like, but does not necessarily follow any specific syntax
Implementation
Implementation is the process of translating a design into source code
Most novice programmers think that writing code is the heart of software development, but it actually should be the least creative
Almost all important decisions are made during requirements analysis and design
Implementation should focus on coding details, including style guidelines
and documentation
Testing
A program should be executed multiple times with various input in an attempt to find errors
Debugging is the process of discovering the cause of a problem and fixing it
Programmers often erroneously think that there is "only one more bug" to fix
Tests should focus on design details as well as overall requirements
Program Development
See Average.java
Follow the process of requirements analysis, design, implementation, and testing
There are always multiple ways to design and implement a program
Any design has advantages and disadvantages; there are always trade-offs
See Average2.java