1 / 28

CMSC 150 Introduction TO Computing

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)

trygg
Download Presentation

CMSC 150 Introduction TO Computing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 150IntroductionTOComputing 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 : • intnumberOfVictims; • double minutesUntilDemise; • char jasonsFirstInitial; • booleanisKevinBaconStillAlive; • String finalPleaForMercy;

  6. In RAM numberOfVictims minutesUntilDemise intnumberOfVictims; double minutesUntilDemise; char jasonsFirstInitial; booleanisKevinBaconStillAlive; String finalPleaForMercy; jasonsFirstInitial your program in memory isKevinBaconStillAlive finalPleaForMercy

  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)

  8. 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

  9. 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!”;

  10. Assignments & Expressions numberOfVictims = 13; numberOfVictims = numberOfVictims + 1; evaluate right-hand side value: 13 numberOfVictims minutesUntilDemise

  11. Assignments & Expressions numberOfVictims = 13; numberOfVictims = numberOfVictims + 1; 13 13 numberOfVictims minutesUntilDemise

  12. Assignments & Expressions numberOfVictims = 13; numberOfVictims = numberOfVictims + 1; evaluate right-hand side value: 13 + 1 13 numberOfVictims minutesUntilDemise

  13. Assignments & Expressions numberOfVictims = 13; numberOfVictims = numberOfVictims + 1; 14 14 numberOfVictims minutesUntilDemise

  14. Assignments & Expressions numberOfVictims = 13; numberOfVictims = numberOfVictims + 1; numberOfVictims = numberOfVictims + 7; // party! evaluate right-hand side value: 14 + 7 14 numberOfVictims minutesUntilDemise

  15. Assignments & Expressions numberOfVictims = 13; numberOfVictims = numberOfVictims + 1; numberOfVictims = numberOfVictims + 7; // party! 21 21 numberOfVictims minutesUntilDemise

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

  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 • 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 + “!”;

  23. 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 + “!”;

  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 ) { 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; */ …

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

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

More Related