Lecture 2(2)

Chapter 2: Elementary Programming

Instructor Information

  • Instructor: Chris Franson

  • UID: [UID details not provided]


Identifiers

  • Definition: Identifiers are names that represent elements: variables, constants, methods, classes, and packages in a program.

  • Character Composition: Composed of letters, digits, underscores (_), and dollar signs ($).

  • Rules:

    • Must start with a letter, underscore, or dollar sign.

    • Cannot start with a digit.

    • Cannot be a reserved word.

    • Cannot be true, false, or null.

    • Can be of any length.

    • Case Sensitivity: Java is case sensitive; e.g., Area, area, and AREA are different identifiers.


Case Sensitivity in Java

  • Java differentiates between upper and lower case:

    • Examples:

      • "John" vs "john"

      • "car" vs "CAR"

      • "int" vs "Int"

      • "if" vs "If", where if is a valid Java keyword.


Declaring Variables

  • Variables store data used throughout a program.

  • Purpose: To hold user input, e.g., reading the radius from the user.

  • Naming: Choose descriptive names (e.g., radius for radius).

  • Declaration Syntax: datatype variableName;


Variables: Overview

  • Definition: A variable is a symbol that can hold/change values.

  • Variable Example Syntax:

    • int radius; // Declare an integer variable

    • double area; // Declare a double variable


Data Types in Java

  • Primitive Data Types (8 total):

    • boolean: true or false.

    • char: 16-bit character.

    • Integer types:

      • byte: -128 to 127.

      • short: -32768 to 32767.

      • int: -2147483648 to 2147483647.

      • long: -9223372036854775808 to 9223372036854775807.

    • Floating-Point Types:

      • float: 32-bit floating-point.

      • double: 64-bit floating-point.


Declaring and Initializing Variables

  • Single Declaration Example:

    • int count; // Declare count to be an integer variable

  • Multiple Declaration Example:

    • double radius, area; // Declare multiple variables of the same type.


Assignment Statements

  • Definition: Assigns a value to a variable after declaration.

  • Syntax: variable = expression; // = is the assignment operator.

  • Examples:

    • int x; x = 1;

    • double radius; radius = 1.0;

    • char a; a = 'A';


Declaring and Initializing in One Step

  • Example: int radius = 1; // Declare and initialize in one step.

  • Others: char firstLetter = 'A';, String aString = "A";


Character Data Type

  • Represents a single character in Java (ASCII or Unicode).

  • Character Literal: Enclosed in single quotes.

  • Examples:

    • char aLetter = 'A';

    • char aDigit = '8';

    • char uLetter = '\u0041';


Writing a Simple Program: Algorithm Example

  • Step 1: Read the circle's radius from user input.

  • Step 2: Compute area: area = π * radius * radius.

  • Step 3: Display the result.


Important Points on Standard Input and Output

  • System.out: Refers to output device (monitor).

  • System.in: Refers to input device (keyboard).

  • Scanner Class: Use to read input; necessitates import java.util.Scanner;

  • Example Creation: Scanner input = new Scanner(System.in);


Common Exercises

  • Write a program to:

    • Convert Celsius to Fahrenheit.

    • Compute area from radius and length of a cylinder.

    • Find averages from keyboard inputs.


Common Errors

  • Undeclared Variables: Accessing variables before declaration/initialization.

  • Integer Overflow: Exceeding the max value for a type, e.g., int value = 2147483647 + 1;

  • Round-off and Division Errors: Example with 1.0 - 0.1 * 5. Check the output to notice inaccuracies in floating-point operations.


Numeric Type Conversion Rules

  • Larger data type takes precedence in operations.

  • Examples: If one operand is double, other will be converted to double.


Type Casting in Java

  • Implicit Casting: Automatic conversion from a smaller to a larger type.

  • Explicit Casting: For converting a larger type to a smaller or specific type, e.g., truncating a double.

  • Example: int i = (int) 3.9; // Result is 3.

robot