9/13: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets –what is an applet vs. an application? –where do applets work? –examples
Objects Objects have methods (behaviors or actions) and data (attributes) associated with them. Objects encapsulate these methods and data in OOP. Inheritance: You can create a new class that takes characteristics from an existing class, then define new characteristics in addition.
Java Applets An applet requires a browser to run. –An applet requires an HTML page to be displayed. –Applet tags are nested inside the HTML to load the class file. –Appletviewer: a “mini-browser” for viewing applets. Applets use different imported classes than applications. –EX: import javax.swing.JApplet; import java.awt.Graphics;
Sample Java Applet //java applet for 9/14 demonstration import javax.swing.JApplet; import java.awt.Graphics; public class Rectangles extends JApplet { public void paint ( Graphics g ) { g.drawRect ( 20, 20, 120, 50 ); g.drawString ( "This is an Applet", 30, 40 ); g.fillArc ( 60, 60, 80, 220, 45, 125 ); } import statements class header method header statements: g.drawRect g.drawString g.fillArc
Java Applets JApplet is referred to as the superclass of Rectangles. –It is the class that Rectangles inherits most of its methods and objects from. “paint” is a method that comes from JApplet. –allows you to draw things on the applet window. “init” is a method that creates instances of classes.
1 st Program of the Day: pg AdditionApplet.java –watch how an application can be ‘converted’ to work inside an applet Second half of class: –Algorithms –if & if/else control structures
Algorithms, if & if/else structures Looking through AdditionApplet.java Algorithms if selection structure if/else selection structure program of the day
AdditionApplet.java import java.awt.Graphics; import javax.swing.*; public class AdditionApplet extends JApplet { double sum; public void init() { String firstNumber, secondNumber; double number1, number2; firstNumber = JOptionPane.showInputDialog ( “Enter 1 st value”); secondNumber = JOptionPane.showInputDialog ( “Enter next value”); import statements class header method header declaring String variables declaring double variables declaring a double variable This is an instance variable. It can be used by all the methods associated with this class. statements: displaying input dialog boxes
AdditionApplet.java, pt.2 number1 = Double.parseDouble ( firstNumber ); number2 = Double.parseDouble ( secondNumber ); sum = number1 + number2; } public void paint ( Graphics g ) { g.drawRect ( 15, 10, 270, 20 ); g.drawString ( “The sum is “ + sum, 25, 25 ); } statements: initializing number1 & number2 statement: initializing sum statements: drawing on the Applet window
Algorithms actions to be executed in a particular order. control structures: three kinds in Java –sequential: “do this, then do that” –selection: “if this, then do that” –repetition: “do this while that”
Selection Structures if if ( condition ) statement ; if / else if ( condition ) statement ; else other statement ; switch discussed later…
if : Multiple Resulting Actions if ( condition ) { statement ; another statement ; yet another statement ; } brackets surround the statements to be done if the condition is true.
if / else if ( condition ) statement ; else other statement ; Notice that the else statement is associated with the immediately preceding if rather than some other one unless told otherwise.
if / else Nesting if/else selection structures allows us to choose from multiple possibilities: if ( age < 18 ) JOptionPane.showMessageDialog ( null, “no vices”); else if ( age < 21 ) JOptionPane.showMessageDialog (null, “cigs only” ); else JOptionPane.showMessageDialog ( null, “choose your vice” );
if / else : writing it differently if ( age < 18 ) JOptionPane.showMessageDialog ( null, “no vices”); else if ( age < 21 ) JOptionPane.showMessageDialog (null, “cigs only” ); else JOptionPane.showMessageDialog (null, “choose your vice” ); You can bump the nested if up to the else line to keep the indentations to a reasonable level and to increase readability.
Program of the Day: Work in Teams Create a program that will ask the user for a number between 0 and 100 ( decimal-type numbers should be acceptable ), and display the corresponding letter grade in a message dialog box: 89.5 – 100 – A 79.5 – 89.4 – B 69.5 – 79.4 – C 59.5 – 69.4 – D less than 59.5 – F