









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: Project; Professor: Cleaveland; Class: OBJECT-ORIENTED PROG I; Subject: Computer Science; University: University of Maryland; Term: Fall 2006;
Typology: Study Guides, Projects, Research
1 / 15
This page cannot be seen from the preview
Don't miss anything!










9/8/
CMSC 131 Fall 2006Rance Cleaveland ©2006 Univeristy of Maryland
2.^
2.^
3.^
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Variables … ^
… are named storage locations ^
Recall that memory is a sequence of bits ^
Question: How much memory to allocate fora variable’s value? ^
Answer: A variable must have a
type
specifying how much storage to allocate.
5
x
Value
Variable
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
The Memory Table Game int x;float y;char c;double
z;
boolean
b;
Bytes
Variable
x y c z b
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Primitive Data Types In Detail^ Integer Types:
byte
1 byte
Range: -128 to +
short
2 bytes
Range: -32,000 to +32,
int
4 bytes
Range: -2 billion to +2 billion
long
8 bytes
Range: -9 quintillion to +9 quintillion
Floating-Point Types
:
float
4 bytes
-3.4x
38
to 3.4x
38 , 7 digits of precision
double
8 bytes
-1.7x
308
to 1.7x
308
, 15 digits of prec.
Other types
:
boolean
1 byte
true, false
char
2 bytes
A single (Unicode) character
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Character and StringConstants ^
Char constants
: Single character in single quotes (‘…’) including:
^
Letters and digits
: ‘A’, ‘B’, ‘C’, …, ‘a’, ‘b’, ‘c’, …, ‘0’, ‘1’, …, ‘9’
^
Punctuation symbols
: ‘*’, ‘#’, ‘@’, ‘$’ (except ‘ and backslash ‘\’)
^
Escape sequences
: (see below)
^
String constants
: 0 or more characters in double quotes (“…”)
^
Escape sequences
: Allows inclusion of ‘, ”, other special characters:
\”
double quote
\n
new-line character (start a new line)
\’
single quote
\t
tab character
\
backslash
^
Examples
:^ char
x
= ’
\’
’^
→
(x contains a single quote)
”\”Hi
there!\”
”^
→
”Hi there!”
”C:\WINDOWS
”^
→
C:\WINDOWS
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Common Numeric Operators ^
Arithmetic operators
:
^
^
^
^
^
^
Comparison operators
:
^
^
^
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Debugging Java Programs ^
Types of errors ^
“Compile time”: caught by Eclipse / Java compiler ^
Syntax
errors: typos, etc.
Type
errors: misuse of variables
“Run time”: appear during program execution ^
Division by 0
Wrong outputs (because of mistakes in programming)
^
Eclipse helps catch compile time errors ^
Red
: error
Yellow
: warning
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Example3.java public class
Example3 {
public static
void
main(String[] args) {
int
x
int
y
double
d = 72.33; boolean
b
true
char
c; String s;x^
y^
y^
d^
x; b^
c^
"cow"
s^
"Here
is something weird "
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
User Input in Java ^
We've done output (System.out); what about input?
Java 5.0 includes the
Scanner class
feature
Can use Scanner to create “scanner objects” ^
Scanner objects convert user input into data
To use Scannner need to
import
a library:
import
java.util.Scanner;
Note difference between
System.out.println
vs.
System.out.print
(println
moves to the
next line after printing.)
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Example5.java import
java.util.Scanner; public
class
Example5 { public static
void
main(String[]
args)
{
int
i; double
d; String s;Scanner
sc
=^
new
Scanner(System.
in );
System.
out
.print(
"Enter an
integer:
");
i =
sc.nextInt(); System.
out
.print(
"Enter a
floating
point
value: "
);
d =
sc.nextDouble(); System.
out
.print(
"Enter a
string: "
);
s =
sc.next(); System.
out
.println(
"Here is
what you entered:
");
System.
out
.println(i);
System.
out
.println(d);
System.
out
.println(s);
} }