Java Fundamentals: Constants, Char Data Types, and Scanner Input Handling

SLIIT - IT1120: Introduction to Programming

  • Institution: Sri Lanka Institute of Information Technology (SLIIT)
  • Faculty: Faculty of Computing
  • Course Code: IT1120
  • Instructional Faculty: Dr. Junius Anjana
  • Academic Timing: Year 01 and Semester 01
  • Lecture Number: Lecture 3
  • Core Topics Covered: Java Constants, Char Data Type, and User Input Handling

Java Constants

  • Definition: In the Java programming language, a constant is defined as a specific type of variable that maintains a single, fixed value throughout the entire execution of a program.
  • Declaration Keywords:
    • final: Used to ensure the variable value cannot be modified once assigned.
    • static: Often used in conjunction with final to define constants at the class level.

Char Data Type

  • General Characteristics:
    • Characters are typically stored using the specialized variable type char.
    • Characters can also be stored within any integer-based data type variable.
    • In Java, characters are versatile and can be treated as either an integer value or a character literal.
  • Character Encoding Standards:
    • A significant number of modern computers utilize the ASCII character set, which stands for American Standard Code for Information Interchange.

Relational Operators and Truncation in Decimal Values

  • Data Types Involved: This discussion pertains specifically to decimal numbers stored as float or double data types.
  • The Concept of Truncation:
    • When computers store these decimal types, they may provide an approximation by rounding off digits that exceed the storage capacity. This phenomenon is formally known as truncation.
  • Functional Warning:
    • Due to the inherent truncation errors associated with storing float and double values, these specific types should never be compared using the standard relational operators for equality (==) or inequality (!=).

Java Libraries and Class Frameworks

  • Primary Purpose: Java libraries are extensive collections of pre-written code modules designed to add specific functionalities to programs. They serve as essential tools for developers to improve efficiency and reduce the effort required during the software building process.
  • Category 1: Standard Libraries (Java Class Libraries):
    • These are included with the Java development environment by default.
    • They provide a significant array of useful methods and classes for fundamental tasks, such as:
      • Reading input provided by the user.
      • Managing and working with various files.
      • Performing complex mathematical calculations.
  • Category 2: External Libraries:
    • These are supplemental libraries that provide specialized functionality for niche or specific tasks that are not covered by the standard distribution.
    • Examples include:
      • Graphics processing for video game development.
      • Establishing connections to external databases.
      • Networking tools for working with the internet.
      • User-written libraries created by individual developers or organizations.

Handling Keyboard User Input with the Scanner Class

  • Library Location: The standard library responsible for input handling is java.util.
  • The Scanner Class: This class is specifically required to capture input entered via the keyboard.
  • Implementation Steps:
    • Step 1: Import the Library: You must explicitly inform the program to include the Scanner class using the syntax: import java.util.Scanner;.
    • Step 2: Object Creation: You must instantiate a Scanner object to facilitate communication with the hardware input stream. Syntax: Scanner input = new Scanner(System.in);.
    • Step 3: Capturing Input: Use the appropriate internal function of the Scanner class based on the type of data being entered.
  • Methods for String Capture:
    1. String name = input.next();: Captures the next token (usually up to the first space).
    2. String name = input.nextLine();: Captures the entire line of text until a newline character is encountered.

Programming Exercise: Circle Geometry Calculations

  • Task Description: Develop a program that accepts the radius of a circle from the keyboard and subsequently displays the circumference and the area.
  • Part 1: Pseudocode Strategy:
    • MAIN:
    • DEFINE radius, area, circumference AS FLOAT
    • INPUT radius
    • circumference = 2 * 22.0 / 7 * radius
    • area = 22.0 / 7 * radius * radius
    • PRINT "Area is ", area
    • PRINT "Circumference is ", circumference
    • ENDMAIN
  • Part 2: Java Programming Implementation:
import java.util.Scanner;

public class lecture3Exercise {
    public static void main(String[] args) {
        // Declare variables
        double radius, area, circumference;

        // Create an object called input to Scanner class
        Scanner input = new Scanner(System.in);

        // Take radius value as a user input
        System.out.println("Enter radius: ");
        radius = input.nextDouble();

        // Calculations using mathematical formulas
        // Formula for circumference: 2×22.07×radius2 \times \frac{22.0}{7} \times \text{radius}
        circumference = 2 * 22.0 / 7 * radius;

        // Formula for area: 22.07×radius2\frac{22.0}{7} \times \text{radius}^2
        area = 22.0 / 7 * radius * radius;

        // Display output results
        System.out.println("Area is " + area);
        System.out.println("Circumference is " + circumference);
    }
}