1 / 22

Big Java

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

nlucio
Download Presentation

Big Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Big Java Chapters 1-2

  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

  3. 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

  4. Java Versions

  5. 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

  6. More output options

  7. On to chapter 2…. Using Objects But first, let’s look at Eclipse

  8. 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!!

  9. 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)

  10. Some String Methods Strings are Immutable!

  11. 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

  12. 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…

  13. Object comparison What will be displayed when this is executed?

  14. 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)

  15. GUI continued

  16. 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:

  17. More Components and Color set color before draw

  18. 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… )

  19. 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>

  20. RectangleApplet Like RectangleComponent except extends JApplet, paint not paintComponent, don’t need main

  21. 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.

  22. 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.

More Related