Exposure Java Slides - Allen Independent School District

Exposure Java Slides - Allen Independent School District

Exposure Java 201 ChapterAP 3 Slides For CS Editio Java Simple Data Types PowerPoint P resentation created by: Mr. John L. M. Schram and Mr. Leon Schram Authors of Ex posure Java Section 3.2 Declaring Variables Java Keywords Reserved Words Part of the Java language Examples: public, static, void Pre-defined Java Identifiers

Defined in Java Libraries Examples: print & println User-defined Identifiers Examples: shown in this chapter // Java0301.java // This program demonstrates how to declare two integer variables // with two different initial values. public class Java0301 { public static void main (String args[]) { "a equals 10 int a = 10; "a becomes 10" int b = 25; System.out.println(); System.out.println(a); System.out.println(b); System.out.println(); } } a 10

b 25 "a stores the value 10 25 // Java0302.java // This program is the same as Java0301.java without assigning values // to the variables. Java does not compile a program that attempts to use // unassigned "simple" data types. public class Java0302 { public static void main (String args[]) { int a; int b; System.out.println(a); System.out.println(b); } } a ? b ?

What does 'a' store? What does 'b' store? // Java0303.java // This program demonstrates that it is possible to declare a variable // identifier and initialize the variable in the same statement. // It is a good habit to initialize variables where they are declared. public class Java0303 { public static void main (String args[]) { int a = 10; int b = 25; a System.out.println(); System.out.println(a); 10 System.out.println(b); System.out.println(); } } b 25 10 25 // Java0304.java

// This program combines output of literals and variables. // "a: " is a string literal, which displays the characters a: // a is an integer variable, which displays its integer value 10. public class Java0304 { public static void main (String args[]) { int a = 10; int b = 25; System.out.println("a: " + a); System.out.println("b: " + b); } } a 10 a: 10 b: 25 b 25 Section 3.3 The Integer Data Types

Java Integer Data Types Data Type Bytes used in memory byte 1 -128 127 2 -32,768 32,767 4 -2,147,483,648 2,147,483,647 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807

short int long Minimum Value Maximum Value // Java0305.java // This program demonstrates the five integer operations. public class Java0305 { public static void main (String args[]) { int a = 0; int b = 25; int c = 10; a = b + c; 25 25 25 25 25 +

* / % 10 10 10 10 10 = = = = = 35 15 250 2 5 // Addition System.out.println(b + " + " + c + " = " + a); a = b c;

// Subtraction System.out.println(b + " - " + c + " = " + a); a = b * c; // Multiplication System.out.println(b + " * " + c + " = " + a); a = b / c; // Integer Quotient Division System.out.println(b + " / " + c + " = " + a); a = b % c; System.out.println(b + " % " + c + " = " + a); } } // Integer Remainder Division Integer Quotient Division Examples 12 12 12

12 12 12 12 / / / / / / / 1 2 3 4 5 6 7 = = = = = = =

12 6 4 3 2 2 1 12 12 12 12 12 12 12 / / / / / / / 8 9 10

11 12 13 0 = = = = = = = 1 1 1 1 1 0 undefined Integer Remainder Division Examples 12 12 12 12 12

12 12 % % % % % % % 1 2 3 4 5 6 7 = = = = = = = 0

0 0 0 2 0 5 12 12 12 12 12 12 12 % % % % % % % 8 9 10 11 12

13 0 = = = = = = = 4 3 2 1 0 12 undefined What do the green numbers have in common? 12 12 12 12 12 12 12

% % % % % % % 1 2 3 4 5 6 7 = = = = = = = 0 0 0

0 2 0 5 12 12 12 12 12 12 12 % % % % % % % 8 9 10 11 12 13 0

= = = = = = = They are all factors of 12. 4 3 2 1 0 12 undefined Flashback To Elementary School Long Division Using / gives you the integer quotient. 4 3)12 12 0

2 5)13 10 3 0 15)12 0 12 0 12)0 0 0 ??? 0)12 ??? ??? Using % gives you the integer remainder. Section 3.4 Real Number Data Types // Java0306.java

10.0 + 3.33333333 = 13.33333333 // This program demonstrates the double data type which is used for real numbers 10.0 3.33333333 = 6.66666667 // It also demonstrates the four real number operations. 10.0 * 3.33333333 = 33.3333333 public class Java0306 10.0 / 3.33333333 = 3.0000000030000002 { public static void main (String args[]) { double d1 = 0; double d2 = 10.0; double d3 = 3.33333333; d1 = d2 + d3; // Addition System.out.println(d2 + " + " + d3 + " = " + d1); d1 = d2 d3; // Subtraction System.out.println(d2 + " - " + d3 + " = " + d1);

d1 = d2 * d3; // Multiplication System.out.println(d2 + " * " + d3 + " = " + d1); d1 = d2 / d3; // Real Number Quotient Division System.out.println(d2 + " / " + d3 + " = " + d1); } } NOTE: Calculations performed with double variables are accurate to 15 decimal places. Java Real Number Data Types/Operations float 4 bytes double 8 bytes Addition: 6.75 + 2.5 = 9.25

Subtraction: 6.75 2.5 = 4.25 Multiplication: 6.75 * 2.5 = 16.875 Real # Quotient Division: 6.75 / 2.5 = 2.7 What About Real # Remainder Division? Java textbooks usually state that remainder or modulus division does not exist for real numbers. Real numbers do not have remainder division in any practical sense. There also is the issue that Java is based on C++, which does not allow remainder division with real number data types. Even though the following examples do not work in C++, they actually do work in Java. 10.0 % 5.0 10.0 % 3.0 3.0 % 10.0 6.75 % 2.5

= = = = 0.0 1.0 3.0 1.75 Section 3.5 Numerical Representation Limits // Java0307.java intNum: 1000 // This program demonstrates memory overflow problems. intNum: 1000000 // Saving memory is important, but too little memory can // also cause problems. intNum: 1000000000 public class Java0307 intNum: -727379968

{ public static void main (String args[]) { int intNum = 1000; System.out.println("intNum: " + intNum); intNum = intNum * 1000; System.out.println("intNum: " + intNum); intNum = intNum * 1000; System.out.println("intNum: " + intNum); intNum = intNum * 1000; System.out.println("intNum: " + intNum); } } The Odometer Analogy in Decimal 9 9 9 9 9 9

+ 0.1 mile = 0 0 0 0 0 0 When many cars reach 100,000 miles their odometers cease to be accurate. The Odometer Analogy with a short integer The largest possible short integer value is 32,767 which in binary looks like this: 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 If we add 1 we get this result: 1 0 0 0 0 0+ 01 = 0 0 0 0 0 0 0 0 0 32767 + 1 = 32768

How Positive Numbers Give Negative Results The first bit in a number is the sign bit It determines if a number is positive or negative 0 = Positive 1 = Negative 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 32767 + 1 = -32768 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Memory Overflow Problems Memory overflow is a situation where the assigned value of a variable exceeds the allocated storage space. The resulting value that is stored will be inaccurate and can change from positive to negative or negative to positive. Avoid memory overflow problems by using a data type that can handle the size of the assigned values. It is important to save computer memory. However, do not be so stingy with memory that overflow problems occur. // Java0308.java num1: 1.012345

// This program shows that there is a memory storage limitation to num2: 1.0123456789 // how many digits are stored beyond the decimal point. num3: 1.0123456789012346 public class Java0308 { public static void main (String args[]) { double num1 = 1.012345; double num2 = 1.0123456789; double num3 = 1.01234567890123456789; System.out.println("num1: " + num1); System.out.println("num2: " + num2); System.out.println("num3: " + num3); System.out.println("\n\n"); } } // Java0309.java num1: 10.0 // This program demonstrates another error. num2: 3.0 // The program output displays a number that is mathematically incorrect.

num3: 3.333333333335 public class Java0309 { public static void main (String args[]) { double num1 = 10.0; double num2 = 3.0; double num3 = num1 / num2; System.out.println("num1: " + num1); System.out.println("num2: " + num2); System.out.println("num3: " + num3); System.out.println("\n\n"); } } Rounding E are commo long decim Section 3.6 Arithmetic Shortcut Notations

// Java0310.java num equals 10 // This program shows "unary" arithmetic shortcut notation in Java. numalways equals 11 // Note that "postfix" x++ & "prefix" ++x don't have the same result. num equals public class Java0310 { num equals public static void main (String args[]) num equals { int num = 10; System.out.println("num equals " + num); num++; System.out.println("num equals " + num); ++num; System.out.println("num equals " + num); System.out.println("num equals " + num++); System.out.println("num equals " + num);

System.out.println("num equals " + ++num); System.out.println("num equals " + num); System.out.println(); } } 12 13 14 Java Unary Operators k++; is the same as: k = k + 1; ++k; is the same as: k = k + 1; k--; is the same as: k = k - 1; --k; is the same as: k = k - 1; Proper Usage of Shortcuts

Proper Usage: k++; System.out.println(k); --k; System.out.println(k); Problematic Usage: System.out.println(k++); System.out.println(--k); // Java0311.java // This program shows arithmetic assignment operations in Java. // x+=10; is the same as x = x + 10; public class Java0311 { public static void main (String args[]) { int x = 10; System.out.println("x equals " + x); x += 10; System.out.println("x equals " + x); x -= 10; System.out.println("x equals " + x); x *= 10; System.out.println("x equals " + x); x /= 10; System.out.println("x equals " + x); x %= 10;

System.out.println("x equals " + x); System.out.println(); } } x x x x x x equals equals equals equals equals equals 10 20 10 100 10 0 Binary Operator Shortcuts No Shortcut Notation Shortcut

Notation k = k + 5; k += 5; k = k - 5; k -= 5; k = k * 5; k *= 5; k = k / 5; k /= 5; k = k % 5; k %= 5; // Java0312.java // This program demonstrates very bad programming style by // combining various shortcuts in oneBefore: statement. It is 10 difficult // to determine what actually is happening. After: 32

public class Java0312 { public static void main (String args[]) { int x = 10; System.out.println("Before: " + x); x += ++x + x++; System.out.println("After: " + x); System.out.println(); } } Do not waste any time trying to figure out why the answer is 32. The point being made here is this code is very confusing and should be avoided. Shortcut Warning Do not combine shortcuts in one program statement!!! ++num +=

++num + num++; Section 3.7 The char & String Data Types // Java0313.java // This program demonstrates the data types. // It also demonstrates how assignment can be "chained" with // multiple variables in one statement. public class Java0313 { public static void main (String args[]) { char c1 = 'A'; char c2 = 'B'; char c3 = 'C'; System.out.println("The three characters are: " + c1 + c2 + c3); c1 = c2 = c3 = 'Q'; System.out.println("The three characters are: " + c1 + c2 + c3);

System.out.println(); } } // Java0313.java // This program demonstrates the data types. // It also demonstrates how assignment can be "chained" with // multiple variables in one statement. c1 c2 c3 public class Java0313 { A B C public static void main (String args[]) { char c1 = 'A'; char c2 = 'B'; char c3 = 'C'; System.out.println("The three characters are: " + c1 + c2 + c3); c1 = c2 = c3 = 'Q'; System.out.println("The three characters are: " + c1 + c2 + c3);

System.out.println(); } } // Java0313.java // This program demonstrates the data types. // It also demonstrates how assignment can be "chained" with // multiple variables in one statement. c1 c2 c3 public class Java0313 { A B C public static void main (String args[]) { c1 c2 c3 char c1 = 'A'; char c2 = 'B'; Q

Q Q char c3 = 'C'; System.out.println("The three characters are: " + c1 + c2 + c3); c1 = c2 = c3 = 'Q'; System.out.println("The three characters are: " + c1 + c2 + c3); System.out.println(); } } This is a special shortcut called CHAINING. // Java0313.java // This program demonstrates the data types. // It also demonstrates how assignment can be "chained" with // multiple variables in one statement. c1 c2 c3 public class Java0313 { A B

C public static void main (String args[]) { c1 c2 c3 char c1 = 'A'; char c2 = 'B'; Q Q Q char c3 = 'C'; System.out.println("The three characters are: " + c1 + c2 + c3); c1 = c2 = c3 = 'Q'; System.out.println("The three characters are: " + c1 + c2 + c3); System.out.println(); } } The three characters are: ABC The three characters are: QQQ // Java0314.java // This program demonstrates the data type. public class Java0314 { firstName lastName public static void main (String args[])

Kathy Smith { String firstName = "Kathy" ; String lastName = "Smith"; System.out.println("firstName: " + firstName); System.out.println("lastName: " + lastName); System.out.println("Complete Name: " + firstName + " " + lastName); System.out.println(); } } firstName: lastName: Complete Name: Kathy Smith Kathy Smith Don't Get Confused! These may all look the

same Value Data Type 7 int 7.0 double '7' char "7" String but the compute treats them different String Concatenation Concatenation is the appending (or joining) of 2 or more strings.

"Hello" + "World" = "HelloWorld" "Hello" + " " + "World" "100" + "200" = = "Hello World" "100200" The plus operator ( + ) is used both for arithmetic addition and string concatenation. The same operator performs 2 totally different operations. This is called overloading. Section 3.8 The boolean Data Type // Java0315.java

// This program demonstrates the data type. // The boolean type can only have two values: true or false. public class Java0315 { public static void main (String args[]) { boolean value = true; System.out.println("value: " + value); value = false; System.out.println("value: " + value); System.out.println(); } } value: true value: false value true value false AP Exam Alert The int, double, boolean and String data types will be tested. The byte, short, long, float and char data types will NOT be tested.

Section 3.9 Declaring Constants // Java0316.java intConst: 100 // This program demonstrates how to create "constant" identifier values with 3.14159 // the keyword. Removing thedoubleConst: comments from the three assignment charConst: Q // statements will result in compile errors. Output with comme public class Java0316 { public static void main (String args[]) { final int intConst = 100; intConst final double doubleConst = 3.14159; 100

final char charConst = 'Q'; // intConst++; doubleConst // doubleConst = 1234.4321; 3.14159 // charConst = 'A'; System.out.println("intConst: " + intConst); System.out.println("doubleConst: " + doubleConst); System.out.println("charConst: " + charConst); System.out.println(); charConst } Q } // Java0316.java // This program demonstrates how to create "constant" identifier values with // the keyword. Removing the comments from the three assignment // statements will result in compile errors. public class Java0316 { public static void main (String args[]) {

final int intConst = 100; intConst final double doubleConst = 3.14159; 100 final char charConst = 'Q'; intConst++; doubleConst doubleConst = 1234.4321; 3.14159 charConst = 'A'; System.out.println("intConst: " + intConst); System.out.println("doubleConst: " + doubleConst); System.out.println("charConst: " + charConst); System.out.println(); charConst } Q } Output with comment Section 3.10 Documenting Your Programs

// Java0317.java // This is an example of a poorly written program with single-letter variables. // Do you have any idea what this program does? public class Java0317 { public static void main (String args[]) { double a; double b; double c; double d; double e; a = 35; b = 8.75; c = a * b; d = c * 0.29; e = c - d; System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); System.out.println("e = " + e); System.out.println(); } } a b

c d e = = = = = 35.0 8.75 306.25 88.8125 217.4375 // Java0318.java // This program does exactly the same thing as the previous program. // By using self-commenting variables, the program is much easier to read and understand. public class Java0318 { Hours Worked: public static void main (String args[]) Hourly Rate: { double hoursWorked; Gross Pay: double hourlyRate;

Deductions: double grossPay; double deductions; Net Pay: double netPay; hoursWorked = 35; hourlyRate = 8.75; grossPay = hoursWorked * hourlyRate; deductions = grossPay * 0.29; netPay = grossPay - deductions; System.out.println("Hours Worked: " + hoursWorked); System.out.println("Hourly Rate: " + hourlyRate); System.out.println("Gross Pay: " + grossPay); System.out.println("Deductions: " + deductions); System.out.println("Net Pay: " + netPay); System.out.println(); } } 35.0 8.75 306.25 88.8125 217.4375

// Java0319.java // This program adds a multi-line comment at the beginning to help explain the program. // Several single-line comments are also added to provide more detail for each variable. /******************************************************************** ** ** ** Payroll Program ** ** Written by Leon Schram 09-09-09 ** ** ** ** This program takes the hours worked and hourly rate of ** ** an employee and computes the gross pay earned. ** ** Federal deductions are computed as 29% of gross pay. ** ** Finally the take-home pay or net pay is computed by ** **

subtraction deductions from gross pay. ** ** ** ********************************************************************/ public class Java0319 { public static void main { double hoursWorked; double hourlyRate; double grossPay; double deductions; double netPay; hoursWorked = 35; hourlyRate = 8.75; (String args[]) // // // // // hours worked per week payrate earned per hour total earnings in a week total federal tax deductions employee take-home pay

The rest of the program is identical to the previous one and is not shown here. Section 3.11 Mathematical Precedence Hidden Math Operations Mathematics Java Source Code 5XY 5*X*Y 4X + 3Y 4*X + 3*Y 6(A - B) 5 7 A + B A - B AB XY

6*(A - B) 5.0/7.0 (A + B)/(A - B) (A * B)/(X * Y) Mathematical Precedence PEMDAS Parentheses Exponents Multiplication & Division Addition & Subtraction Please Excuse My Dear Aunt Painted Elephants March Do // Java0320.java a = 1000.0 b = 100.0 // This program demonstrates mathematical precedence in Java operations. public class Java0320 a + b * { public static void main (String args[]) (a + b) { double a, b, c, result; a = 1000;

a / b * b = 100; c = 2.5; c = 1250.0 * c = 2750.0 c = 25.0 System.out.println(); a / (b * c) = 4.0 System.out.println("a = " + a + " b = " + b + " c = " + c); result = a + b * c; System.out.println("\na + b * c = " + result); result = (a + b) * c; System.out.println("\n(a + b) * c = " + result); result = a / b * c; System.out.println("\na / b * c = " + result); result = a / (b * c); }

} System.out.println("\na / (b * c) = " + result); System.out.println(); c = 2.5 Section 3.12 Type Casting // Java0321.java // This program demonstrates that the intended computation may not be is // performed by Java. The expression on the right side Why of the assignment an // operator is performed without knowledge of thethe type answer on the left side. integer? public class Java0321 { public static void main (String args[]) {

int nr1 = 1000; int nr2 = 3000; int nr3 = 6000; double mean; mean = (nr1 + nr2 + nr3) / 3; System.out.println("The mean equals: " + mean); System.out.println(); } The mean equals: } 3333.0 // Java0321.java // This program demonstrates that the intended computation may not be // performed by Java. The expression on the right side of the assignment // operator is performed without knowledge of the type on the left side. public class Java0321 { public static void main (String args[]) { int nr1 = 1000; The sum of 3 int nr2 = 3000; int nr3 = 6000;

is an integer. double mean; mean = (nr1 + nr2 + nr3) / 3; System.out.println("The mean equals: " + mean); System.out.println(); } The mean equals: } 3333.0 integers // Java0321.java // This program demonstrates that the intended computation may not be // performed by Java. The expression on the right side of the assignment // operator is performed without knowledge of the type on the left side. public class Java0321 { public static void main (String args[]) { int nr1 = 1000; When an integer is divided int nr2 = 3000; by another integer, you get

int nr3 = 6000; integer quotient division. double mean; mean = (nr1 + nr2 + nr3) / 3; System.out.println("The mean equals: " + mean); System.out.println(); } The mean equals: } 3333.0 // Java0321.java // This program demonstrates that the intended computation may not be // performed by Java. The expression on the right side of the assignment // operator is performed without knowledge of the type on the left side. public class Java0321 { public static void main (String args[]) { The fact that mean is a double int nr1 = 1000; has absolutely NO effect on the

int nr2 = 3000; calculations which take place int nr3 = 6000; on the other side of the = sign. double mean; mean = (nr1 + nr2 + nr3) / 3; System.out.println("The mean equals: " + mean); System.out.println(); } The mean equals: } 3333.0 // Java0322.java // This program corrects the logic error of Java0321.java. // Type casting is used to "force" real number quotient division. public class Java0322 { public static void main (String args[]) { int nr1 = 1000; int nr2 = 3000; int nr3 = 6000;

double mean; mean = (double) (nr1 + nr2 + nr3) / 3; System.out.println("The mean equals: " + mean); System.out.println(); } The mean equals: 3333.3333333333335 } // Java0323.java (double) // This program demonstrates (char) // "type casting" between (int) // different data types. intVal intVal dblVal (char) dblVal (int) chrVal public class Java0323 (double) chrVal { public static void main (String args[])

{ int intVal = 65; double dblVal = 70.1; char chrVal = 'B'; System.out.println("(double) intVal 65 System.out.println("(char) intVal 65 System.out.println("(int) dblVal 70.1 System.out.println("(char) dblVal 70.1 System.out.println("(int) chrVal B System.out.println("(double) chrVal B System.out.println(); } } 65 65 70.1 70.1 B B becomes becomes becomes

becomes becomes becomes 65.0 A 70 F 66 66.0 becomes " + (double) intVal); becomes " + (char) intVal); becomes " + (int) dblVal); becomes " + (char) dblVal); becomes " + (int) chrVal); becomes " + (double) chrVal);

Recently Viewed Presentations

  • The Gilded Age - Northland Preparatory Academy

    The Gilded Age - Northland Preparatory Academy

    Era of Change The Gilded Age gets its name from a book by Mark Twain, criticizing political leaders of the day Formative days of the US- in both politics and business Massive agricultural expansion into the west= influx of goods...
  • Commissioned Corps of the U.S. Public Health Service

    Commissioned Corps of the U.S. Public Health Service

    Vulnerable Populations. The United States Public Health Service has committed to prioritizing service to domestic vulnerable populations. All new calls to active duty are limited to three federal agencies with direct service populations, and based off of the needs of...
  • The Role of the SAAMF (South African Automotive

    The Role of the SAAMF (South African Automotive

    It is the fifth largest auto industry recall in U.S. history. Ford said the recall of 1994-2002 model-year vehicles includes the company's hot-selling F-150 pickup truck, Ford Expedition, Lincoln Navigator and Ford Bronco. The company said it would start sending...
  • Mental Skills Progression Framework - LT Scotland

    Mental Skills Progression Framework - LT Scotland

    The mental skills progression framework has been put together by the Argyll and Bute numeracy co-ordinators, Pauline Inglis and myself, using a wide range of documents and resources to help us, particularly the Dumfries and Galloway Mental Agility Framework, the...
  • Project No. and Title - Ship Structure

    Project No. and Title - Ship Structure

    SR 1459 - Criteria for Determining Fracture Repair Procedures Ge (George) Wang ABS Corporate Technology February 2010 SR 1459 - Criteria for Determining Fracture Repair Procedures Goals of Project Develop a comprehensive and usable steel hull fracture repair guide Analyze...
  • Diapositiva 1 - WordPress.com

    Diapositiva 1 - WordPress.com

    agua/dÍa para aumentar la diuresis 4- aumentar la excresiÓn de cis por el uso de d- penicil-amina (b,b - dimetilcisteÍna) que forma un di sulfuro mezcla cisteÍna- penicilamina, mas soluble se excreta por orina - este tratamiento si bien es...
  • Odyssey Analytical Discussion

    Odyssey Analytical Discussion

    OdysseyAnalytical Discussion. Books 11 & 12. Book 11The Kingdom of the Dead. ... Relationship between Odyssey and Anticleia - What can we conclude? ... Ajax. Minos. Orion. Tityus. Tantalus. Sisyphus. Heracles (Hercules) Encounter with Achilles. Concepts of . nostos.
  • Citizen Review Panels Home Visiting Symposium October 11,

    Citizen Review Panels Home Visiting Symposium October 11,

    BTPL: works or plays cooperatively with others, follows oral directions or classroom rules, completes home or class work on time. ... $1,000 to serve a family during pregnancy is totally recovered before a child's birth due to reductions in LBW.