LEC3

Numbers and Mathematics
  • Introduction to Programming: Covers numerical data types, operations, functions, and reading numbers from the console.
Announcements
  • Assignment Deadlines:
    • Assignment 1: Due on April 9, 11:59 PM
    • Assignment 2: Released on April 9, Due April 16, 11:59 PM
Numerical Data Types
  • Numerical Data Types in Java: Some common types include:
    • byte: Range: [−128,127][-128, 127], Storage: 8-bit signed
    • short: Range: [−32768,32767][-32768, 32767], Storage: 16-bit signed
    • int: Range: [−2147483648,2147483647][-2147483648, 2147483647], Storage: 32-bit signed
    • long: Range: [−9223372036854775808,9223372036854775807][-9223372036854775808, 9223372036854775807], Storage: 64-bit signed
    • float: Range: [−3.4028235E+38,3.4028235E+38][-3.4028235E+38, 3.4028235E+38], 32-bit IEEE 754
    • double: Range: [−1.7976931348623157E+308,1.7976931348623157E+308][-1.7976931348623157E+308, 1.7976931348623157E+308], 64-bit IEEE 754
Number Literals
  • Definition: A constant value directly present in the program like int i = 34;
    • Common examples: 34, 1000000, 5.0
Integer Literals
  • Integer Assignment Rules: An integer literal can be assigned to an integer variable if it fits the variable's range. E.g.,
    • byte b = 1000; causes a compilation error.
  • Types:
    • Default is int unless specified as long using L or l (preferably L).
Floating-point Literals
  • Characteristics: Written with a decimal point, treated as double by default.
    • To define a float, append f or F, for double, use d or D.
  • Example: 100.2f for float, 100.2d for double.
Scientific Notation
  • Utilization: Floating-point literals can also be in scientific notation, e.g.,
    • 1.23456e+2 is equivalent to ( 123.456 ) and 1.23456e-2 is equivalent to ( 0.0123456 ).
Numeric Operations
  • Basic Operations Overview:
    • + : Addition
    • - : Subtraction
    • * : Multiplication
    • / : Division
    • % : Remainder
Accuracy in Floating-point Calculations
  • Imprecision: Floating-point numbers are approximated leading to rounding errors:
    • Example: ( 1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 ) yields ( 0.5000000000000001 )
  • Integers: Stored precisely with exact results in operations.
Division and Remainder
  • Integer Division Behavior: Truncates fractional parts.
    • Example: ( 5 / 2 ) results in 2.
  • Remainder Operator Use: Allows checking even/odd.
    • Example: ( ext{day ext{(Saturday) }+ 10 ext{ days}} = (6 + 10) mod 7 = 2 )
Augmented Assignment Operators
  • Common Operators:
    • += : i += 8 equivalent to i = i + 8
    • -= : i -= 8
    • *= : i *= 8
    • /= : i /= 8
    • %= : i %= 8
Increment & Decrement Operators
  • Types:
    • Pre-decrement: --var
    • Post-decrement: var--
    • Pre-increment: ++var
    • Post-increment: var++
  • Example:
    • int j = ++i; (j is 2, i is 2)
Type Conversion Rules
  • Automatically: 1. If double, convert others to double; 2. If float, convert to float; 3. If long, convert to long; 4. Otherwise, to int.
Type Casting
  • Implicit Casting: E.g., double d = 3; (widening).
  • Explicit Casting: E.g., int i = (int)3.0;, truncates fractional part.
Operator Precedence
  • Order of operations defined by:
    • Parentheses > Unary Operators > Type Casting >
    • Multiplication/Division/Remainder > Addition/Subtraction >
    • Relational > Equality > Logical > Assignment.
Operator Associativity
  • Associativity: Determines order with same precedence, generally left-to-right except for assignment which is right-to-left.
Reading Numbers from the Console
  • Creating Scanner: Scanner input = new Scanner(System.in);
  • Methods for Reading:
    • nextByte(), nextShort(), nextInt(), nextLong(), nextFloat(), nextDouble().
  • Example Usage:
    • double d = input.nextDouble();
Mathematical Functions
  • Math Class Methods:
    • Constants: Math.PI, Math.E
    • Trigonometric Methods: Math.sin(), Math.cos(), etc.
    • Exponent Methods: Math.pow(), Math.sqrt(), etc.
    • Rounding Methods: Math.ceil(), Math.floor(), etc.
    • Miscellaneous: Math.min(), Math.max(), Math.abs(), and Math.random().
Next Topics Review
  • Focus on characters and strings in the next lecture.