1 / 17

Announcements

This announcement provides information on the built-in data types for variables in Java, naming variables and constants, input and output, and expressions. It also mentions what students should know for the upcoming quiz.

jhiltner
Download Presentation

Announcements

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. Announcements • Quiz 1 Next Monday

  2. Built-In (or Primitive) Data Types for Variables • int: Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) • float: Real Number (i.e., integer part, decimal part, and exponent part) Range of Typically (+/-) 10e-45 to 10e38 • double: Larger Real Number ((+/-) 10e-324 to 10e308) • char: Character

  3. Naming Variables in JAVA: Identifiers • Can Use Letters: Remember That JAVA is Case Sensitive (e.g., NumWidgets Is Not the Same as numwidgets) • Can Use Digits 0-9, $, and Underscore • Cannot Start with a Digit • Cannot Contain Spaces or Other Characters • Cannot Use JAVA Keywords

  4. Naming Variables (Cont) • Should Use a Meaningful, Descriptive Name so that Variable’s Use Is Easily Understood: • Examples: counter, second, minute, length, width • Be Consistent with Case; Usually Lower Case with Upper Case for Second Part of Variable Name • Examples: averageRainfall, totalStudentGrades, maxBuildingHeight, minPackageWeight;

  5. Named Constants • Constant: An Identifier that Is Initialized to a Value that Cannot Change • Usually Declared at Top of Program using Keyword final • Standard Naming of Constants Is to Use All Upper Case Letter with or without Underscore between Words • All Constants Should Be Initialized • Syntax: final int MAXHT = 100;

  6. Advantages of Constants • Easier to Understand • Easier to Modify • Example: Compare using Number 100 in Program versus Constant MAXHT

  7. Named Constants public class MyProgram { public static void main( String [ ] args) { final int MAXHT = 100; ... currentHeight > MAXHT ... bridgeHeight == MAXHT ... bridgeHeight + newAddition >= MAXHT ... // used MAXHT 223 times in this program } }

  8. Literal Constants (Values) • Constants Whose values Are Already Known: Characters (Specified Inside Single Quotes): ‘A’ , ‘a’ , ’5’ , ’ ‘ , ’\n’ (newline) , ’\0’ (NULL Character) Integers: 10, 1345, -34 Float or Double: 2.3, -45.18, 10.6e6 String (Specified Inside Double Quotes): “HELLO” , “What a great deal.” , “5”

  9. Input and Output • Also Known as I/O • Output : System.out.println(“Hello World!!”); • Input (Scanner): import java.util.Scanner; Scanner scan = new Scanner(System.in); scan.nextInt(); scan.nextFloat(); scan.nextDouble(); scan.next(); //String to whitespace scan.nextLine(); // String to end of line • All Wait until User Inputs Particular Type

  10. Input and Output Example import java.util.Scanner; public class MyProgram { public static void main( String [ ] args) { Scanner scan = new Scanner(System.in); int numEntered; System.out.print(“Enter an integer:”); /// The following waits until user hits Enter numEntered = scan.nextInt(); System.out.println(“You entered: “ + numEntered); } }

  11. Characters and Strings • Strings in java.lang package • Java.lang imported automatically

  12. JAVA Strings • Declaration: String yourName; • Assigning a Value to a String: yourName = “A. Goose”; • String Constants (Values) Must Be Enclosed in Double Quotes

  13. Input String Example import java.util.Scanner; public class MyProgram { public static void main( String [ ] args) { Scanner scan = new Scanner(System.in); String lastname; System.out.print(“Enter last name:”); lastname = scan.next(); System.out.println(“You entered: “ + lastname); } }

  14. Expressions • Expression: A Sequence of One or More Identifiers and Operators that Evaluates to a Value • Operator: A Symbol Expressing a Way to Modify a Value or Values (e.g., + for Addition) • Operand: A Value Being Modified by an Operator • Example Expressions: 5 currentHeight currentHeight + 10

  15. Arithmetic Expressions • Standard Arithmetic Operations Can Be Performed: Addition, Subtraction, Multiplication, Division • Standard Arithmetic Operators Can Be Used for These Operations: +, -, *, / • Others: % - “Modulo (Mod)” – Remainder after Integer Division -- Decrement (Subtract 1) ++ Increment (Add 1)

  16. Order of Operations • Precedence: Level of Importance of Operations Multiplicative Operators Have Higher Precedence than Additive Operators: *, /, % Higher +, - Lower • Associativity: Order of Operation for Equal Level Precedence Most Operators Have Left-to-Right Associativity Use Parentheses to Force Differing Precedence of Operations

  17. Know for the Quiz • All Terms (Underlined Items) • Variable Declaration, Initialization, and Assignment • Constant Declaration • Expressions • Operators • Input (Scanner) and Output (System.out) • Everything in Homework through Lab 2

More Related