Download presentation
Presentation is loading. Please wait.
Published byFelicity Patterson Modified over 9 years ago
1
CMSC 150 INTRODUCTION TO COMPUTING CS 150: Fri 13 Jan 2012
2
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”)
3
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
4
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
5
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;
6
In RAM int numberOfVictims; double minutesUntilDemise; char jasonsFirstInitial; boolean isKevinBaconStillAlive; String finalPleaForMercy; numberOfVictims minutesUntilDemise jasonsFirstInitial isKevinBaconStillAlive finalPleaForMercy your program in memory your program in memory
7
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 ) literals (type must match variable type )
8
In RAM numberOfVictims = 13; minutesUntilDemise = 2.1; jasonsFirstInitial = ‘J’; isKevinBaconStillAlive = false; finalPleaForMercy = “Help Me!” numberOfVictims minutesUntilDemise jasonsFirstInitial isKevinBaconStillAlive finalPleaForMercy your program in memory your program in memory 13 2.1 J false Help Me!
9
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!”;
10
Assignments & Expressions numberOfVictims = 13; numberOfVictims = numberOfVictims + 1; evaluate right-hand side value: 13 evaluate right-hand side value: 13 numberOfVictims minutesUntilDemise
11
Assignments & Expressions numberOfVictims = 13; numberOfVictims = numberOfVictims + 1; 13 numberOfVictims minutesUntilDemise 13
12
Assignments & Expressions numberOfVictims = 13; numberOfVictims = numberOfVictims + 1; numberOfVictims minutesUntilDemise evaluate right-hand side value: 13 + 1 evaluate right-hand side value: 13 + 1 13
13
Assignments & Expressions numberOfVictims = 13; numberOfVictims = numberOfVictims + 1; 14 numberOfVictims minutesUntilDemise 14
14
Assignments & Expressions numberOfVictims = 13; numberOfVictims = numberOfVictims + 1; numberOfVictims = numberOfVictims + 7; // party! numberOfVictims minutesUntilDemise evaluate right-hand side value: 14 + 7 evaluate right-hand side value: 14 + 7 14
15
Assignments & Expressions numberOfVictims = 13; numberOfVictims = numberOfVictims + 1; numberOfVictims = numberOfVictims + 7; // party! 21 numberOfVictims minutesUntilDemise 21
16
Incrementing Increment : add one to a variable Three ways: numberOfVictims = numberOfVictims + 1; numberOfVictims += 1; numberOfVictims++; All result in value increasing by one
17
Expressions Numerical operators: +−*/% Examples of expressions: 3.14159 + 2 (11 – 3) * 4 2 / 4 2.0 / 4 4 % 3 m * (c * c)
18
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);
19
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
20
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
21
Be Wary double myShare = (1/2) * inheritance; double onePointSixRepeat = (double)(5 / 3); double threeFifths = 3/5; double wormHole = someValue / threeFifths;
22
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 + “!”;
23
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 + “!”;
24
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”
25
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
26
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; */ …
27
// 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; …
28
/* 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; …
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.