1 / 24

JAVA Programming (Session 5)

Instructor : รัฐภูมิ เถื่อนถนอม Email: ratapoom@orjix.com. JAVA Programming (Session 5). “When you are willing to make sacrifices for a great cause, you will never be alone.”. The Numbers Classes.

Download Presentation

JAVA Programming (Session 5)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Instructor: รัฐภูมิ เถื่อนถนอม Email: ratapoom@orjix.com JAVA Programming (Session 5) “When you are willing to make sacrifices for a great cause, you will never be alone.”

  2. The Numbers Classes • When working with numbers, most of the time you use the primitive types in your code. There are, however, reasons to use objects in place of primitives, and the Java platform provides wrapper classes for each of the primitive data types. These classes "wrap" the primitive in an object. • Often, the wrapping is done by the compiler—if you use a primitive where an object is expected, the compiler boxes the primitive in its wrapper class for you. Similarly, if you use a number object when a primitive is expected, the compiler unboxes the object for you. All of the numeric wrapper classes are subclasses of the abstract class Number:

  3. The Numbers Classes • There are three reasons that you might use a Number object rather than a primitive: • As an argument of a method that expects an object (often used when manipulating collections of numbers). • To use constants defined by the class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type. • To use class methods for converting values to and from other primitive types, for converting to and from strings, and for converting between number systems (decimal, octal, hexadecimal, binary).

  4. Methods Implemented by all Subclasses of Number

  5. Conversion Methods, Integer Class

  6. DecimalFormat Class • You can use the java.text.DecimalFormat class to control the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator.

  7. Beyond Basic Arithmetic • The methods in the Math class are all static, so you call them directly from the class • The Math class includes two constants: • Math.E, which is the base of natural logarithms, and • Math.PI, which is the ratio of the circumference of a circle to its diameter.

  8. Basic Math Methods

  9. Exponential and Logarithmic Methods

  10. Trigonometric Methods

  11. Random Numbers • The random() method returns a pseudo-randomly selected number between 0.0 and 1.0. The range includes 0.0 but not 1.0. In other words: 0.0 <= Math.random() < 1.0. • To get a number in a different range, you can perform arithmetic on the value returned by the random method. • For example, to generate an integer between 0 and 9, you would write: • int number = (int)(Math.random() * 10); • By multiplying the value by 10, the range of possible values becomes 0.0 <= number < 10.0. • Using Math.random works well when you need to generate a single random number. If you need to generate a series of random numbers, you should create an instance of java.util.Random and invoke methods on that object to generate numbers.

  12. Characters • Most of the time, if you are using a single character value, you will use the primitive char type. There are times, however, when you need to use a char as an object—for example, as a method argument where an object is expected.

  13. Useful Methods in the Character Class

  14. Escape Sequences • A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler.

  15. Strings • Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. • "Hello world!" is a string literal—a series of characters in your code that is enclosed in double quotes. Whenever it encounters a string literal in your code, the compiler creates a String object with its value—in this case, Hello world!. • The Java programming language does not permit literal strings to span lines in source files, so you must use the + concatenation operator at the end of each line in a multi-line string. String Length Concatenating Strings

  16. Converting Between Numbers and Strings • The Number subclasses that wrap primitive numeric types ( Byte, Integer, Double, Float, Long, and Short) each provide a class method named valueOf that converts a string to an object of that type. • Each of the Number subclasses that wrap primitive numeric types also provides a parseXXXX() method (for example, parseFloat()) that can be used to convert strings to primitive numbers.

  17. Converting Numbers to Strings Each of the Number subclasses includes a class method, toString(), that will convert its primitive type to a string.

  18. Getting Characters and Substrings by Index • You can get the character at a particular index within a string by invoking the charAt() accessor method. The index of the first character is 0, while the index of the last character is length()-1. • If you want to get more than one consecutive character from a string, you can use the substring method.

  19. Other Methods for Manipulating Strings

  20. Replacing Characters and Substrings into a String

  21. An Example

  22. Comparing Strings and Portions of Strings

  23. Comparing Strings and Portions of Strings

  24. References • http://java.sun.com/docs/books/tutorial/essential/index.html • http://java.sun.com/ • http://www.eclipse.org/ • http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

More Related