Java Programming Object Basics and Simple Data Objects

Java Programming: Object Basics and Simple Data Objects Vyacheslav Grebenyuk CTDE, AI dept. , Kh. NURE (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006


java. lang Number n Number. Format and Decimal. Format (java. text) n Print. Stream and Print. Writer (java. io) n Math n (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 3

The Numbers Classes int i = 500; n float gpa = 3. 65; n byte mask = 0 xff; n (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 4

Two reasons of using a Number class n When an object is required ¨ Array. List<E> ¨ Array. List<Integer> n MIN_VALUE and MAX_VALUE, or methods for converting values to other types, for converting to strings (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 5

Instance Methods Common to the Number Classes Method Description byte. Value() short. Value() int. Value() long. Value() float. Value() double. Value() Convert the value of this number object to the primitive data types of byte, short, int, long, float, and double. compare. To(…) Compare this number object to the argument. Note that the Number class does not implement the Comparable interface, but most of the subtypes implement Comparable. So the Integer class has a compare. To(Integer) method, the Big. Decimal class has a compare. To(Big. Decimal) method, and so on. boolean equals(Object) Determine whether this number object is equal to the argument. Note that the Number class does not override the equals method from Object. Most of the subtypes implement equals(Object), though they may not behave as you might expect. Consult the documentation for a particular Number subtype to see how the equals method behaves. (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 6
![Converting Strings to Numbers public class Value. Of. Demo { public static void main(String[] Converting Strings to Numbers public class Value. Of. Demo { public static void main(String[]](http://slidetodoc.com/presentation_image_h2/da8fe18b825d94dbe5ee2511061d9e2b/image-7.jpg)
Converting Strings to Numbers public class Value. Of. Demo { public static void main(String[] args) { //this program requires two arguments on the command line if (args. length == 2) { //convert strings to numbers float a = Float. value. Of(args[0]). float. Value(); float b = Float. value. Of(args[1]). float. Value(); a + b = 91. 7 a - b = -82. 7 //do some arithmetic System. out. println("a + b = " + (a + b) ); a * b = 392. 4 System. out. println("a - b = " + (a - b) ); a / b = 0. 0516055 System. out. println("a * b = " + (a * b) ); a % b = 4. 5 System. out. println("a / b = " + (a / b) ); System. out. println("a % b = " + (a % b) ); } else { System. out. println("This program requires two arguments. "); } } } (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 7

Converting Numbers to Strings int i; String s 1 = "" + i; //Concatenate "i" with an empty string; //conversion is handled for you. String s 2 = String. value. Of(i); //The value. Of class method. String s 3 = Integer. to. String(i); //The to. String class method. public class To. String. Demo { public static void main(String[] args) { double d = 858. 48; String s = Double. to. String(d); int dot = s. index. Of('. '); System. out. println(s. substring(0, dot). length() + " digits before dec. point. "); System. out. println(s. substring(dot+1). length() + " digits after decimal point. "); } } (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 8

Formatting Numbers n Quick and Dirty Formatting java. io. Print. Stream public Print. Stream format(String format, Object. . . args) public Print. Stream printf(String format, Object. . . args) int i = 461012; System. out. format("%d%n", i); (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 9

Examples of Formatter using long n = 461012; System. out. format("%d%n", n); System. out. format("%08 d%n", n); System. out. format("%+8 d%n", n); System. out. format("%, 8 d%n", n); System. out. format("%+, 8 d%n%n", n); double pi = Math. PI; System. out. format("%f%n", pi); System. out. format("%. 3 f%n", pi); System. out. format(Local. FRANCE, "%s, 10. 3 f%n%n", pi * 1000); Calendar c = Calendar. get. Instance(); System. out. format("%t. B %te, %t. Y%n", c, c, c); System. out. format("%tl: %t. M%tp%n", c, c, c); System. out. format("%d. D%n", c); (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 "461012“ "00461012“ "+461012“ "461, 012“ "+461, 012“ "3. 141593“ "3. 142“ "3, 141, 593“ "July 9, 1959“ "11: 52 pm“ "07/09/59" 10

Formatting with Number. Format Internationalization (i 18 n) - next lect n Example n double amount = 345987. 246; Number. Format number. Formatter; String amount. Out; number. Formatter = Number. Format. get. Number. Instance(); amount. Out = number. Formatter. format(amount); System. out. format("%s%n", amount. Out); (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 11

Beyond Basic Arithmetic n java. lang. Math import static java. lang. Math. *; . . . round(34. 87); instead Math. round(34. 87); (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 12

Math Class Basic Mathematical Functions: absolute value, round, etc. n Comparative Functions: min, max n Exponential Functions: exp, log, pow, sqrt n Trigonometric Functions: cos, sin, etc. n (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 13
- Slides: 13