7 Class documentation and libraries

Slides:



Advertisements
Similar presentations
Using Java without BlueJ BlueJ projects A BlueJ project is stored in a directory on disk. A BlueJ package is stored in several different files.
Advertisements

More Sophisticated Behaviour 1 Using library classes to implement more advanced functionality.
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
 2005 Pearson Education, Inc. All rights reserved Introduction.
More sophisticated behaviour Using library classes to implement some more advanced functionality.
How to Create a Java program CS115 Fall George Koutsogiannakis.
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0.
Using Java without BlueJ Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling BlueJ projects A BlueJ.
Using Java without BlueJ Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling BlueJ projects A BlueJ.
Unit2: Object-oriented programming Getting started with Java Jin Sa.
Using Java without BlueJ
More sophisticated behavior Using library classes to implement some more advanced functionality 3.0.
Introduction to Java.
public static void main (String[] args)
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
M1G Introduction to Programming 2 4. Enhancing a class:Room.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
(C) 2010 Pearson Education, Inc. All rights reserved.  Java programs normally go through five phases  edit  compile  load  verify  execute.
Java Packages and Libraries M Taimoor Khan
UCSC All rights reserved. No part of this material may be reproduced and sold. 1 IT1202-Fundamentals Of Programming (Using JAVA) Interacting with.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Java Spring PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
POS 406 Java Technology And Beginning Java Code
Board Activity Find your seat on the seating chart Login – Remember your login is your first initial your last name and the last three numbers of your.
The Joy of Programming (also known as) Introduction to Object-Oriented Programming.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
More sophisticated behavior Using library classes to implement some more advanced functionality 5.0.
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.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
SOFTWARE AND PROGRAMMING 1 Lecture: MB33 7:30-9:00 (except 11& ) Lab: B43, MB321, MB536 6:00-7:30 (from ) [each student must have obtained.
SOFTWARE AND PROGRAMMING 1 Lecture: UCL BC57:30-9:00 (9& – from 6:00) Lab: SH 131, SH B12 6:00-7:30 (from ) (students will be assigned.
Computer Science 101 Spring 2000 Section E8TBA Registration Code 1693 Dr. Christopher Vickery.
23-October-2002cse JavaIntro © 2002 University of Washington1 Intro to Java CSE 413, Autumn 2002 Programming Languages
SOFTWARE AND PROGRAMMING 1 Lecture: MB33 7:30-9:00 (11& – from 6:00) Lab: B43, MB321, MB536 6:00-7:30 (from ) EACH student must have obtained.
JAVA Programming “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
22-July-2002cse142-13B-Development © 2002 University of Washington1 Development Tools CSE 142, Summer 2002 Computer Programming 1
CS 4244: Internet Programming Network Programming in Java 1.0.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
Creating a Java Application and Applet
Objects First With Java A Practical Introduction Using BlueJ Using Java without BlueJ 1.0.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Using Java without BlueJ BlueJ projects A BlueJ project is stored in a directory on disk. A BlueJ package is stored in several different files.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
CS-140 Dick Steflik Lecture 3. Java C++ Interpreted optimized for the internet Runs on virtual ized machine Derived from C++ Good object model Widely.
Lecture 1b- Introduction
Advanced Programing practices
More Sophisticated Behavior
Working with Java.
Running Programs & IDEs Reference: COS240 Syllabus
Introduction to Advanced Java Programming
Programming without BlueJ Week 12
Java programming lecture one
Introduction to C Topics Compilation Using the gcc Compiler
CompSci 230 Software Construction
More sophisticated behavior
Introduction to javadoc
Creating your first C program
Advanced Programing practices
Introduction to javadoc
Building a program (Java libraries) - an example
Chap 1. Getting Started Objectives
Chap 4. Programming Fundamentals
Review of Previous Lesson
Outcome of the Lecture Upon completion of this lecture you will be able to understand Fundamentals and Characteristics of Java Language Basic Terminology.
Presentation transcript:

7 Class documentation and libraries Objektorienterad programmering d2, förel. 7 7 Class documentation and libraries BK Appendix I, J, K.1, K.2 Compiling Java programs DAT050, DAI2, 17/18, lp 1

Main concepts to be covered Objektorienterad programmering d2, förel. 7 Main concepts to be covered Part I Using library classes Reading documentation Writing documentation Javadoc documentation tool Part II Compiling and executing java programs Java version naming conventions Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 The Java class library Thousands of classes Tens of thousands of methods Many useful classes that make life easier A competent Java programmer must be able to work with the libraries Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Working with the library Objektorienterad programmering d2, förel. 7 Working with the library You should: know some important classes by name know how to find out about other classes Remember: We only need to know the interface, not the implementation Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Reading class documentation Objektorienterad programmering d2, förel. 7 Reading class documentation Documentation of the Java libraries in HTML format; Readable in a web browser Class API: Application Programmers’ Interface Interface description for all library classes Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Interface vs implementation Objektorienterad programmering d2, förel. 7 Interface vs implementation The documentation includes the name of the class a general description of the class a list of constructors and methods return values and parameters for constructors and methods a description of the purpose of each constructor and method the interface of the class Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Interface vs implementation Objektorienterad programmering d2, förel. 7 Interface vs implementation The documentation does not include private fields (most fields are private) private methods the bodies (source code) for each method the implementation of the class Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 Using library classes Classes from the library must be imported using an import statement (except classes from java.lang) They can then be used like classes from the current project Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 Packages and import Classes are organised in packages Single classes may be imported: import java.util.ArrayList; Whole packages can be imported: import java.util.*; Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Writing class documentation Objektorienterad programmering d2, förel. 7 Writing class documentation Your own classes should be documented the same way library classes are Other people should be able to use your class without reading the implementation Make your class a 'library class'! Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Elements of documentation Objektorienterad programmering d2, förel. 7 Elements of documentation Documentation for a class should include: the class name a comment describing the overall purpose and characteristics of the class a version number the authors’ names documentation for each constructor and each method Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Elements of documentation Objektorienterad programmering d2, förel. 7 Elements of documentation The documentation for each constructor and method should include: the name of the method the return type the parameter names and types a description of the purpose and function of the method a description of each parameter a description of the value returned Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 javadoc Class comment: /** * The LectureGenerator class is used to create * automatic lecture objects. * * @author U.Holmer * @version 1.0.0.0.0.0.0.0.7z (23.Nov.2015) */ Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 javadoc Method comment: /** * Read a line of text from standard input (the text * terminal), and return it as a set of words. * * @param prompt A prompt to print to screen. * @return A set of Strings, where each String is * one of the words typed by the user */ public HashSet<String> getInput(String prompt) { ... } Demo BlueJ (t.ex. techsupport från förel. 5) DOS Kopiera först simple_project och package_project till D:\temp Simple: javadoc –d doc *.java Package: (senare) javadoc –sourcepath src –d doc main mailsystem Privata medlemmar: -private Se sista sidan för mer info (URL till Sun) Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 Review Java has an extensive class library A good programmer must be familiar with the library The documentation tells us what we need to know to use a class (interface) The implementation is hidden (information hiding) We document our classes so that the interface can be read on its own (class comment, method comments) Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Part II Using Java without BlueJ Objektorienterad programmering d2, förel. 7 Part II Using Java without BlueJ Kör dessa bilder fram till sid. 32 och demonstrera då principerna på simple_project och package_project 3.0 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 Topics Java file naming conventions The BlueJ file structure Compilation and execution of Java programs without BlueJ The main method Compilation of package structures Java version naming conventions Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Standard Java file types Objektorienterad programmering d2, förel. 7 Standard Java file types Source files File suffix: .java Text files Java source files contain the source code in readable form, as typed in by the programmer Same name base and spelling as class name Ex. class Game is stored in Game.java Normally one file per class explain difference between source and compiled code tell a bit about compiled and interpreted languages Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Standard Java file types cont. Objektorienterad programmering d2, förel. 7 Standard Java file types cont. Code files File suffix: *.class Binary files Java class files contain byte code (a machine readable version of the class). They are generated by the compiler from the source file Same name base and spelling as class name. Ex. The byte code for class Game is stored in Game.class One file per class explain difference between source and compiled code tell a bit about compiled and interpreted languages .class innehåller även typinformation Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 BlueJ projects A BlueJ project is stored in a directory on disc A BlueJ package is stored in several different files Some files store the source code, some store the compiled code, some store additional information BlueJ uses standard Java format for some files and adds some additional files with extra information use laptop show BlueJ example package on disk, using Windows explorer Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

The BlueJ file structure Objektorienterad programmering d2, förel. 7 The BlueJ file structure bluej.pkg - the package file. Contains information about classes in the package, one per package bluej.pkh – backup of the package file *.ctxt - BlueJ context file. Contains extra information for a class, one per class *.java - standard Java source file *.class - standard Java code file Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

The BlueJ directory structure Objektorienterad programmering d2, förel. 7 The BlueJ directory structure c:\bluej\calculator\ project: calculator bluej.pkg bluej.pkh Calculator.java Calculator.class Calculator.ctxt UserInterface.java UserInterface.class UserInterface.ctxt CalcEngine.java CalcEngine.class CalcEngine.ctxt Calculator UserInterface look at package in BlueJ then look at directory compare files in directory to classes in package CalcEngine Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

The edit-compile-execute cycle Objektorienterad programmering d2, förel. 7 The edit-compile-execute cycle 011010 110101 1001 10 source file class file 011010 110101 010001 1 1 1 0111 0110110 explain the process from the editor to execution explain virtual machine Alla verktyg baseras på Oracles basverktyg. compiler (javac) editor virtual machine (java) Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 Editing A file can be edited in any text editor Notepad, emacs, jEdit, PFE, vi, ... Make sure to save with a .java filename before compiling! some remarks about editing warning: word is not good! Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Command line invocation Objektorienterad programmering d2, förel. 7 Command line invocation Compilation and execution of Java in JDK are done from a command line On Microsoft systems: DOS shell On Unix: Unix shell Must make sure that the commands for compiler and runtime are in the command path On laptop: open DOS window, show command line commands Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 Compiling Name of the JDK compiler: javac To invoke: javac <source name> compiles <source name> and all classes it depends on Example: cd C:\bluej\zuul javac Game.java do the same live: edit, then compile a class Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 Error messages > javac Game.java Game.java:22: ';' expected. private Parser parser ^ 1 error > insert an error into the source and compile. Show error message format The programmer has to open the file in the editor, find the line number, fix the error and recompile. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 Execution > java Game The Java virtual machine is started The named class is loaded and execution begins Other classes are loaded as needed Only possible if class has been compiled show execution (at least an attempt). Do this first with a class that has no main. OBS! Ej .class vid körning! Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 Problem: Execute what? If we try: > java Game Exception in thread "main" java.lang.NoSuchMethodError: main The problem: how does the system know which method to execute? This error should be reported. Explain. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 The main method The java system always executes a method called main with a certain signature: public static void main(String[] args) { ... } For this to work, such a method must exist! Now write a main method, then try again. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 The main method (2) “main” must exist “main” must be public “main” must be static (class method) “main” must have a String array parameter Only “main” can be invoked Inga instansvariabler I klassen Main. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 Main method - example public static void main(String[] args) { Game game = new Game(); game.play(); } Consider placing main in a separate class, containing just this The main method should create an object call the first method Kör exemplet nu (se manus) Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 Another example A simple model of a mail system Classes MailServer MailClient MailItem Main: simple test class containing the main method File organization: All files are in the directory simple_project Kör simple_project - Sen åter hit Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Compiling the mail system (1) Objektorienterad programmering d2, förel. 7 Compiling the mail system (1) Compile > cd ... simple_project > javac Main.java Execute > java Main Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Adding package structure Objektorienterad programmering d2, förel. 7 Adding package structure As an application grows it should be modularized using packages package mailsystem contains the classes MailServer, MailClient and MailItem package main contains the Main class Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Adding package structure (2) Objektorienterad programmering d2, förel. 7 Adding package structure (2) Package declarations have to be added in the source (.java) files MailServer.java: package mailsystem; class MailServer { ... } MailClient.java: class MailClient { ... } MailItem.java: class MailItem { ... } Main.java: package main; import mailsystem.*; class Main { public static void main ... } Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Adding package structure (3) Objektorienterad programmering d2, förel. 7 Adding package structure (3) A possible directory structure organization -- package_project |-- bin | |-- mailsystem | | |-- MailClient.class | | |-- MailItem.class | | |-- MailServer.class | |--main | |-- Main.class |-- src |-- mailsystem | |-- MailClient.java | |-- MailItem.java | |-- MailServer.java |-- main |-- Main.java Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Compiling the mail system (2) Objektorienterad programmering d2, förel. 7 Compiling the mail system (2) > cd ... package_project > mkdir bin (if necessary) > javac -d bin -sourcepath src src/main/Main.java Where to put binaries (.class) Source root directory (.java) What to compile Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Executing the mail system (2) Objektorienterad programmering d2, förel. 7 Executing the mail system (2) > cd ... package_project > java -classpath bin main.Main Root directory of binaries Kör package_project + javadoc Main class (in package main) Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Java names and versions Objektorienterad programmering d2, förel. 7 Java names and versions Platform name Java SE = Java Platform Standard Edition Products under the platform JDK = Java SE Development Kit Tools: Compiler, Javadoc, ... JRE = Java SE Runtime Environment Abbreviation Full name Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Java 8 version numbering Objektorienterad programmering d2, förel. 7 Java 8 version numbering Java SE 8 = Java Platform Standard Edition version 8 Version number 1.8.0_xy Version string Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Java SE version strings Objektorienterad programmering d2, förel. 7 Java SE version strings command > java -version Update 7 (Bug fixes) 1.8.0_07-b11 Version 8 Build 11 Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Example: Directory naming Objektorienterad programmering d2, förel. 7 Example: Directory naming Java Development Kit jdk1.8.0_07 Java Runtime Environment jre8 Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1

Objektorienterad programmering d2, förel. 7 Some useful links Tools like javadoc, javac, etc. and Java version naming conventions http://docs.oracle.com/javase/8/docs/technotes/tools/ Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 1