COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
-
date post
22-Dec-2015 -
Category
Documents
-
view
218 -
download
2
Transcript of COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
![Page 1: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/1.jpg)
COMP 14:I/O and Boolean Expressions
May 24, 2000
Nick Vallidis
![Page 2: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/2.jpg)
Announcements
Anyone tried the assignment yet?
![Page 3: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/3.jpg)
Review
What are the 2 parts of a program? What are the 2 types of data in Java? What is a data type? What are the 4 primitive data types we’ll
be using in this class? What is a method?
![Page 4: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/4.jpg)
Review part 2
What is a string literal? How do we concatenate things onto a
string? How do we put newline, \, and " in a
string? What are these things called?
![Page 5: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/5.jpg)
Today
Input/Output (I/O) Expressions Boolean Expressions
![Page 6: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/6.jpg)
Input/Output
How do we get data into the program from the keyboard?
How do we display data to the user?
![Page 7: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/7.jpg)
Output
You’ve already seen the basics of this one:
System.out.println("Beefcake!!!");
![Page 8: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/8.jpg)
What about displaying data?
We talked yesterday about concatenation…
int level = 3;
System.out.println("You are on level: " + level);
double price = 2.75;
System.out.println("Please pay: $" + price);
![Page 9: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/9.jpg)
Input
Input is a little more complicated We are not using the "Keyboard" class
described in the book. Let's take a look at the current
assignment...
![Page 10: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/10.jpg)
Part of program P1
BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in ));
int age; // Holds the age of the personString name; // Holds name.
// Ask the user's nameSystem.out.println( "\"Hello\"");System.out.print( "What is your name? -> " );name = stdin.readLine();
![Page 11: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/11.jpg)
Part of program P1
BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in ));
int age; // Holds the age of the personString name; // Holds name.
// Ask the user's nameSystem.out.println( "\"Hello\"");System.out.print( "What is your name? -> " );name = stdin.readLine();
This is some setup code. We create an object stdin. You don't have to understand the details of this.
![Page 12: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/12.jpg)
Part of program P1
BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in ));
int age; // Holds the age of the personString name; // Holds name.
// Ask the user's nameSystem.out.println( "\"Hello\"");System.out.print( "What is your name? -> " );name = stdin.readLine();
Here we actually read in what the user types.
![Page 13: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/13.jpg)
Another piece of program P1
// Ask the user's ageSystem.out.print( "Please enter your age (doesn't
have to be true). -> ");age = Integer.parseInt(stdin.readLine( ) );
And here we read it in and then convert it to an integer
![Page 14: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/14.jpg)
Input Summary
Put this at beginning (on one line)
To read in a String
To read in an int
To read in a double
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
<variable> = stdin.readLine();
<variable> = Integer.parseInt(stdin.readLine());
<variable> = (new Double(stdin.readLine())).doubleValue();
![Page 15: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/15.jpg)
Expressions: the revenge
We talked about +, -, *, / and % Just like with arithmetic, there is an
order
* and / have higher precedence than + and -
18 / 5 + 1 is 4, not 3
![Page 16: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/16.jpg)
Using parenthesis
There are many levels of precedence (see pg. 69 in book)
You can force whatever order you want using parenthesis
18 / 5 + 1 is 4, BUT
18 / (5 + 1) is 3
![Page 17: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/17.jpg)
Mixing data types
Avoid this if you can Guidelines
– only mix int and double– the result should be doubledouble total, shoePrice = 54.99;
int numShoes = 4;
total = shoePrice * numShoes;
Should be a double
![Page 18: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/18.jpg)
Methods can be in expressions
If a method returns a value, then it can be in an expression too.
This brings us to a special kind of method...
age = Integer.parseInt(stdin.readLine( )) - 21;
![Page 19: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/19.jpg)
Class Methods
Normally, you have to instantiate an object of a class before you can use its methods
But not if the method has the word static in front of it
Where have you seen this before?
![Page 20: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/20.jpg)
Class (or static) methods
How about main?
public class Simple
{
public static void main(String[] args)
{
System.out.println(“Hello!”);
}
}
![Page 21: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/21.jpg)
Aside: method data types
public class Simple
{
public static void main(String[] args)
{
System.out.println(“Hello!”);
}
}
What's this?
![Page 22: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/22.jpg)
Aside: method data types
public class Simple
{
public static void main(String[] args)
{
System.out.println(“Hello!”);
}
}
It's a data type! It tells you whether the method returns a value (if it can be used in an expression)
![Page 23: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/23.jpg)
This can be very useful
The Math class contains many methods that are useful in expressions:
double result;
// calculate the absolute value
result = Math.abs(-17.2);
// calculate the sin of pi radians
result = Math.sin(3.1416);
![Page 24: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/24.jpg)
Boolean expressions
These are expressions that result in a value of the data type boolean
They almost always result from the use of equality operators, relational operators or logical operators
![Page 25: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/25.jpg)
Equality operators
These are == and !=– == means "equal to"– != means "not equal to"
They go between two expressions:2 == 2 evaluates to true
2 == 5 evaluates to false
15 != 7 evaluates to true
14 != 14 evaluates to false
![Page 26: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/26.jpg)
Equality Operators
Of course you can also use variables and more complex expressions
int myAnswer = 5;
int trueAnswer = 7;
myAnswer == trueAnswer evaluates to false
int myRaise = 0;
int yourRaise = 100000;
myRaise != yourRaise evaluates to true
![Page 27: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/27.jpg)
Relational Operators
These are <, <=, > and >=– < means "less than"– <= means "less than or equal to"– > means "greater than"– >= means "greater than or equal to"
They work the same way as the equality operators
![Page 28: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/28.jpg)
Last bunch of boolean operators
Logical operators: !, && and ||– ! means "NOT"– && means "logical AND"– || means "logical OR"
![Page 29: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/29.jpg)
Truth tablesa !a
false truetrue false
a b a&&b a||bfalse false false falsefalse true false truetrue false false truetrue true true true
![Page 30: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/30.jpg)
Logical operators in action
Much like relational operators, but each side has to be a boolean expression
(3<5) && (6<=7) evaluates to true
!(2!=5) evaluates to false
(3!=3) || (2==2) evaluates to true
![Page 31: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/31.jpg)
Examples
![Page 32: COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.](https://reader031.fdocuments.in/reader031/viewer/2022032105/56649d7a5503460f94a5e0a4/html5/thumbnails/32.jpg)
Homework
Read 3.1-3.2, 3.4 Don't forget that P1 is due tomorrow!