CS0007: Introduction to Computer Programming Getting Started
with Java
Slide 2
Review A program is a set of instructions that tell a computer
what to do. Programs can also be called software. Hardware refers
to the physical components that a computer is made of. The CPU has
two parts: Control Unit Arithmetic and Logic Unit (ALU) A Memory
Address is a unique identifying number associated with a byte in
memory.
Slide 3
Review Input is any data the computer collects from the outside
world. Output is any data the computer sends to the outside world.
A User is a person who uses a computer program. Program Development
Cycle 1. Analyze Define the Problem 2. Design Plan a Solution for
the Problem 3. Write the Code Implement a solution An algorithm is
a precise sequence of steps to solve a problem
Slide 4
The Java Programming Language Java was created in 1991 by James
Gosling (Sun Microsystems) Why? They needed a programming language
that could be used to write a wide variety of applications and be
processed on very different devices. Traditional programming
languages cannot do this. Why? Most general-purpose programming
languages at that time were platform specific. oA General-Purpose
Programming Language is a programming language designed to be used
for writing software in a wide variety of application domains. oA
programming language is platform-specific, if once compiled, it can
only be guaranteed to be able to run on the machine it was compiled
for. Java is NOT platform-specific.
Slide 5
From Source Code to a Running Program Code of any programming
language must be saved into a file. This file contains the source
code for the program, and thus is called a source file. Source Code
is the human written code for a program. Java is known as a
high-level language. A High-Level Programming Language is a
language that is easily read and written by humans, and is needed
to be translated before a machine can use it. It provided a high
level of abstraction from the details of the workings of the
computers hardware Most programming by humans is done in high-level
languages The source file for a high-level language needs to be
translated into a form that a machine can understand through a
process called compilation. Compilation is the process of
translating high-level language into machine language for
platform-specific languages or some other intermediate code
representation for other languages. A program called a compiler
does this. Machine Language is a sequence of instructions directly
executed by a processing unit (CPU in most cases). When you compile
Java source code it results in Java Byte Code, NOT Machine
(Specific) Language
Slide 6
From Source Code to a Running Program Running a compiler
successfully on source code results in an executable file. An
Executable File is a file that can be directly executed or run by a
processing unit (again, most often the CPU) or run by another
program that executes the file. When a program executes (runs) the
instructions of that program are carried-out. Executable files
containing machine code can run without aid by a processing unit.
Executable files created by the Java compiler are in Java Byte
Code, which are not specific to any platform, and thus cannot be
run directly by a processing unit. Java executable files must be
run by a special program installed on a machine called the Java
Virtual Machine (JVM). The Java Virtual Machine (JVM) is a program
that reads instructions from Java Byte Code and executes them for
the platform that the JVM is installed on. The JVM is often called
an interpreter, because it interprets java byte code to be run on
whatever machine it is installed on. For this reason, Java is often
called an interpreted language, as opposed to a true compiled
language
Slide 7
From Source Code to a Running Program Steps to create and run
Java Program: 1. Write the source code in the Java programming
language. 2. Run the source code through the Java Compiler
(Interpreter) 3. Run the resulting Java Byte Code through the JVM
4. The programs executes!
Slide 8
Errors A compilation will not be successful if there are syntax
errors in the program. Syntax Errors are grammatical errors in the
Java programming language. Meaning, if you enter code that the java
compiler does not understand, it will not complete compiling. There
are two other kinds of errors that cannot be caught by the Java
compiler: Run-Time Errors Errors that cause the execution of a
program to end abnormally (crash). Logical Errors Errors in the
programmers logic that cause incorrect results.
Slide 9
Portability So, why was Java created again? So the programs
written in it can be run on different devices easily. This means
that Java programs are very easily portable. Portability in
programming means that a program can be written on one computer and
run on many different ones. In traditional compiled languages, you
must recompile your program from the source code for every
different machine you want to run it on. Tedious Can run into
problems between systems As long as the machine you want to execute
the program has a compatible version of the JVM, your Java program
can be run on it without recompiling it! If the JVM is correctly
installed, there is (almost) no problems.
Slide 10
Java Applications and Applets There are two kinds of programs
that can be created with Java: Application stand alone programs
that runs on a computer. Applets Small applications designed to be
transmitted over the internet and executed in a web browser. Most
web pages are written in Hypertext Markup Language (HTML) HTML is
not very sophisticated, Java allows for much of the power of a
normal application. Whats the main problem with allowing users to
create full-fledged applications that can be sent over the internet
and used in a web browser? Answer: Security! Applications have
access to memory and secondary storage. Solution: Applets run in a
secure part of memory and cannot access anything outside of that
unless the user specifically states so.
Slide 11
Procedural Programming There are two primary methods of
programming in use today The first is Procedural Programming a
programming methodology in which a program was made up of one of
more procedures. A procedure is a set of programming statements
that, together, perform a specific task. Typically, in this model,
data is passed between procedures. Here data and the code that acts
on it are entirely separate. Is there a problem with this? What if
the form of the data changes? If the data and the code acting on it
are completely separate, can it still function?
Slide 12
Object-Oriented Programming The response to this is
Object-Oriented Programming (OOP) a programming methodology in
which a programs parts are abstracted into the interaction between
Objects. An object is an abstraction of a real-world (or sometimes
not) entity that consists of data (attributes) and the procedures
that act upon the objects data (methods). Attributes can be seen as
the information kept about the object whether it be the current
state of the object or unchanging characteristics about it, and the
methods can be seen as the behavior of the object that change the
state. In Java the generalization of all the possible individual
objects of a kind is called a class. An individual instance of the
class is called an object. You can think of a class as being a
blueprint or recipe and an object as being a single result from the
blueprint or recipe.
Slide 13
Object Example Example Class: Cup Attributes: Total Volume
Current Volume Color Dimensions Methods: Fill Pour
Slide 14
Object-Oriented Exercise I want you to create a car class.
Think about what information about the state and characteristics of
a car can be kept. Also, think about what behaviors a car has that
change its state.
Slide 15
Encapsulation and Data Hiding Two important terms that describe
how OOP addresses the data/code separation that Procedural
Programming presents: Encapsulation refers to the combining of data
and code into a single object. Advantages: When the data in the
object changes, the methods that interact with the data can be
easily located and changed. Data Hiding refers to the objects
ability to hide its data from outside the object. Things outside of
the object (objects, or other code) can access and manipulate the
data inside of an object through the methods defined by the object.
This has many advantages: When using an object, you do not need to
know the details about how it was implemented, you just need to
know the methods needed to do a task. Outside sources can only
manipulate the data of an object in the ways the object designer
intends. In Java, almost everything is an object, but you can still
program procedurally.
Slide 16
Your First Java Program Hello World Every programming language
has a Hello World program. Its purpose is to do the most basic
functionality of a program: Simply display text. No computations No
information kept Just spit out text Often languages are compared by
the Hello World programs in terms of verbosity.
Slide 17
Compiling and Running From Command Line You can compile a java
source file from either a Windows or Unix command prompt with the
following command: javac YourProgramName.java This will create a
file called YourProgramName.class . This contains the Java Byte
Code for your program. You can run a java program from either a
Windows or Unix command prompt with the following command: java
YourProgramName Notes: Your source file should end in .java When
you run your Java program just use the name of your source file
WITHOUT .java
Slide 18
Hello World At a Glance Line 1: // Hello World Program This
line is called a comment A Comment is a line(s) in a program that
is ignored by the compiler entirely. Why do we have comments?
Include documentation inside of the code to allow other programmers
(or ourselves) to understand what the code does, who wrote it,
when, etc. To temporarily make lines of code not executed instead
of removing them outright Comments in Java start with // You can
have multiple line comments starting with /* and ending with */ /*
Here is a comment on one line......and it continues here */
Slide 19
Hello World At a Glance Line 2: Blank Blank lines, spaces, and
tabs are known as whitespace in programming. Java ignores
whitespace. Programmers often include whitespace in order to make
the code more readable.
Slide 20
Hello World At a Glance Line 3: public class HelloWorld { This
line is known as a class header. A Class Header starts a class
definition and describes certain features of a class. A Class
Definition includes all the attributes and methods for a class. In
Java almost everything is a class. Here, this class is a container
for our application. A program MUST have at least one class
definition. This line has three words and a character: public class
HelloWorld {
Slide 21
Hello World At a Glance public is a key word in Java that is
known as an access specifier. A Key Word (Reserved Word) are words
that have special meaning in a programming language and can only be
used for their intended purpose. An Access Specifier determines
where an element (a class in this case) can be accessed from. Here
public means that the class is unrestricted (open to the public).
We will talk more about access specifiers later. Note that public
is all lowercase. Java is case-sensitive. A language is
Case-Sensitive if it differentiates between the same elements with
different cases. For instance public and Public are not the same (
Public is not a keyword)
Slide 22
Hello World At a Glance class is another keyword indicating the
beginning of a class definition. HelloWorld is the name of the
class. This was determined by me, the programmer. It is known as
Programmer-Defined-Name or an identifier. A Programmer-Defined-Name
(Identifier) is a name created and used by the programmer. It
identifies some place in memory and parts in the program created by
the programmer. Identifiers follow naming rules which we will go
over later. I could just as easily named it Dog or Pizza or
Banana
Slide 23
Hello World At a Glance In short the three words tell the
compiler that we are creating a public class that is named Hello
World Notes on Classes: You may have multiple classes per source
file, but you may only have one public class per source file. If
there is a public class in a source file, the name of the file must
be the same name as the class (Note the HelloWorld class was in
HelloWorld.java ) The { indicates the beginning of the class
definition for the HelloWorld class. All of the lines of code
associated with the class are between it and the corresponding }
(Line 8) Everything inside the braces is called the body of the
class. Lines are grouped together as such are called a block.
Slide 24
Hello World At a Glance Line 5: public static void
main(String[] args) { This is known as a method header, because it
marks the beginning of a method. A Method can be thought of as a
group of one or more programming statements the collectively has a
name. This line needs to tell the compiler many things about the
method. LOTS going on here! At this point what I want you to focus
on is the name of the method main. Every Java application needs a
main method, this is where the execution of the program starts. For
now, just use this line to start the main method of all of your
Java applications (Sorry). Everything between the { and the } on
line 7 is the body of the method.
Slide 25
Hello World At a Glance Line 6: System.out.println("Hello
World"); This is the first line that actually does something of
purpose. In short, this displays the words Hello World on the
screen (without the quotation marks). The group of characters
inside of the quotation marks is called a string literal We will
talk more about literals and strings later. Note that the line ends
with a semicolon. This is part of the syntax of the Java language
that marks the end of a statement. The semicolon is known as a
terminating character in Java, because it terminates the statement.
The Syntax of a programming language determines the rules that must
be followed when writing the programs. Syntax is the grammatical
rules that need to be followed (punctuation in this case). A
Statement in a programming language is a line of code that ends in
a semicolon. Not every line ends with a semicolon, and at first it
may seem not clear at all when to use one and when not to. Here are
some cases for when not to use one (you should get a feel for it
with experience) Comments ignored by the compiler anyway Headers
terminated by body Braces simply not statements