java fundamentals - purdue university fort wayne
TRANSCRIPT
CHAPTER 2
Java
Fundamentals
2-2
Chapter Topics
Chapter 2 discusses the following main topics:
– The Parts of a Java Program
– The print and println Methods, and the
Java API
– Variables and Literals
– Primitive Data Types
– Arithmetic Operators
– Combined Assignment Operators
2-3
Chapter Topics (Continued)
– Creating named constants with final
– The String class
– Scope
– Comments
– Programming style
– Using the Scanner class for input
– Dialog boxes
Parts of a Java Program
■ Example program:
This area is the body of the class Simple.
All of the data and methods for this class
will be between these brackets.
Analyzing The Example
This is a Java comment. It is ignored by
the compiler.
This is the class header
for the class Simple
Analyzing The Example
This is the method header for the main
method.
The main method is where a Java
application begins.
Analyzing The Example
This area is the body of the main method.
All of the instructions to be completed during
the main method will be between the
brackets.
Analyzing The Example
This is the Java statement that
is executed when the program
runs.
Programming LanguagesCommon Language Elements
■ There are some concepts that are common to virtually all programming languages.
■ Common concepts:
– Keywords
– Operators
– Punctuation
– Programmer-defined identifiers
Programming LanguagesSample Program
Keywords
Programmer-Defined Names
Operators Punctuatio
n
Language ElementsKeywords
■ Keywords are reserved and cannot be used for anything other than their designated purpose.
■ For example, the int keyword signifies an integer variable is going to be declared.
■ Key words are lowercase (Java is a case sensitive language).
Language ElementsKeywords
Language ElementsProgrammer-Defined Names
■ These words or names are defined by the programmer.
■ They are not part of the Java language.
■ Using sensible programmer-defined names increases the readability of programs.
Language ElementsProgrammer-defined Names
Language ElementsOperators
■ The operators perform various operations on data known as operands.
■ Examples:– + * / =
Languages ElementsOperators
■ Semicolons are used to end Java statements; however, not all lines of a Java program end a statement.
■ Part of learning Java is learning where to properly use the punctuation.
Language ElementsPunctuation
Languages ElementsPunctuation
Lines vs Statements
■ There are differences between lines and statements when discussing source code.
■ 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.
■ All statements end with a semicolon → ;
Where we don’t place a semicolon■ Comments are ignored by the Java compiler
so they do not need a semicolon.
■ Other Java code elements that do not need semicolons include:– class headers
■ Terminated by the code within its curly braces.
– method headers
■ Terminated by the code within its curly braces.
– brackets
■ Part of language framework (syntax) that does not need semicolon termination.
Programming LanguagesVariables
■ What are the values of the variable a and b, after executing each statement.
int a, b;
a = 3;
b = 5;
a = a + b;
b = b * a;
■ What are the values of the variable a and b, after executing each statement.
int a, b;
a = 3;
b = 5;
a = a + b;
b = b * a;
a b
3
3 5
8 5
408
Programming LanguagesVariables
Programming LanguagesVariables
■ Variables are the primary way a Java program stores an item of data.
■ Need a data type and a name.
■ Examples:
Programming LanguagesVariables
■ The following is a program expressed as English statements. What would display on the screen?
– The variable x starts with the value 0.
– The variable y starts with the value 5.
– Add 1 to x.
– Add 1 to y.
– Add x and y, and store the result in y.
– Display the value in y on the screen.
Programming LanguagesVariables – Answer!
■ The following is a program expressed as English statements. What would display on the screen?
■ The answer is 7!
Live Demos
■ Instructions:
The variable a starts with the value 10.
The variable b starts with the value 2.
The variable c starts with the value 4.
Store the value of a times b in a.
Store the value of b times c in c.
Add a and c, and store the result in b.
Display the value of b on the screen.
Live Demos - Critical Thinking
■ You tell me:
How would we swap the values in variables a, b, c such that:
a = b, b = c, c = a?
Live Demos - Critical Thinking
■ You tell me:
How would we swap the values in variables a, b, c such that:
a = b, b = c, c = a?
Hint: placeholder
Live Demos
■ Swapping variable values:
– How would we write the code:
– Given a = 5, b = 8, c = 9
– a = b, b = c, c = a
– Print out the proof with System.out.println()
Live Demos
■ Let’s consider a problem, rather than a list of steps:
Fred likes to buy oranges and likes to use Java to write simple programs. Fred is not sure how he would write a program to multiply oranges by their price to plan his orange budget.
Fred’s Oranges
■ Our problem is this:– Find a way to label the oranges
– Find a way to label the price
– Perform the multiplication
– Inform Fred of his needed orange budget
■ What do we need from Fred?– The oranges he wants to buy
– The price of them
■ Let’s try and write the output for a given orange count and price
Fred’s Oranges
■ Break the story down into steps:
declare int orangesdeclare double pricedeclare double budgetdisplay the budget to Fred
Am I missing anything?
Fred’s Oranges – logic errors
■ Even if it seems obvious, it is always a good idea to state every major step!
■ Otherwise, we may forget and make an error
■ Declare and set int orangesDeclare and set double priceDeclare double budget
Set budget = oranges * price
Display the budget to Fred
Now let’s write and run the code!
What we just did – secretly the programming process!
1. Clearly define what the program is to do
1. Purpose: Calculate Fred’s orange budget
2. Input: Orange price and number of oranges
3. Process: Multiply price by the orange number
4. Output: Display a message with Fred’s budget
2. Visualize the program running
1. We wrote it on the board
3. Use design tools to create a model
1. Psuedocode in the slides
4. Check the model for logical errors
4. We forgot to set our variable values
5. Write the code and compile it
6. Correct any errors found during compilation, repeat steps 5-6 as needed
7. Run the program with test data
8. Correct any runtime errors found, repeat 5-8 as needed
9. Validate the results
What we just did – secretly the programming process!
Live Demo – Story
■ Another problem:
Jim is not very smart, because he purchased an overpriced PS4 for $800 from a man of questionable morals. The tax rate for the purchase was 7%. Write pseudocode for a program that displays the total sales price and the sales tax.
Another story, with partners
■ Jim is still not a very smart. After buying one piece of technology from a man with bad morals, he wants to buy more. Using the programming process discussed, let’s plan how Jim can write a program to figure out how much he needs to spend to buy:
■ Object a for $600, two of object b for $700, and object c for $50
Programming style
Procedural Programming
• Older programming languages were procedural.
• A procedure is a set of programming language statements that, together, perform a specific task.
• Procedures typically operate on data items that are separate from the procedures.
• In a procedural program, the data items are commonly passed from one procedure to another.
Procedural Programming
Procedure A
Data Element
Procedure B
Procedural Programming
• In procedural programming, procedures are
developed to operate on the program’s data.
• Data in the program tends to be global to the
entire program.
• Data formats might change and thus, the
procedures that operate on that data must
change.
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.
Object-Oriented ProgrammingObject
Attributes (data)
Methods
(behaviors / procedures)
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 object’s 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.
Object-Oriented Programming
ObjectAttributes (data)
typically private to this object
Methods
(behaviors / procedures)
Other
objects
Programming
Interface
How this all happens –hardware and software
■ Hardware refers to the physical components that make up a system
– CPU, Main Memory, Storage, Input & Output
■ Software is made up of the programs
– The OS -> manage hardware and devices
– Applications -> things that are directly useful to the user
Computer System Setup
input memory output
ALU CU
Arithmetic
Logic
Unit
Control
Unit
CPU
• Variables are simply a name given to represent a place in memory.
So how do variables work?
0x000
0x001
0x002
0x003
0x004
0x005
0x006
0x007
40
Assume that thisvariable declaration
has been made.int hours;
hours = 40;
The variable hours
is a symbolic name
for the memory
location 0x004.
Main Memory
■ Commonly known as random-access memory (RAM)
■ RAM is divided into units called bytes.
■ Each byte in memory is assigned a unique number known as an address.
■ A byte consists of eight bits that may be either on or
off(switch).1 1 1 1
0 0 0 0A byte is made up of 8 bits.0 1 0 0 1 1 0 1