CHAPTER 1
Introduction to
Computers
and Java
Copyright © 2016 Pearson Education, Ltd.
Objective
• Transition students with a background in structural
programming to the use of object oriented
programming (OOP) and design concepts.
– Learn principles and features of object oriented design and
programming. ·
– Use inheritance, encapsulation, abstraction and
polymorphism. ·
– Using Java classes, objects and interfaces.
– Become familiar with the most essential classes of the Java
API.
©2016 Pearson Education, Ltd.
Textbook
• Starting out with JAVA” by Tony Gaddis (6th
Edition)
©2016 Pearson Education, Ltd.
Mark distribution
Assessment Grade
Course project 20%
Lab (2 quizzes, 5 marks for each) 10%
Midterm 25%
Final exam 45%
©2016 Pearson Education, Ltd.
IMPORTANT NOTES
• Project (20 points):
– Details will be announced later
• Assignments (0 point):
– Will be given at the end of each chapter.
– Assignments’ solutions will not be given, you have to solve it by yourself and discuss
with your instructor. There will be some online tutorials for discussion.
• Lab tasks (0 point):
– Attendance is REQUIRED. If you don’t show up for lab sessions, there will be a
deduction of point in your total mark of the course.
– Lab tasks’ solutions will not be given, you have to solve it by yourself during lab
sessions and discuss with instructor.
• 2 Lab Quizzes (10 points)
• Exams:
– Mid Exam (25 points): (Exact dates will be announced before that)
– Final Exam (45 points): (Exact dates will be announced before that)
– Exams questions are similar to assignments and lab tasks.
©2016 Pearson Education, Ltd.
IMPORTANT NOTES
• Cheating on the project, lab quizzes, or midterm will result in an
automatic 0; there will be No WARRING!
• Cheating in the final test will result in an F (fail) in your course,
even if your remaining marks are over 60.
• Possession of the mobile during any test is considered cheating,
even if there is no proof of cheating in the mobile.
• Copying work from another student and those who give their work
to others will be considered cheating, and the punishment will be
applied to them.
• Copying work from the Internet, a book, or any place is cheating.
©2016 Pearson Education, Ltd.
TOPICS COVERAGE DURATIONS
Topic Number of lectures
Introduction to Computers and Java 1
Java Fundamentals 3
Decision Structures 3
Loops and Files 3
Methods 3
A First Look at Classes 6
Arrays and the ArrayList Class 5
A Second Look at Classes and Objects 6
Inheritance 9
©2016 Pearson Education, Ltd.
Chapter Topics
Chapter 1 discusses the following main topics:
– Introduction
– Programming Languages
– What Is a Program Made Of?
– Object-Oriented Programming
©2016 Pearson Education, Ltd. 1-8
Java History
• 1991 - Green Team started by Sun
Microsystems (now owned by Oracle).
• Their first project was named *7 Handheld
controller for multiple entertainment systems.
• There was a need for a programming language
that would run on various devices.
• Java (first named Oak) was developed for this
purpose.
©2016 Pearson Education, Ltd. 1-9
Introduction
• Java enabled web browser (HotJava)
demonstrated at 1995 Sun World conference.
• Java incorporated into Netscape shortly after.
• Java is “cross platform”, meaning that it can run
on various computer operating systems.
©2016 Pearson Education, Ltd. 1-10
Java Applications and Applets
• Java programs can be of two types:
– Applications
• Stand-alone programs that run without the aid of a web
browser.
• Relaxed security model since the user runs the program
locally.
– Applets
• Small applications that require the use of a Java enabled
web browser to run.
• Enhanced security model since the user merely goes to a
web page and the applet runs itself.
©2016 Pearson Education, Ltd. 1-11
Programming Languages
Common Language Elements
• There are some concepts that are common to
virtually all programming languages.
• Common concepts:
– Key words
– Operators
– Punctuation
– Programmer-defined identifiers
– Strict syntactic rules.
©2016 Pearson Education, Ltd. 1-12
Java
Sample Program
public class HelloWorld
{
public static void main(String[] args)
{
String message = "Hello World";
System.out.println(message);
}
}
©2016 Pearson Education, Ltd. 1-13
Java
Sample Program
• Key words in the sample program are:
•public •static
•class •void
• Key words are lower case (Java is a case
sensitive language).
• Key words cannot be used as a programmer-
defined identifier.
• Look at page 10 in the book for more java
keywords.
©2016 Pearson Education, Ltd. 1-14
Java
Sample Program
• Semi-colons are used to end Java statements;
however, not all lines of a Java program end a
statement.
System.out.println(
message);
• This is one Java statement written using two
lines. Do you see the difference?
• A statement is a complete Java instruction that
causes the computer to perform an action.
©2016 Pearson Education, Ltd. 1-15
Java
Variables
• Data in a Java program is stored in memory.
• Variable names represent a location in memory.
• Variables in Java are sometimes called fields.
• Variables are created by the programmer who assigns
it a programmer-defined identifier.
example: int hours = 40;
• In this example, the variable hours is created as an
integer and assigned the value of 40.
©2016 Pearson Education, Ltd. 1-16
Programming Languages
Variables
Assume that the this
variable declaration
0x000 has been made.
The Java Virtual 0x001 int length = 72;
Machine (JVM) 0x002
actually decides 0x003 72
where the value 0x004 The variable length
will be placed 0x005 is a symbolic name
in memory. 0x006 for the memory
location 0x003.
0x007
©2016 Pearson Education, Ltd. 1-17
The Compiler and the Java Virtual
Machine
• A programmer writes Java programming
statements for a program.
• These statements are known as source
code.
• A text editor is used to edit and save a Java
source code file.
• Source code files have a .java file extension.
• A compiler is a program that translates
source code into an executable form.
©2016 Pearson Education, Ltd. 1-18
The Compiler and the Java Virtual
Machine
• A compiler is run using a source code file as
input.
• Syntax errors that may be in the program will
be discovered during compilation.
• Syntax errors are mistakes that the programmer
has made that violate the rules of the
programming language.
• The compiler creates another file that holds the
translated instructions.
©2016 Pearson Education, Ltd. 1-19
The Compiler and the Java Virtual
Machine
• Most compilers translate source code into
executable files containing machine code.
• The Java compiler translates a Java source file
into a file that contains byte code instructions.
• Byte code instructions are the machine
language of the Java Virtual Machine (JVM)
and cannot be directly executed directly by the
CPU.
©2016 Pearson Education, Ltd. 1-20
The Compiler and the Java Virtual
Machine
• Byte code files end with the .class file
extension.
• The JVM is a program that emulates a micro-
processor.
• The JVM executes instructions as they are read.
• JVM is often called an interpreter.
• Java is often referred to as an interpreted
language.
©2016 Pearson Education, Ltd. 1-21
Program Development Process
Saves Java statements
Text editor Source code
(.java)
Produces Byte code
Java compiler (.class)
Java Results in Program
Virtual Execution
Machine
©2016 Pearson Education, Ltd. 1-22
Portability
• Portable means that a program may be written on one
type of computer and then run on a wide variety of
computers, with little or no modification.
• Java byte code runs on the JVM and not on any
particular CPU; therefore, compiled Java programs are
highly portable so that programmers do not have to
recompile for different platforms.
• JVMs exist on many platforms:
•Windows •Unix
•Mac •BSD
•Linux •Etc.
©2016 Pearson Education, Ltd. 1-23
Portability
Byte code
(.class)
Java Virtual Java Virtual
Machine for Windows Machine for Unix
Java Virtual Java Virtual
Machine for Linux Machine for Mac
©2016 Pearson Education, Ltd. 1-24
Java Versions
• The software you use to write Java programs is
called the Java Development Kit, or JDK.
• There are different editions of the JDK:
– Java SE - Java2 Standard Edition.
– Java EE - Java2 Enterprise Edition.
– Java ME - Java2 Micro Edition.
• Available for download at
http://java.oracle.com
©2016 Pearson Education, Ltd. 1-25
©2016 Pearson Education, Ltd.
Compiling a Java Program
• The Java compiler is a command line utility.
• The command to compile a program is:
javac filename.java
• javac is the Java compiler.
• The .java file extension must be used.
Example: To compile a java source code file named
Payroll.java you would use the command:
javac Payroll.java
©2016 Pearson Education, Ltd. 1-27
Object-Oriented Programming
• Object-oriented programming is centered on
creating objects rather than procedures.
• Objects are a melding of data and procedures
that manipulate that data.
• Data in an object are known as attributes.
• Procedures in an object are known as methods.
©2016 Pearson Education, Ltd. 1-28
Object-Oriented Programming
Object
Attributes (data)
Methods
(behaviors / procedures)
©2016 Pearson Education, Ltd. 1-29
Object-Oriented Programming
• Object-oriented programming combines data and
behavior via encapsulation.
• Data hiding is the ability of an object to hide data from
other objects in the program.
• Only an objects methods should be able to directly
manipulate its attributes.
• Other objects are allowed manipulate an object’s
attributes via the object’s methods.
• This indirect access is known as a programming
interface.
©2016 Pearson Education, Ltd. 1-30
Object-Oriented Programming
Object
Attributes (data)
Programming typically private to this object
Interface
Other
objects
Methods
(behaviors / procedures)
©2016 Pearson Education, Ltd. 1-31