240 likes | 251 Views
Learn about the String class, packages, formatting, and output in Java software solutions. Explore methods, random numbers, math functions, and enumerated types.
E N D
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images
The String class • Because strings are so common • We don’t have to use the new operator • To create a String object title = "Java Software Solutions"; • This is a special syntax that works only for strings • Each string literal (enclosed in double quotes) • Represents a String object
String class • Once a String object is created • Its value can not be lengthened or shortened • Nor can any of its characters change • => String objects are immutable • However, several methods in String • Allow to create new String objects • As a result of the modification of the original String
The use of String class and its methods • Refer to StringMutation.java • Some methods exercised in the program • Refer to the index of a particular character • The index of the first character in a String is zero, • The index of the next one is 1, and so on • Example: “Hello” is a String object where the index of ‘H’ is zero and the character at index four is ‘o’
Some methods of the String class • char charAt (int index) • Char c; String S = “hi there”; c = S.charAt(0); // c = h • boolean equals (String str) • boolean equalsIgnoreCase(String str) • Refer to StringMutations.java
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
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
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);
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
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
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images
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();
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
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
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images
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
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
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
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
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images
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);
Wrapper classes in the JAVA class library • For each primitive type in JAVA • There exists a corresponding wrapper class (java.lang)
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
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