Java JavaPC programming environment Java syntax rules Java

  • Slides: 79
Download presentation
Java ▮ Java/PC programming environment ▮ Java syntax rules ▮ Java documentation ▮ Java/RAD

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

Setting up the environment ▮ Files are identified by a name, myfile. doc, and

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 Filesmyfile. 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 2016 2

Setting up the environment ▮ Having to type the fully-qualified name of every file

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 indicates where to search for non-qualified. class file references Chapter 2 © copyright Janson Industries 2016 3

Setting up the environment ▮ Defining Path and CLASSPATH every time is time consuming

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 2016 4

Setting the Path (Windows 7) Right click: “Computer” Choose: Properties Click: Advanced system settings

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

Setting the Path (Windows 7) In the System Variables pane: Click the variable Path

Setting the Path (Windows 7) In the System Variables pane: Click the variable Path Then the Edit button Chapter 2 © copyright Janson Industries 2016 6

Setting the Path (Windows 7) In the Edit System Variable window, add to the

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

Setting the Classpath (Win 7) ▮ If there is no classpath environment variable, create

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

Setting the Path (Windows 10) Click the Start button, then All Apps Scroll down

Setting the Path (Windows 10) Click the Start button, then All Apps Scroll down to and expand the Windows Systems item Chapter 2 Click Control Panel On the Control panel, click System and Security © copyright Janson Industries 2016 9

Setting the Path (Windows 10) Click System Then click Advanced system settings Chapter 2

Setting the Path (Windows 10) Click System Then click Advanced system settings Chapter 2 © copyright Janson Industries 2016 10

Setting the Path (Windows 10) Click the Environment Variables button Chapter 2 © copyright

Setting the Path (Windows 10) Click the Environment Variables button Chapter 2 © copyright Janson Industries 2016 11

Setting the Path (Windows 10) In the System Variables pane: Click the variable Path

Setting the Path (Windows 10) In the System Variables pane: Click the variable Path Then the Edit button Chapter 2 12

Setting the Path (Windows 10) In the Edit environment variable window, click New button

Setting the Path (Windows 10) In the Edit environment variable window, click New button and type the new Path value: c: jdkbin Then Click the OK button Change CLASSPATH by adding the path of your java application (i. e. . class files), c: Chapter 2 © copyright Janson Industries 2016 13

Setting the Classpath (Win 10) ▮ If there is no classpath environment variable, create

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

Setting the Classpath (Win 10) ▮ Click the OK Button ▮ New variable should

Setting the Classpath (Win 10) ▮ Click the OK Button ▮ New variable should be displayed 15

General steps to run on PC ▮ Save the source code (E. g. Customer.

General steps to run on PC ▮ Save the source code (E. g. Customer. java) ▮ Bring up the command prompt ▮ Set path (path=f: jdkbin) 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 2016 16

Java Concepts ▮ Java “program” is called a class ▮ Java classes grouped into

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 2016 17

Java Language Organization Project Package Class Variable Class Method Variable Chapter 2 Class Package

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

Java Language Concepts ▮ Let’s concentrate on the source code ▮ Java classes comprised

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 2016 19

Classes Class Variable Method Statement z Class comprised of class variable definitions and methods

Classes Class Variable Method Statement z Class comprised of class variable definitions and methods z Methods contain method variable definitions and executable statements Chapter 2 © copyright Janson Industries 2016 20

Java Class Example // Customer. java public class Customer { String cust. Name, Class

Java Class Example // Customer. java public class Customer { String cust. Name, Class variables cust. Street, cust. CSZ; methods public Customer() { cust. Name = "No Name"; cust. Street = "No Street"; statements cust. CSZ = "No City. State. Zip"; } public void print. Cust. Info(){ System. out. println("CUSTOMER NAME IS: " + cust. Name); System. out. println("CUSTOMER STREET IS: " + cust. Street); System. out. println("CUSTOMER CSZ IS: " + cust. CSZ); } public static void main( String[] args ) { Customer a. Cust = new Customer(); Method variable a. Cust. cust. Name = "Joe Customer"; a. Cust. print. Cust. Info(); } } Chapter 2 © copyright Janson Industries 2016 21

Classes ▮ Divided into the header and the body ▮ Header defines: ▮ The

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: Chapter 2 ▮ public class Customer { BODY © copyright Janson Industries 2016 } 22

Java Class “A class contains a template for creating objects” Class Definition: Class access

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

Java Method ▮ Comprised of a header/definition and body ▮ Header/definition comprised of: ▮

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. “get. Mailing. Label”, “main”) ▮ Parameter(s)/received value(s) in parenthesis (e. g. (String name), (int age), () means no params) Chapter 2 © copyright Janson Industries 2016 24

Java Method ▮ When a parameter is defined, must specify the data type and

Java Method ▮ When a parameter is defined, 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 25

Java Method ▮ Method header/definition examples: ▮ public void set. Customer. Name(String cust. Name)

Java Method ▮ Method header/definition examples: ▮ public void set. Customer. Name(String cust. Name) ▮ public String get. Mailing. Label() ▮ public static void main(String[ ] args) ▮ public void set. Tax. Rate(double tax. Rate) Chapter 2 © copyright Janson Industries 2016 26

Java Method ▮ Method "access" modifiers (cont’d): ▮ Private methods: ▮ only accessed by

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 2016 27

Java Concepts ▮ Java classes can be invoked many ways ▮ 2 primary ways

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 2016 28

Java Concepts ▮ If java class is invoked with the java command: ▮ main

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 2016 29

Specialized Methods ▮ Constructors are methods that: ▮ Can not return any values ▮

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 2016 30

main Method When main is invoked, object not created Main expects an array of

main Method When main is invoked, object not created Main expects an array of Strings and the variable associated with the array is called args public static void main(String[ ] args) Any class can use Method name No values returned Chapter 2 © copyright Janson Industries 2016 31

Customer has a class header, constructor, and main method that follow the previously defined

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

Method body ▮ Comprised of: ▮ variable definitions ▮ executable statements ▮ Enclosed in

Method body ▮ Comprised of: ▮ variable definitions ▮ executable statements ▮ Enclosed in braces { } public Customer(String name, String street, String city. State. Zip) { private String cust. Name = null; private String cust. Street = null; private String cust. City. State. Zip = null; cust. Name = name; cust. Street = street; cust. City. State. Zip = city. State. Zip; } Chapter 2 © copyright Janson Industries 2016 33

Java Language ▮ Variables usually defined at the beginning of a class or method

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. “cust. ID”, “cust. Name”) Initial value (optional) End with a semicolon (; ) ▮ Examples: private int cust. ID = 0; public String cust. Name = null; Chapter 2 © copyright Janson Industries 2016 34

Java Language ▮ Comment examples: /* This is an example */ Multiple line comment

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

Programming Conventions ▮ Class header and start of body defined public class Customer {

Programming Conventions ▮ Class header and start of body defined public class Customer { ▮ Class variables defined private String cust. City. State. Zip = null; private static int last. Cust. ID = 100000; ▮ Methods defined public Customer(String city. State. Zip) { cust. City. State. Zip = city. State. Zip; int cust. ID = last. Cust. ID + 1; } ▮ Class body ended } Chapter 2 © copyright Janson Industries 2016 36

Source Code Rules ▮ Statements end with a semicolon (; ) private String cust.

Source Code Rules ▮ Statements end with a semicolon (; ) private String cust. City. State. Zip = null; private static int last. Cust. ID = 100000; ▮ Very forgiving re: extra spaces and lines private String cust. City. State. Zip = null public Customer(String city. State. Zip) { cust. City. State. Zip = city. State. Zip; int cust. ID = last. Cust. ID + 1; ; } ▮ To invoke a method from a method in the same class (and pass many parms) : this. method. Name(parm, etc); return. Value = this. method. Name(parm, etc. ); Chapter 2 © copyright Janson Industries 2016 37

Source Code Rules ▮ To invoke a method from outside the class, instantiate an

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: Example: Class. Name variable. Name = new Class. Name(); Customer my. Cust = new Customer(); ▮ Then using the variable invoke the object’s method: Syntax: Example: Chapter 2 variable. Name. method. Name(); my. Cust. print. Cust. Info(); © copyright Janson Industries 2016 38

Source Code Rules ▮ Weird? Not really. To use Word Processing commands, don’t you

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 my. Cust = new Customer(); Chapter 2 my. Cust. print. Cust. Info(); © copyright Janson Industries 2016 39

New Class header Class variables Method header Statements Chapter 2 © copyright Janson Industries

New Class header Class variables Method header Statements Chapter 2 © copyright Janson Industries 2016 40

Creating an Object Class header Method header Statements What will be the result of

Creating an Object Class header Method header Statements What will be the result of running Customer. App as an application? Chapter 2 © copyright Janson Industries 2016 41

Execution Results ▮ Customer. App’s main method bytecode converted to machine language and loaded

Execution Results ▮ Customer. App’s main method bytecode converted to machine language and loaded into main memory ▮ First statement in main executed: a Customerc 2 sl 31 object is created. This means: ▮ The entire class’ bytecode is converted to machine language and loaded into main memory ▮ 3 String objects are created, 3 String class level variables (cust. Name, cust. Street and cust. CSZ) are created, and the String objects are assigned to the variables ▮ The object’s constructor is executed. This means: Chapter 2 © copyright Janson Industries 2016 42

Execution Results ▮ 3 String objects are created with the text “No Name”, “No

Execution Results ▮ 3 String objects are created with the text “No Name”, “No Street”, “No City. State. Zip” ▮ The 3 new String objects are assigned to the variables cust. Name, cust. Street and cust. CSZ ▮ Execution returns to the first statement in the main method and ▮ The Customer variable a. Cust is created ▮ The Customer object is assigned to a. Cust ▮ Second statement in main executed: Chapter 2 ▮ A String object is created with the text “Joe Customer” ▮ The new String object with the value “Joe Customer” is assigned to the Customerc 2 sl 31 object’s class level String variable cust. Name © copyright Janson Industries 2016 43

Execution Results ▮ Third statement in main executed: the Customerc 2 sl 31 object’s

Execution Results ▮ Third statement in main executed: the Customerc 2 sl 31 object’s print. Cust. Info 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 2016 44

Proving main vs. constructor What will be the result of running this? Let’s create

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

Chapter 2 © copyright Janson Industries 2016 Click on the src folder name and

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

Chapter 2 © copyright Janson Industries 2016 Specify the name of the new class

Chapter 2 © copyright Janson Industries 2016 Specify the name of the new class (Cust 1), click Finish 47

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

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

Enter the source code and run What are the results and why? Chapter 2

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

Exporting: Moving an app from RAD to the production environment Click FILE then EXPORT

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

Click File system then Next Chapter 2 © copyright Janson Industries 2016 51

Click File system then Next Chapter 2 © copyright Janson Industries 2016 51

In real world, usually export to JAR (Java ARchive) files. They are compressed files.

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

1. Select the java files to export 2. Enter export destination or. . .

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

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

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

Go out and verify Cust 1. java is there. Chapter 2 © copyright Janson

Go out and verify Cust 1. java is there. Chapter 2 © copyright Janson Industries 2016 55

Open with notepad to display Chapter 2 © copyright Janson Industries 2016 56

Open with notepad to display Chapter 2 © copyright Janson Industries 2016 56

Make sure current directory and path variable are set javac and java Chapter 2

Make sure current directory and path variable are set javac and java Chapter 2 © copyright Janson Industries 2016 57

Exporting ▮ Moving between/setting up environments is one the most difficult thing (conceptually) for

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 2016 58

Exporting ▮ This is where packages can mess you up! ▮ Ex. For a

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

Exporting and Running ▮ When file exported to Windows, Customer file must be in

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

Exporting and Running ▮ Need to add Proj 1 to the classpath and refer

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

Exporting and Running ▮ In other words, on the export window choose third option

Exporting and Running ▮ In other words, on the export window choose 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 Chapter 2 © copyright Janson Industries 2016 62

Exporting and Running ▮ No project folder or src folder will be created ▮

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

Assgs ▮ Export file(s) or package (recommended) out of RAD workspace as a single

Assgs ▮ Export file(s) or package (recommended) out of RAD workspace as a single jar file ▮ Have to send at least the source (. java) files ▮ Send the jar file as an email attachment to wsjavaws@yahoo. com ▮ 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 2016 64

Assgs ▮ When exporting as a jar, you must specify that the source files

Assgs ▮ When exporting as a jar, you must specify that the source files be exported Chapter 2 © copyright Janson Industries 2016 65

Assgs ▮ How to check you exported a jar file correctly ▮ Create a

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 new project and confirm that all the packages and source code files are there Chapter 2 © copyright Janson Industries 2016 66

Documentation ▮ Online documentation available. Ex: http: //download. oracle. com/javase/8/docs/api/ ▮ Gives an overview

Documentation ▮ Online documentation available. Ex: http: //download. oracle. com/javase/8/docs/api/ ▮ 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 2016 67

Shows a brief description of all packages in V 8 Click the package name

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

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

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

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

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

Documentation ▮ You can generate the same documentation for your classes ▮ First, you

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: jdkbinjavadoc. exe Chapter 2 © copyright Janson Industries 2016 71

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

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

Select Javadoc and click Next Chapter 2 © copyright Janson Industries 2016 73

Select Javadoc and click Next Chapter 2 © copyright Janson Industries 2016 73

RAD should know that javadoc is in the JDK in RAD Specify where the

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

Chapter 2 © copyright Janson Industries 2016 75

Chapter 2 © copyright Janson Industries 2016 75

Console shows Javadoc messages Chapter 2 © copyright Janson Industries 2016 76

Console shows Javadoc messages Chapter 2 © copyright Janson Industries 2016 76

. html file with same name as class is there Double click to display

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

The “signature” of each method is shown Signature = method name + parameters The

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 2016 78

Non-graded Assg ▮ Create Cust 1 ▮ Export Cust 1 and Run ▮ Export

Non-graded Assg ▮ Create Cust 1 ▮ Export Cust 1 and Run ▮ Export Cust 1 documentation ▮ Send Cust 1. java, Cust 1. class, and Cust 1. html files as email attachments to wsjavaws@yahoo. com Chapter 2 © copyright Janson Industries 2016 79