1 / 15

Building Java Programs Chapter 2

Building Java Programs Chapter 2. Variables and string concatenation. Variables. Variable declaration. Declaring a variable tells Java to set aside a place to store a value. Variables must be declared before they can be used. Syntax: <type> < name> ; // The name is an identifier .

shay-cannon
Download Presentation

Building Java Programs Chapter 2

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. Building Java ProgramsChapter 2 Variables and string concatenation

  2. Variables

  3. Variable declaration • Declaring a variable tells Java to set aside a place to store a value. Variables must be declared before they can be used. • Syntax: <type> <name>; // The name is an identifier. int x; double myGPA; x myGPA

  4. Variable assignment • Assigning a value to a variable updates its contents, dumping out whatever used to be there. The value can be an expression: the variable stores the result. • Syntax: <name> = <expression>; • intmyFirstVariable; • myFirstVariable = 307; • myFirstVariable = -3 - 4; myFirstVariable 307 myFirstVariable -7 myFirstVariable

  5. Variable assignment • Assignment uses = • It is not an algebraic equation. • = means: "store the value at right in variable at left“ • The right side expression is evaluated first,and then its result is stored in the variable at left. • What happens here? int x = 3; x = x + 2; // ??? 3 X 5 X

  6. Expressions with variables • Variables can stand in for values in expressions. • double cookiesPerMinute = 3.5; • intcookiesInJar = 12; • double totalMinutes = cookiesInJar / cookiesPerMinute; • int minutes = (int) totalMinutes; • int seconds = ((int) (totalMinutes * 60)) % 60; 3.429 totalMinutes 3 minutes 25 seconds

  7. Shorthand Arithmetic Operators • Some operations are so commonly done that shorthand operators are included in Java. • total = total + 33; => total += 33; • price = price * taxRate; => price *= taxRate; • numAwards = numAwards + 1; => ++numAwards; • or numAwards++; • cookiesInJar = cookiesInJar – 1; => --cookiesInJar; • or cookiesInJar--;

  8. Expanded Precedence Table This table is on page 86 of your book

  9. Programming practice • Write a program that will calculate the percent you earned on your Monday quiz. • Declare variables for • percent, points earned, points possible

  10. String Concatenation • Like “adding” strings together, it uses the + or += operators. • String myMessage = "Hello" + " and welcome"; • myMessage += " to my party!"; • myMessage += "\nIt's been going on for " + 3 + " years straight!"; • System.out.println(myMessage); Hello and welcome to my party! It's been going on for 3 years straight!

  11. example • "2 + 2" + 3 + 4 • "2 + 23" + 4 • "2 + 234"

  12. example • 3 + 4 + " 2 + 2" • 7 + " 2 + 2" • "7 2 + 2"

  13. example • 2 + " times " + 3 + " = " + 2 * 3 • 2 + " times " + 3 + " = " + 6 • "2 times " + 3 + " = " + 6 • "2 times 3" + " = " + 6 • "2 times 3 = " + 6 • "2 times 3 = 6"

  14. In your notebook… • 2 + " plus " + 3 + " = " + 2 + 3 • "2 plus" + 3 + " = " + 2 + 3 • … • "2 plus 3 = 23" • How do you fix it? • 2 + " plus " + 3 + " = " + (2 + 3) • … • "2 plus 3 = 5"

  15. Programming practice 2 • Create a Java class called Area that has the following methods: • Square • Rectangle • Triangle • Circle • Sphere • Each method should declare 1-2 variables (one for base, height, length, etc.), initialize the variables to 1, and print the area or surface area of the shape. • Have the main method call each shape method to test that your program works!

More Related