Introduction To Scientific Programming Chapter 2

Preview:

Citation preview

Introduction To Scientific Programming

Chapter 2

S.Horton/107/Ch. 2 Slide 2

Chapter 2

I. Computer Storage

II. Primitive Data Types & Expressions

III. Strings – Part I

IV. Keyboard & Screen (Console) I/O

V. Software Development A. Defining Projects

B. Setting up your own programming environment

S.Horton/107/Ch. 2 Slide 3

I. How A Computer Stores Information

• Everything on a computer is fundamentally stored as a one or a zero (irreducible state):

• Two state transistor (on/off) in silicon memory• Magnetic pole orientation (up/down) in hard drive• Reflection (pit/valley) in optical storage

• This single piece of binary (or digital) information is called a “bit”.

• Each storage location has an address and a value.

S.Horton/107/Ch. 2 Slide 4

Computer Storage (cont.)• By convention, data is moved through the CPU in

groups of 2, 8, 16, 32, or now 64 (powers of 2).

• The basic addressable unit is 8 bits = 1 “byte”.

• Today, storage is quoted in KB, MB, GB, or TB!

S.Horton/107/Ch. 2 Slide 5

Computer Numbering Systems You are probably most familiar with the decimal

numbering system – i.e. (0-9) per digit. This is called a Base 10 system.

This is only one of many possible “bases”.

Binary numbering is Base 2 (0-1).

The other important numbering system used extensively on computers, is Base 16, called hexadecimal (0-9,A-F).

S.Horton/107/Ch. 2 Slide 6

Basic Number Types In a program, you must declare a type for your data. The basic types are:

int and long for integer numbers float and double for real numbers

S.Horton/107/Ch. 2 Slide 7

Java Declaration ExamplesSyntax:Type Variable_1, Variable_2, … ;

Examples:int count;

long i;

float total;

double marketValue;

Variants include:int count = 0;

long i = 1000L;

float total = 1.0F;

double marketValue = 1234.56D, interestRate = 5.75D;

S.Horton/107/Ch. 2 Slide 8

Character Representation The most common standard for character reference is known

as ASCII which uses 8 bits to reference all individual characters. How many possibilities are there?

A newer extended character set is known as Unicode which uses 16 bits for reference addresses. Java uses Unicode!

How many bytes are used to reference a Unicode character?

The type used for a single characters is:

Type Storage Min Maxchar 2 bytes 0X0000 0XFFFF

S.Horton/107/Ch. 2 Slide 9

ASCII Character Set Many characters that you might wish to output are

not available through the keyboard, so you need to use it’s “character code”.

S.Horton/107/Ch. 2 Slide 10

The Unicode Character Set An ‘A’ is Unicode code (0X0041) A whitespace or blank is code (0X0020). The  is the last Unicode character at (0XFFFC)

S.Horton/107/Ch. 2 Slide 11

Java Character Declaration Examples

Syntax:Type Variable_1, Variable_2, … ;

Examples:

char questionMark;

Variants include:

char questionMark, blank, slash;

char questionMark = ‘?’;

char blank = ‘ ‘, copyRightSymbol = 0X00A9;

S.Horton/107/Ch. 2 Slide 12

Another Important Primitive Data Type The boolean data type has only two possible values

True or False.

Only requires 1 bit!

Used for many logical comparisons, expressions, and states.

Example:

boolean completed = false;

Type Storage Values (no range)boolean 1 bit True or False

S.Horton/107/Ch. 2 Slide 13

Notes About Variable Names Only use letters, digits, and ‘_’. A name can’t begin with a digit.

Java is case-sensitive – TimeOfDay is different than timeOfDay

Use capitalization conventions: MAX_COUNT, myVariable, System.Out

Don’t use reserved words – i.e. public, true, … (see below)

S.Horton/107/Ch. 2 Slide 14

Basic Operators, Expressions & Assignments

Variables are assigned values or the results of an expression.

An expression is made up of variables and operators.

The basic math operators are: +,-,*,and / .

Example:int a=0, b=47, c;

c = a + b;

c = ?

S.Horton/107/Ch. 2 Slide 15

Example: what is c = 14 + 8 / 2; (11 or 18)?

Hint - always use parenthesis to force the evaluation you intended. Example, c = (14+8)/2;

Be Careful About “Operator Precedence”

S.Horton/107/Ch. 2 Slide 16

Handy Extensions Unary operators ++ and --

Example:

count++; is equivalent to count = count + 1;

Specialized assignments +=, -=, and *= Example:

cnt += 2; is equivalent to cnt = cnt + 2;

S.Horton/107/Ch. 2 Slide 17

Type Casting What happens when you need to convert a variable of

one type to another type?

You have two choices: Have the compiler guess Explicitly state it yourself by placing (type) in front of variable!

Hint – always state it yourself to avoid errors!

Example:final static double quarter = 0.25D; int quantity = 3;double totalMoney;

totalMoney = (float) quantity * quarter;

S.Horton/107/Ch. 2 Slide 18

Type Casting - II There are a number of details associated with casting:

Rules for “Promotion” in expression evaluation Widening conversions between types, i.e. float to double Narrowing conversions between types, i.e. double to int

Example:int a=8,b=3,result;

float fresult;

result = a/b; // result = 2

fresult = a/b; // fresult = 2.0

fresult = (float)a / (float)b; // fresult = 2.66667

S.Horton/107/Ch. 2 Slide 19

III. Strings Strings are not a primitive type in Java, they are a Class.

Declaration is straightforward:Syntax:String Variable_1 = Expression_1, Variable_2 = Expression_2, … ;

Example:String title1, title2 = “I Love JAVA”;

String storage in memory:

0 1 2 3 4 5 6 7 8 9 10

I L O V E J A V A

Index

Value

S.Horton/107/Ch. 2 Slide 20

More About Strings Conceptually, you perform an operation on (or

manipulate) a string and assign it to a new string

String concatenation with “+” operator:String newtitle, title1 = “I Love”;

String title2 = “ JAVA”;

newtitle = title1 + title2; //This is string concatenation

Combining string concatenation with type casting

System.out.println(“The answer =“ + fresult);

S.Horton/107/Ch. 2 Slide 21

Even More About Strings As a “Class”, strings have

associated with them a group of “Methods” that can perform string manipulations.

Example:int len;

String newtitle, title1 = “I Love”;

String title2 = “ JAVA”;

newtitle = title1 + title2;

len = newtitle.length();

System.out.println(“Length=“ + len);

S.Horton/107/Ch. 2 Slide 22

A Final Word About Strings There are some special characters that have

special meaning inside a string.

They are called “escape” sequences.

They either clear up ambiguity or are codes to perform non-printable, “control” operations.

S.Horton/107/Ch. 2 Slide 23

IV. Keyboard & Screen I/O For user interaction in our class programs, we will stick

with very fundamental Input/Output (I/O) using a keyboard (Input) and a DOS window on the video monitor or screen (Output).

The field of user interface is a very deep, rich topic in itself but not the focus of this class.

Later in this class, we will explore more sophisticated methods involving data I/O.

S.Horton/107/Ch. 2 Slide 24

Output We will utilize the System.out Class for displaying text

output to the DOS window.

The two main methods are: print and println

Example:System.out.println(“Mortgage table for “ + interestRate + “%”);System.out.println(“Your monthly payment = $“ + monthly);System.out.print(“i \t Principle \t Remaining Balance \r\n”);System.out.print(“_ \t _________ \t __________________”);

S.Horton/107/Ch. 2 Slide 25

Input – The “SavitchIn” Input Class

We will use a predefined Class from the course book to perform keyboard input named “SavitchIn”.

SavitchIn contains a group of methods which will read input from the keyboard and perform some basic type and range checking.

S.Horton/107/Ch. 2 Slide 26

V. More Software Development Review basic Java Syntax rules:

Java is case sensitive Java ignores whitespaces All statements must end with “;” Blocks of code begin and end with { } Comment block must begin and end with /* */ “In-line” comments begin with //

S.Horton/107/Ch. 2 Slide 27

JCreator IDE Development Structure

Start by setting up a “Project”. File-New-Project

Choose “Empty Project” Type name – ex. “107Project1” Click Next-Next-Finish

Copy the Java source files into the new Project directory You will now see them in the “File View” under your

project name.

You must compile each source file into byte code (.class).

Then you can run your main program.

S.Horton/107/Ch. 2 Slide 28

Setup Your Own Outside Java Programming Environment!

1. Go to JCreator website

2. Click on “Download” section

3. Download “JCreator LE v3.50”

4. Use link to also download Sun J2SE SDK 1.5 (Warning - 50 MB)

5. Install J2SE SDK first!

Recommended