Georgia Institute of Technology Workshop for Programming And Systems Management Teachers Chapter 9 Source File Anatomy.

Slides:



Advertisements
Similar presentations
The Web Warrior Guide to Web Design Technologies
Advertisements

Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Applets. The Applet Class public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when the Web page.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.
1 Recitation 11. Applet Applets. An applet is a Java program that is started by a browser (e.g. netscape or internet explorer) when an html file has a.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
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.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Developing User Interfaces (DUI) Chris North cs3724: HCI.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Java: Chapter 1 Computer Systems Computer Programming II.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
JAVA APPLETS By: Ms. Humaira Siddiqui. Java and the Internet Java is considered to be a language well suited to be used in the internet. In contrast with.
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
Applets Life Cycle Drawing and Event Handling Methods for UI Components Applet Capabilities Example.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
1 Introduction to Applets. 2 Applications Programs that execute using the java interpreter Executes in command windows.
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
POS 406 Java Technology And Beginning Java Code
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.
Introduction to Java Programming. Introduction Course Objectives Organization of the Book.
1 CSC Computer Education (P) Ltd. DESIGNED BY K PRAKASH,
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Pravin Yannawar, DOCS, NMU Jalgaon Basic Java : Applets 2 Objectives of This Session Identify the need for Applets Distinguish between Applets and Applications.
1 JAVA API & Packages Starring: Java Documentation Co-Starring: BlueJ IDE.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming1 Programming.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CreatingClasses-SlideShow-part41 Creating Classes part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
1 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Applets, Images, and Audio Chapter 14 CSCI CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start.
Creating a Java Application and Applet
1 Applets. 2 What is an applet? Applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages.
Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
1 Java Applet Basics Chapter Eight. 2 Applets vs. Applications l Applications: Stand alone Java programs run by interpreter l Applets WWW browser embedded.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
Introduction to Java part 2
UNIT-5.
Java applets 1/3/2019.
Workshop for Programming And Systems Management Teachers
Introduction to javadoc
Applet Fundamentals Applet are small applications that are accessed on an Internet server, transported over the Internet, automatically installed and run.
Chapter 6 Objects and Classes
Presentation transcript:

Georgia Institute of Technology Workshop for Programming And Systems Management Teachers Chapter 9 Source File Anatomy

Georgia Institute of Technology Learning Goals Understand at a conceptual and practical level the anatomy of a Java source file –Optional Comments –Optional Package Declaration –Import Statements as needed –Class Declarations Attributes Constructors Methods –Applet Anatomy

Georgia Institute of Technology Structure of a Java Source File Comments (like copyright) Package statement Import statements Class definition { Attributes Constructors Methods Main method (if application) }

Georgia Institute of Technology Comments There are three types of comments in Java –A multi-line comment /* here is a multi-line comment it ends here */ –A single line comment // comments out the rest of this line –A javadoc comment /** Method to return the name of the person name - adds an author text - adds a version name description - adds a parameter description - describes the return value of a method

Georgia Institute of Technology Comment Exercise Open SimplePicture.java and read some of the comments –Which are Javadoc comments? –Which are single line comments? –Which are multi-line comments? Click on the Javadoc button to generate documentation from the Javadoc comments –Compare the html documentation to the original Javadoc comments

Georgia Institute of Technology Packages Packages allow you to organize your classes into a hierarchy. The package hierarchy is also used for the directory structure. –edu.gatech.ice.Class should be in edu/gatech/ice/ Package names for packages intended for release outside the organization are like the reverse of the internet domain names. –com.sun.java.swing –edu.gatech Every class belongs to a package. If none is specified then the unnamed package is used. java.lang

Georgia Institute of Technology Common Packages java.lang - Basic classes, including strings and objects java.util - Utility classes including generic data structures java.awt - User interface and graphics javax.swing - Newer user interface classes java.applet - Support for applets java.net - Networking support (URLs, TCP, UDP) java.io - Input and output to files and streams

Georgia Institute of Technology Package Statement The package statement has to be the first line of code in a source file, if it is specified. It can follow a comment. –package packageName; for example /** Copyright notice */ package edu.gatech.ice;

Georgia Institute of Technology Import Statements If you wish to use classes from a different package than the declared package you can import the class using import java.awt.Color; // import just Color or import java.awt.*; // all classes in java.awt –doesn’t include the file like in C so there is no penalty to using the wildcard –doesn’t extend to children of that level. For example importing java.awt.* doesn’t import java.awt.event.*; Or you can use the full name –java.awt.Color

Georgia Institute of Technology How do you know what to import? If you use a class that is part of Java and you get a compiler error –Error: Undefined class Use the all classes list of the API to find the class and check the documentation for the package it is in

Georgia Institute of Technology Import Exercise Change SimplePicture.java to specify all the classes that are being imported –Rather than using import java.awt.*; Import java.io.*; Comment out the import statements with a wildcard ‘*” –select lines-> Edit-> Comment Line(s) Compile and see what classes it says are undefined Find the classes in the API and add a fully qualified import statement for each –import java.awt.Color;

Georgia Institute of Technology Class Definition [Class modifiers] class ClassName [extends ClassName] [implements Interface1,…] { attributes, constructors, methods } Visibility Class Modifiers –no modifier means the class is visible to all classes in the same package –public - visible to any class. A file can have only one public class. –private - visible only to classes in the same file

Georgia Institute of Technology Class Definition Examples public class Person implements Comparable { attributes, methods, and inner classes } private class Helper extends Object { attributes, methods, and inner classes }

Georgia Institute of Technology Optional Extends Clause The “extends Class” clause tells you which class is the parent class –The one the current class inherits from –The parent, super, or base class If there is no “extends Class” clause –The class inherits from Object In the package java.lang

Georgia Institute of Technology Inheritance Exercise What is the parent class of each of these classes? –Picture.java –SimplePicture.java –Sound.java –Pixel.java Where is the show method defined that we have been using to show Picture objects? Where is the play method defined that we used to play Sound objects?

Georgia Institute of Technology Optional Implements Clause The “implements interface1, interface2” lets the class specify the interfaces that the class promises to implement –Any methods defined in the interfaces must be defined in the class Or the code will not compile An interface is like a communications contract –How two classes will communicate –Without worrying what about what classes they are An interface name can be used as a type –For variable declarations

Georgia Institute of Technology Comparable Interface What if you want to compare two objects? –To find out if one is less than another (< 0) –Or equal to the other (= 0) –Or greater than the other (> 0) How would you compare people? –playing cards? –shoppers? Each class needs to decide what this means –But we need to know what method to call int compareTo(Object o)

Georgia Institute of Technology Interface Exercise What interface does SimplePicture.java implement? Find that interface –Defined in a file with the interface Name.java What methods are declared in the interface? Find the same methods in SimplePicture.java

Georgia Institute of Technology Attributes Attributes are the data that each object knows about itself. –All object methods in the class have access to the object attributes Can use just the attribute name Can also use this.attribute name Can also use getAttribute() if you follow Java naming conventions –Class and object methods have access to the class attributes Declared with “static” keyword

Georgia Institute of Technology Attribute Exercise Open SimplePicture.java What are the attributes (fields)? –Which are object fields? –Which are class fields? Change the load method to use –setFileName(fileName) instead of this.fileName = fileName; –Compile –Test by creating a picture and using System.out.println(picture);

Georgia Institute of Technology Declaring Attributes Attributes are declared as [visibility] type name [= value]; Visibility none specified - classes in the same package public - all classes protected - class, subclasses, and other classes in the same package private - only the class itself Type - the type can be one of the following primitive type object type: class, interface, or array

Georgia Institute of Technology Visibility Exercise Open SimplePicture.java –Which fields have public visibility? Why are these public? –Which fields have private visibility? Why are these private?

Georgia Institute of Technology Constructors Constructors are called when the new object is created. They initialize the new object. Person currPerson = new Person(“Fred Flintstone”); Constructors don’t have a return type and have the same name as the class. public Person ()// no argument constructor There can be many constructors but their parameter signatures must be different. public Person() {} public Person(String theName) { name = theName; } }

Georgia Institute of Technology Advanced Constructor Information The compiler will add a no-argument (no parameters) constructor –All fields will have their default values Objects default to null Numbers default to zero Booleans default to false If you add any constructors –The compiler will no longer add any for you –You will need to write the no-argument constructor One constructor can invoke another –Use this(parameter List) as first line

Georgia Institute of Technology Inheritance and Constructors If classes have private attributes –Meaning they can’t be directly accessed outside the class –How can you initialize inherited attributes? Use super(parameters) to call the parent constructor Must be the first line in a constructor If not present the compiler will add a super() as the first line in a constructor Means the parent class must have a no-argument constructor if you aren’t calling super(parameters) in your constructors

Georgia Institute of Technology Constructor Exercise Open SimplePicture.java –How many constructors does it have? –Why do the constructors call super()? Open Picture.java –How many constructors does it have? What is different about the constructors? –How would the complier decide which one to use?

Georgia Institute of Technology Methods Methods are the things that object of the class know how to do. Methods are declared as [visibility] returnType methodName (parameter list) { … [return x;] // if returnType is not void } Example public String getName() { return name;}

Georgia Institute of Technology Method Visibility and Return Type Visibility –none specified - classes in the same package –public - all classes –protected - class, subclasses, and other classes in the same package –private - only the class itself Return Type - the return type can be one of the following –primitive type: int, float, double... –object type: class, interface, or array –void if no value is returned

Georgia Institute of Technology Method Parameter List Can have no parameters –public String getName() Parameters are listed as type name –public void setName(String name) Separate parameters with commas –public void multiplyTwo(int x, int y) You can have several methods with the same name but different parameter signatures. –public void multiplyTwo(float x, float y) –public void multiplyTwo(int x, int y)

Georgia Institute of Technology Main Method Each class can have a main method public static void main(String[] argv) {} You tell Java which class to start with java ClassName Execution starts with the main method –There are no objects when you begin so the main method is static (a class method) In the main method you create object(s) of the class –Then start the simulation

Georgia Institute of Technology Add Main Method Exercise Add a main method to Picture.java to –Create a picture from a specified file –Negate the picture –Show the picture Execute by Tools->Run Document’s Main Method –Notice that the interactions pane shows java Picture

Georgia Institute of Technology Applications Versus Applets A Java application will have at least one main method –Execution begins at this main –The Java classes are stored on the machine they run on An applet is a special Java program that runs in a browser –The applet tag is used in an html page –The Java classes are downloaded from a server –The applet runs in the browser window

Georgia Institute of Technology Applet Basics All applets must inherit from java.awt.Applet or javax.swing.JApplet They don’t have constructors –Use public void init() instead to set-up the applet before it is displayed Add applet tags to an html document to display the applet in the html page

Georgia Institute of Technology How to Embed Applets Message Applet

Georgia Institute of Technology Applet Tag Attributes of the applet tag –code - the name of the.class to start the applet –width - the horizontal size in pixels –height - the vertical size in pixels –codebase - where to find.class files –align - how to align the applet in the page –name - used for applet to applet communication –archive - gives the name of the jar file(s) (1.1+) –hspace - horizontal space around the applet –vspace - vertical space around the applet

Georgia Institute of Technology Using Parameters Use the param tag to specify the parameters in the html file. – Use getParameter in the applet to read a parameter as a string. –public String getParameter(String paramName) String name = getParameter(“name”); String numString = getParameter(“numItems”); if (numString != null) int numItems = Integer.parseInt(numString); else int numItems = 0;

Georgia Institute of Technology Message Applet Exercise Modify MessageApplet.java to get the string to display as a parameter. Modify MessageApplet.html to pass in the message as a parameter.

Georgia Institute of Technology Steps in Exercise Edit MessageApplet.java –Add the following line in the init method to get the passed parameter String message = getParameter(“message”); –Compile MessageApplet.java javac MessageApplet.java Edit MessageApplet.html –Add the parameter tag between the and tags Test the applet –appletviewer Name.html

Georgia Institute of Technology Applet Methods Applets inherit methods from the Applet class –init() - initialize and set-up the applet –start() - start execution of applet or restart –stop() - stop execution of applet –destroy() - clean up before termination init()start()stop() destroy() Browser calls init to setup applet or on reload Start is called to start execution (applet visible) Stop is called when leaving the page or quitting. Final cleanup

Georgia Institute of Technology Run Applet Exercise Use appletviewer to test the applet –Open a command prompt window –Start->Programs- >Accessories->Command Prompt –Change to the correct directory –appletviewer RunApplet.html Stop the applet Start the applet Restart the applet

Georgia Institute of Technology Disadvantages to Applets You must download the code from the server –user may have to wait Using html, animated gifs, dhtml, and scripting may be easier for some tasks Version problems with browsers –3.0 browsers support Java –4.0 browsers support Java 1.1 –Need plug-in for 1.2 (only Sun and windows) Security restrictions

Georgia Institute of Technology Advantages to Applets Code is downloaded from server –no distribution –no installation –users all on same version of code Cross platform GUI environment Multimedia support The power of an object-oriented programming language Create interactive and dynamic web pages

Georgia Institute of Technology Applet Resources Sun Tutorial for AWT style applets Sun tutorial for Swing style applets nts/applet.html nts/applet.html Applet Home Page Example Applet –Visual Human

Georgia Institute of Technology Summary There are three kinds of comments in Java –// comment or /* comment */ or /** comment */ The package statement is the first code in a class file. –package edu.gatech.ice; Import statements follow the package statement. –import edu.gatech.edu.*; The class definition follows the import statement. Inside a class definition you declare attributes, constructors, and methods.