1 / 24

Random number

Random number. Random numbers are often needed when writing software Flight simulator use random numbers to determine how often simulated flights has engine trouble are generated in JAVA through the Random class Part of the java.util package

boughton
Download Presentation

Random number

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. Random number • Random numbers • are often needed when writing software • Flight simulator use random numbers • to determine how often simulated flights has engine trouble • are generated in JAVA through the Randomclass • Part of the java.util package • Picks a number at random out of a range of values

  2. Random class • Methods of Random class • float nextFloat() • returns a random number • between 0.0 (inclusive) and 1.0 (exclusive) • int nextInt() • Returns a random number over all possible int value • int nextInt(int num) • Returns a random number in the range 0 to num-1 • Refer to RandomNumbers.java

  3. Math class • Mathclass • provides a large number of basic mathematical functions • Helpful in making calculations • is defined in java.langpackage • includes static methods • => methods can be invoked through the name of the class • They can be used without having to instantiate an object • Return values used in expressions as needed value = Math.cos(90) + Math.sqrt(delta);

  4. Using Math class • Sample program • Use Math class • to compute the roots of a quadratic equation • ax2 + bx +c • Algorithm • Read values (a, b, and c) • Evaluate the roots of the equation • Refer to Quadratic.java

  5. Some methods of the Math class • static int abs(int num) • The absolute value of num • static double cos(double angle) • Returns the angle (in radians) cosine • static double exp(double power) • Returns the value of e raised to the specified power

  6. Using Math class • Sample program • Use Math class • to compute the roots of a quadratic equation • ax2 + bx +c • Algorithm • Read values (a, b, and c) • Evaluate the roots of the equation • Refer to Quadratic.java

  7. Some methods of the Math class • static int abs(int num) • The absolute value of num • static double cos(double angle) • Returns the angle (in radians) cosine • static double exp(double power) • Returns the value of e raised to the specified power

  8. Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images

  9. Formatting output • It is often necessary to format values • output looks appropriate when printed or displayed • NumberFormat part of java.text package • Provides generic formatting capabilities • is not instantiated using the new operator • instead by requesting an object • From one of the static methods invoked thru the class name • NumberFormat fmt = NumberFormat.getCurrencyInstance();

  10. Creating NumberFormat instance • NumberFormat objects • are created using • getCurrencyInstance() invoked thru class name • returns a formatter for monetary values • getPercentInstance() invoked thru class name • returns an object that formats a percentage • are used to format numbers using method format() • Refer to Purchase.java NumberFormat fmt = NumberFormat.getCurrencyInstance() double subtotal=19.35; System.out.println(fmt.format(subtotal) ); Output: $19.35

  11. DecimalFormat class • DecimalFormat part of java.text package • allows to format values based on a pattern • To determine how many digits should be printed • To the right of the decimal point (for instance) • is instantiated in the traditional way • using the new operator • Its constructor DecimalFormat takes a String • That represents a pattern for the formatted number • Refer to CircleStats.java

  12. Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images

  13. Enumerated types • Java allows you to define an enumerated type • Which can then be used to declare variables • as the type of a variable • establishes all possible values for a variable of that type • By listing, or enumerating the values • Where the values are identifiers, and can be anything desired • enum Season {winter, spring, summer, fall} • There is no limit to the number of listed values • Any number of values can be listed

  14. Declaring and using an enumerated type • Once a type is defined • A variable of that type can be declared • enum Grade {A, B, C, D, F}; Grade score; • And it can be assigned a value • Thru the name of the type score = Grade.A; • Enumerated types are type-safe • You cannot assign any value other than those listed

  15. Ordinal values • Internally, each value of an enumerated type • is stored as an integer, called its ordinal value • The first value has an ordinal value of zero • The second one, and so on • You cannot assign a numeric value • to enumerated type, even if it corresponds to an ordinal value

  16. Enumerated types: methods • The declaration of an enumerated type • is a special type of class • And each variable of that type is an object • methods associated with enumerated objects • The ordinal() method returns the numeric value • Of an enumerated type • The name() returns the name of the value • Refer to IceCream.java

  17. Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images

  18. Wrapper classes • A wrapper class • is used to wrap a primitive value into an object • Ex: create an object that serves as a container to hold an int • represents a particular primitive type • Ex: Integer class represents a simple integer value • instantiated, stores a single primitive type value • Ex: Integerobject store a single int value • its constructor accept the primitive value to store • Ex: Integer ageObj = new Integer (40);

  19. Wrapper classes in the JAVA class library • For each primitive type in JAVA • There exists a corresponding wrapper class (java.lang)

  20. Wrapper classes methods • Wrapper classes methods • manages the associated primitive type • Ex: Integerprovides methods returning the int stored in it • Some methods of the Integer class • Integer (int value) • Constructor: creates an new Integer object storing value • float floatValue() • returns the value of this integer as the float type • static int parseInt (String str) • Returns the int corresponding to the value in str

  21. Autoboxing/Unboxing • Autoboxing is the automatic conversion between • Primitive value and corresponding wrapper object • Integer obj1; int num1 = 69; Obj1 = num1; // automatically creates an Integer object • Unboxing is the reverse condition • Integer obj2 = new Integer (69); int num2; num2 = Obj2; // automatically extracts the int value • Refer to wrapper_error.java

  22. Using Dialog Boxes for Input/Output • Another way to gather input is to use a GUI • such as JOptionPane offered by Java • Which allows a programmer to use GUI for I/O • Making I/O more efficient and the program more attractive • JOptionPane • Contained in the package javax.swing • Offers two methods: • ShowInputDialog: allows user to input a string from keyboard • ShowMessageDialog: allows user to display results

  23. Syntax for ShowInputDialog • To display a dialog box • Containing the string in the object stringExpression • Storing the input data in the String object str • The syntax to use the method is: • str = JOptionPane.showInputDialog(stringExpression); • A dialog appears on the screen • prompting user to enter data • Data is returned as a string and assigned to variable str • The user enters the data in the white area • Called text field

  24. Syntax for showMessageDialog • The syntax to use showMessageDialog is • JoptionPane.showMessageDialog(null, messageStringExpression, boxTitleString, messageType). • messageStringExpression:evaluated and value appears in box • boxTitleString: the title of the dialog box • messageType: type of icon appearing in the dialog box • JoptionPane.ERROR_MESSAGE • JoptionPane.INFORMATION_MESSAGE • JoptionPane.Question_MESSAGE • JoptionPane.PLAIN_MESSAGE • JoptionPane.WARNING_MESSAGE

More Related