Lecture Slides on Java Expressions | CMSC 131, Study Guides, Projects, Research of Computer Science

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

Pre 2010

Uploaded on 07/30/2009

koofers-user-z7s
koofers-user-z7s 🇺🇸

10 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
9/8/2006 CMSC 131 Fall 2006
Rance Cleaveland
©2006 Univeristy of Maryland
Lecture 4:
Java Expressions
Last time:
1. CVS and project submission
2. Basics of Java programs
Today:
1. Variables and types
2. Expressions in Java
3. User input
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Lecture Slides on Java Expressions | CMSC 131 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

9/8/

CMSC 131 Fall 2006Rance Cleaveland ©2006 Univeristy of Maryland

Lecture 4:

Java Expressions

Last time: 1.^

CVS and project submission

2.^

Basics of Java programs

Today: 1.^

Variables and types

2.^

Expressions in Java

3.^

User input

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

:

^

Unary negation:

-x

^

Addition/subtraction:

x+y

x-y

^

Multiplication/division:

x*y

x/y

^

Division between integer types

truncates

to integer:

^

x%y

returns the

remainder

of

x^

divided by

y:

^

Division with real types yields a real result:

^

Comparison operators

:

^

Equality/inequality:

x == y

x != y

^

Less than/greater than:

x < y

x > y

^

Less than or equal/greater than or equal:

x <= y

x >= y

These comparison operators return a

boolean

value:

true

or

false

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 "

  • x + y;

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);

} }

Create new scanner objectto read from keyboard

Input an integer^ Input a double Input a string (upto white space)