280 likes | 361 Views
CMSC 150 Introduction TO Computing. 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)
E N D
CMSC 150IntroductionTOComputing 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 : • intnumberOfVictims; • double minutesUntilDemise; • char jasonsFirstInitial; • booleanisKevinBaconStillAlive; • String finalPleaForMercy;
In RAM numberOfVictims minutesUntilDemise intnumberOfVictims; double minutesUntilDemise; char jasonsFirstInitial; booleanisKevinBaconStillAlive; String finalPleaForMercy; jasonsFirstInitial your program in memory isKevinBaconStillAlive finalPleaForMercy
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 13 numberOfVictims 2.1 minutesUntilDemise numberOfVictims = 13; minutesUntilDemise = 2.1; jasonsFirstInitial = ‘J’; isKevinBaconStillAlive = false; finalPleaForMercy = “Help Me!” jasonsFirstInitial J your program in memory false isKevinBaconStillAlive Help Me! finalPleaForMercy
Declaration + Assignment • Combine into a single statement • Syntax: type variableName = expression; • Examples : • intnumberOfVictims = 13; • double minutesUntilDemise = 2.1; • char jasonsFirstInitial = ‘J’; • booleanisKevinBaconStillAlive = 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 13 numberOfVictims minutesUntilDemise
Assignments & Expressions numberOfVictims = 13; numberOfVictims = numberOfVictims + 1; evaluate right-hand side value: 13 + 1 13 numberOfVictims minutesUntilDemise
Assignments & Expressions numberOfVictims = 13; numberOfVictims = numberOfVictims + 1; 14 14 numberOfVictims minutesUntilDemise
Assignments & Expressions numberOfVictims = 13; numberOfVictims = numberOfVictims + 1; numberOfVictims = numberOfVictims + 7; // party! evaluate right-hand side value: 14 + 7 14 numberOfVictims minutesUntilDemise
Assignments & Expressions numberOfVictims = 13; numberOfVictims = numberOfVictims + 1; numberOfVictims = numberOfVictims + 7; // party! 21 21 numberOfVictims minutesUntilDemise
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 Type • 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);
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 • intquoteLength= 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 • intquoteLength= 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 ) { intnumberOfVictims = 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 ) { intnumberOfVictims = 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 ) { intnumberOfVictims = 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; …