3 - MCCC Faculty Page

Download Report

Transcript 3 - MCCC Faculty Page

Java 3
More Java Then Java
Libraries & Packages
• Java standard class library is composed of
useful classes that help us to achieve our
programming goals but are not a part of the
Java language
• Java packages are groups of classes that
we can call by a single name…the String
class is part of the java.lang package.
• Some packages are listed on p. 94
Class Libraries
• “Set of classes that supports the
development of programs.”
• Included with compiler or available
from 3rd party vendors.
• Provide common methods
programmers come to depend on.
Packages
• Groups of related packages.
• String class is part of the java.lang
package.
• Some packages contain many classes
so be careful when using “*” imports.
Importing Classes
• Java.lang packages are automatically
available to every program we write.
• To access other packages we must use a
fully qualified reference to the package or
import it.
import java.util.*;
import cs1.Keyboard;
Class Methods
• Also called static methods.
• Invoked through the class name.
• Examples:
Math.pow(2,8);
Keyboard.readInt();
Applets
• Java programs intended to be run using a
Web browser.
• Can also be viewed using Sun’s
appletviewer.
• Applets don’t have a “main” method.
• Graphical applets need to import…
java.applet.Applet
java.awt.*
Applets Cont’d.
• Classes that define applets inherit from the
master Applet class.
• In Java terms, they EXTEND the Applet
class.
• Applet classes must be public (more on this
later)
• Must be bytecode before being downloaded
by the browser ( a .class file)
Applets Cont’d.
• Applets are embedded in HTML pages
using the <applet> tag.
<applet code=“my.class”
width=“100” height=“100”>
</applet>
Basic Conditional
Statements
if (this > that) {
this++;
}
else {
that++;
}
Basic Conditional
Statements
// single line shortcut
if (this > that)
this++;
else
that++;
Basic Conditional
Statements
• Equality Operators…
==
!=
<
<=
>
>=
equal to
not equal to
less than
less than or equal to
greater than
greater than or equal to
More Conditionals
if (this > that)
this++;
else if
that++;
else if
theotherthing++;
else
nothing++;
Logical Operators
• Logical Operators…
!
&&
||
not
and
or
More Operators
• Increment and Decrement
++
Increment
I = I + 1
--
Decrement
I = I - 1
Switch Statements
switch(something)
case ‘y’:
this++;
break;
case ‘n’:
that++;
break;
Comparing Strings
• Don’t use ==, we’ll discuss that later.
• Do use
objname.equals(otherobj);
objname.compareTo(otherobj);
Comparing Floats
• Watch your precision. “==“ compares ONLY the
underlying binary representations and it is unlikely
they will match.
• Do use the absolute value of the difference of 2
numbers and compare to a pre-set value.
final float TOLERANCE = .01;
if(Math.abs(fl1 – fl2) < TOLERANCE)
System.out.println(“Equal enough”);