9/13: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.

Slides:



Advertisements
Similar presentations
Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth.
Advertisements

Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.
1 Outline Chapter 4 Introduction Control Structures if Single-Selection Statement if else Selection Statement while Repetition Statement Assignment Operators.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
1 L44 Introduction to Java Applets. 2 OBJECTIVES  To differentiate between applets and applications.  To observe some of Java's exciting capabilities.
 2002 Prentice Hall. All rights reserved. 1 Week 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 24 - Introduction to Java Applications and Applets Outline 24.1Introduction 24.2Basics of a Typical.
Java Applets. Applets The term Applet refers to a little application. In JAVA the applet is a java program that is embedded within a HTML document and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software Development Kit The TicTacToe Applet.
Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software.
JAPPLET.
11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
1 Cash = Double.parseDouble( JOptionPane.showInputDialog("Cash: ") ); Package – Class (application programming interface)
10/5: Primitives, the for loop Primitive data types –why we mention them Return to counter-controlled repetition.
9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.
Chapter 3 Introduction to Java Applets 1 3 There is no main method in a Java Applet. A Java Applet can only run in a browser. An Applet is run.
Java I--Copyright © Tom Hunter. Chapter 3 Introduction to Java Applets.
10/11: do/while, Logical Operators the revised SwitchTest.java notes on JApplets the do / while repetition structure break continue Logical operators.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。
Review of CIS 120 Concepts: What you said you want….
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
2/4: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance Java Applets –what is an applet vs. an application? –where do applets.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.3Sample.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators.
10/4: the for loop & the switch structure Primitive data types –why we mention them Return to counter-controlled repetition.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
7/19: Primitives, the for loop Primitive data types –why we mention them Return to counter-controlled repetition.
2/4: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
CS202 Java Object Oriented Programming GUI Programming – Applets Chengyu Sun California State University, Los Angeles.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
Topic: Applets Course : JAVA PROGRAMMING Paper Code: ETCS-307
Lecture 09 Applets.
“Form Ever Follows Function” Louis Henri Sullivan
2.5 Another Java Application: Adding Integers
Introduction to Java Applets
Chapter 3 - Introduction to Java Applets
Java I.
Chapter 2 - Introduction to Java Applications
UNIT-5.
Chapter 24 - Introduction to Java Applications and Applets
Constructor Laboratory /12/4.
Java Applets.
Chapter 3 - Introduction to Java Applets
Applet in Java.
Applet 2019/4/23.
Outline Character Strings Variables and Assignment
Chapter 5 – Control Structures: Part 2
Chapter 5 – Control Structures: Part 2
Presentation transcript:

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