Title: CPE-101
1CPE-101
- Clark Savage Turner, J.D., Ph.D.
- csturner_at_csc.calpoly.edu
- 756-6133
- Slides adapted for use with Kaufman and Wolz by
Clark S. Turner, and - Some lecture slides were adapted from those
developed by - John Lewis and William Loftus
- to accompany Java Software Solutions, 2d ed.
- Some slides adapted from slides by Carol
Scheftic, Cal Poly CSC Dept.
2Focus of the course
- Object-oriented software development
- problem solving
- program design and implementation
- object-oriented concepts
- objects
- classes
- interfaces
- inheritance
- polymorphism
- graphics and Graphical User Interfaces
- the Java programming language
3Programs are written to solve problems!
- The general steps in problem solving are
- Understand the problem
- What is the problem? What is not part of the
problem? - Dissect the problem into manageable pieces.
- Design a solution
- Have you seen this problem before? Have you seen
a similar one? - Can you restate the problem? Can you solve it?
Part of it? - Consider alternatives to your first solution and
refine it. - Implement the solution
- Check each step as you proceed. Test the final
solution. - If necessary, fix any problems that exist, and
repeat the previous step. - Reflect on what you learned from the problem
- What did you learn from the problem? the
solution? - What did you learn from any difficulties you
encountered?
4More on Problem Solving
- Many software projects fail because the developer
didn't really understand the problem to be
solved. - We must avoid assumptions and clarify
ambiguities. - As problems and their solutions become larger, we
must organize our development into manageable
pieces. - This technique is fundamental to software
development. - We will dissect our solutions into pieces called
classes, objects and methods, taking an
object-oriented approach.
5Brief Java History
- 1990-95 James Gosling at Sun began to develop a
new programming language for consumer electronics
software. - It was originally known as Oak
- It was small, reliable, and architecture
independent. - 1990-94 Tim Berners-Lee at CERN in Geneva began
to develop the World Wide Web. - It was powerful, global, and architecture
independent. - 1993 The Java team wrote a browser called
HotJava. - It was the first web browser to support Java
applets. - It demonstrated the power of Java to the rest of
the programming world.
6But what is Java?
- A high-level programming language.
- Object-oriented.
- Architecture-neutral and portable.
- Sort of interpreted, sort of compiled.
- CAUTION
- 101-102-103 now focus on object-oriented (OO)
design. - C permits an OO approach, but...
- Java REQUIRES an OO approach!
7Interpret or Compile?
- BASIC is interpreted
- High-level source code is run by an interpreter
on a specific microprocessor. - Every time the program is run, it must be
re-interpreted. - C and C are compiled
- High level source code is run through a compiler.
- Each compilation is specific to a given
microprocessor. - To run the program, you run the appropriate
compiled code. - Java is a hybrid
- High level source code is run through a compiler
to create bytecode. - To run the bytecode, you can
- Interpret that through a specific Java bytecode
interpreter, OR - Compile that into specific machine-code.
8Compilers and Interpreters
- We write source code files in the Java language.
Human beings can read those files. - We then compile the source file down to Java
bytecode using a Java compiler - javac MyFile.java yields
MyFile.class - Our Java bytecode file can be run in one of two
ways - It can be run on a Java interpreter.
- It can be further compiled to native machine code
for a particular machine.
9Executing a Java Program
- When someone invokes the java MyFile command...
- The Java runtime environment first invokes a
class loader. - The class loader loads the bytecodes for all the
required classes from disk. - Once they are loaded, two operations are
performed - A bytecode verifier confirms that all bytecodes
are valid. - All bytecodes are checked to see that they do not
violate Javas security restrictions. - The bytecodes are then passed to
- A Java interpreter,
- Or to a Java just-in-time compiler (faster
execution).
10Java Translation and Execution
Java source code
Java bytecode
Java compiler
Java program interpreter
Bytecode compiler
Machine code
11Programming Language Levels
- Four levels of programming language
- machine language
- assembly language
- high-level language
- procedural (e.g., Algol, Basic, C, Cobol,
Fortran, Interlisp, Pascal) - functional (e.g., APL, Lisp, Scheme)
- parallel (e.g., High Performance Fortran, Linda)
- logic (e.g., Duck, Prolog)
- combos (e.g., proceduralparallelAda or
proceduralOOLoops) - object-oriented (e.g., C, Java, Scheme, Visual
Basic) - fourth-generation language
- Each type of CPU has its own specific machine
language - The levels beyond that were created to make it
easier for a human being to write programs
12Java Development Environments
- There are many development environments that
develop Java software - Sun Java Software Development Kit (SDK)
- Borland JBuilder
- MetroWork CodeWarrior
- Symantec Café
- Microsoft Visual J
- Though the details of these environments differ,
the basic compilation and execution process is
essentially the same.
13Syntax and Semantics
- The syntax rules of a language define how we can
put symbols, reserved words, and identifiers
together to make a valid program. - The semantics of a program statement define what
that statement means (its purpose or role in a
program). - A program that is syntactically correct is not
necessarily logically (semantically) correct. - A program will always do what we told it to do,
not what we meant to tell it to do.
14Java Program Structure
- In the Java programming language
- A program is made up of one or more classes
- A class contains one or more methods
- A method contains program statements
- These terms will be explored in detail throughout
the course - A Java application always contains a method
called main - (A Java applet does not)
15Java Program Structure
// comments about the class
public class MyProgram
class header
class body
Comments can be added almost anywhere
16Java Program Structure
// comments about the class
public class MyProgram
// comments about the method
public static void main (String args)
method header
method body
17Identifiers
- Identifiers are the words a programmer uses in a
program. - An identifier can be made up of letters, digits,
the underscore character (_), and the dollar
sign. - They cannot begin with a digit.
- Java is case sensitive, therefore Total and total
are different identifiers. - this makes for many simple to make, hard to find,
errors
18Identifiers
- Sometimes we choose identifiers ourselves when
writing a program. - Sometimes we are using another programmer's code,
so we use the identifiers that they chose (such
as println). - does it come with the language?
- no, it is an example of reuse of code
(libraries) - Often we use special identifiers called reserved
words that already have a predefined meaning in
the language itself. - a reserved word cannot be used in any other way.
19Reserved Words
abstract boolean break byte byvalue case cast catc
h char class const continue
default do double else extends false final finally
float for future generic
goto if implements import inner instanceof int int
erface long native new null
operator outer package private protected public re
st return short static super switch
synchronized this throw throws transient true try
var void volatile while
20Comments
- Comments in a program are also called inline
documentation - They should be included to explain the purpose of
the program and describe processing steps - They do not affect how a program works
- Java comments typically take one of three forms
// this comment runs to the end of the line
/ this comment runs to the terminating
symbol, even across line breaks /
/ this comment form, which we will not use
yet, is for the javadoc system /
21White Space
- Spaces, blank lines, and tabs are collectively
called white space. - White space is used to separate words and symbols
in a program. - Extra white space is useful to humans, and
ignored by machines. Use lots of it!!! (And
curly braces too.) - A valid Java program can be formatted many
different ways. - Programs should be formatted to enhance
readability, using consistent indentation. - Coding standards are enforced for many projects
- It is quite a serious business
- consider code maintenance
22Naming Conventions
- Give identifiers semantic meaning.
- Recall that programmers write identifiers for a
purpose - How does this work with comments in the code?
- Make identifiers easy to read r versus radius
- Follow consistent capitaliztion
- Use lowercase for variable and method names,
except for the first letter of later words
currentTemperature - All reserved words must be in lower case.
- Can you use a capitalized reserved word any way
you like? Try this. Why would you ever want to
do it? - Use Title Case for class, package, and interface
names TemperatureConverter - Use UPPERCASE for constants, separating words
with an underscore ABSOLUTE_ZERO
23Three Types of Program Errors
- Compile-time errors problems with syntax and
other basic issues that are found by the
compiler. - If compile-time errors exist, an executable
version of the program is not created - in our case, bytecode is not produced, right?
- Run-time errors a problem that occurs during
program execution and causes a program to
terminate abnormally, such as trying to divide by
zero. - Logical Errors a program may run, but still
produce incorrect results. - What would you suspect incorrect means in this
case?