Download presentation
Presentation is loading. Please wait.
1
Chapter 3, cont Sept 20, 2004
2
Reminder – Hardcopy due today!
In class – Stapled. Anything turned in after lecture will be considered 10 points late. Late submissions are turned in timely if you get them in by the next class day after your successful run. Any late penalty points on the submission still apply. © 2004 Pearson Addison-Wesley. All rights reserved
3
Today Review exercise on terminology.
Work alone for 10 minutes. You may use your book. Work with a partner when announced. 10 minutes. Work with another group for the remaining 5 minutes. © 2004 Pearson Addison-Wesley. All rights reserved
4
The Random Class The Random class is part of the java.util package
It provides methods that generate pseudorandom numbers A Random object performs complicated calculations based on a seed value to produce a stream of seemingly random values See RandomNumbers.java (page 126) – book © 2004 Pearson Addison-Wesley. All rights reserved
5
The Math Class The Math class is part of the java.lang package
The Math class contains methods that perform various mathematical functions These include: absolute value square root exponentiation trigonometric functions © 2004 Pearson Addison-Wesley. All rights reserved
6
value = Math.cos(90) + Math.sqrt(delta);
The Math Class The methods of the Math class are static methods (also called class methods) Static methods can be invoked through the class name – no object of the Math class is needed value = Math.cos(90) + Math.sqrt(delta); This means that we are dealing with only one thing; we will not have different “Maths” in our program. Math class provides us with services that we can use with other programs and does not define a reference type. See Quadratic.java (page 129) - examples We discuss static methods further in Chapter 6 © 2004 Pearson Addison-Wesley. All rights reserved
7
Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean void Void © 2004 Pearson Addison-Wesley. All rights reserved
8
Integer age = new Integer(40);
Wrapper Classes The following declaration creates an Integer object which represents the integer 40 as an object Integer age = new Integer(40); An object of a wrapper class can be used in any situation where a primitive value will not suffice For example, some objects serve as containers of other objects Primitive values could not be stored in such containers, but wrapper objects could be © 2004 Pearson Addison-Wesley. All rights reserved
9
num = Integer.parseInt(str);
Wrapper Classes Wrapper classes also contain static methods that help manage the associated type For example, the Integer class contains a method to convert an integer stored in a String to an int value: num = Integer.parseInt(str); The wrapper classes often contain useful constants as well For example, the Integer class contains MIN_VALUE and MAX_VALUE which hold the smallest and largest int values © 2004 Pearson Addison-Wesley. All rights reserved
10
Some of you may have seen
Keyboard class If you input a bad value the system will give you a message about the Min_Value being used. That value is found in the corresponding wrapper class for the return type of the method. © 2004 Pearson Addison-Wesley. All rights reserved
11
In Chapter 3 We are not covering Enumerated Types or the Graphical chapters. © 2004 Pearson Addison-Wesley. All rights reserved
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.