programming

advertisement

Hello

What is a program?

Series of instructions

Ordered, unambiguous, terminates

In a programming language, executable

Algorithm - Series of instructions

Ordered, unambiguous, terminates

Written in Pseudocode

Programming Languages

High Level:

Python, Perl, Javascript, Ruby, PHP, Lisp

Objected Oriented: C++, Java, C#

Structured: Basic, FORTRAN, Pascal, C, Ada

Low Level:

Assembly, Java Bytecode – assembly for Java

Virtual Machine (JVM)

Executable Code:

Binary, Machine

Matlab, IDL

Steps to Programming:

1) Define & Analyze the problem: a.

Program specifications b.

Test data – input & corresponding output

Write a program to compute a GPA.

Sample Run:

Welcome to the GPA program!

How many classes? 4

Enter grade & credits for 1: A 3

Enter grade & credits for 2: b- 4

Enter grade & credits for 3: A+ 4

Enter grade & credits for 4: c 2.5

Your gpa is 3.24

2) Design the solution a.

Identify the components b.

Write pseudocode

3) Write program code a.

Edit/type b.

Compile c.

Rewrite/fix/debug d.

Repeat as necessary

4) Testing a.

Debugging run-time errors, logic errors

5) Document

6) Maintain & upgrade

Operating Systems: Linux, Windows, Mac OS, Unix,

BSD, DOS

Three types of program control:

1) Sequential (default, top to bottom) a.

Variable declarations b.

Assignments c.

Method calls d.

Block statement { }

2) Decisions a.

if (boolean expression)

True-statement; b.

if (Boolean expression)

true-statement;

else

false-statement; c.

switch (integer expression) { case val1: case val2: stmt-1;

break;? case val3: stmt-2;

Stmt-3; [break?]

default: last-stmt;

}

Each case val must be literal constant (not a variable)

3) Loops/Repetition

Calculating a GPA:

Enter grade & credits for 1: A 3

4 * 3 = 12

Enter grade & credits for 2: b- 4

2.7 * 4 = 10.8

Enter grade & credits for 3: A+ 4

4 * 4 = 16

Enter grade & credits for 4: c 2.5

2 * 2.5 = 5

Your gpa is ????

Coursepoint total: 43.8

Credit total: 13.5

Your GPA is 43.8 / 13.5 = 3.244444

PRIMITIVE DATA TYPES IN JAVA (reserved words in bold) boolean – true / false values only integer: byte, short, char, int, long real: float, double eg: 1234.596788 -> .1234596788 e4 eg: .000000345 -> .345 e-6 float use f or F at end of literal float number = 13.5f; conversions: going from one data type to another widening: from smaller type to larger type can do implicitly eg: float number = 13; (int to float) narrowing: from larger type to smaller must do explicitly with typecast eg: int value = (int) -13.5; (truncates) eg: char ch = (char) 65; characters: Unicode system

‘0’ – ‘9’ in order starting with 48

‘A’ – ‘Z’ in order starting with 65

‘a’ – ‘z’ in order starting with 97

‘M’ < ‘a’ so be careful

Special chars: escape sequences

‘\”’ ‘\n’ ‘\t’ ‘\\’

OPERATORS - PRECEDENCE

(), (casting), - (negation), ! (not)

/ (divide), * (multiply), % (mod = integer division remainder: 51%5 -> 1, 8%12 -> 8)

+ (add), - (subtract)

< (less than) > (greater than) <= >= == !=

&& (and)

|| (or)

= (assignment)

Identifier (variables, methods, classes) naming rules:

Can use any letter (A-Z, a-z)

Can use any digit (0-9)

Can use _ (such my_favorite_class)

Can NOT start with digit

Boolean operators: (truth table)

A && B

True && true => true

True && false => false

False && true => false

False && false => false

A || B

True || true => true

True || false => true

False || true => true

False || false => false

!A

True => false

False => true if (!(4 < 16/3) && 30%4 >= -1 || 5 !=

Math.sqrt(30)) // => true

System.out.println(“stand up!”);

if (((!(4 < (16/3))) && ((30%4) >= -1)) ||

(5 != Math.sqrt(30))) // => true

System.out.println(“stand up!”);

DeMorgan’s Laws:

!(A && B) == !A || !B

!(A || B) == !A && !B

Comparing Strings:

String word1, word2; word1 = kb.next(); word2 = kb.next(); if (word1.equals(word2))

… word1.equalsIgnoreCase(word2) word1.compareTo(word2) negative if word1 < word2 positive if word1 > word2

0 if word1 equals word2

All comparisons are character by character using

UNICODE value

Table < table apple < table

Apple < table

Table < apple ***** apple > aPPle if (word1.compareToIgnoreCase(word2) > 0)

System.out.println(word2 + “ comes before “

+ word1);

(more alphabetical ordering)

POLYMORPHISM

“many forms”

Method OVERLOADING – same name, different parameter lists (signature of a method)

Eg: printRA (our sample code)

Random : nextInt() or nextInt(10)

Constructors

String : substring(4) or substring(0,4)

INHERITANCE – create class that is derived from another class

Original is called “Base” or “Super” or “Parent”

New is called “Derived” or “Sub” or “Child”

Playing cards: Deck is a Hand

Vehicles: #wheels, speed, mileage, #passengers, cargo size

Power Vehicles: engine

Car

Sedan

Toyota Camry

Honda Accord

Pontiac Grand Am

Sports: #gears, 0-60 time

Porche 911

Honda S2000

Wagon

Volvo ???

Ford Taurus

Subaru Forester

Truck

SUV

Ford Escape

Honda CRV

Train

Plane

Motorcycle

Human Vehicles

Cycle

Bike

Unicycle

Tricycle

Object – base of all classes in Java

Data: memory address, class name

Methods: toString(), equals(Object), clone() – makes shallow copy

Method OVERRIDING – if derived class has new definition for a method in its base class

(method signatures exactly the same)

Eg: toString() – original definition is in

Object

New Inheritance System:

Circle: radius

Triangle: 3 side lengths, 2 angles

Right Triangle: one angle is always 90

Parallelogram: 2 sides, 1 angles

Rectangle: angles all 90

Square: sides all same

Square “is a” Rectangle

Rectangle “is a” Parallelogram

INTERFACE – collection of method headers, good for collections of things that have common methods, general algorithms

Shape: area(), perimeter(), draw()

Comparable: int compareTo()

TESTING

Test cases: boundary cases, typical, valid data, invalid data

Data sources: make up constants, random, loop through range of values

Unit testing: individually test each component of program (each method)

Black-box testing: No knowledge of code

White-box testing: Look at code and make sure that every possible branch is tested.

Regression testing: keep test data & reuse for new versions of a program

Evaluating test results:

Do calculations by hand

Compare to “oracle” – anything already known to compute the right result

DEBUGGING

Go to TAs

Put print statements everywhere o Comment out when done o Make sure buffer gets flushed

 println (not print)

 System.out.flush()

Comment out possible problem sections

IDE debugging tools (eg, in Jgrasp) o Checkpoints

o Watch variables o Etc.

Download