Back

Module 1 Summary

divider

This summary covers the essential building blocks of Java programming, from displaying information and handling user input to working with data, performing calculations, and using advanced mathematical tools. Mastering these concepts is crucial for success in AP Computer Science A.


1. Console I/O

An application's ability to communicate with the user is fundamental. This involves both displaying information (output) and receiving information (input).

Displaying Output

System.out.println(): Prints the given data to the console and moves the cursor to the next line.

System.out.print(): Prints the given data to the console and leaves the cursor on the same line. This is useful for prompting the user for input.

Getting user Input with the Scanner Class

The Scanner class is the standard tool for reading keyboard input, making programs interactive. Using it involves three steps:

  1. Import: Add import java.util.Scanner; to the top of your file.
  2. Instantiate: Create a Scanner object in your main method: Scanner input = new Scanner(System.in);.
  3. Invoke: Call methods on the Scanner object to read data.
    • input.nextLine(): Reads an entire line of text as a String.
    • input.nextInt(): Reads the next token of input as an int.
    • input.nextDouble(): Reads the next token of input as a double.

The Scanner Issue: A common bug occurs when mixing numeric and string input. Methods like nextInt() and nextDouble() read the number but leave the newline (\n) character in the input stream. A subsequent call to nextLine() will read this leftover newline as an empty string.


2. Data and Variables

Java is a statically-typed language, meaning all data must have a defined type that cannot be changed.

Data Types

Primitive Types: Store simple, single values. The core primitives for the AP exam are:

  • int: Whole numbers (100, -5).
  • double: Numbers with decimal points (98.6, -0.5).
  • boolean: Logical values, true or false (covered in more detail later).

Objects: Store more complex data and can have associated behaviors (methods).

  • String: A sequence of characters enclosed in double quotes (").

Variables and Constants

Variable: A named container for storing a value. The value can be changed.

  • Declaration: int score;
  • Initialization: score = 0;
  • Reassignment: score = 100;

Constant: A variable whose value cannot be changed after its initial assignment. It is declared with the final keyword.

  • Declaration: final double TAX_RATE = 0.07;

Naming Conventions

  • Variables: Use camelCase (playerScore, lastName).
  • Constants: Use ALL_CAPS with underscores (MAX_HEALTH, TAX_RATE).

3. Operators and Expressions

Operators are special symbols used to perform operations on variables and values.

Arithmetic and Concatenation

Arithmetic: + (add), - (subtract), * (multiply), / (divide), % (modulus/remainder).

Integer Division: When / is used with two integers, the result is truncated (the decimal part is discarded). 10 / 3 is 3.

String Concatenation: The + operator joins strings. If one operand is a String, the other is converted to a String. Use parentheses () to control the order of operations:

  • "Result: " + (5 + 10) → Result: 15
  • "Result: " + 5 + 10 → Result: 510

Assignment Shorthands

These operators provide a concise way to modify a variable's value.

Compound Assignment: score += 5; is shorthand for score = score + 5;. This works for +, -, *, /, and %.

Increment/Decrement: ++ and -- add or subtract 1 from a variable. Their behavior depends on their position:

  • Prefix (++x): The value is changed before it is used in an expression.
  • Postfix (x++): The original value is used in the expression, and then it is changed.

4. Other Tools and Techniques

Comments

Use comments (//) to document your code and make it understandable for yourself and others.

Type Casting

Type casting is the explicit conversion of one data type to another. The syntax is (type) value.

  • Narrowing (double -> int): Truncates the decimal. (int) 9.99 becomes 9.
  • Widening (int -> double): Forces floating-point division. (double) 10 / 3 becomes 3.333....

The Math Class

A built-in Java library for common mathematical functions. No import is needed.

  • Math.PI: The constant π.
  • Math.abs(x): Returns the absolute value of x.
  • Math.pow(base, exp): Returns base raised to the power of exp.
  • Math.sqrt(x): Returns the square root of x.
  • Math.round(x): Rounds a double to the nearest whole number (long).
  • Math.random(): Returns a random double value in the range [0.0,1.0). To generate a random integer in a specific range, use the formula:
    • (int)(Math.random() * range) + startValue

String Escape Sequences

Special character sequences inside a String literal.

  • \n Newline
  • \" Double quote
  • \\ Backslash
  • \t Tab