Moment 2 Flashcards
How to access the Scanner class?
Use an import declaration:
import java.util.Scanner;
What happens if the user enters a string when the program requires an integer?
An error will occur and the program will terminate.
A Java application is a?
A Java application is a computer program that executes when you use the java command to launch the Java Virtual Machine (JVM).
What is white space?
Blank lines (like line 3), space characters and tabs can make programs easier to read. Together, they’re known as white space. The compiler ignores white space.
What are Keywords?
Keywords are reserved for use by Java and are spelled with all lowercase letters. Ex: The "class" keyword introduces a class declaration and is immediately followed by the class name
Example code: "public class Welcome1 { "
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.
What is a 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.
What is a Method?
Methods perform tasks and can return information when they complete their tasks.
EX: Keyword void indicates that this method will not return any information.
“public static void main(String[] args) {“
The parentheses, after the identifier main, indicate that it’s a program building block (is a method).
What is the “Welcome to Java Programming!” in the ex below called?
System.out.println(“Welcome to Java Programming!”);
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—later we’ll show how to conveniently deal with long strings.
The string in the parentheses is the method’s argument.
What is the “System.out.” part from the ex below called?
System.out.println(“Welcome to Java Programming!”);
An object.
This particular on: is known as the standard output object. It allows a program to display information in the command window from which the program executes.
What is a statement?
The entire (se ex below), including System.out.println, the argument “Welcome to Java Programming!” in the parentheses and the semicolon (;), is called a statement. A method typically contains statements that perform its task. Most statements end with a semicolon.
System.out.println(“Welcome to Java Programming!”);
What characters will make a string appear on several lines?
\n
The backslash () is an escape character, which has special meaning to System.out’s print and println methods.
What are the common escape sequences for a string?
\n Newline. Position the screen cursor at the beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next tab stop.
\r Carriage return. Position the screen cursor at the beginning of the current
line—do not advance to the next line. Any characters output after the carriage
return overwrite the characters previously output on that line.
\ Backslash. Used to print a backslash character.
" Double quote. Used to print a double-quote character. For example,
System.out.println(“"in quotes"”);
displays “in quotes”.
Calling a method is also referred to as
invoking a method.
What are format specifier for println?
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.
All Java variables must be declared with?
a name and a type before they can be used.
What is the function of a variable’s name?
A variable’s name enables the program to access the variable’s value in memory.
What is the function of a variable’s type?
A variable’s type specifies what kind of information is stored at that location in memory.
What does the variable type “Scanner” do?
A 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.
Why should you choose meaningful variable names?
Choosing meaningful variable names helps a program to be self-documenting (i.e., one can understand the program simply by reading it rather than by reading associated documentation or creating and viewing an excessive number of comments).
Portions of statements that contain calculations are called?
expressions.
In fact, an expression is any portion of a statement that has a value.
int sum = number1 + number2;
In the preceding statement, the addition operator is a binary operator, because?
it has two operands—number1 and number2.
A condition is an?
expression that can be true or false.
An “If” selection statement helps a program to?
It allows a program to make a decision based on a condition’s value.
For example, the condition “grade is greater than or equal to 60” determines whether a student passed a test. If an if statement’s condition is true, its body executes. If the condition is false, its body does not execute.
Conditions in if statements can be formed by using?
the equality operators (== and !=) and relational operators (>, = and <=)
What is the meaning of “x != y”
x is not equal to y
What is the meaning of “x == y”
x is equal to y
What is the meaning of “x >= y”
x is greater than or equal to y
A syntax error occurs when?
the compiler encounters code that violates Java’s language rules. It’s similar to a grammar error in a natural language.
(p. 89)
Blank lines, space characters and tab characters are known as?
white space (p. 90). White space makes programs easier to read and is normally ignored by the compiler.
By convention, all class names in Java begin with a?
capital letter and capitalize the first letter of each word they include (e.g., SampleClassName).
A public class declaration must be saved in a?
file with the same name as the class followed by the “.java” filename extension.
(p. 90)
Java is case sensitive, which means?
It’s uppercase and lowercase letters are distinct.