bpj444: business programming using java java basics tim mckenna seneca@york

22
BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

Upload: kathryn-dorsey

Post on 12-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

BPJ444: Business Programming using JavaJava basics

Tim McKenna

Seneca@York

Page 2: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

Outline

Java classes and members primitive and reference fields operators control flow statements the BigDecimal class the wrapper classes

Page 3: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

Java Class

source code file name: Employee.java instance and static fields (variables)

state or attributes instance and static methods

behaviour constructors create objects setters and getters control access to fields the keyword: this nOTe: Java cares deeply about case

Page 4: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

ClassName

noun or noun phrase use Title Case: CapitalizeEachWord class Employee

class EmployeeSalary extends Employee

name.java file must be exactly the same as class name within the file

Page 5: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

nameMethod

verb or verb phrase start in lowercase capitalizeEachSubsequentWord e.g.void giveRaise(int increase)public String getAddress()

Page 6: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

fields or variables

noun or noun phrase start in lowercase capitalizeEachSubsequentWord try for short and memorable e.g.String empName;int empStartYear;

a method’s local variables and parameter names can be even shorter

Page 7: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

Primitive Data Types

summary (The Java Tutorial) http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html

numeric types: int, double boolean logic type: boolean character type: char

primitives contain a simple numeric value for language efficiency

primitives are not objects

Page 8: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

Primitive Literals byte b = 2; // an int literal is used short x = 23; // an int literal is used

int y = 23; // default int literal

int z = 0123; // octal value (decimal value=83) int v = 0x1111; // hex value (decimal value=4361)

long t = 2200000000L; // a long integer literal

float r = 3.14f; // a float literal double s = 3.14; // default double literal

Page 9: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

more Primitive Literals

boolean exit = true,done = false;

char c1 = 'a', // single character c2 = '\u0041', // Unicode escape seq.

// is 4 hex digitsc3 = '\n'; // ASCII escape seq.

\n is new line (works in some GUI widgets) \t is tab (does not work in any GUI

widgets)

Page 10: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

Object Reference Types

field containing a single value which refers to an object i.e. a memory address, a pointer not modifiable by programmer initial value is null until assigned examples: object names, array names,

class names Example: References.java

Page 11: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

Using Classes

examples: Employee.java

a template for an employee Employees.java

creates a company of employees

Page 12: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

Data Type Conversion

automatic widening conversion if it fits, no casting needed int auto cast to long l = i; int auto cast to double d = i; char auto cast to int i = 'a';

casting: explicit narrowing conversion Example: Casting.java …be careful about overflow. see e.g.

Page 13: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

Operators in Java

summary of operators (Java Tutorial) http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html

types of operators: arithmetic, logical, relational, conditional, assignment

precedence of operators all similar to C language

Page 14: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

Control Flow Statements

summary (The Java Tutorial) http://java.sun.com/docs/books/tutorial/java/nutsandbolts/flow.html

branching statements break (same as C ) break with a label (new in Java) continue (same as C ) continue with a label (new in Java)

Examples: BreakDemo.java, BreakWithLabelDemo.java, ContinueDemo.java, ContinueWithLabelDemo.java

Page 15: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

The BigDecimal Class

98.7% of numeric data in business applications has a predetermined number of decimals: currency, exchange rates, unit costs, discounts, taxes precision: digits in number including decimals scale: number of decimal places

JDBC maps DB decimal fields to BigDecimal BigD class provides control of rounding behaviour Note: double primitive is only an approximation! Examples: PaySchedule.java, PaySchedule2.java,

DontUseDouble.java

Page 16: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

Why use BigDecimal instead of double?

from the IBM computer scientist who wrote the new, improved BigDecimal class: Using Java 5.0 BigDecimaland Decimal Arithmetic FAQ

from an assignment that didn’t use BigDecimal…Type Vegan Survey Last Date RestaurantCode Y/N Amount Surveyed Name, LocationCF Y 3.59 2003-07-04 Blueberry Hill, YLM

Please enter survey amount (+ add, - subtract) > -3.59

Unable to subtract this amount -3.589999999999999857891452847979962825775146484375

because there is only 3.589999999999999857891452847979962825775146484375 left!

Page 17: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

BigDecimal arithmetic

BigDecimal sum, difference, …; // note: immutable class

sum = addend.add(augend); // a = b + c

sum = sum.add(anotherBigDecimal); // a += d

difference = minuend.subtract(subtrahend); // a = b - c

Page 18: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

BigDecimal arithmetic

import static java.math.RoundingMode.HALF_UP;// standard rounding, import the constant

BigDecimal product, quotient; // immutable product = multiplicand.multiply(factor); product = // round result to 2 decimals

product.setScale(2, HALF_UP); product = // multiply and round

multiplicand.multiply(factor).setScale(2, HALF_UP); quotient = // round result to 2 decimals

dividend.divide(divisor, 2, HALF_UP);

Page 19: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

BigDecimal comparisons

import static java.math.BigDecimal.ZERO; payment.compareTo(interest) > 0

"if payment is greater than interest " principal.compareTo(payment) <= 0

"if principal is less than/equal to payment" principal.compareTo(ZERO) == 0

"if principal is equal to zero" principal.equals(ZERO)

…may not be what you mean. see API.

Page 20: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

The Wrapper Classes

classes representing primitive data types: Byte, Short, Integer, Long, Float, Double,

Character primitive data type may need to be

wrapped in an object, e.g. to be stored in a List which accepts only objects.

J2SE 1.5 now does autoboxing to/from primitives and object wrappers

Example: WrapperDemo.java

Page 21: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

Integer Class

constructors wrap primitive integer value into an object:

Integer intWrap = 123; // autoboxedInteger intFromString = new Integer(“123”)

extraction of a value from the object public int intValue ( )

e.g. int i = intWrap.intValue(); // old way int I = intWrap; // new way autoboxed

public String toString() e.g. String s = intWrap.toString();

Page 22: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York

Integer Class

three static methods (class or utility methods) static int Integer.parseInt ( String s )

e.g. String s = "123456789";int i = Integer.parseInt ( s )

static String Integer.toString ( int i) e.g. int i = 123456789;

String s = Integer.toString ( i ) static Integer Integer.valueOf ( String s )

e.g. String s = "123456789";Integer intWrap = Integer.valueOf ( s )

or Integer intWrap = new Integer ( s )