1 problem solving b the purpose of writing a program is to solve a problem b the general steps in...
TRANSCRIPT
1
Problem SolvingProblem Solving
The purpose of writing a program is to solve a problemThe purpose of writing a program is to solve a problem
The general steps in problem solving are:The general steps in problem solving are:
• Understand the problemUnderstand the problem
• Dissect the problem into manageable piecesDissect the problem into manageable pieces
• Design a solutionDesign a solution
• Consider alternatives to the solution and refine itConsider alternatives to the solution and refine it
• Implement the solutionImplement the solution
• Test the solution and fix any problems that existTest the solution and fix any problems that exist
2
Problem SolvingProblem Solving
Many software projects fail because the developer didn't Many software projects fail because the developer didn't really understand the problem to be solvedreally understand the problem to be solved
We must avoid assumptions and clarify ambiguitiesWe must avoid assumptions and clarify ambiguities
As problems and their solutions become larger, we must As problems and their solutions become larger, we must organize our development into manageable piecesorganize our development into manageable pieces
This technique is fundamental to software developmentThis technique is fundamental to software development We will dissect our solutions into pieces called classes and We will dissect our solutions into pieces called classes and
objects, taking an objects, taking an object-oriented approachobject-oriented approach
3
The Java Programming The Java Programming LanguageLanguage
A A programming languageprogramming language specifies the words and symbols specifies the words and symbols that we can use to write a programthat we can use to write a program
A programming language employs a set of rules that dictate A programming language employs a set of rules that dictate how the words and symbols can be put together to form how the words and symbols can be put together to form valid valid program statementsprogram statements
Java was created by Sun Microsystems, Inc.Java was created by Sun Microsystems, Inc. It was introduced in 1995 and has become quite popularIt was introduced in 1995 and has become quite popular It is an object-oriented languageIt is an object-oriented language
4
Java Program StructureJava Program Structure
In the Java programming language:In the Java programming language:• A program is made up of one or more A program is made up of one or more classesclasses
• A class contains one or more A class contains one or more methodsmethods
• A method contains program A method contains program statementsstatements
These terms will be explored in detail throughout the These terms will be explored in detail throughout the coursecourse
A Java application always contains a method called A Java application always contains a method called mainmain
See See Lincoln.javaLincoln.java (page 26)(page 26)
5
Java Program StructureJava Program Structure
public class MyProgram
{
}
// comments about the class
class headerclass header
class bodyclass body
Comments can be added almost anywhereComments can be added almost anywhere
6
Java Program StructureJava Program Structure
public class MyProgram
{
}
public static void main (String[] args)
{
}
// comments about the class
// comments about the method
method headermethod headermethod bodymethod body
7
CommentsComments
Comments in a program are also called Comments in a program are also called inline inline documentationdocumentation
They should be included to explain the purpose of the They should be included to explain the purpose of the program and describe processing stepsprogram and describe processing steps
They do not affect how a program worksThey do not affect how a program works Java comments can take two forms:Java comments can take two forms:
// this comment runs to the end of the line
/* this comment runs to the terminating symbol, even across line breaks */
8
IdentifiersIdentifiers
IdentifiersIdentifiers are the words a programmer uses in a program are the words a programmer uses in a program
An identifier can be made up of letters, digits, the An identifier can be made up of letters, digits, the underscore character (_), and the dollar signunderscore character (_), and the dollar sign
They cannot begin with a digitThey cannot begin with a digit
Java is Java is case sensitivecase sensitive, therefore, therefore Total Total andand total total are are different identifiersdifferent identifiers
9
IdentifiersIdentifiers
Sometimes we choose identifiers ourselves when writing a Sometimes we choose identifiers ourselves when writing a program (such as program (such as LincolnLincoln))
Sometimes we are using another programmer's code, so we Sometimes we are using another programmer's code, so we use the identifiers that they chose (such as use the identifiers that they chose (such as printlnprintln))
Often we use special identifiers called Often we use special identifiers called reserved wordsreserved words that that already have a predefined meaning in the languagealready have a predefined meaning in the language
A reserved word cannot be used in any other wayA reserved word cannot be used in any other way
10
Reserved WordsReserved Words
The Java reserved words:The Java reserved words:
abstractbooleanbreakbytebyvaluecasecastcatchcharclassconstcontinue
defaultdodoubleelseextendsfalsefinalfinallyfloatforfuturegeneric
gotoifimplementsimportinnerinstanceofintinterfacelongnativenewnull
operatorouterpackageprivateprotectedpublicrestreturnshortstaticsuperswitch
synchronizedthisthrowthrowstransienttruetryvarvoidvolatilewhile
11
White SpaceWhite Space
Spaces, blank lines, and tabs are collectively called Spaces, blank lines, and tabs are collectively called white white spacespace
White space is used to separate words and symbols in a White space is used to separate words and symbols in a programprogram
Extra white space is ignoredExtra white space is ignored A valid Java program can be formatted many different A valid Java program can be formatted many different
waysways Programs should be formatted to enhance readability, Programs should be formatted to enhance readability,
using consistent indentationusing consistent indentation SeeSee Lincoln2.java Lincoln2.java andand Lincoln3.javaLincoln3.java
12
Programming Language LevelsProgramming Language Levels
There are four programming language levels:There are four programming language levels:• machine languagemachine language
• assembly languageassembly language
• high-level languagehigh-level language
• fourth-generation languagefourth-generation language
Each type of CPU has its own specific Each type of CPU has its own specific machine languagemachine language
The other levels were created to make it easier for a human The other levels were created to make it easier for a human being to write programsbeing to write programs
13
Programming LanguagesProgramming Languages
A program must be translated into machine language A program must be translated into machine language before it can be executed on a particular type of CPUbefore it can be executed on a particular type of CPU
This can be accomplished in several waysThis can be accomplished in several ways
A A compilercompiler is a software tool which translates is a software tool which translates source codesource code into a specific target languageinto a specific target language
Often, that target language is the machine language for a Often, that target language is the machine language for a particular CPU typeparticular CPU type
The Java approach is somewhat differentThe Java approach is somewhat different
14
Java Translation and ExecutionJava Translation and Execution
The Java compiler translates Java source code into a The Java compiler translates Java source code into a special representation called special representation called bytecodebytecode
Java bytecode is not the machine language for any Java bytecode is not the machine language for any traditional CPUtraditional CPU
Another software tool, called an Another software tool, called an interpreterinterpreter, translates , translates bytecode into machine language and executes itbytecode into machine language and executes it
Therefore the Java compiler is not tied to any particular Therefore the Java compiler is not tied to any particular machinemachine
Java is considered to be Java is considered to be architecture-neutralarchitecture-neutral
15
Java Translation and ExecutionJava Translation and Execution
Java sourcecode
Machinecode
Javabytecode
Javainterpreter
Bytecodecompiler
Javacompiler
16
Development EnvironmentsDevelopment Environments
There are many development environments which develop There are many development environments which develop Java software:Java software:• Sun Java Software Development Kit (SDK)Sun Java Software Development Kit (SDK)
• Borland JBuilderBorland JBuilder
• MetroWork CodeWarriorMetroWork CodeWarrior
• Microsoft Visual J++Microsoft Visual J++
• Symantec CaféSymantec Café
Though the details of these environments differ, the basic Though the details of these environments differ, the basic compilation and execution process is essentially the samecompilation and execution process is essentially the same
17
Syntax and SemanticsSyntax and Semantics
The The syntax rulessyntax rules of a language define how we can put of a language define how we can put symbols, reserved words, and identifiers together to make a symbols, reserved words, and identifiers together to make a valid programvalid program
The The semanticssemantics of a program statement define what that of a program statement define what that statement means (its purpose or role in a program)statement means (its purpose or role in a program)
A program that is syntactically correct is not necessarily A program that is syntactically correct is not necessarily logically (semantically) correctlogically (semantically) correct
A program will always do what we tell it to do, not what we A program will always do what we tell it to do, not what we meantmeant to tell it to do to tell it to do
18
ErrorsErrors
A program can have three types of errorsA program can have three types of errors
The compiler will find problems with syntax and other The compiler will find problems with syntax and other basic issues (basic issues (compile-time errorscompile-time errors))• If compile-time errors exist, an executable version of the program is If compile-time errors exist, an executable version of the program is
not creatednot created
A problem can occur during program execution, such as A problem can occur during program execution, such as trying to divide by zero, which causes a program to trying to divide by zero, which causes a program to terminate abnormally (terminate abnormally (run-time errorsrun-time errors))
A program may run, but produce incorrect results (A program may run, but produce incorrect results (logical logical errorserrors) )
19
Introduction to GraphicsIntroduction to Graphics
The last one or two sections of each chapter of the textbook The last one or two sections of each chapter of the textbook focus on graphical issuesfocus on graphical issues
Most computer programs have graphical componentsMost computer programs have graphical components
A picture or drawing must be digitized for storage on a A picture or drawing must be digitized for storage on a computercomputer
A picture is broken down into pixels, and each pixel is A picture is broken down into pixels, and each pixel is stored separatelystored separately
20
Representing ColorRepresenting Color
A black and white picture can be stored using one bit per A black and white picture can be stored using one bit per pixel (0 = white and 1 = black)pixel (0 = white and 1 = black)
A color picture requires more information, and there are A color picture requires more information, and there are several techniques for representing a particular colorseveral techniques for representing a particular color
For example, every color can be represented as a mixture For example, every color can be represented as a mixture of the three primary colors Red, Green, and Blueof the three primary colors Red, Green, and Blue
In Java, each color is represented by three numbers In Java, each color is represented by three numbers between 0 and 255 that are collectively called an between 0 and 255 that are collectively called an RGB valueRGB value
21
Coordinate SystemsCoordinate Systems
Each pixel can be identified using a two-dimensional Each pixel can be identified using a two-dimensional coordinate systemcoordinate system
When referring to a pixel in a Java program, we use a When referring to a pixel in a Java program, we use a coordinate system with the origin in the upper left cornercoordinate system with the origin in the upper left corner
Y
X(0, 0)
(112, 40)
112
40