Getting Started with Java - Chapter 2

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Sample - Scope

// This program can't find its variable. public class Scope { public static void main(String[] args) { System.out.println(value); // ERROR! int value = 100; } }

Thread

A process running in the computer

\n (newline)

Advances the cursor to the next line for subsequent printing

Combined Assignment Operator

Allows the programmer to perform an arithmetic operation and assignment with a single operator.; +=, -=, *=, /=, %=

Escape Sequence

Allows the programmer to print characters that otherwise would be unprintable; comprised of two characters, but treated by the compiler as a single character.

Objects

Can have adjectives Can have object-specific verbs

Scope

The part of a program that has access to a variable's contents.

Sample - Commenting Code

/** This class creates a program that calculates company payroll. */ public class Comment3 { /** The main method is the program's starting point. */ public static void main(String[] args) { double payRate; // Holds the hourly pay rate double hours; // Hours holds the hours worked int employeeNumber; // Holds the employee number // The Remainder of This Program is Omitted. } }

System.exit Method

A program that uses JOptionPane does not automatically stop executing when the end of the main method is reached; requires an integer argument

Source Code File

- Contains one or more Java classes. - If there is more than one class,, only one of them may be public. - The public class and the filename must match.

Sample - Boolean

// A program for demonstrating boolean variables public class TrueFalse { public static void main(String[] args) { boolean bool; bool = true; System.out.println(bool); bool = false; System.out.println(bool); } }

Sample - String Objects

// A simple program demonstrating String objects. public class StringDemo { public static void main(String[] args) { String greeting = "Good morning, "; String name = "Herman"; System.out.println(greeting + name); } }

Sample"

// This is a simple Java program. public class Simple { public static void main(String[] args) { System.out.println("Programming is great fun!"); } }

Sample - Arithmetic Operators

// This program calculates hourly wages plus overtime. public class Wages { public static void main(String[] args) { double regularWages; // The calculated regular wages. double basePay = 25; // The base pay rate. double regularHours = 40; // The hours worked less overtime. double overtimeWages; // Overtime wages double overtimePay = 37.5; // Overtime pay rate double overtimeHours = 10; // Overtime hours worked double totalWages; // Total wages regularWages = basePay * regularHours; overtimeWages = overtimePay * overtimeHours; totalWages = regularWages + overtimeWages; System.out.println("Wages for this week are $" + totalWages); } }

Sample - String Length Method

// This program demonstrates the String class's length method. public class StringLength { public static void main(String[] args) { String name = "Herman"; int stringSize; stringSize = name.length(); System.out.println(name + " has " + stringSize + " characters."); } }

Sample - Char

// This program demonstrates the char data type. public class Letters { public static void main(String[] args) { char letter; letter = 'A'; System.out.println(letter); letter = 'B'; System.out.println(letter); } }

Sample - Unicode

// This program demonstrates the close relationship // between characters and integers. public class Letters2 { public static void main(String[] args) { char letter; letter = 65; System.out.println(letter); letter = 66; System.out.println(letter); } }

Sample - Floating-Point

// This program demonstrates the double data type. public class Sale { public static void main(String[] args) { double price, tax, total; price = 29.75; tax = 1.76; total = 31.51; System.out.println("The price of the item " + "is " + price); System.out.println("The tax is " + tax); System.out.println("The total is " + total); } }

Sample - Variable

// This program has a variable. public class Variable { public static void main(String[] args) { int value; value = 5; System.out.print("The value is "); System.out.println(value); } }

5 Arithmetic Operators

1) + - addition 2) - - substraction 3) * - multiplication 4) / - division 5) % - modulus

Rules for Identifiers

1) An identifier may only contain: - letters a-z or A-Z - the digits 0-9 - underscores _ - the dollar sign $ 2) The first character may not be a digit. 3) Case sensitive. 4) Identifiers cannot include spaces.

Input Dialog

A quick and simple way to ask the user to enter data.

Dialog Box

A small graphical window that displays a message to the user or requests input; can be displayed using the JOptionPane class.

String Concatenation

A string literal value cannot span lines in a Java source code file; ______ can join various data types

Literal

A value that is written into the code of a program

Contsants

Allows the programmer to use a name rather than a value throughout the program; gives a singular point for changing those values when needed; keep the program organized and easier to maintain; identifiers that can hold only a single value; declared using the keyword final; must be initialized before they are used or a compiler error will be generated; all upper case and words are separated by the underscore character.

=

Assignment operator; operand on the left side must be a variable name, operand on the right side must be either a literal or expression that evaluates to a type that is compatible with the type of the variable.

/*...*/

Block comment. Everything beginning with /* and ending with the first */ will be ignored by the compiler. This comment type cannot be nested.

Primitive Data Types

Built into the Java language and are not derived from classes.

Boolean Data Type

Can have two possible values: - true - false ; the value of a boolean variable may only be copied into a boolean variable.

\\ (backslash)

Causes a backslash to be printed

\" (double quote)

Causes a double quotation mark to be printed

\' (single quote)

Causes a single quotation mark to be printed

\b (backspace)

Causes the cursor to back up, or move left, one position

\r (carriage return)

Causes the cursor to go to the beginning of the current line, not the next line

\t (tab)

Causes the cursor to skip over to the next tab stop

Unicode

Characters are stored as numbers

String Class

Data type that holds a series of characters.

Floating-Point Numbers

Data types that allow fractional value; 2 types - float and double

{}

Encloses a group of statements, such as the contents of a class or a method.

""

Encloses a string of characters, such as a message that is to be printed on the screen

8 Primitive Data Types

Int (whole number) Float (decimal) Double (decimal) Char (single character) Boolean (true or false) Short (small number) Long Byte

Exit Code

Integer argument that is passed back to the operating system; usually ignored, however, it can be used outside the program to indicate whether the program ended successfully (0) or as the result of a failure.

Integer Literals

Integers embedded into Java source code

Java Applications Programming Interface (API)

Java classes in the standard Java library are accessed using ___.

/**...*/

Javadoc comment. This is a special version of the previous block comment that allows comments to be documented by the javadoc utility program. Everything beginning with the /** and ending with the first */ will be ignored by the compiler. This comment type cannot be nested.

//

Marks the beginning of a single line comment.

;

Marks the end of a complete programming statement

Parse Methods

Methods each of the numeric wrapper classes has that converts a string to a number

println method

Performs the task of sending characters to the output device; places a newline character at the end of whatever is being printed out.

Primitive vs Reference

Primitive Variable: contains the value that it's been assigned. Reference Variable: contains the memory address of the object's location.

Identifier

Programmer-defined names for: - classes - variables - methods ; may not be any of the Java reserved keywords.

Char Data Type

Provides access to single characters; enclosed in single quote marks.

JOptionPane Class

Provides methods to display each type of dialog box.

//

Single line comment; anything after be ignored by the compiler.

Sample Methods

The String class contains many methods that help with the manipulation of String object; string objects are immutable, meaning that they cannot be changed; many of the methods of a String object can create new versions of the object.

Standard Output Device

The console window

Standard Input Device

The keyboard

{ }

This area is the body of the class Simple. All of the data and methods for this class will be between these curly braces.

{ }

This area is the body of the main method. All of the actions to be completed during the main method will be between these curly braces.

"// This is a simple Java program."

This is a Java comment. It is ignored by the compiler.

"System.out.println("Programming is great fun!"); "

This is the Java Statement that is executed when the program runs.

"public class Simple "

This is the class header for the class Simple

"public static void main(String[] args) "

This is the method header for the main method. The main method is where a Java application begins.

Integer Division

Truncate any decimal remainder

()

Used in a method header to mark the parameter list.

JOptionPane.showMessageDialog

Used to display a message dialog

Scanner Class

Used to read input from the keyboard

Assignment Statement

Used to store a value in a variable

Local Variables

Variables declared inside a method (like the main method); scope begins at the declaration of the variable and ends at the end of the method in which it was declared.

Floating-Point Literals

When floating point numbers are embedded into Java source code, they are called ________; A double value is not compatible with a float variable because of its size and precision; however, a double can be forced into a float by appending the letter F/f to the literal; cannot contain embedded currency symbols or commas.

Grouping with Parenthesis

When parenthesis are used in an expression, the inner most parenthesis is processed first. If two sets of parenthesis are at the same level, they are processed left to right.

print statement

Works very similarly to the println statement, but does not put a newline character at the end of the output.

out object

___ contains the methods print and println.

System class

______ contains methods and objects that perform system level tasks.

Integer Data Types

byte, short, int, and long; can hold whole numbers, but not decimals

+ Operator

can be used in two ways: - as a concatenation operator - as an addition operator ; if either side is a string, the result is a string

Sample - Scanner

import java.util.Scanner; // Needed for the Scanner class /** This program demonstrates the Scanner class. */ public class Payroll { public static void main(String[] args) { String name; // To hold a name int hours; // Hours worked double payRate; // Hourly pay rate double grossPay; // Gross pay // Create a Scanner object to read input. Scanner keyboard = new Scanner(System.in); // Get the user's name. System.out.print("What is your name? "); name = keyboard.nextLine(); // Get the number of hours worked this week. System.out.print("How many hours did you work this week? "); hours = keyboard.nextInt(); // Get the user's hourly pay rate. System.out.print("What is your hourly pay rate? "); payRate = keyboard.nextDouble(); // Calculate the gross pay. grossPay = hours * payRate; // Display the resulting information. System.out.println("Hello, " + name); System.out.println("Your gross pay is $" + grossPay); } }


Set pelajaran terkait

M/C Exam 1: Ped Growth & Development

View Set

Principles of Management Chapter 14

View Set

Chemistry 5.10 Quiz : Le Chatelier's Principle

View Set