Title: Big Java
1Big Java
2Java History
- 1991 use in consumer devices
- 1994 use in browsers
- programmers embraced because
- simpler than C
- rich library
- portable programs
- micro edition and enterprise edition provide
support for wide range of apps, from cell phones
to large Internet servers - safe (must trust applets when you download!)
- Java virtual machine (JVM) catches many mistakes,
makes it easier to use
3Java drawbacks
- Not designed for beginners must learn fair
amount of syntax (although somewhat less than
C) - Has been extended many times can be confusing
which version you are using - Rich libraries provide many capabilities, but too
much to learn all at once
4Java Versions
5Simple First Program
Unlike C, ALL code is encapsulated in a class
Like C, program starts at main
program can take array of Strings
end statement with
System.out is the console (monitor)
object println is a method that displays its
parameters, followed by a newline (can do print,
no newline)
Each method has a public/private access
declaration (vs C, with public and private
sections)
static indicates that main does not operate on an
object
6More output options
7- On to chapter 2.
- Using Objects
- But first, lets look at Eclipse
8Variables
- Rules for identifiers
- Can include letters, digits, _, , cant start
with digits - Spaces and other characters not permitted
- Cannot use reserved words (e.g., public)
- Case sensitive
- Conventions you should follow
- variable and method names should start with lower
case, may include uppercase within (camel case).
e.g., luckyNumber - Class names should begin with uppercase
- Variable and class names should be meaningful!!
9Assignment and Initialization
- As in C, variables have a type
- Unlike C, variables MUST be assigned a value
before being used - int luckyNumber
- System.out.println(luckyNumber) // ERROR
- if (tot 5) // ERROR (unlike C, because must
- be boolean)
10Some String Methods
Strings are Immutable!
11A few more String methods
- is string concatenation
- String message Hello name
- To extract numbers from strings, use parseInt or
parseDouble - int count Integer.parseInt(input)
- double price Double.parseDouble(input2)
- Use substring to extract parts of a string
- String sub greeting.substring(0, 5)
- String tail greeting.substring(7)
- // copies from 7 to end of string
12Objects
import rather than include for libraries (called
packages in java). Could use if multiple
classes.
Always use new for objects!! The variable box is
a reference.
Determining the expected result in advance is an
important part of testing. This class relies on
the user to compare the expected and achieved
result. Is that optimal?
NOTE Well come back to this as an exercise
13Object comparison
What will be displayed when this is executed?
14Graphical Applications
- JFrame toplevel container, has title bar with
minimize/maximize, content pane to hold
components
GUI components in javax (notice the x) Swing
package
can also use setTitle
Doesnt automatically close (app may
have multiple windows)
Dont display til all components in place (common
error forget to display)
15GUI continued
16Drawing Components
Inheritance
Graphics2D more object- oriented, extra
methods Graphics object has state color, font
etc.
draw takes various objects
create component and add to frame, paintComponent
is called automatically
Result
17More Components and Color
set color before draw
18Applets
- Applets run inside browsers
- Will extend Applet or JApplet
- Method paint is called automatically (no main, no
paintComponent) - Can view from browser or appletviewer (or
Eclipse) - Need Java 2-enabled browser (may need to update
IE with Java plugin, Netscape and others should
be OK )
19RectangleViewer as html
- lthtmlgt
- ltheadgt
- lttitlegtTwo rectangleslt/titlegt
- lt/headgt
- ltbodygt
- ltpgtHere is my ltemgtfirst appletlt/emgtlt/pgt
- ltapplet code"RectangleApplet.class"
width"300" height"300"gt - lt/appletgt
- lt/bodygt
- lt/htmlgt
20RectangleApplet
Like RectangleComponent except extends JApplet,
paint not paintComponent, dont need main
21Lab 1
- With a partner
- Create an applet with a drawing that you think
would be a good logo for a company (before you
start what is your company?) - Use a variety of colors, shapes etc.
- Thought question (well discuss, nothing to
submit) would it be a good idea to use your
applet on your companys website? - Well be showing some of these in class, be
prepared.
22What methods does an object have?
- Classes and methods listed in the API
documentation. - Exercise
- visit http//java.sun.com/javase/6/docs/api/index.
html - How is the site organized? What format is used
for each class? How do you know the instance
fields? The methods? - Take a look at BigDecimal or BigInteger class.
Write a small test program similar to MoveTester.