java.lang package
String and Math classesjava.lang and some of java.utilnew operator
Example:
Thing myThing = new Thing("left", 21);
null.) accesses methods of an object.Examples
myThing.setWeight(12);
String myString = new String("home sweet home");
int len = myString.length();
What happens if the variable is null?
See Figure 3.1. Examples—evaluate:
String s = "Mississippi";
s.charAt(0)
s.charAt(2)
s.compareTo("Alabama")
"cat".concat("dog")
"cat".equals("dog")
"cat".equalsIgnoreCase("Cat")
s.length()
s.replace('s', 't')
s.replace('s', 't').replace('p', 'q').replace('i', 'j').replace('M', 'N')
s // unchanged
s.substring(0, 4)
s.substring(4, 8)
s.toLowerCase()
s.toUpperCase()
Compare Java and Python string operations
phrase again, to show that it really is unchanged.When you're writing your program, and you don't make a package declaration, by default you're writing for the unnamed package.
Import a package to use its classes
import java.util.*;
Or import a single class:
import java.util.Scanner;
After import, the classes can be named without their package names
The unnamed package is not imported
java.awt and javax.swing.Random = java.util.RandomExamples:
import java.util.Random;
Random r = new Random();
r.nextFloat()
r.nextInt()
r.nextInt(100) // overloaded
Details: Fig. 3.4
Listing 3.2: RandomNumber
Math = java.lang.MathMath.sqrt(16.2)Examples:
Math.abs(-7)
Math.floor(7.9)
Math.floor(-7.9)
Math.ceil(7.1)
Math.ceil(-7.1)
Math.pow(2, 8)
Math.exp(8) // e to the 8th power
Math.sqrt(2.0)
Math.cos(0);
Details: Fig. 3.5
To solve quadratic equations
ax2 + bx + c = 0
Use the quadratic formula:
x = [−b ± sqrt(b2 − 4 ac)] / (2 a)
It has two solutions.
java.text.NumberFormat
java.text.DoubleFormatnew to create an instanceformat method with the instanceimport java.text.NumberFormat;
NumberFormat nf;
nf = NumberFormat.getCurrencyInstance();
nf.format(12345.6789)
nf = NumberFormat.getPercentInstance();
nf.format(12345.6789)
nf = NumberFormat.getNumberInstance();
// or getInstance()
nf.format(12345.6789)
new won't workTwo ways to run gedit in French:
$ env LANG=fr_FR gedit
$ export LANG=fr_FR
$ gedit
fr_CA, es_ES, en_UK, …locale -a gives a list of available codesNot all programs are ready for this
Listing 3.4 Purchase
double data as decimal numbersnewimport java.text.DecimalFormat;
DecimalFormat df = new DecimalFormat("0.###");
df.format(123.4)
df.format(123.456789)
Listing 3.5 CircleStats
Example:
enum Size {Small, Medium, Large};
Within another class:
enum TYPENAME {VALUE1, ..., VALUEN};
Standalone:
public enum TYPENAME {VALUE1, ..., VALUEN};
toString () → Stringname () (like toString)ordinal () → intListing 3.6 IceCream
enum declaration within IceCream class
Byte
Short
Integer // int
Long
Float
Double
Character // char
Boolean
Void // what's this?
Integer w = new Integer(67);
int i = w.intValue(); // 67
Conversion methods to and from String
Examples: Integer.parseInt, Integer.toString
Examples using base ≠ 10
Makes the compiler do the boring part:
Integer w = 67;
int i = w;
Examples
w.toString(); // "26"
int i = Integer.parseInt("26"); // 26
Integer.toString(37) // "37"
JFrame
JPanel.
JLabel represents a labelListing 3.7 Authority
import javax.swing.ImageIcon;
ImageIcon icon = new ImageIcon("IMAGEFILE");
// gif, jpeg, png
new JLabel(TEXT, ICON, POSITION)
where
String,ImageIconSwingConstants.LEFT|CENTER|BOTTOM.Setting position of text in label:
label.setHorizontalTextPosition(HPOSITION);
label.setVerticalTextPosition(VPOSITION);
where VPOSITION allows SwingConstants.TOP|CENTER|BOTTOM.
Listing 3.9 LabelDemo
Overview of Lab 3