CMSC 150 Introduction TO Computing
description
Transcript of CMSC 150 Introduction TO Computing
![Page 1: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/1.jpg)
CMSC 150INTRODUCTION
TOCOMPUTING
CS 150: Fri 13 Jan 2012
![Page 2: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/2.jpg)
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”)
![Page 3: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/3.jpg)
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
![Page 4: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/4.jpg)
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
![Page 5: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/5.jpg)
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;
![Page 6: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/6.jpg)
In RAM
int numberOfVictims;double minutesUntilDemise;char jasonsFirstInitial;boolean isKevinBaconStillAlive;String finalPleaForMercy;
numberOfVictims
minutesUntilDemise
jasonsFirstInitial
isKevinBaconStillAlive
finalPleaForMercy
your programin memory
![Page 7: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/7.jpg)
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)
![Page 8: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/8.jpg)
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!
![Page 9: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/9.jpg)
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!”;
![Page 10: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/10.jpg)
Assignments & ExpressionsnumberOfVictims = 13;
numberOfVictims = numberOfVictims + 1;evaluate right-hand
side
value: 13
numberOfVictims
minutesUntilDemise
![Page 11: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/11.jpg)
Assignments & ExpressionsnumberOfVictims = 13;
numberOfVictims = numberOfVictims + 1;
13
numberOfVictims
minutesUntilDemise
13
![Page 12: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/12.jpg)
Assignments & ExpressionsnumberOfVictims = 13;
numberOfVictims = numberOfVictims + 1;
numberOfVictims
minutesUntilDemise
evaluate right-hand side
value: 13 + 113
![Page 13: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/13.jpg)
Assignments & ExpressionsnumberOfVictims = 13;
numberOfVictims = numberOfVictims + 1;
14
numberOfVictims
minutesUntilDemise
14
![Page 14: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/14.jpg)
Assignments & ExpressionsnumberOfVictims = 13;
numberOfVictims = numberOfVictims + 1;numberOfVictims = numberOfVictims + 7; // party!
numberOfVictims
minutesUntilDemise
evaluate right-hand side
value: 14 + 7 14
![Page 15: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/15.jpg)
Assignments & ExpressionsnumberOfVictims = 13;
numberOfVictims = numberOfVictims + 1;numberOfVictims = numberOfVictims + 7; // party!
21numberOfVictims
minutesUntilDemise
21
![Page 16: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/16.jpg)
Incrementing Increment : add one to a variable
Three ways: numberOfVictims = numberOfVictims + 1; numberOfVictims += 1; numberOfVictims++;
All result in value increasing by one
![Page 17: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/17.jpg)
Expressions Numerical operators:
+ − * / %
Examples of expressions: 3.14159 + 2 (11 – 3) * 4 2 / 4 2.0 / 4 4 % 3 m * (c * c)
![Page 18: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/18.jpg)
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);
![Page 19: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/19.jpg)
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
![Page 20: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/20.jpg)
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
![Page 21: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/21.jpg)
Be Wary double myShare = (1/2) * inheritance;
double onePointSixRepeat = (double)(5 / 3);
double threeFifths = 3/5; double wormHole = someValue / threeFifths;
![Page 22: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/22.jpg)
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 + “!”;
![Page 23: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/23.jpg)
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 + “!”;
![Page 24: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/24.jpg)
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”
![Page 25: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/25.jpg)
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
![Page 26: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/26.jpg)
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;
*/
…
![Page 27: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/27.jpg)
// 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;
…
![Page 28: CMSC 150 Introduction TO Computing](https://reader035.fdocuments.net/reader035/viewer/2022062520/568164d4550346895dd70bf0/html5/thumbnails/28.jpg)
/* 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;
…