lvp_java_text
Abstract Data Types
Definition: A variable can be declared using an abstract data type (ADT), one common form being a class.
Classes in Java: Many predefined classes in Java, as well as user-defined classes, encapsulate data and methods.
Object: A variable declared with a class is referred to as an object, which stores a reference to the object's data and methods.
Instantiation: The process of creating a new object is termed instantiation.
Creating an Object:
Syntax:
<class> <variable name> = new <class>(<arguments>);Example:
Circle spot = new Circle(4);creates aCircleobject initialized with a radius of 4.
Method Access: Use the dot operator to access members of a class. Example:
spot.getRadius()retrieves the radius of thespotobject.
Java Packages
Java Runtime Environment (JRE): Comes with several packages (collections of classes).
Key Packages:
java.lang: Fundamental classes.java.util: Utility classes (e.g., reading input, random number generation).
Package Documentation: Summarizes available classes and respective data/methods, often with code examples.
Naming Convention:
Packages start with
java.followed by dots and the package name.Example:
import java.util.Scanner;imports the Scanner class.
Accessing Packages: The import statement allows access to package members. Example:
import java.util.*;imports all classes in theutilpackage.
Obtaining User Input
Input Stream: Sequence of characters from an input device (e.g., keyboard).
Scanner Class: Utilized to read integers, floats, and strings from an input stream.
Instantiation with Input Stream:
Scanner input = new Scanner(System.in);
Scanner Methods:
next()- Returns a string from input until a space.nextLine()- Returns the whole line.nextInt()- Reads an integer.nextDouble()- Reads a double.nextBoolean()- Reads a boolean.close()- Closes the input stream.
Important Note: Use
nextLine()carefully afternextInt()to avoid reading empty strings due to end-of-line character.Example Code: An application that calculates the area of a rectangle:
public class RectangleArea2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter length: ");
int length = input.nextInt();
System.out.print("Enter width: ");
int width = input.nextInt();
input.close();
int area = length * width;
System.out.println("Area: " + area);
}
}Numeric Expressions
Operators:
Arithmetic:
+,-,*,/,%.
Numeric Expression: Contains at least one operand and may include operators.
Example:
6 + 5.
Division Behavior:
Integer division when both operands are
int.Real division returns the entire quotient including the decimals and is performed if one or more operator is a
double.
Modulus Operator
:Modulus division returns the remainder resulting from division. The % operator truncates the operands, if necessary, to return an integer:this means that when using the modulus operator with integer values, any decimal portion of the operands is discarded, ensuring that the result is always an integer.Modulus division is useful for retrieving digits of a number.
Operator Precedence:
Order: Multiplication/Division > Addition/Subtraction.
Parentheses can override default precedence. Example:
(6 + 4) * (2 - 1).
Assignment Operators
Basic Assignment:
=assigns the evaluated expression on the right to the variable on the left.Compound Assignment:
+=,-=,*=,/=,%=perform operations before assigning.Example:
numPlayers += 2;increasesnumPlayersby 2.
Decimal Formatting: The
DecimalFormatclass allows formatting of numbers with specified decimal places.
Constants
Definition: A constant is a variable declared with
finalkeyword whose value cannot be changed after assignment.Syntax:
final <type> <identifier>.Example:
final double PI = 3.14;
Naming Convention: Constant identifiers are typically uppercase with underscores.
Usage: Used for values that maintain consistency throughout the program, adding clarity.
Identifiers and Keywords
Identifying Rules: Start with a letter; case-sensitive.
Keywords: Special reserved words in Java that cannot be used as identifiers. Examples include
class,int,final.
Error Types in Programming
Syntax Error: Rules of Java are violated, identified by the compiler.
Logic Error: Program executes but produces incorrect results; must be diagnosed by the programmer.
Run-time Error (Exception): Occurs during execution, throwing an error (e.g., division by zero).
Case Study: Birthday Game
Objective: Create a game that determines a player’s birthday based on a mathematical calculation.
Algorithm: Steps for the player to compute a number from their birth month and day.
Code Structure: Includes input handling, calculations, and displaying results.
Example: The player calculates a modified number and inputs it; the program outputs their birthday based on the calculations.