Chapter 2© copyright Janson Industries 20141 Java ▮ Java/PC programming environment ▮ Java syntax rules ▮ Java documentation ▮ Java/RAD coding example.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.
Advertisements

Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Introducing Java CSC1401. Course Goals Teaching programming concepts In a “real” language.
Using Eclipse. Getting Started There are three ways to create a Java project: 1:Select File > New > Project, 2 Select the arrow of the button in the upper.
The Web Warrior Guide to Web Design Technologies
 2005 Pearson Education, Inc. All rights reserved Introduction.
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Classes and Instances. Introduction Classes, Objects, Methods and Instance Variables Declaring a Class with a Method and Instantiating an Object of a.
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
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.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
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]
1 Mobile Computing Java, Android, and Eclipse Copyright 2015 by Janson Industries.
Using Eclipse. What is Eclipse? The Eclipse Platform is an open source IDE (Integrated Development Environment), created by IBM for developing Java programs.
Chapter 1a© copyright Janson Industries RAD ▮ Explain Eclipse, RAD, and their relationship ▮ Basic RAD concepts ▮ How to install RAD ▮ How to import.
Introduction to Programming Writing Java Beginning Java Programs.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Chapter 1© copyright Janson Industries Java ▮ Java programming concepts ▮ Downloading the JDK ▮ Java coding and executing example This presentation.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Appendix E The EZJava.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
POS 406 Java Technology And Beginning Java Code
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
CPSC1301 Computer Science 1 Overview of Dr. Java.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Creating and running a Java program. Eclipse Interactive Development Environment (IDE)  Eclipse is an Interactive Development Environment (IDE) for Java.
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.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Programming Writing Java Beginning Java Programs.
Chapter 4 Introduction to Classes, Objects, Methods and strings
JAVA Practical Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
A brief introduction to javadoc and doxygen. What’s in a program file? 1. Comments 2. Code.
Creating a Java Application and Applet
Chapter 4© copyright Janson Industries More GUI ▮ Events, Actions, and Listeners ▮ The Visual Editor ▮ More GUI component properties Non-graded assg.
Chapter 12© copyright Janson Industries Java Server Faces ▮ Explain the JSF framework ▮ SDO (service data objects) ▮ Facelets ▮ Pagecode classes.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Object Oriented Programming Object and Classes Lecture 3 MBY.
1/16: Intro to Java, Languages, and Environments Types of programming languages –machine languages –assembly languages –high-level languages Java environment.
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.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
© copyright Janson Industries 2014
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Chapter 1.2 Introduction to C++ Programming
Introduction to javadoc
Some Tips for Using Eclipse
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Anatomy of a Java Program
Introduction to javadoc
Object Oriented Programming in java
Workshop for Programming And Systems Management Teachers
Presentation transcript:

Chapter 2© copyright Janson Industries Java ▮ Java/PC programming environment ▮ Java syntax rules ▮ Java documentation ▮ Java/RAD coding example This presentation is stored in 4thed.Ch02.ppt Non-graded assg

Chapter 2© copyright Janson Industries Setting up the environment ▮ Files are identified by a name, myfile.doc, and location (aka path), C:\Program Files\ ▮ When a file is identified by the path and file name, C:\Program Files\myfile.doc, it is considered a fully-qualified reference ▮ If a file is referred to with only name, myfile.doc, it is a non-qualified reference

Chapter 2© copyright Janson Industries Setting up the environment ▮ Having to type the fully-qualified name of every file is time consuming and error prone ▮ The PATH system variable tells Windows where to search for non-qualified file references ▮ The CLASSPATH indictates where to search for non-qualified.class file references

Chapter 2© copyright Janson Industries Setting up the environment ▮ Defining Path and CLASSPATH every time is time consuming ▮ Could create a.bat program called init ▮ Init.bat would contain the commands to define Path and CLASSPATH ▮ Simply run init each time ▮ Or, define the system variables permanently

Chapter 2© copyright Janson Industries Setting the Path Right click: “Computer” Choose: Properties Click: Advanced system settings Click: Environment Variables button

Chapter 2© copyright Janson Industries In the System Variables pane: Click the variable Path Then the Edit button Setting the Path

Chapter 2© copyright Janson Industries In the Edit System Variable window, add to the end of the Path value: ;c:\jdk\bin Then Click the OK button Change CLASSPATH by adding the path of your java application (i.e..class files) to the variable ; c:\ Setting the Path (Windows 7)

Chapter 2© copyright Janson Industries Setting the Classpath ▮ If there is no classpath environment variable, create one ▮ Click New and specify the variable name (classpath) and value

Chapter 2© copyright Janson Industries General steps to run on PC ▮ Save the source code (E.g. Customer.java) ▮ Bring up the command prompt ▮ Set path (path=f:\jdk\bin) to identify the loaction of the javac and java commands ▮ Set classpath (Set classpath=.;f:\) to identify location of java class files (i.e..class files) ▮ Or, change the environment values ▮ Convert to byte code (javac Customer.java) ▮ Run the program (java Customer)

Chapter 2© copyright Janson Industries Java Concepts ▮ Java “program” is called a class ▮ Java classes grouped into projects and packages ▮ Java classes comprised of global/class variable definitions and methods ▮ Methods contain executable statements and local/method variable definitions ▮ Are you familiar with what variables do?

Chapter 2© copyright Janson Industries Java Language Organization Project Package Package Package Class Class Class Variable Method Method Method Variable Variable Statement Statement

Chapter 2© copyright Janson Industries Java Language Concepts ▮ Let’s concentrate on the source code ▮ Java classes comprised of class ( global) variable definitions and methods ▮ Methods contain executable statements and method ( local) variable definitions

Chapter 2© copyright Janson Industries Classes Class Variable Variable Method Method Method Variable Statement Statement zClass comprised of class variable definitions and methods zMethods contain method variable definitions and executable statements

Chapter 2© copyright Janson Industries Java Class Example // Customer.java public class Customer { String custName, custStreet, custCSZ; public Customer() { custName = "No Name"; custStreet = "No Street"; custCSZ = "No CityStateZip"; } public void printCustInfo(){ System.out.println("CUSTOMER NAME IS: " + custName); System.out.println("CUSTOMER STREET IS: " + custStreet); System.out.println("CUSTOMER CSZ IS: " + custCSZ); } public static void main( String[] args ) { Customer aCust = new Customer(); aCust.custName = "Joe Customer"; aCust.printCustInfo(); } methods Class variables statements Method variable

Chapter 2© copyright Janson Industries Classes ▮ Divided into the header and the body ▮ Header defines: ▮ The source code as a class (e.g. “class”) ▮ Access allowed (e.g. “private”, “public”) ▮ The name of class ▮ Must begin with an upper case letter ▮ Must match.java file name ▮ Body is enclosed in braces and contains class variables and methods ▮ Simple class header example: ▮ public class Customer { BODY }

Class names  Are case sensitive!  Begin with a capital letter  No spaces Body of class begins and ends with braces Chapter 2© copyright Janson Industries Class Definition: Header Bodypublic class Customer { class variables method{} } Java Class Class access Class name “A class contains a template for creating objects”

Chapter 2© copyright Janson Industries Java Method ▮ Comprised of a header/definition and body ▮ Header/definition comprised of: ▮ Modifiers (e.g. private, public, static) ▮ Return value type (e.g. void, String) ▮ Method name begins with a lower case letter (e.g. “getMailingLabel”, “main”) ▮ Parameter(s)/received value(s) in parenthesis (e.g. (String name), (int age), () means no params)

18 Java Method ▮ When a parameter is define must specify the data type and the variable name to hold the data ▮ So with (String name) String is the data type and the variable that will hold the passed value is name ▮ (int age), data type is integer (int) and the variable name is age

Chapter 2© copyright Janson Industries Java Method ▮ Method header/definition examples: ▮ public void setCustomerName(String custName) ▮ public String getMailingLabel() ▮ public static void main(String[ ] args) ▮ public void setTaxRate(double taxRate)

Chapter 2© copyright Janson Industries Java Method ▮ Method "access" modifiers (cont’d): ▮ Private methods: ▮ only accessed by other methods within the class ▮ Public methods: ▮ can be accessed by objects external to the class ▮ comprise the class “interface” ▮ Variables can also be public or private ▮ public can be accessed by objects external to the class

Chapter 2© copyright Janson Industries Java Concepts ▮ Java classes can be invoked many ways ▮ 2 primary ways ▮ Run with the java command ▮ Instantiated ▮ An object (aka an instance) of the class type is created ▮ An object is an "instance of a class" and is usually associated with a variable

Chapter 2© copyright Janson Industries Java Concepts ▮ If java class is invoked with the java command: ▮ main method bytecode converted to machine language ▮ main method machine language loaded into main memory ▮ main method is executed ▮ If Java class is instantiated: ▮ Entire class' bytecode converted to machine language and loaded into main memory ▮ i.e. An object of that class type is created ▮ Class (global) variables created ▮ constructor method is executed

Chapter 2© copyright Janson Industries Specialized Methods ▮ Constructors are methods that: ▮ Can not return any values ▮ Have the same name as the class ▮ Are run when the object is instantiated ▮ Are used to initialize variables and perform setup operations (e.g. open files, assign user supplied values, establish communication links, etc.) ▮ Static method “main”: ▮ Java command invokes main method in applications (main is not run when object instantiated) ▮ Main method header must be defined precisely as follows:

Chapter 2© copyright Janson Industries public static void main(String[ ] args) main Method No values returned Any class can use The method expects an array of String variables (called args) When main is invoked, object not created Method name

Chapter 2© copyright Janson Industries Notice how much the class and constructor method headers look alike What's the difference? Customer has a class header, constructor, and main method that follow the previously defined rules

Chapter 2© copyright Janson Industries Method body ▮ Comprised of: ▮ variable definitions ▮ executable statements ▮ Enclosed in braces { } public Customer(String name, String street, String cityStateZip) { private String custName = null; private String custStreet = null; private String custCityStateZip = null; custName = name; custStreet = street; custCityStateZip = cityStateZip; }

Chapter 2© copyright Janson Industries Java Language ▮ Variables usually defined at the beginning of a class or method ▮ Variable definitions: Access modifier (e.g. “private”, “public”) (optional) Data type (e.g. “int”, “String”) Name (e.g. “custID”, “custName”) Initial value (optional) End with a semicolon (;) ▮ Examples: private int custID = 0; public String custName = null;

Chapter 2© copyright Janson Industries Java Language Multiple line comment Single line comment Multiple line comment ▮ Comment examples: /* This is an example */ /** This is an example */ public int custID = 0; // This is an example ▮ In RAD, comment out and uncomment any line(s) by selecting and pressing CTRL + /

Chapter 2© copyright Janson Industries Programming Conventions ▮ Class header and start of body defined public class Customer { ▮ Class variables defined private String custCityStateZip = null; private static int lastCustID = ; ▮ Methods defined public Customer(String cityStateZip) { custCityStateZip = cityStateZip; int custID = lastCustID + 1; } ▮ Class body ended }

Chapter 2© copyright Janson Industries Source Code Rules ▮ Statements end with a semicolon (;) private String custCityStateZip = null; private static int lastCustID = ; ▮ Very forgiving re: extra spaces and lines private String custCityStateZip = null ; public Customer(String cityStateZip) { custCityStateZip = cityStateZip; int custID = lastCustID + 1;} ▮ To invoke a class method from another class method (and pass many parms): this.methodName(parm, parm, etc); returnValue = this.methodName(parm, parm, etc.);

Chapter 2© copyright Janson Industries Source Code Rules ▮ To invoke a method from outside the class, instantiate an object of the class type, create a variable of the class type and assign the object to the variable : ▮ Syntax: ClassName variableName = new ClassName(); Example: Customer myCust = new Customer(); ▮ Then invoke the object’s method: Syntax: variableName.methodName(); Example: myCust.printCustInfo();

Chapter 2© copyright Janson Industries Source Code Rules ▮ Weird? Not really. To use Word Processing commands, don’t you need to create a document? ▮ To use queue commands, doesn’t a queue have to be created (and referenced in each command) ? ▮ So why shouldn’t you have to create a Customer object to use a customer function? Customer myCust = new Customer(); myCust.printCustInfo();

Chapter 2© copyright Janson Industries Class header Class variables Method header Statements Method header Statements Method header Statements What will be the result of running Customerc2sl31 as an application?

Chapter 2© copyright Janson Industries Execution Results ▮ main method bytecode converted to machine language and loaded into main memory ▮ First statement in main executed: a Customer object is created. This means: ▮ The entire Customer class’ bytecode is converted to machine language and loaded into main memory ▮ 3 String objects are created, 3 String class level variables (custName, custStreet and custCSZ) are created, and the String objects are assigned to the variables ▮ The Customer object’s constructor is executed. This means:

Chapter 2© copyright Janson Industries Execution Results ▮ 3 String objects are created with the text “No Name”, “No Street”, “No CityStateZip” ▮ 3 new String objects are assigned to the variables custName, custStreet and custCSZ ▮ Execution returns to the first statement in the main method and ▮ The Customer variable aCust is created ▮ The Customer object is assigned to aCust ▮ Second statement in main executed: ▮ A String object is created with the text “Joe Customer” ▮ The new String object with the value “Joe Customer” is assigned to the Customer object’s class level String variable custName

Chapter 2© copyright Janson Industries Execution Results ▮ Third statement in main executed: the Customer object’s printCustInfo method invoked. This means: ▮ First println statement is executed, what is shown? ▮ Second println statement is executed, what is shown? ▮ Third println statement is executed, what is shown?

Chapter 2© copyright Janson Industries What will be the result of running this? Let’s create a new class and run… Proving main vs. constructor

Chapter 2© copyright Janson Industries Click on the src folder name and then File, New, Class

Chapter 2© copyright Janson Industries Specify the name of the new class (Cust1), click Finish

Chapter 2© copyright Janson Industries Starts you with a framework Move the cursor and start typing

Chapter 2© copyright Janson Industries Enter the source code and run What are the results and why?

Chapter 2© copyright Janson Industries Exporting: Moving an app from RAD to the production environment Click FILE then EXPORT

Chapter 2© copyright Janson Industries Click File system then Next

Chapter 2© copyright Janson Industries In real world, usually export to JAR (Java ARchive) files. They are compressed files.

Chapter 2© copyright Janson Industries Select the java files to export...click browse and specify the location 2. Enter export destination or Click OK 4. Click Finish

Chapter 2© copyright Janson Industries Click Finish Notice options to create directories and overwriting files

Chapter 2© copyright Janson Industries Go out and verify Cust1.java is there.

Chapter 2© copyright Janson Industries Open with notepad to display

Chapter 2© copyright Janson Industries javac and java Make sure classpath and path variables are set

Chapter 2© copyright Janson Industries Exporting ▮ Moving between/setting up environments is one the most difficult thing (conceptually) for new programmers ▮ Programmers develop apps within an IDE (like RAD) and export to the production environment

Chapter 2© copyright Janson Industries Exporting ▮ This is where packages can mess you up! ▮ Ex. For a Customer class stored in project Proj1 and package C1: ▮ Customer must have a package statement at beginning of class package C1; ▮ Class name is now considered C1.Customer

Chapter 2© copyright Janson Industries Exporting and Running ▮ When file exported to Windows, Customer file must be in a directory/folder called C1 ▮ Project and packages (except default pkg) implemented in Windows as directories ▮ So, if "create directory structure" specified ▮ Both Proj1 and C1 directories are created ▮ Also a src folder is created ▮ javac Customer will fail because Customer not class name ▮ javac C1/Customer will fail because Proj1 not in classpath

Chapter 2© copyright Janson Industries Exporting and Running ▮ Need to add Proj1 to the classpath and refer to the class with package name included in the class name ▮ Set classpath = f:\Proj1\src ▮ javac C1/Customer ▮ Be careful of directory structure when exporting! ▮ I suggest just exporting the package as a directory

Chapter 2© copyright Janson Industries ▮ In other words, on the export window choose the third option "create only selected directories" ▮ Then click the java file and the package's checkboxes ▮ I.e. select the package/directory ▮ This will result in a folder with the package name and the java file inside it Exporting and Running

Chapter 2© copyright Janson Industries ▮ No project folder or src folder will be created ▮ You wont have to add them to the path ▮ Still need to set the path to the location of the package/folder though ▮ Then issue javac command with the package/folder name and the java file name Exporting and Running

Chapter 2© copyright Janson Industries Graded Assgs (class room based class) ▮ Export out of RAD workspace onto thumb drive ▮ Source and.class files ▮ Create batch file so it executes the new thumb drive files ▮ Assgs will be graded in class ▮ Display source files (including bat file) in notepad ▮ Run the app ▮ I will run test data

Chapter 2© copyright Janson Industries Non - Graded Assgs ▮ Export file(s) or package (recommended) out of RAD workspace as a jar file ▮ Source and.class files ▮ Send the jar file as an attachment to ▮ I will acknowledge receiving them within one business day ▮ If you do not hear from me, I did not receive them

Chapter 2© copyright Janson Industries Non - Graded Assgs ▮ How to check you exported a jar file correctly ▮ Create a new project ▮ Import the jar into the project by ▮ Choosing Import archive file ▮ Specify the jar file ▮ Expand the project and confirm that all the packages and source code files are there

Chapter 2© copyright Janson Industries Graded Assgs ▮ Export package(s) or entire project(s) out of RAD workspace as a jar file ▮ Source and.class files ▮ Send the file(s) as an attachment to ▮ I will acknowledge receiving them within one business day ▮ If you do not hear from me, I did not receive them

Chapter 2© copyright Janson Industries Graded Assgs ▮ How to check you exported the jar correctly ▮ Create a new project ▮ Import the jar into the project by ▮ Choosing Import archive file ▮ Specify the jar file ▮ Expand the project and confirm that all the packages and source code files are there

Chapter 2© copyright Janson Industries Documentation ▮ Online documentation available. Ex: ▮ Gives an overview of the JDK supplied classes and a short description of the methods ▮ The JDK classes grouped into packages!

Chapter 2© copyright Janson Industries Shows a brief description of all packages in V8 Or click a class in the index on the left Click the package name to see all classes in the package

Chapter 2© copyright Janson Industries Shows a hierarchy for the class Brief description and examples

Chapter 2© copyright Janson Industries A list and brief summary of the methods in the class

Chapter 2© copyright Janson Industries Documentation ▮ You can generate the same documentation for your classes ▮ First, you may have to tell RAD where the javadoc command is (in the JDK) ▮ In our case: F:\jdk\bin\javadoc.exe

Chapter 2© copyright Janson Industries Right click the class and select Export

Chapter 2© copyright Janson Industries Select Javadoc and click Next

Chapter 2© copyright Janson Industries Specify where the documentation should go, click Finish RAD should know that javadoc is in the JDK in RAD

Chapter 2© copyright Janson Industries

Chapter 2© copyright Janson Industries Console shows Javadoc messages

Chapter 2© copyright Janson Industries html file with same name as class is there Double click to display in browser

Chapter 2© copyright Janson Industries The “signature” of each method is shown Signature = method name + parameters The signature uniquely defines each method, not the name

Chapter 2© copyright Janson Industries Non-graded Assg ▮ Create Cust1 ▮ Export Cust1 and Run ▮ Export Cust1 documentation ▮ Send Cust1.java, Cust1.class, and Cust1.html files as attachments to