Last saved 31 days ago
EL

Lecture 4(1)

robot
knowt logo

Lecture 4(1)

Formatting Console Output

General Overview

  • Console output in Java can often require specific formatting for readability and presentation purposes.


Using printf and format Methods

Purpose

  • To display numbers in a desired format.

  • Example:

    • double investment = 986.236548;

Syntax

  • System.out.printf(format, item1, item2, ...);

  • System.out.format(format, item1, item2, ...);

    • format: a string that may consist of substrings and format specifiers.

    • format specifier: determines how an item should be displayed and begins with a percent sign (%) and can represent numeric values, characters, or Booleans.


Format Specifiers with Examples

  1. Integer in 8 spaces:

    • System.out.printf("%8d", numCourses);

  2. Floating-point numbers with 2 decimals in 10 spaces:

    • System.out.printf("%10.2f", cost);

  3. String in 12 spaces:

    • System.out.printf("%12s", aWord);


Frequently-Used Format Specifiers

  • %b: boolean value (true/false)

  • %c: character

  • %d: decimal integer

  • %f: floating-point number

  • %e: standard scientific notation

  • %s: string


Formatting Specifiers of Boolean Values

  • %b: basic format

    • Example: System.out.printf("%b", isGrad);

  • %nb: format with n output spaces, right-justified


Formatting Specifiers of Character Values

  • %c: basic format

    • Example: System.out.printf("%c", aCharacter);

  • %nc: n output spaces, right justified


Formatting Specifiers of Integer Values

  • %d: basic format

    • Example: System.out.printf("%d", k);

  • %nd: n output spaces, right justified


Formatting Specifiers of Floating-point Values

  • %f: basic format

    • Example: System.out.printf("%f", cost);

  • %n.kf: n output spaces, k digits after the decimal point, right justified


Formatting Specifiers for Scientific Values

  • %e: basic format

    • Example: System.out.printf("%e", cost);

  • %n.kf: n output spaces, k digits after the floating point, right justified


Formatting Specifiers for String Values

  • %s: basic format

    • Example: System.out.printf("%s", aWord);

  • %ns: n output spaces, right justified


Notes on Format Specifiers

  • Automatically increases space if an item requires more than the specified width.

  • Format strings can include substrings for additional information.


Print a New Line

  • Use "
    " at the end of the format string to print a new line after the output.


Important Output Items and Justification

  • Output alignment is right justified by default; left justification is indicated with a minus sign.

  • If an item requires more output spaces than specified, the width is adjusted automatically.


Getting Current Time

  • Use Calendar class to get current date/time and format output appropriately.

  • Example:

    • System.out.printf("%tB %te, %tY%n", b, b, b);


Chapter 4: Mathematical Functions, Characters, and Strings

Objectives

  • Introduce mathematical functions, characters, strings, and their application in Java.


Mathematical Functions in Java

  • The Math class is essential for performing mathematical operations.

    • Common Functions include pow(a, b), sqrt(a), and more.

    • Constants such as Math.PI, Math.E for various computations.


Character Data Type and Operations

  • Represents a single character.

  • Character literals are enclosed in single quotes.


Commonly Used Character Encoding

  • Unicode and ASCII are the common encoding schemes. Examples of how characters are represented in both formats.


String Data Type

  • Represents a sequence of characters, distinguished by enclosing in double quotes.

  • String methods can be performed like length(), charAt(index), toUpperCase(), etc.


Practice Exercises

  • Various exercises demonstrate the use of Java features such as mathematical computations, string manipulation, and user input reading.