1 / 25

Review of CIS 120 Concepts: What you said you want….

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

ghalib
Download Presentation

Review of CIS 120 Concepts: What you said you want….

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. Review of CIS 120 Concepts:What you said you want….

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

  3. 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);”

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

  5. HTML file for Draw3 applet <html> <applet code = “Draw3.class” width = “300” height = “300”> </applet> </html>

  6. To run an applet • appletviewer Draw3.hmtl • Or open the html file from within a web browser

  7. Control Structures • Order of statements specified by algorithm • This order is called program control • Program control is handled by control structures: • Sequence • Selection • Repetition

  8. 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);

  9. Selection • If • If Else • Nested If • Switch

  10. if example if (studentGrade >= 60) System.out.println (“Passed” );

  11. If-else example If (grade >= 60) System.out.println (“Passed” ); Else System.out.prinlty (“Failed” );

  12. 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”);

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

  14. Repetition • When to test: • Prettest (test before loop) • while • for • Posttest (test after loop) • do-while • Types of control • Counter-controlled • Sentinel-controlled

  15. Example of while (also counter-controlled) while (counter <= 10) { g.drawLine (10, 10, 250, counter * 10); counter++; }//end while

  16. Example of for(counter controlled) For (int count = 1; count <= 10; count++) g.drawLine (10, 10, 250, count * 10);

  17. Example of do-while(also counter-controlled) do { g.drawLine (110 – count * 10, 110 – count * 10, count * 20, count * 20); ++count; } while (count <= 10);

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

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

More Related