250 likes | 344 Views
Review of CIS 120 Concepts: What you said you want…. Applets: Chapter 3, sections: 3.1-3.4. HTLM file is executed Web browser or appletviewer needed JApplet is the superclass (“extends”) A “template” or “blueprint” for all applets Provides behaviors (methods) init start paint
E N D
Applets: Chapter 3, sections: 3.1-3.4 • HTLM file is executed • Web browser or appletviewer needed • JApplet is the superclass (“extends”) • A “template” or “blueprint” for all applets • Provides behaviors (methods) • init • start • paint • Provides attributes (data)
Applet Methods Ch 3, sect: 3.1-3.4; Ch 6 sec. 6.10 • public void init( ) • Called only once • Intializes “fields” or instance variables • Creates the GUI if exists • public void start( ) • For animations • Returns from web links • public void paint(Graphics g) • Draws graphics on the applet • Called by the applet container • First line should be: “super.paint(g);”
Applet example import java.awt.Graphics; // import class Graphics import javax.swing.*; // import package javax.swing public class Draw3 extends JApplet { // draw shapes on applet's background public void paint( Graphics g ) { // draw rectangle starting at (10, 10) that is 50 // pixels wide and 50 pixels tall g.drawRect( 10, 10, 50, 50 ); // draw oval starting at (10, 10) that is 50 pixels // wide and 50 pixels tall g.drawOval( 10, 10, 50, 50 ); } } // end class Draw3
HTML file for Draw3 applet <html> <applet code = “Draw3.class” width = “300” height = “300”> </applet> </html>
To run an applet • appletviewer Draw3.hmtl • Or open the html file from within a web browser
Control Structures • Order of statements specified by algorithm • This order is called program control • Program control is handled by control structures: • Sequence • Selection • Repetition
Sequence • Step-by-step • In order Example: number1 = Integer.parseInt(firstNumber); number2 = Integer.parseInt(secondNumber); sum = number1 + number2; JOptionPane.showMessageDialog( null, “The sum is “ + sum, “Results”, JOptionPane.PLAIN_MESSAGE);
Selection • If • If Else • Nested If • Switch
if example if (studentGrade >= 60) System.out.println (“Passed” );
If-else example If (grade >= 60) System.out.println (“Passed” ); Else System.out.prinlty (“Failed” );
Nested if example if (studentGrade >= 90) System.out.println (“A”); else if (studentGrade >= 80) System.out.println (“B”); else if (studentGrade >= 72) System.out.println (“C”); else System.out.println (“You must retake course”);
Switch example (p. 183) public void paint (Graphics g) { super.paint (g); for ( int i = 0; i < 10; i++) { switch (choice) { case 1: g.drawLine (10, 10, 250, 10 + i * 10); break; case 2: g.drawRect (10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10); break; case 3: g.drawOval (10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10); break; default: g.drawString (“Invalid value entered”, 10, 20 + i * 15); }//end switch }//end for }//end paint
Repetition • When to test: • Prettest (test before loop) • while • for • Posttest (test after loop) • do-while • Types of control • Counter-controlled • Sentinel-controlled
Example of while (also counter-controlled) while (counter <= 10) { g.drawLine (10, 10, 250, counter * 10); counter++; }//end while
Example of for(counter controlled) For (int count = 1; count <= 10; count++) g.drawLine (10, 10, 250, count * 10);
Example of do-while(also counter-controlled) do { g.drawLine (110 – count * 10, 110 – count * 10, count * 20, count * 20); ++count; } while (count <= 10);
Sentinel-controlled repetition while (grade != -1) { total = total + grade; gradeCounter = gradeCounter + 1; gradeString = JOptionPane.showInputDialog (“Enter Integer Grade or -1 to Quit:” ); grade = Integer.parseInt (gradeString); }//end while
Summary • 3.3 is a greater review of applets AND intro to objects! • Read over the chapters and these slides • Ask questions! • Practice! • Practice! • Practice!