Math class services functions Primitive vs reference data

  • Slides: 7
Download presentation
Math class services (functions) Primitive vs reference data types Scanner class Suppose you want

Math class services (functions) Primitive vs reference data types Scanner class Suppose you want to find the absolute value of a number. You could square it and then take the square root. OR You can ask Math for some help. Math is a class of services automatically provided to help us with a range of math services. To request Math’s help in this problem, we essentially say, Math, calculate the value of this number. In code, you would see something like this: double original; double result; original = -17. 25; result = Math. abs(original);

In code, you would see something like this: double original; double result; Appendix G

In code, you would see something like this: double original; double result; Appendix G – Math functions Lab will include some play with math functions original = -17. 25; result = Math. abs(original); The argument to the method (value that the method will act upon) Name of class Name of the function providing service (static method) We say that this method, abs, returns a value. In this case that value is assigned to the variable result. Math class services (functions) Math. Play. java Primitive vs reference data types Scanner class

Primitives int byte long short float double char boolean int num; num = 5;

Primitives int byte long short float double char boolean int num; num = 5; num 5 Reference Types When a variable references an object, it contains the memory address of the object’s location. Then it is said that the variable references the object. String city. Name = "Charleston "; The object that contains the character string “Charleston” city. Name Math class services (functions) Address to the object Charleston Primitive vs reference data types Scanner class Reference types take up 2 chunks of memory, one that is the named variable that contains an address or referenceand the other that contains the data contents of the object.

Math class services (functions) Primitive vs reference data types Scanner class Decimal Formatter –

Math class services (functions) Primitive vs reference data types Scanner class Decimal Formatter – Another class in Java Creating an object Data type (class name) variables Decimal. Format two. Decimals; Decimal. Format three. Decimals; two. Decimals = new Decimal. Format(“##0. 00”); three. Decimals = new Decimal. Format(“##0. 000”); Formatting. java We say that we are instantiating a new Decimal. Format object with the new operation. Below, we use those formats. double number 1; number 1 = 345. 123456; System. out. println(two. Decimals. format(number 1); System. out. println(three. Decimals. format(number 1); Book Chap 3. 10

Math class services (functions) Primitive vs reference data types Scanner class keyboard System. in

Math class services (functions) Primitive vs reference data types Scanner class keyboard System. in Data type (class name) variable Standard input Scanner keyboard; keyboard = new Scanner (System. in); We say that we are instantiating a new Scanner object with the new operation. 23. 4n 567. 98n. This little piggyn

Math class services (functions) keyboard Primitive vs reference data types System. in Scanner class

Math class services (functions) keyboard Primitive vs reference data types System. in Scanner class If I want to read in data, I will use keyboard’s methods to do so, since I want to read from standard input and keyboard references the Scanner object which will read from standard input. Examples – assume all variables Scanner methods have been initialized to the int: next. Int() appropriate types double: next. Double() String: next() abc = keyboard. next. Int(); String: next. Line() text = keyboard. next. Line(); MPG. java Book chapter 2. 13

Scanner methods (except for next. Line()) treat “whitespace” as a delimiter. For each next

Scanner methods (except for next. Line()) treat “whitespace” as a delimiter. For each next method, it will pluck off the next token. If the input is not correct, such as using next. Int and the user has entered “abc”, you will get a runtime error. If you try to read in a String after you have read in a number, you must “consume” the new line character prior to reading the String. See page 88 (purple) or 86 (green). Math class services (functions) Input. Problem. java Primitive vs reference data types Scanner class