Presentation is loading. Please wait.

Presentation is loading. Please wait.

BPJ444: Business Programming using Java Java basics Tim McKenna

Similar presentations


Presentation on theme: "BPJ444: Business Programming using Java Java basics Tim McKenna"— Presentation transcript:

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

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

3 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

4 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

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

6 fields or variables noun or noun phrase start in lowercase capitalizeEachSubsequentWord try for short and memorable e.g. StringempName; intempStartYear; a method’s local variables and parameter names can be even shorter

7 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

8 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

9 more Primitive Literals booleanexit = true, done = false; char c1 = 'a', // single character c2 = '\u0041', // 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)

10 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

11 Using Classes examples: Employee.java a template for an employee Employees.java creates a company of employees

12 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.

13 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

14 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

15 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

16 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 FAQUsing Java 5.0 BigDecimalDecimal Arithmetic FAQ from an assignment that didn’t use BigDecimal… Type Vegan Survey Last Date Restaurant Code Y/N Amount Surveyed Name, Location CF 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!

17 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

18 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);

19 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.

20 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

21 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();

22 Integer Class three static methods (class or utility methods)  static intInteger.parseInt ( String s ) e.g. String s = "123456789"; int i = Integer.parseInt ( s )  static StringInteger.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 ) orInteger intWrap = new Integer ( s )


Download ppt "BPJ444: Business Programming using Java Java basics Tim McKenna"

Similar presentations


Ads by Google