Chapter 2: Introduction to Java Applications; Input/Output and Operators

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Condition

A *condition* is an expression that can be *true* or *false*

Class Body

A *left brace* (as in line 5 of image), *{*, begins the body of every class declaration. A corresponding *right brace* (at line 11 of image),* }*, must end each class declaration. Lines 6-10 in image are indented

Identifier

A class name is an *identifier* - a series of characters consisting of letters, digits, underscores (_), and dollar signs ($) that does not begin with a digit and does not contain spaces. Some valid identifiers are Welcome1, $value, _value, m_inputField1, and button7

Java Application

A computer program that executes when you use the *java command* to launch the Java Virtual Machine(JVM)

Packages

A great strength of Java is its rich set of predefined classes that you can reuse rather than "reinventing the wheel." These classes are grouped into *packages*—named groups of related classes—and are collectively referred to as the *Java class library*, or the *Java Application Programming Interface (Java API)*

Variable

A location in the computer's memory where a value can be stored for use later in a program. All Java variables must be declared with a *name* and a *type* before they can be used

Filename for a public Class

A public class must be placed in a file that has a filename of the form ClassName.java, so class Welcome1 is stored in the file Welcome1.java

Newline Characters

A single statement can display multiple lines by using newline characters, which indicate to System.out's print and println methods when to position the output cursor at the beginning of the next line in the command window

The Format Specifier %s

The format specifier %s is a placeholder for a string. The format string in line 9 specifies that printf should output two strings, each followed by a newline character.

Format Specifier %d

The format specifier *%d* in this image is a placeholder for an int value (in this case the value of sum)—the letter d stands for "decimal integer."

Body of the Method Declaration

The left brace in line 8 of this image begins the body of the method declaration. A corresponding right brace must end it (line 10). Line 9 in the method body is indented between the braces

new Keyword

This expression uses the *new* keyword to create a Scanner object that reads characters typed by the user at the keyboard

if Selection Statement

Allows a program to make a decision based on a condition's value. If the condition in an if statement is true, the body of the if statement executes. If the condition is false, the body does not execute

Arithmetic Expressions in Straight-Line Form

Arithmetic expressions in Java must be written in *straight-line form* to facilitate entering programs into the computer. Thus, expressions such as "a divided by b" must be written as a / b, so that all constants, variables and operators appear in a straight line

Equality and Relational Operators

Conditions in if statements can be formed by using the equality operators (== and !=) and relational operators (>, <, >= and <=). Both equality operators have the same level of precedence, which is lower than that of the relational operators. The equality operators associate from left to right. The relational operators all have the same level of precedence and also associate from left to right.

Prompt

Directs the user to take a specific action. This image uses System.out.print to display the message "Enter first integer: "

Expressions

Portions of statements that contain calculations

Variables to Store Data

Programs remember numbers and other data in the computer's memory and access that data through program elements called *variables*

Integer Division

*Integer division* yields an integer quotient. For example, the expression 7 / 4 evaluates to 1, and the expression 17 / 5 evaluates to 3. Any fractional part in integer division is simply truncated (i.e., discarded)—no rounding occurs

Variable Name

A variable's name enables the program to access the value of the variable in memory. A variable's name can be any valid identifier—again, a series of characters consisting of letters, digits, underscores (_) and dollar signs ($) that does not begin with a digit and does not contain spaces.

Variable Type

A variable's type specifies what kind of information is stored at that location in memory

Syntax Error

An error that occurs when the compiler encounters code that violates Java's language rules. Syntax errors are also called *compiler errors, compile-time errors, or compilation errors*, because the compiler detects them when compiling the program

Keywords(Reserved Words)

Are words reserved for use by Java and are always spelled with all lowercase letters

Class Names

By convention, class names begin with a capital letter and capitalize the first letter of each work they include (e.g., SampleClassName)

java.lang

Class *System* is part package. By default, package *java.lang* is imported in every Java program; thus, classes in java.lang are the only ones in the Java API that do not require an import declaration.

End-of-line Comments

Comments that terminate at the end of the line on which the // appears. An end-of-line comment need not begin a line; it also can begin in the middle of a line and continue until the end

Format Specifier

Each format specifier is a placeholder for a value and specifies the type of data to output. Format specifiers also may include optional formatting information. Format specifiers begin with a percent sign (%) followed by a character that represents the data type

Scanner

Enables a program to read data (e.g., numbers and strings) for use in a program. The data can come from many sources, such as the user at the keyboard or a file on disk. Before using a Scanner, you must create it and specify the source of the data.

Standard input object, System.in

Enables applications to read bytes of data typed by the user. The Scanner translates these bytes into types (like ints) that can be used in a program

public Keyword

Every class we define begins with the *public* keyword

Fixed Text

Fixed text is output by printf just as it would be by print or println

Type float and double

Hold real numbers. Real numbers contain decimal points, such as in 3.4, 0.0 and -11.19

Nested Parentheses

If an expression contains nested parentheses, such as, ((a + b) * c, )the expression in the innermost set of parentheses (a + b in this case) is evaluated first

Command Line

In UNIX/Linux/Mac OS X, the command window is called a *terminal window* or a *shell*. Many programmers call it simply the *command line*.

Assignment Operator(=)

In line 18 of this image, we place the result of the call to method nextInt (an int value) in variable number1 by using the *assignment operator, =*. The statement is read as "number1 gets the value of input.nextInt()." Operator = is called a *binary operator*, because it has two operands—number1 and the result of the method call input.nextInt().

Keyword void

Indicates that this method will not return any information

Traditional Comments

Java also has *traditional comments*, which can be spread over several lines. These begin and end with delimiters, /* and */. The compiler ignores all text between the delimiters

Rules of Operator Precendence

Java applies the operators in arithmetic expressions in a precise sequence determined by the *rules of operator precedence*, which are generally the same as those followed in algebra

Java is Case Sensitive

Java is *case sensitive*—uppercase and lowercase letters are distinct—so value and Value are different (but both valid) identifiers

Javadoc Comments

Java provides comments of a third type - *Javadoc comments*. These are delimited bby /** and */. The compiler ignores all text between the delimiters. These comments allow you to embed program documentation directly in your programs

Remainder Operator

Java provides the *remainder operator*, %, which yields the remainder after division. The expression x % y yields the remainder after x is divided by y. Thus, 7 % 4 yields 3, and 17 % 5 yields 2.

Variable Declaration Statement

Like other statements, declaration statements end with a semicolon (;). Line 11 of this image, Scanner input = new Scanner(System.in);, is a variable declaration statement that specifies the name (input) and type (Scanner) of a variable that's used in this program

nextInt Method

Line 18 of this image uses Scanner object input's *nextInt method* to obtain an integer from the user at the keyboard. At this point the program waits for the user to type the number and press the Enter key to submit the number to the program

import Declaration

Line 3 of this image, import java.util.Scanner; // program uses class Scanner, is an import declaration that helps the compiler locate a class that's used in this program. It indicates that the program uses the predefined Scanner class (discussed shortly) from the package named *java.util*. The compiler then ensures that you use the class correctly

Declaring a Class

Line 4 of this image, public class Welcome1, begins a *class declaration* for class Welcome1. Every Java program consists of at least one class that you (the programmer) define. The *class keyword* introduces a class declaration and is immediately followed by the *class name* (Welcome1)

main Method

Line 6 in this image, // main method begins execution of Java application, is an end-of-line comment indicating the purpose of lines 7-10 of the program. Line 7 in this image, *public static void main(String[] args)*, is the starting point of every Java application. For a Java application, one of the methods must be called main and must be defined as shown in line 7; otherwise, the Java Virtual Machine (JVM) will not execute the application

Performing Output with System.out.println

Line 9 of this image, System.out.println("Welcome to Java Programming!");, instructs the computer to perform an action—namely, to display the characters contained between the double quotation marks (the quotation marks themselves are not displayed)

Calling System.out.printf

Lines 9-10 in this image call method System.out.printf to display the program's output. The method call specifies three arguments. When a method requires multiple arguments, they're placed in a *comma-separated list*. Calling a method is also referred to as *invoking* a method.

Method System.out.println

Method *System.out.println* displays (or prints) a line of text in the command window. The string in the parentheses in line 9 of this image is the *argument* to the method. When System.out.println completes its task, it positions the output cursor (the location where the next character will be displayed) at the beginning of the next line in the command window

Format String

Method printf's first argument is a *format string* that may consist of *fixed text* and *format specifiers*.

Parentheses for Grouping Expressions

Parentheses are used to group terms in Java expressions in the same manner as in algebraic expressions. For example, to multiply a times the quantity b + c, we write, a * (b + c)

Alternate Way of Declaring Variables

Several variables of the same type may be declared in a single declaration with the variable names separated by commas (i.e., a comma-separated list of variable names)

Arithmetic Operators

Shown in image. The *asterisk (*)* indicates multiplication, and the percent sign *(%)* is the *remainder operator*. The arithmetic operators shown in image are binary operators, because each operates on two operands

System.out Object

The *System.out* object—which is predefined for you—is known as the *standard output object*. It allows a Java application to display information in the *command window* from which it executes

Displaying Text with printf

The *System.out.printf* method (f means "formatted") displays formatted data. This image uses this method to output on two lines the strings "Welcome to" and "Java Programming!".

Backslash(\)

The *backslash (\)* is an *escape character*, which has special meaning to System.out's print and println methods. When a backslash appears in a string, Java combines it with the next character to form an *escape sequence*—\n represents the newline character

javadoc Utility Program

The *javadoc utility program* (part of the JDK) reads Javadoc comments and uses them to prepare program documentation in HTML format

Declaring a Method

The *parentheses* after the identifier main in this image indicate that it's a program building block called a *method*. Java class declarations normally contain one or more methods. Methods perform tasks and can return information when they complete their tasks

Initialized

The = in line 11 of this image indicates that Scanner variable input should be initialized (i.e., prepared for use in the program) in its declaration with the result of the expression to the right of the equals sign—new Scanner(System.in)

Statement

The entire line 9, including System.out.println, the argument "Welcome to Java Programming!" in the parentheses and the *semicolon* (;), is called a *statement*. A method typically contains one or more statements that perform its task. Most statements end with a semicolon.

Primitive Types

The types int, float, double and char are called primitive types. Primitive-type names are keywords and must appear in all lowercase letters

Type int

They can hold integer values (whole numbers such as 72, -1127 and 0). The range of values for an int is -2,147,483,648 to +2,147,483,647.

String

Together, the quotation marks and the characters between them are a *string*—also known as a *character string* or a *string literal*. White-space characters in strings are not ignored by the compiler. Strings cannot span multiple lines of code.

Memory Concepts

Variable names such as number1 actually correspond to locations in memory. Every variable has a *name*, a *type*, a *size*(in bytes), and a *value*. In the statement, number1 = input.nextInt(); // read first number from user, the number typed by the user is placed into a memory location corresponding to the name number1

Type char

Variables of type char represent individual characters, such as an uppercase letter (e.g., A), a digit (e.g., 7), a special character (e.g., * or %) or an escape sequence (e.g., the tab character, \t.

Comments

We insert *comments* to *document programs* and improve their readability. The Java compiler ignores comments, so they do not cause the computer to perform any action when the program is run

%n Format Specifier

the %n format specifier, which is a line separator that's portable across operating systems. You cannot use %n in the argument to System.out.print or System.out.println; however, the line separator output by System.out.println after it displays its argument is portable across operating systems


Kaugnay na mga set ng pag-aaral

Med Surg II Exam 3 Practice Questions

View Set

Test 1-ch. 9 & 10, generator videos & safety

View Set

Chapter 1: What is interaction design?

View Set

OpenStax US History Ch. 5-8 Review Questions & Terms

View Set

Module 4&5 ATI Questions (Respiratory, GI, and Cardiovascular Systems)

View Set