Download BPJ444: Business Programing Using Java Week 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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
Java Class


source code file name: Employee.java
instance and static fields (variables)


instance and static methods





state or attributes
behaviour
constructors create objects
setters and getters control access to fields
the keyword: this
nOTe: Java cares deeply about case
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
nameMethod




verb or verb phrase
start in lowercase
capitalizeEachSubsequentWord
e.g.
void giveRaise(int increase)
public String getAddress()
fields or variables





noun or noun phrase
start in lowercase
capitalizeEachSubsequentWord
try for short and memorable
e.g.
StringempName;
int
empStartYear;

a method’s local variables and
parameter names can be even shorter
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
Primitive Literals

byte b = 2;
short x = 23;
// an int literal is used
// an int literal is used

int
y = 23;
// default int literal


int
int
z = 0123; // octal value (decimal value=83)
v = 0x1111; // hex value (decimal value=4361)

long t = 2200000000L;

float r = 3.14f; // a float literal
double s = 3.14;// default double literal


// a long integer literal
more Primitive Literals

boolean exit = true,
done = false;

char c1 = 'a',
c2 = '\u0041',


// single character
// Unicode escape seq.
// is 4 hex digits
c3 = '\n';
// ASCII escape seq.
\n is new line (works in some GUI widgets)
\t is tab (does not work in any GUI widgets)
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
Using Classes



examples:
Employee.java
a template for an employee
Employees.java
creates a company of employees
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.
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
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
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
Why use BigDecimal instead
of double?


from the IBM computer scientist who wrote the new,
improved BigDecimal class: Using Java 5.0 BigDecimal
and Decimal Arithmetic FAQ
from an assignment that didn’t use BigDecimal…
Type
Code
CF
Vegan
Y/N
Y
Survey
Amount
3.59
Last Date
Surveyed
2003-07-04
Restaurant
Name, Location
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!
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
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);
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.
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
Integer Class

constructors


wrap primitive integer value into an object:
Integer intWrap = 123; // autoboxed
Integer 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();
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 )