




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Doom; Class: Computer Programming II; Subject: Computer Science; University: Wright State University-Main Campus; Term: Unknown 1989;
Typology: Study notes
1 / 28
This page cannot be seen from the preview
Don't miss anything!





















Acknowledgements: These slides were created by Dr. Travis Doom with information, graphics, materials, or kindly aidprovided by Gaddis’s “Starting Out with Java”, McConnell’s “Code Complete”, Sierra’s “Head First Java”, Guzdal’s
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
p^
y^
g^
,^
p^
,^
,
“Introduction to computing and programming with Java”, and Barnes’s “Objects First with Java”.
Computer programming
p^
p^
g^
g
Semantics, syntax, and style
Variables and data type Methods and decomposition
Methods for I/O in Java
z^
Prior to this class you should have learned how to:–
create an executable program (
semantics
,^ sy
ntax
,^ and
s
tyle
p^
g^
y^
y^
create and use
typed variables
to store
primitive
and
reference
values
use standard
operators
to manipulate variable and literal values
decompose
a design using routines (
methods
iteration
and
selection
decompose
a design using routines (
methods
iteration
, and
selection
manage simple collections of data (
arrays
and ArrayLists)
handle basic console and file
tasks
z^
In this class you will learn how to:–
create/design your own
classes
and
objects
decompose a design by developing
object types
when and why!)
p^
g^
y^
p^
g^
j^
yp
y )
create, test, debug, and manage applications of moderate complexity
-^
to use more sophisticated library objects and interfaces
z^
Including libraries to manage events threads and graphics Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
z^
Including
libraries to manage events, threads, and graphics
z^
Essentially,
computers
are
simple devices that:
Memory MAR
MDR
simple devices that:–
allow input to memory
-^
allow output from memoryll
li^
it d
i
Processing Unit
Input
Output
ALU
TEMP
C^
l U i
(keyboard)
(monitor)
allow limited processing(instructions) on memory
z^
Computer programming
is the
t^
k^
f^
i^
th
li^
it d
Control Unit PC
IR
(^
)
task of
s
equencing
th
e limited
processing operations towardssolving a specific design goal. z^
Hi h l
l l
ll
CompilerComputer A
source code
t bl
syntax errorswarnings
compile time
z^
Hi
gh level languages
allow us
to specify tasks without beingaware of the specificinstructions need on a system
Computer A
OS
input from user
output to user display
inputs fromfil^
/^
k/
output to files/networks/etc
executable program
c run time^5
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
instructions
need on a system.
files/network/etc
z^
Java uses both compilation and interpretationin a two-step process z^
Compiles program into
bytecode
Compiler
source code
syntax errorswarnings
time
z^
Compiles program into
bytecode
-^
bytecode is a generic “machinelanguage” for a “virtual machine”
-^
does not correspond to any particularmachine
Computer A
Virtual
bytecode
warnings
compile ion
z^
Virtual Machine
(VM) interprets bytecodes
into native machine language and runs it–
different VM implementation exists foreach native computer machine language
MachineComputer A
input from user
output to user display
inputs fromfiles/network/etc
output to files/networks/etc
interpretati
z^
Same
Java bytecodes can be used on
different
computers
without re-compiling source code
-^
each VM interprets same bytecodes
-^
allows you to run Java programs by
CompilerComputer A
source code
syntax errorswarnings
ompile time
getting just bytecodes from Web page
z^
This makes Java code run
cross-platform
-^
“Write once, run anywhere!”
Computer B
VirtualMachine
input from user
output to user display
inputs from
output to files/networks/etc
bytecode
co erpretation
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
inputs
from files/network/etc
inte
z^
Java is VERY object-oriented z^
Java is popular in the field z^
Java is popular in the field–
Early versions of Java were slow, buggy, and hard to use.
-^
Platform independence still made it amazingly popular
-^
Current versions of Java have most/all features that are popular inother programming languages
-^
Java and C++ are close cousins
z^
Java run complete with thousands of library classes, nearly everywhere–
More power! Easier to develop with! Powerful (and free) IDEs!
z^
Although the fundamental concepts of this course apply to ALL
oug
e u da
e^
a co cep s o
s cou se app y o
programming, the specifics we will use for illustration and forformative/summative experiences will be in Java z^
If you’ve never used Java before, talk to your instructor ASAP
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
y^
y
public class HelloWorld {
public static void main (String[] args) {
System.out.println(“Hello World”);
} // end method main
} // end class HelloWorld z^
Put a class in a source file-
A source code file (with a .java extension) holds one class definition
-^
So far you may have only used once class (and thus one file)
y^
y^
y^
-^
In general, each class is just a piece (unit) of the application
z^
Put each method in a class-
Methods are used to perform specific tasksMethods are used to perform specific tasks
-^
Exactly one “main” method must exist in your source file(s)
z^
Put statements in each method-
Finally we actually get to DO something!
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
Finally
, we actually get to DO something!
z^
Do something z^
Sequential executionf
z^
Do somethingunder thiscondition
z^
Do again and again z^
Iteration
of statements … statement1;statement2;
condition z^
Selection … if (condition)
{
… while
(condition)
{
statement(s); }
state e t ;… {
statement(s); } -or-
} -or-for
(initializer;condition;
update
//
a
block
of
// statements
are
// treated
as
// one
statement
if (condition)
{
statement(s); } else
{
statement(s);
expression)
{
statement(s); } -or-
// one
statement
}
statement(s); } …
ordo {
statement(s); } while
(condition)
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
…
Variable declaration
int
value;
Assignment statement
v
alue
Initialized declaration
i
nt
value
0x
0x0010x0020x
5
This is a String
literal
. It will be printed as is.
0x
System.out.print("The
value
is
");
System.out.println(value);
The integer 5 willbe printed out here.
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
otice no quote marks
z^
Select data types with care!–
int, double, boolean, and char most frequent
What about?int x = 2.0/3;
q
z^
Be wary of compiler assumptions! In Java:–
integer literals are
cast
as int
real number literals are
cast
as double
found
: double required: int
int x = 2.0/3; 1 errorBUILD FAILED (t t l ti
real number literals are
cast
as double
results of mixed types are
promoted
Example: 2/3 is cast int 0
BUILD FAILED (total time:0 seconds)
Example: 2.0/3 is cast double 0.
z^
Java automatically promotes lower precision typesto higher precision types but not visa-versa!
What about?float x = 2.0/3;found
: doublei d fl
byte -> short -> int -> long -> float -> double
z^
The rules can be overridden by explict typecasting
float
x
(float)
required: float
float x = 2.0/3; 1 errorBUILD FAILED (total time:0 seconds)
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
float
x
(float)
total = sum(value1, value2);
40
20
public
static
int
sum(int
num1,
int
num2)
int
result;
result
num
num2;
return
result;
end
method
sum
(^60) z^
At thi
i t
h^
ld b
f^
ili
ith
bli
t ti
th d
th t
z^
At this point, you should be familiar with
public static
methods that
return primitive data types.^ z
Later: other accesses modifiers (public, protected, private)
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
z^
Later: static Vs. regular (non-static) methods
z^
A local variable is declared inside a method and is not accessible tostatements outside the method.–
Scope
-^
Different methods can have local variables with the same namesbecause the methods cannot see each other’s local variables.
-^
A method’s local variables exist only while the method is executing.
z^
The values/arguments passed to the parameters of the method are copidand become local variables to that method.–
Call by value
-^
Changes to the local copies of variables do not change the original
-^
Use returned value to make changes to primitive data types
z^
If a reference to an object is passed to an method, then the method makesa copy of the reference and can use that reference to make changes to theactual object.
C ll b
f
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
all by reference
import
java.util.Scanner;
public
class
Main
{
public
static
int
getValue
(String
prompt)
{
Scanner
keyboard
=
new
Scanner(System.in);
int
userValue; System out println(prompt);System.out.println(prompt); userValue
=
keyboard.nextInt();
return
userValue;
}^
//
end
method
getValue
public
static
void
main(String
[]
args){
int
inputValue
=
getValue("Enter
integer
value:
");
System.out.println("Value
is
",
inputValue);
} //
end
method
main
} //
end
method
main
} //
end
class
GetValue
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
import
java.io.File;
import
java.util.Scanner;
public
class
Main
{
public
static
void
main
(String[]
args)
throws
Exception
{
String
filename
=
"Data.txt";
//
uses
working
directory
il
fil
dl
il (fil
)
File
fileHandle
=
new
File(filename);
Scanner
inputFile
=
new
Scanner
(fileHandle);
String
line;
String
line;
while
(
inputFile.hasNextLine()
)
{
line
=
inputFile.nextLine();
System.out.println(line);
} inputFile.close();
}^
//
end
method
main
} //
d^
l^
M i
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
}^
//
end
class
Main