java fundamentals - purdue university fort wayne
TRANSCRIPT
![Page 1: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/1.jpg)
CHAPTER 2
Java
Fundamentals
![Page 2: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/2.jpg)
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
![Page 3: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/3.jpg)
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
![Page 4: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/4.jpg)
Parts of a Java Program
■ Example program:
![Page 5: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/5.jpg)
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
![Page 6: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/6.jpg)
Analyzing The Example
This is the method header for the main
method.
The main method is where a Java
application begins.
![Page 7: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/7.jpg)
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.
![Page 8: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/8.jpg)
Analyzing The Example
This is the Java statement that
is executed when the program
runs.
![Page 9: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/9.jpg)
Programming LanguagesCommon Language Elements
■ There are some concepts that are common to virtually all programming languages.
■ Common concepts:
– Keywords
– Operators
– Punctuation
– Programmer-defined identifiers
![Page 10: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/10.jpg)
Programming LanguagesSample Program
Keywords
Programmer-Defined Names
Operators Punctuatio
n
![Page 11: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/11.jpg)
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).
![Page 12: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/12.jpg)
Language ElementsKeywords
![Page 13: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/13.jpg)
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.
![Page 14: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/14.jpg)
Language ElementsProgrammer-defined Names
![Page 15: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/15.jpg)
Language ElementsOperators
■ The operators perform various operations on data known as operands.
■ Examples:– + * / =
![Page 16: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/16.jpg)
Languages ElementsOperators
![Page 17: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/17.jpg)
■ 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
![Page 18: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/18.jpg)
Languages ElementsPunctuation
![Page 19: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/19.jpg)
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 → ;
![Page 20: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/20.jpg)
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.
![Page 21: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/21.jpg)
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;
![Page 22: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/22.jpg)
■ 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
![Page 23: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/23.jpg)
Programming LanguagesVariables
■ Variables are the primary way a Java program stores an item of data.
■ Need a data type and a name.
■ Examples:
![Page 24: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/24.jpg)
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.
![Page 25: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/25.jpg)
Programming LanguagesVariables – Answer!
■ The following is a program expressed as English statements. What would display on the screen?
■ The answer is 7!
![Page 26: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/26.jpg)
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.
![Page 27: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/27.jpg)
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?
![Page 28: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/28.jpg)
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
![Page 29: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/29.jpg)
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()
![Page 30: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/30.jpg)
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.
![Page 31: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/31.jpg)
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
![Page 32: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/32.jpg)
Fred’s Oranges
■ Break the story down into steps:
declare int orangesdeclare double pricedeclare double budgetdisplay the budget to Fred
Am I missing anything?
![Page 33: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/33.jpg)
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
![Page 34: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/34.jpg)
Now let’s write and run the code!
![Page 35: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/35.jpg)
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
![Page 36: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/36.jpg)
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!
![Page 37: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/37.jpg)
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.
![Page 38: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/38.jpg)
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
![Page 39: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/39.jpg)
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.
![Page 40: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/40.jpg)
Procedural Programming
Procedure A
Data Element
Procedure B
![Page 41: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/41.jpg)
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.
![Page 42: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/42.jpg)
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.
![Page 43: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/43.jpg)
Object-Oriented ProgrammingObject
Attributes (data)
Methods
(behaviors / procedures)
![Page 44: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/44.jpg)
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.
![Page 45: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/45.jpg)
Object-Oriented Programming
ObjectAttributes (data)
typically private to this object
Methods
(behaviors / procedures)
Other
objects
Programming
Interface
![Page 46: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/46.jpg)
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
![Page 47: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/47.jpg)
Computer System Setup
input memory output
ALU CU
Arithmetic
Logic
Unit
Control
Unit
CPU
![Page 48: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/48.jpg)
• 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.
![Page 49: Java Fundamentals - Purdue University Fort Wayne](https://reader031.vdocument.in/reader031/viewer/2022012016/615b16e5244f350a866e1cf0/html5/thumbnails/49.jpg)
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