![Page 1: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/1.jpg)
BPJ444: Business Programming using JavaJava basics
Tim McKenna
Seneca@York
![Page 2: BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/21.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022051001/56649e915503460f94b96874/html5/thumbnails/22.jpg)
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 )