Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
What is programming?
This book is all about how to learn to program using the Java
programming language, but many of you may be wondering what you are
getting into. You may have heard statements like these about
programming:
The programming language you will be learning in this book is Java. Select
the link below to view the video to learn more about the Java programming
language. After watching each video you will need to hit the Back button
in your browser to return back to this document.
Here is a list of object oriented concepts that you need to learn know:
Class: A class describes a group of objects that are all of the same
type. A class defines: 1) the attributes or important
characteristics that need to be remembered for each object of
the class, and 2) the functions that the objects of this class
can perform. A class serves as blueprint for defining and
creating objects that are all of the same type.
The file name of each of source file must be the same as the name of the
class and end with the file extension .java. For example, the name of the
source file for the PlayingPiece class is PlayingPiece.java.
One of the class files in the program must always contain a function
called, main(). The main() function tells the computer where to start
executing your program. The computer looks for the class that contains
the main() function and then starts execution of the program at the first
statement in that function. The main() function is usually placed in the
class that represents the program itself.
The block of code after the class statement defines the attributes and
functions of the class.
Begins block
Indent
Block
Ends block
A block in Java starts with a left curly brace{and ends with a right curly
brace }. A block defines a group statements that are logically related
together.
Unique memory locations are assigned to each of the variables for each
object instance when an object is created. This is illustrated in the two
PlayingPiece object instances below. Each of the class instance
The class functions typically follow the class instance variables (attributes).
There move() and attack() functions are defined in the PlayingPiece
class below.
Class functions
Function
signature
Indent
Block
Ends block
Indent
Indent
Case sensitive
Java is case sensitive. This means that it recognizes the difference
between upper and lower case letters. For example, it distinguishes the
difference between the words, price, and Price.
Free-form
Writing a Java statement is very similar to writing a sentence in english.
They can start anywhere in a line and can flow from one line to another.
White space (blanks) is used to distinguish one token in a statement from
another. In english, a sentence must end with either a period, exclamation
or a question mark. In Java, every statement must end with either a
semicolon (;) or a right curly brace (}) .
Case sensitive
Java is case sensitive. This means that it recognizes the difference
between upper and lower case letters. For example, it distinguishes the
difference between the words time, Time, TIME, timE, and tImE.
• Can only contain lower and upper case letters (a-z, A-Z), numbers
(0-9), and the special characters, dollar sign and underscore. No other
characters are allowed,
The names tom&jerry and rate*time are invalid because they contain
the illegal characters & and * respectively. The name 14thday is illegal
because it starts with a number. The name day of week is illegal
because it contains blanks.
Meaningful names
You should use mnemonic names for identifiers.This means that the
names should be descriptive of what they are representing. Sometimes
this requires that we combine multiple words together; however, Java does
not allow blanks in an identifier.
Camel Case
One of the rules for identifier names is that they can not contain blanks. To
overcome this restriction a common convention called “camel case” is
used by most Java programmers to create meaningful identifiers that
contain multiple words. This is done by stringing the words together
without blanks and capitalizing the first letter of each word after the first
word (e.g., currentRetailPrice,timeOfDay,totalWeight,etc).
Naming conventions
Java programmers follow specific conventions when naming variables,
functions, classes, and constants. This allows those reading the code to
immediately identify what type of identifier the name is referring to.
Literals
There are times when you want the computer to treat what you type as a
literal value and not be interpreted as an identifier or a keyword in a
command. The way you do this is different for numbers, individual
characters and a text string.
Integer and floating point (decimal numbers) are typed as regular numbers.
Never use a currency symbol (e.g., $) or commas in a literal number.
Individual characters must be surrounded by single quotes ('B'). Text
strings can must be surrounded by double quotes ("Hello world!").
Comments
It is a good programming practice to document your code by adding
comments. This makes your code easier to read by others and is
particularly useful when you have to make changes to your code after you
have not seen it for significant period of time. There are three different
types of comments.
Block Used for a comment where the text will flow over
multiple lines. The comment start with /* and ends with
*/.
Block comment
Documentation
comment
Line comment
1. Start by download the latest Java Development Kit from the Internet at:
http://www.oracle.com/technetwork/java/javase/downloads/index.html
2. Select the button to download the JDK. Accept the License Agreement
and then select the download file associated with the version of the
operating system that you are using.
3. Once the file is downloaded, locate the file and double click on the file
to install the JDK on your machine. Take all of the defaults during the
installation process.
Microsoft Windows
1. Locate and find file path of the JDK bin directory using File Explorer.
Normally, this will be found under the C:\Program Files\Java directory
and look similar to this except that the version number (e.g..
jdk1.8.0_91) may be different.
C:\Program Files\Java\jdk1.8.0_91\bin
4. Scroll down and select the Path environment variable under the
System Variables section and then select the Edit button.
5. Type a semicolon at the end of the value and then type the fully
qualified path of the JDK bin directory as illustrated in the example
below.
C:\WINDOWS\system32;C:\WINDOWS;C:\Program
Files\Java\jdk1.8.0_91\bin
Mac
1. Locate and find the file path of the JDK bin directory in the Finder
program. Normally, this will be in the following folder except that the
value of the version number (e.g., jdk1.8.0_91.jdk) may be
different.
/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/
Contents/Home/bin
vi ./.bash_profile
3. Use the arrow keys to move down to the bottom of the file and then
type the letter ‘O’. This put you into insert mode.
4. Type the following command on one line. Use the fully qualified path
of the JDK bin directory you located in step 1.
5. Press the esc key and type the colon key ‘:’ and. This puts you into
command mode.
6. Save and quit the file by typing “wq” on the command line at the
bottom of the screen and pressing Enter.
1. Create the source files to define the class for your program. The source
file is always the same name as the class and is saved with a file
extension of .java.
2. Compile the source files. The compiler is a program that checks the
syntax of all of the statements in a source file. It makes sure that none
of rules of the language language have been violated (e.g., missing curly
brace, a word is misspelled, etc.). It is very similar to running a grammar
checker in a word processing program. The compiler then translates the
code from Java into a machine language called bytecodes if there are
no syntax errors, and creates a file with the same name as the class
followed by the file extension .class.
3. Run the program. Execute the bytecodes .class file with the main()
function in the Java Virtual Machine (JVM).
Text Editor
You will need a text editor to create the source files for the Java classes in
your program. There are some very good text editors that you can
download and install for free from the Internet. Search the Internet using
the keywords “text editors for Windows” or “text editors for mac” to find a
list of text editors that you can download. Two good free open source
editors are Notepad++ for Microsoft Windows and TextWrangle for Mac.
Both Windows and Mac systems also ship a very rudimentary text editors
programs if you do not want to download one from the Internet. (i.e.,
NotePad on windows, TextEdit on mac).
Lets start by creating the source file for the ConvertWeight class and
add a main() function to it. The main() function will initially have only
one statement to print out the welcome message. We will then compile
and run the program to test to see if it works correctly. The remaining
functionality will be added later.
a. First view the contents of the current working directory to see if the
file is in the current working directory
>dir
03/20/2015 10:03 AM <DIR> .
03/20/2015 10:03 AM <DIR> ..
03/20/2015 10:15 AM <DIR> Contacts
03/20/2015 10:15 AM <DIR> Desktop
03/20/2015 10:17 AM <DIR> Documents
03/20/2015 10:18 AM <DIR> Downloads
03/20/2015 10:19 AM <DIR> Links
03/20/2015 10:20 AM <DIR> Music
03/20/2015 10:21 AM <DIR> Pictures
03/20/2015 10:22 AM <DIR> Videos
>ls
Applications Library
Desktop Movies
Documents Music
Downloads Pictures
b. If you do not see the file in the current working directory you will
need to using the cd command to navigate down the hierarchy to the
directory (folder) where you saved the ConvertWeight.java file. In
the example below, the file was saved in the Desktop directory so
we typed the following command.
>cd Desktop
c. Again view the contents of the directory using either the dir
(Windows) or ls (Mac, Linux) command to see if the file is in the
directory you just changed to.
>dir
03/20/2015 10:15 AM <DIR> .
03/20/2015 10:15 AM <DIR> ..
03/22/2015 09:45 PM <DIR> apps
04/22/2015 10:25 AM ConvertWeight.java
The javac command checks for syntax errors. It then translates the
Java code into bytecodes if there are no errors and saves the result in a
file called ConvertWeight.class. A command prompt is then
displayed indicating that no syntax errors were found in your program.
If your program compiled successfully without errors, list the files in the
current directory using either the dir (Windows) or ls (Mac or Linux)
command to make sure that the ConvertWeight.class file was
created.
Windows
Mac
Go to the line number (14) listed in the message. Check the line
specified and/or the previous line for a missing semicolon and then
modify the source code to add a semicolon at the end this statements.
Start searching backward and make sure that there is a right curly
brace for every left curly brace in the file, and that there is an ending
quote for every beginning quote in the file.
Your program should execute successfully and display the following text in
the console.
javac ConvertWeight.java
Modify your source file and recompile until there are no more syntax
errors.
java ConvertWeight
Your program should execute successfully and display the following in the
console.
******************************************************************
Fred Flintstone, The weight is 68.0388 kilograms
******************************************************************