260 likes | 285 Views
Big Java. Chapters 1-2. Java 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
E N D
Big Java Chapters 1-2
Java 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
Java 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
Simple 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
On to chapter 2…. Using Objects But first, let’s look at Eclipse
Variables Rules for identifiers: • Can include letters, digits, _, $, can’t 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!!
Assignment 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)
Some String Methods Strings are Immutable!
A 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
Objects 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: We’ll come back to this as an exercise…
Object comparison What will be displayed when this is executed?
Graphical 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 Doesn’t automatically close (app may have multiple windows) Don’t display til all components in place (common error – forget to display)
Drawing 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:
More Components and Color set color before draw
Applets • 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… )
RectangleViewer as html <html> <head> <title>Two rectangles</title> </head> <body> <p>Here is my <em>first applet</em>:</p> <applet code="RectangleApplet.class" width="300" height="300"> </applet> </body> </html>
RectangleApplet Like RectangleComponent except extends JApplet, paint not paintComponent, don’t need main
Lab 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 (we’ll discuss, nothing to submit): would it be a good idea to use your applet on your company’s website? • We’ll be showing some of these in class, be prepared.
What 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.