BPJ444: Business Programming using Java Java basics Tim McKenna

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Introduction to Computers and Programming Lecture 7:
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Miscellaneous OOP topics Java review continued. Simple data types & wrapper classes Simple data types are the built-in types provided as part of the Java.
Java Data Types  Everything is an Object  Except Primitive Data Types  For efficiency  Platform independent  Portable  “slow”  Objects are often.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
String Escape Sequences
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Types and Expressions. 3.2 Primitive and Reference Types Each data value has a type The type must be declared for all variables & constants The compiler.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
The need for BigDecimal Tim McKenna Why BigDecimal? BigDecimal provides exact decimal values doubles only approximate decimals use BigDecimal.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
1 CSE1020: Introduction to Computer Science I. 2 Today’s Lecture Course administrative details Material from Chapter 1 “Introduction to Programming” 19:00-22:00.
Java 2 More Java Then Starbucks ;-). Important Terms Primitive Data – Basic, built-in values (characters & numbers) Data Type – Used to talk about values.
CPS120: Introduction to Computer Science
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 Operations Making Things Happen (Chap. 3) Expressions.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Copyright Curt Hill Variables What are they? Why do we need them?
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables.
Primitive/Reference Types and Value Semantics
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Sachin Malhotra Saurabh Choudhary
Java Primer 1: Types, Classes and Operators
Object Oriented Programming
Multiple variables can be created in one declaration
Chapter 2.
The Building Blocks Classes: Java class library, over 1,800 classes:
Chapter 2 Variables.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Primitive Types and Expressions
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Variables.
Presentation transcript:

BPJ444: Business Programming using Java Java basics Tim McKenna

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)  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

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; intempStartYear; a method’s local variables and parameter names can be even shorter

Primitive Data Types summary (The Java Tutorial)  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;// 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 = L; // a long integer literal float r = 3.14f;// a float literal double s = 3.14;// default double literal

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)

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) types of operators: arithmetic, logical, relational, conditional, assignment precedence of operators all similar to C language

Control Flow Statements summary (The Java Tutorial) 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 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 Blueberry Hill, YLM Please enter survey amount (+ add, - subtract) > Unable to subtract this amount because there is only 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 intInteger.parseInt ( String s ) e.g. String s = " "; int i = Integer.parseInt ( s )  static StringInteger.toString ( int i) e.g. int i = ; String s = Integer.toString ( i )  static Integer Integer.valueOf ( String s ) e.g. String s = " "; Integer intWrap = Integer.valueOf ( s ) orInteger intWrap = new Integer ( s )