Java Applet Programming Barry Sosinsky Valda Hilley

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to Programming G51PRG University of Nottingham Revision 1
INTRODUCTION Chapter 1 1. Java CPSC 1100 University of Tennessee at Chattanooga 2  Difference between Visual Logic & Java  Lots  Visual Logic Flowcharts.
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
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.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Unit 2: Java Introduction to Programming 2.1 Initial Example.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
 Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library.
Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 1 CSIS-120: Java Intro. What is Programming?  A: It is what makes computer so useful.  The flexibility of a computer is amazing  Write a term.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Chapter 6: User-Defined Functions
POS 406 Java Technology And Beginning Java Code
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Your name CSCI/CMPE 3326 Object-Oriented Programming in Java Dongchul Kim Department of Computer Science University of Texas – Pan American 1.Applet.
By Mr. Muhammad Pervez Akhtar
Creating a Java Application and Applet
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
OOP Basics Classes & Methods (c) IDMS/SQL News
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
Information and Computer Sciences University of Hawaii, Manoa
Array in C# Array in C# RIHS Arshad Khan
The eclipse IDE IDE = “Integrated Development Environment”
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Java Course Review.
Computer Programming BCT 1113
Java Primer 1: Types, Classes and Operators
Text by: Lambert and Osborne
Data types and variables
Numeric Arrays Numeric Arrays Chapter 4.
Getting Started with C.
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
CompSci 230 Software Construction
Programmazione I a.a. 2017/2018.
Arrays in C.
Introduction to Programming in Java
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Statements, Comments & Simple Arithmetic
Arrays, For loop While loop Do while loop
7 Arrays.
An Introduction to Java – Part I, language basics
Object Oriented Programming
Coding Concepts (Sub- Programs)
Introduction CSC 111.
Classes and Objects.
7 Arrays.
Introduction to Data Structure
Loops and Arrays in JavaScript
Object Oriented Programming in java
Java SE 7 One and Multi Dimensional Arrays Module 6
Lecture 13 Introduction to High-Level Programming (S&G, §§7.1–7.6)
Chap 1. Getting Started Objectives
Chap 4. Programming Fundamentals
Presentation transcript:

Java Applet Programming Barry Sosinsky Valda Hilley Chapter 11 Introduction to Java Applet Programming Barry Sosinsky Valda Hilley Programming the Web

Learning Objectives What is a Java applet Java applet structure Create a Java applet Embed an applet in an HTML document How to execute a Java applet

Getting to Know Java The Java programming language is a modern, evolutionary computing language that combines an elegant language design with powerful features that were previously available only in specialty languages. The Java programming language is a true object-oriented (OO) programming language. Object-oriented languages provide a framework for designing programs that represent real-world entities like cars, employees, or insurance policies. Java is not JavaScript. Java is not related to JavaScript in any way.

Java Applications There are 2 basic types of Java applications: Standalone: These run as a normal program on the computer. They may be a simple console application or a windowed application. These programs have the same capabilities of any program on the system. For example, they may read and write files. Applets: These run inside a web browser. They must be windowed and have limited power. They run in a restricted JVM (Java Virtual Machine) called the sandbox from which file I/O and printing are impossible.

Java Syntax Initial class statement // HelloWorld.java class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); } Main method When the main method is called it does only one thing: print "Hello World" to the standard output, generally a terminal monitor or console window

Compiling and Running Java Applications

Between the Blocks and Braces In Java a source code file is broken up into parts separated by opening and closing braces, i.e. the { and } characters. Everything between { and } is a block and exists independently of everything outside of the braces. The braces are used to group related statements together. Everything between matching braces executes as one statement (though depending on the code logic not everything may execute every time). Blocks can be hierarchical. One block can contain one or more subsidiary blocks.

Data and Variables The program now says hello back to Valda. This was done by creating a String variable called "name" and storing the name Valda as the value. // This is the Hello Reader program in Java class HelloReader { public static void main (String args[]) { // I'm going to replace "Reader" with my name String name = "Valda"; /* Now, say hello */ System.out.print("Hello "); System.out.println(name); }

Command Line Arguments You can also have the program automatically change or personalize the greeting at runtime rather than at compile time by using command-line arguments: // This is the Hello program in Java class Hello { public static void main (String args[]) { /* Now let's say hello */ System.out.print("Hello "); System.out.println(args[0]); }

If Statements All programming languages have some form of an if statement that allows you to test conditions. Here, we test the length of the args array: // This is the Hello program in Java class Hello { public static void main (String args[]) { /* Now say hello */ System.out.print("Hello "); if (args.length > 0) { System.out.println(args[0]); }

Else Statements What we need is an else statement that will catch any result other than the one we hope for, and luckily Java provides exactly that: // This is the Hello program in Java class Hello { public static void main (String args[]) { /* Now say hello */ System.out.print("Hello "); if (args.length > 0) { System.out.println(args[0]); } else { System.out.println("whomever you are");

Classes and Objects Everything in Java is either a class, a part of a class, or describes how a class behaves. Methods are defined inside the classes they belong to. class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); } class GoodbyeWorld { System.out.println("Goodbye World!"); This second class is a completely independent program

Methods As programs grow in size, it begins to make sense to break them into parts. Each part can perform a particular calculation and possibly return a value. This is especially useful when the calculation needs to be repeated at several different places in the program. It also helps to define a clearer picture of the flow of the program, much like an outline shows the flow of a book. Each calculation part of a program is called a method. You can write and call your own methods too.

Methods (2) Methods break up a program into logically separate algorithms and calculations. In still larger programs, it’s necessary to break up the data as well. The data can be separated into different classes and the methods attached to the classes they operate on. static long factorial (int n) { int i; long result=1; for (i=1; i <= n; i++) { result *= i; } return result;

Arrays An array is a group of variables that share the same name and are ordered sequentially from zero to one less than the number of variables in the array. The number of variables that can be stored in an array is called the array’s dimension. Each variable in the array is called an element of the array. There are three steps to creating an array: declaring it allocating it initializing it

Declaring Arrays Like all other variables in Java, an array must be declared. When you declare an array variable you suffix the type with [] to indicate that this variable is an array. Here are some examples: int[] k; float[] yt; String[] names; You declare an array like you’d declare any other variable, except you append brackets to the end of the variable type.

Allocating Arrays When we create an array we need to tell the compiler how many elements will be stored in it k = new int[3]; yt = new float[7]; names = new String[50]; The numbers in the brackets specify the dimension of the array; i.e. how many slots it has to hold values. This is commonly called allocating the array since this step actually sets aside the memory in RAM that the array requires.

Initializing Arrays Individual elements of the array are referenced by the array name and by an integer which represents their position in the array. The numbers we use to identify them are called subscripts or indexes into the array. k[0] = 2; k[1] = 5; k[2] = -2; Yt[6] = 7.5f; names[4] = "Fred"; This step is called initializing the array or, more precisely, initializing the elements of the array.

Shortcuts Java has shorthand for declaring, dimensioning and strong values in arrays. We can declare and allocate an array at the same time like this: int[] k = new int[3]; float[] yt = new float[7]; String[] names = new String[50]; We can even declare, allocate, and initialize an array at the same time providing a list of the initial values inside brackets like so: int[] k = {1, 2, 3}; float[] yt = {0.0f, 1.2f, 3.4f, -9.87f, 65.4f, 0.0f, 567.9f};

Hello World Applet HelloWorldApplet.class HelloWorldApplet.html import java.applet.Applet; import java.awt.Graphics; public class HelloWorldApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); } HelloWorldApplet.html <HTML> <HEAD> <TITLE> Hello World </TITLE> </HEAD> <BODY> This is the applet:<P> <APPLET codebase="classes" code="HelloWorldApplet.class" width=200 height=200 ></APPLET> </BODY> </HTML>

Passing Parameters to Applets DrawStringApplet.class import java.applet.Applet; import java.awt.Graphics; public class DrawStringApplet extends Applet { String input_from_page; public void init() { input_from_page = getParameter("String"); } public void paint(Graphics g) { g.drawString(input_from_page, 50, 25); DrawStringApplet.html <HTML> <HEAD> <TITLE> Draw String </TITLE> </HEAD> <BODY> This is the applet:<P> <APPLET codebase="classes" code="DrawStringApplet.class" width=200 height=200><PARAM name="String" value="Howdy, there!"></APPLET> </BODY> </HTML>

The End