Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.4 Using Java Built-in Classes Produced by.

Similar presentations


Presentation on theme: "CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.4 Using Java Built-in Classes Produced by."— Presentation transcript:

1 CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.4 Using Java Built-in Classes Produced by Harvey Peters, 2008 Copyright SAIT

2 Please read the following documentation in the Java API Math String Calendar Georgian Calendar java.lang Wrapper classes (Byte, Short, Integer, Long, Float, Double, String, Character, Boolean) Data-type Conversion Explore the parse methods in the wrapper classes CPRG 215 Module 2.4- Using Java Built-in Classes Copyright SAIT

3 Math Some of the methods/properties –PI –abs() –ceil() –floor() –random() –round() –sqrt() All methods and properties are static – they can be used without creating an object first Contains common math operations and constant values CPRG 215 Module 2.4- Using Java Built-in Classes Copyright SAIT Topic 2.4.1

4 Math example –System.out.println(Math.PI); –int myRandom = Math.floor(Math.random() * 100); CPRG 215 Module 2.4- Using Java Built-in Classes Copyright SAIT Topic 2.4.1

5 String Concatenation with + + performs String concatenation produces a new String object String salutation = “Dr.”; String name = “Pete “ + “Seymour”; String title = salutation + name; If at least one piece is a String object all non-strings are converted to String objects automatically CPRG 215 Module 2.4- Using Java Built-in Classes Copyright SAIT Topic 2.4.2

6 String Concatenation with concat() method Once a String is created it can’t be altered – Java creates a new String when the + is used and if assigned to the current name, a new object results Using the concat() method does not alter existing string, but returns a new String object that contains the concatenated value CPRG 215 Module 2.4- Using Java Built-in Classes Copyright SAIT Topic 2.4.3

7 StringBuffer used when a String needs changes StringBuffer sb = new StringBuffer(“abc”); –methods: sb.append(“ghi”); sb.insert(3, “def”); sb.reverse(); CPRG 215 Module 2.4- Using Java Built-in Classes Copyright SAIT Topic 2.4.4

8 Formatting Numbers The NumberFormat class –java.text package NumberFormat nf = NumberFormat.getCurrencyInstance(); System.out.println(nf.format(Double.parseDouble(args[0]))); CPRG 215 Module 2.4- Using Java Built-in Classes Copyright SAIT Topic 2.4.5

9 Dates & Calendars Date is mostly deprecated –use Calendar instead Calendar.getInstance() returns a Calendar object –can specify Timezone and Locale set() and get() –Modify calendar values –need field number – see Fields documentation in API Docs CPRG 215 Module 2.4- Using Java Built-in Classes Copyright SAIT Topic 2.4.6

10 Wrapper Classes Found in java.lang Are used to look at primitive data items as objects CPRG 215 Module 2.4- Using Java Built-in Classes Copyright SAIT Topic 2.4.7 Primitive Data TypeWrapper Class booleanBoolean byteByte charCharacter shortShort intInteger longLong floatFloat doubleDouble

11 Wrapper Classes Data Type conversion –parse methods convert String to numeric –example String yearString = args[0]; int yearInt = Integer.parseInt(yearString); - if the String has any non-numeric characters this will cause an “Exception” and crash the program -Exception handling is coming up in a later topic CPRG 215 Module 2.4- Using Java Built-in Classes Copyright SAIT Topic 2.4.7


Download ppt "CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.4 Using Java Built-in Classes Produced by."

Similar presentations


Ads by Google