Unformatted text preview:

COMP 14: I/O and Boolean ExpressionsAnnouncementsReviewReview part 2TodayInput/OutputOutputWhat about displaying data?InputPart of program P1Slide 11Slide 12Another piece of program P1Input SummaryExpressions: the revengeUsing parenthesisMixing data typesMethods can be in expressionsClass MethodsClass (or static) methodsAside: method data typesSlide 22This can be very usefulBoolean expressionsEquality operatorsEquality OperatorsRelational OperatorsLast bunch of boolean operatorsTruth tablesLogical operators in actionExamplesHomeworkCOMP 14:I/O and Boolean ExpressionsMay 24, 2000Nick VallidisAnnouncementsAnyone tried the assignment yet?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?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?TodayInput/Output (I/O)ExpressionsBoolean ExpressionsInput/OutputHow do we get data into the program from the keyboard?How do we display data to the user?OutputYou’ve already seen the basics of this one:System.out.println("Beefcake!!!");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);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...Part of program P1BufferedReader 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();Part of program P1BufferedReader 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.Part of program P1BufferedReader 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.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 integerInput SummaryPut this at beginning (on one line)To read in a StringTo read in an intTo read in a doubleBufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));<variable> = stdin.readLine();<variable> = Integer.parseInt(stdin.readLine());<variable> = (new Double(stdin.readLine())).doubleValue();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 3Using parenthesisThere are many levels of precedence (see pg. 69 in book)You can force whatever order you want using parenthesis18 / 5 + 1 is 4, BUT18 / (5 + 1) is 3Mixing 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 doubleMethods 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;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?Class (or static) methodsHow about main?public class Simple{public static void main(String[] args){System.out.println(“Hello!”);}}Aside: method data typespublic class Simple{public static void main(String[] args){System.out.println(“Hello!”);}}What's this?Aside: method data typespublic 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)This can be very usefulThe Math class contains many methods that are useful in expressions:double result;// calculate the absolute valueresult = Math.abs(-17.2);// calculate the sin of pi radiansresult = Math.sin(3.1416);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 operatorsEquality operatorsThese are == and !=–== means "equal to"–!= means "not equal to"They go between two expressions:2 == 2 evaluates to true2 == 5 evaluates to false15 != 7 evaluates to true14 != 14 evaluates to falseEquality OperatorsOf course you can also use variables and more complex expressionsint myAnswer = 5;int trueAnswer = 7;myAnswer == trueAnswer evaluates to falseint myRaise = 0;int yourRaise = 100000;myRaise != yourRaise evaluates to trueRelational 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 operatorsLast bunch of boolean operatorsLogical operators: !, && and ||–! means "NOT"–&& means "logical AND"–|| means "logical OR"Truth tablesa !afalse truetrue falsea b a&&b a||bfalse false false falsefalse true false truetrue false false truetrue true true trueLogical 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 trueExamplesHomeworkRead 3.1-3.2, 3.4Don't forget that P1 is due


View Full Document

UNC-Chapel Hill COMP 14 - LECTURE NOTES

Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?