CMSC 150 INTRODUCTION TO COMPUTING CS 150: Fri 13 Jan 2012.

Post on 04-Jan-2016

215 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

CMSC 150INTRODUCTION

TOCOMPUTING

CS 150: Fri 13 Jan 2012

Variable Declarations

Use to request space in RAM to store values

Syntax: type variableName;

Types: int : whole number (e.g., 0, 1, 100,

1234) double : real-valued number (e.g.,

3.14159) char : one character (e.g., ‘A’, ‘9’,

‘q’) boolean : true or false String : character sequence (e.g.,

“Jason”)

Variable Declarations

Use to request space in RAM to store values

Syntax: type variableName;

Types: int : whole number (e.g., 0, 1, 100,

1234) double : real-valued number (e.g.,

3.14159) char : one character (e.g., ‘A’, ‘9’,

‘q’) boolean : true or false String : character sequence (e.g.,

“Jason”)

primitive types

Variable Declarations

Use to request space in RAM to store values

Syntax: type variableName;

Types: int : whole number (e.g., 0, 1, 100,

1234) double : real-valued number (e.g.,

3.14159) char : one character (e.g., ‘A’, ‘9’,

‘q’) boolean : true or false String : character sequence (e.g.,

“Jason”)

class type

Variable Declarations

Use to request space in RAM to store values

Syntax: type variableName;

Examples : int numberOfVictims; double minutesUntilDemise; char jasonsFirstInitial; boolean isKevinBaconStillAlive; String finalPleaForMercy;

In RAM

int numberOfVictims;

double minutesUntilDemise;

char jasonsFirstInitial;

boolean isKevinBaconStillAlive;

String finalPleaForMercy;

numberOfVictims

minutesUntilDemise

jasonsFirstInitial

isKevinBaconStillAlive

finalPleaForMercy

your programin memory

Assignment Statements

Use to put values into variables Syntax: variableName = expression;

Examples : numberOfVictims = 13; minutesUntilDemise = 2.1; jasonsFirstInitial = ‘J’; isKevinBaconStillAlive = false; finalPleaForMercy = “Help Me!”;

literals(type must

match variable

type)

In RAM

numberOfVictims = 13;

minutesUntilDemise = 2.1;

jasonsFirstInitial = ‘J’;

isKevinBaconStillAlive = false;

finalPleaForMercy = “Help Me!”

numberOfVictims

minutesUntilDemise

jasonsFirstInitial

isKevinBaconStillAlive

finalPleaForMercy

your programin memory

13

2.1

J

false

Help Me!

Declaration + Assignment

Combine into a single statement Syntax: type variableName =

expression;

Examples : int numberOfVictims = 13; double minutesUntilDemise = 2.1; char jasonsFirstInitial = ‘J’; boolean isKevinBaconStillAlive = false; String finalPleaForMercy = “Help Me!”;

Assignments & Expressions

numberOfVictims = 13;

numberOfVictims = numberOfVictims + 1;evaluate right-hand

side

value: 13

numberOfVictims

minutesUntilDemise

Assignments & Expressions

numberOfVictims = 13;

numberOfVictims = numberOfVictims + 1;

13

numberOfVictims

minutesUntilDemise

13

Assignments & Expressions

numberOfVictims = 13;

numberOfVictims = numberOfVictims + 1;

numberOfVictims

minutesUntilDemise

evaluate right-hand side

value: 13 + 1

13

Assignments & Expressions

numberOfVictims = 13;

numberOfVictims = numberOfVictims + 1;

14

numberOfVictims

minutesUntilDemise

14

Assignments & Expressions

numberOfVictims = 13;

numberOfVictims = numberOfVictims + 1;

numberOfVictims = numberOfVictims + 7; // party!

numberOfVictims

minutesUntilDemise

evaluate right-hand side

value: 14 + 7 14

Assignments & Expressions

numberOfVictims = 13;

numberOfVictims = numberOfVictims + 1;

numberOfVictims = numberOfVictims + 7; // party!

21numberOfVictims

minutesUntilDemise

21

Incrementing

Increment : add one to a variable

Three ways: numberOfVictims = numberOfVictims + 1; numberOfVictims += 1; numberOfVictims++;

All result in value increasing by one

Expressions

Numerical operators: + − * / %

Examples of expressions: 3.14159 + 2 (11 – 3) * 4 2 / 4 2.0 / 4 4 % 3 m * (c * c)

Expressions

Numerical operators: + − * / %

Examples of expressions (with variables): double piPlusTwo = 3.14159 + 2; double thirtyTwo = (11 – 3) * 4; double zero = 2 / 4; double oneHalf = 2.0 / 4; double remainder = 4 % 3; double e = m * (c * c);

Expression takes on type of largest-storage operand in expression

Examples of expressions (with variables): double piPlusTwo = 3.14159 + 2; double thirtyTwo = (11 – 3) * 4; double zero = 2 / 4; double oneHalf = 2.0 / 4; double remainder = 4 % 3; double e = m * (c * c);

Expression Type

Division

Works differently for integers vs. floating-point

Examples: 8.0 / 3.0 gives true floating-point result

8 / 3 gives integer quotient

8 % 3 gives integer remainder

Be Wary

double myShare = (1/2) * inheritance;

double onePointSixRepeat = (double)(5 / 3);

double threeFifths = 3/5; double wormHole = someValue / threeFifths;

Strings & String Methods

String homerSez = “Donuts. Is there anything they can’t do?”; 0123456789112345678921234567893123456789 0 0 0

int quoteLength = homerSez.length(); // 40 char firstChar = homerSez.charAt( 0 ); // ‘D’ char period = homerSez.charAt( 6 ); char questionMark = homerSez.charAt( 39 ); char eye = homerSez.charAt( homerSez.indexOf( ‘I’ ) );

String donutsCap = homerSez.substring(0, 6); // “Donuts” String donuts= donutCap.toLowerCase(); // “donuts”

String nuts = donuts.substring( 2 ); // 2 to end String nutsCubed = nuts + “ ” + nuts + “ ” + nuts + “!”;

Strings & String Methods

String homerSez = “Donuts. Is there anything they can’t do?”; 0123456789112345678921234567893123456789 0 0 0 int quoteLength = homerSez.length(); // 40 char firstChar = homerSez.charAt( 0 ); // ‘D’ char period = homerSez.charAt( 6 ); char questionMark = homerSez.charAt( 39 ); char eye =

homerSez.charAt( homerSez.indexOf( ‘I’ ) );

String donutsCap = homerSez.substring(0, 6); // “Donuts” String donuts = donutCap.toLowerCase(); // “donuts”

String nuts = donuts.substring( 2 ); // 2 to end String nutsCubed = nuts + “ ” + nuts + “ ” + nuts + “!”;

Digits

int one = 1; char uno = ‘1’; String ein = “1”;

All are different Only one contains the integer value 1

1 + 1 // 2 ‘1’ + ‘1’ // 98 (more on this later) “1” + “1” // “11”

Comments

Allow you to annotate your programs Two options:

inline // this is an example of an inline comment

block /* this is an example of a block comment */

Java does not execute comments

Use to describe (in English) what your code does

Also use to (temporarily) disable parts of code

public class ClassWithComments

{

public static void main( String[] args )

{

int numberOfVictims = 13; // number of people Jason has greeted

double minutesToDemise = 2.1; // mins from start of movie to 1st death

// Jason just met another person, so add one to # of victims

numberOfVictims = numberOfVictims + 1;

// Jason crashed a party, so really bump the victim count

numberOfVictims = numberOfVictims + 7;

/*

Let’s comment out the Kevin Bacon code for now...

Turns out that was just a dream sequence, but we might need

this code later!

// Jason killed Kevin Bacon’s character

numberOfVictims = numberOfVictims + 1;

*/

// Author: Kyra Sedgwick (you may need to search for the reference…)

// Date: 13 Friday 2012

// Purpose: This program keeps count of the number of victims of the

// “protagonist” Jason from the slasher movie “Friday The 13th”,

// along with some other interesting movie trivia.

public class ClassWithComments

{

public static void main( String[] args )

{

int numberOfVictims = 13; // number of people Jason has greeted

double minutesToDemise = 2.1; // mins from start of movie to 1st death

// Jason just met another person, so add one to # of victims

numberOfVictims = numberOfVictims + 1;

// Jason crashed a party, so really bump the victim count

numberOfVictims = numberOfVictims + 7;

/* Author: Kyra Sedgwick

Date: 13 Friday 2012

Purpose: This program keeps count of the number of victims of the

“protagonist” Jason from the slasher movie “Friday The 13th”,

along with some other interesting movie trivia.

*/

public class ClassWithComments

{

public static void main( String[] args )

{

int numberOfVictims = 13; // number of people Jason has greeted

double minutesToDemise = 2.1; // mins from start of movie to 1st death

// Jason just met another person, so add one to # of victims

numberOfVictims = numberOfVictims + 1;

// Jason crashed a party, so really bump the victim count

numberOfVictims = numberOfVictims + 7;

top related