1 / 32

OLD BUSINESS: Return last exam. Any questions from last time?? Go over homework -- 2.3 and 2.9 – use victim to see who d

Class 11. OLD BUSINESS: Return last exam. Any questions from last time?? Go over homework -- 2.3 and 2.9 – use victim to see who does Show & Tell NEW BUSINESS: Following the lecture notes. Do some interesting examples Assignment for next time . For Class 12. Assignment for next time.

caden
Download Presentation

OLD BUSINESS: Return last exam. Any questions from last time?? Go over homework -- 2.3 and 2.9 – use victim to see who d

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. Class 11 OLD BUSINESS: Return last exam. Any questions from last time?? Go over homework -- 2.3 and 2.9 – use victim to see who does Show & Tell NEW BUSINESS: Following the lecture notes. Do some interesting examples Assignment for next time

  2. For Class 12 • Assignment for next time. • Ground rules: • Feel free to work by yourself or with one other person – whichever fits your style (NOT with two other people.) • Be prepared to demo your program for show and tell on Thursday. I’ll use the “victim” program to choose who gets to S & T. • Do Problems PP2.11 on page 110 (or a page close to that) and PP5.1 on page 284 (or near there) – JSubmit it by Wednesday at 11:59 PM.

  3. A Problem Similar To Homework //********************************************************** // JBEvenAndOdd.java Author: Breecher // // This is a solution to Problem 5.5 // The problem states: // Design and implement an application that determines and // prints the number of odd, even and zero digits in an // integer value read from the keyboard. //********************************************************** import java.util.Scanner; public class JBEvenAndOdd { public static void main (String[] args) { int numberOfEvens = 0, numberOfOdds = 0, numberOfZeros = 0; int rightMostDigit; int input; Scanner scan = new Scanner( System.in ); System.out.println( "Give me a number: " ); input = scan.nextInt(); while ( input > 0 ) { rightMostDigit = input % 10; if ( rightMostDigit == 0 ) numberOfZeros++; else if ( (rightMostDigit % 2) == 0 ) numberOfEvens++; else numberOfOdds++; input = input / 10; } System.out.println( "The result is: "); System.out.println( " Number of even digits = " + numberOfEvens ); System.out.println( " Number of odd digits = " + numberOfOdds ); System.out.println( " Number of zero digits = " + numberOfZeros); } // End of main } // End of class

  4. Primitive Data There are eight primitive data types in Java Four of them represent integers: byte, short, int, long Two of them represent floating point numbers: float, double One of them represents characters: char And one of them represents boolean values: boolean 4

  5. Numeric Primitive Data The difference between the various numeric primitive types is their size, and therefore the values they can store: Type byte short int long float double Storage 8 bits 16 bits 32 bits 64 bits 32 bits 64 bits Min Value -128 -32,768 -2,147,483,648 < -9 x 1018 +/- 3.4 x 1038 with 7 significant digits +/- 1.7 x 10308 with 15 significant digits Max Value 127 32,767 2,147,483,647 > 9 x 1018 5

  6. Characters A char variable stores a single character Character literals are delimited by single quotes: 'a' 'X' '7' '$' ',' '\n' Example declarations: char topGrade = 'A'; char terminator = ';', separator = ' '; Note the distinction between a primitive character variable, which holds only one character, and a String object, which can hold multiple characters 6

  7. Character Sets A character set is an ordered list of characters, with each character corresponding to a unique number A char variable in Java can store any character from the Unicode character set The Unicode character set uses sixteen bits per character, allowing for 65,536 unique characters It is an international character set, containing symbols and characters from many world languages The ASCII characters are a subset of the Unicode character set, including: uppercase letters lowercase letters punctuation digits special symbols control characters A, B, C, … a, b, c, … period, semi-colon, … 0, 1, 2, … &, |, \, … carriage return, tab, ... 7

  8. Boolean A boolean value represents a true or false condition The reserved words true and false are the only valid values for a boolean type boolean done = false; A boolean variable can also be used to represent any two states, such as a light bulb being on or off 8

  9. Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion If/for/while -- sections 5.2, 5.5, 5.8 Interactive Programs Graphics Applets Drawing Shapes

  10. Addition Subtraction Multiplication Division Remainder + - * / % Expressions • An expression is a combination of one or more operators and operands • Arithmetic expressions compute numeric results and make use of the arithmetic operators: If either or both operands used by an arithmetic operator are floating point, then the result is a floating point 14 / 3equals • If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded) 4 8 /12equals 0 The remainder operator (%) returns the remainder after dividing the second operand into the first 14 % 3equals 2 8 % 12equals 8

  11. Operator Precedence • Operators can be combined into complex expressions result = total + count / max - offset; • Multiplication, division, and remainder are evaluated before addition, subtraction, and string concatenation • Arithmetic operators with the same precedence are evaluated from left to right, but parentheses can be used to force the evaluation precedence

  12. Operator Precedence • What is the order of evaluation in the following expressions? a + b + c + d + e a + b * c - d / e 1 2 3 4 3 1 4 2 a / (b + c) - d % e 2 1 4 3 a / (b * (c + (d - e))) 4 3 2 1

  13. Increment and Decrement • The increment and decrement operators use only one operand • The increment operator (++) adds one to its operand • The decrement operator (--) subtracts one from its operand • The statement count++; is functionally equivalent to count = count + 1; • Often we perform an operation on a variable, and then store the result back into that variable • Java provides assignment operators to simplify that process • For example, the statement • num += count; • is equivalent to • num = num + count;

  14. Assignment Operators • The right hand side of an assignment operator can be a complex expression • The entire right-hand expression is evaluated first, then the result is combined with the original variable • Therefore result /= (total - MIN) % num; is equivalent to result = result / ((total - MIN) % num);

  15. Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion If/for/while -- sections 5.2, 5.5, 5.8 Interactive Programs Graphics Applets Drawing Shapes

  16. Data Conversion • Sometimes it is convenient to convert data from one type to another • For example, in a particular situation we may want to treat an integer as a floating point value • These conversions do not change the type of a variable or the value that's stored in it – they only convert a value as part of a computation

  17. Data Conversion • Conversions must be handled carefully to avoid losing information • Widening conversions are safest because they tend to go from a small data type to a larger one (such as a short to an int) • Narrowing conversions can lose information because they tend to go from a large data type to a smaller one (such as an int to a short) • In Java, data conversions can occur in three ways: • assignment conversion • promotion • casting

  18. Conversions • Assignment conversion occurs when a value of one type is assigned to a variable of another • If money is a float variable and dollars is an int variable, the following assignment converts the value in dollars to a float money = dollars • Promotion happens automatically when operators in expressions convert their operands • For example, if sum is a float and count is an int, the value of count is converted to a floating point value to perform the following calculation: • result = sum / count;

  19. Data Conversion • Promotion happens automatically when operators in expressions convert their operands • For example, if sum is a float and count is an int, the value of count is converted to a floating point value to perform the following calculation: result = sum / count;

  20. Casting • Casting is the most powerful technique for conversion • Both widening and narrowing conversions is accomplished by explicitly casting a value • For example, if total and count are integers, but we want a floating point result when dividing them, we can cast total: result = (float) total / count; • Is this the same as result = (float) (total / count);

  21. Let’s Try It Out! //********************************************************** // JBLetsTryItOut02.java Author: Breecher // // Try converting between the various types using // Assignment int <- double, double <- int???? // Promotion double = double / int // Casting int = (int)double // Do also examples of precedence. //********************************************************** import java.util.Scanner; public class JBLetsTryItOut02 { public static void main (String[] args) { int i = 3, j = 4, k = 5; double d = 3.14159, f, g; double result; System.out.println( " 3 / 4 = " + ( 3 / 4 ) ); //i = d / 4; d = i; //i = d; f = (double)i / j; g = (double)(i / j ); System.out.println( " D = " + d + " F = " + f + " G = " + g); } }

  22. Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion If  brought in from Section 5.2 Interactive Programs Graphics Applets Drawing Shapes

  23. Flow of Control • Unless specified otherwise, the order of statement execution through a method is linear: one statement after another • The program can decide: • to execute a particular statement – or not • To execute a statement over and over • Decisions are based on boolean expressions (or conditions) that evaluate to true or false. • The order of statement execution is called the flow of control • The Java conditional statements for now are: • if statement • if-else statement

  24. The condition must be a boolean expression. It must evaluate to either true or false. condition evaluated ifis a Java reserved word true false statement If the condition is true, the statement is executed. If it’s false, the statement is skipped. The if Statement • The if statement has the following syntax: if ( condition ) statement;

  25. Boolean Expressions • A condition often uses one of Java's equality operators or relational operators, which all return boolean results: ==equal to !=not equal to <less than >greater than <=less than or equal to >=greater than or equal to • There’s a difference between the equality operator (==) and the assignment operator (=)

  26. The if Statement • Here’s an if statement: if (sum > MAX) delta = sum - MAX; System.out.println ("The sum is " + sum); First the condition is evaluated -- the value of sum is either greater than the value of MAX, or it is not If the condition is true, the assignment statement is executed -- if it isn’t, it is skipped. The statement(s) controlled by the if statement are indented. The use of a consistent indentation style makes a program easier to read and understand

  27. The if Statement • What do the following statements do? if (top >= MAXIMUM) top = 0; Sets top to zero if the current value of top is greater than or equal to the value of MAXIMUM if (total != stock + warehouse) inventoryError = true; Sets a flag to true if the value of total is not equal to the sum of stock and warehouse Which is performed first; the + or the != ???

  28. Logical Operators • Boolean expressions can also use the following logical operators: ! Logical NOT &&Logical AND ||Logical OR • They all take boolean operands and produce boolean results • Logical NOT is a unary operator (it operates on one operand) • Logical AND and logical OR are binary operators (each operates on two operands)

  29. Logical AND and Logical OR • The logical AND expression a && b is true if both a and b are true, and false otherwise • The logical OR expression a || b is true if a or b or both are true, and false otherwise

  30. Logical Operators • Expressions that use logical operators can form complex conditions final int MAX = 3; int total = 7; boolean found = false; if (total < MAX+5 && !found) System.out.println (“Is this line printed?"); What is the precedence here? How can you be sure? Boolean a = true, b = false, c = true if ( a && b || c ) System.out.println (“Is this line printed?…"); What happens on this line?

  31. Boolean Expressions • Here are some more examples

  32. Example Program //******************************************************************** // MinOfThree.java Listing 5.6 - Modified // Reads three integers from the user and determines the smallest value. //******************************************************************** import java.util.Scanner; public class MinOfThree { public static void main (String[] args) { int num1, num2, num3, min = 0; Scanner scan = new Scanner (System.in); System.out.println ("Enter three integers: "); num1 = scan.nextInt(); num2 = scan.nextInt(); num3 = scan.nextInt(); if (num1 < num2) if (num1 < num3) min = num1; else min = num3; else if (num2 < num3) min = num2; else min = num3; System.out.println ("Minimum value: " + min); } }

More Related