Exam 1 Flashcards
Algorithm
A step-by-step description of how to accomplish a task.
Program
A list of instructions to be carried out by a computer.
Hardware
The physical components that make up a computer: central processing unit, or CPU, memory (often called random access memory, or RAM, hard disk as a larger permanent storage
Software
Computer programs are collectively called software. The primary piece of software running on a computer is its operating system. An operating system provides an environment in which many programs may be run at the same time; it also provides a bridge between those programs, the hardware, and the user (the person using the computer). The programs that run inside the operating system are often called applications.
Digital
Based on numbers that increase in discrete increments, such as the integers 0, 1, 2, 3, etc
Modern computers are digital because everything that is stored on the computer is stored as a sequence of integers
Binary Number
A number composed of just 0s and 1s, also known as a base-2 number.
Program Execution
The act of carrying out the instructions contained in a program.
Compiler
A program that translates a computer program written in one language into an equivalent program in another language (often, but not always, translating from a high-level language into machine language).
executable
A compiler that translates directly into machine language creates a program that can be executed directly on the computer, known as an executable.
native compilers
We refer to such compilers as native compilers because they compile code to the lowest possible level (the native machine language of the computer).
Java bytecodes
Instead of compiling into machine language, Java programs compile into what are known as Java bytecodes. One set of bytecodes can execute on many different machines. These bytecodes represent an intermediate level: They aren’t quite as high-level as Java or as low-level as machine language. In fact, they are the machine language of a theoretical computer known as the Java Virtual Machine (JVM).
Java Virtual Machine
A theoretical computer whose machine language is the set of Java bytecodes.
To actually execute a Java program, you need another program that will execute the Java bytecodes. Such programs are known generically as Java runtimes, and the standard environment distributed by Oracle Corporation is known as the Java Runtime Environment (JRE).
Java Runtime
A program that executes compiled Java bytecodes.
Most people have Java runtimes on their computers, even if they don’t know about them. For example, Apple’s Mac OS X includes a Java runtime, and many Windows applications install a Java runtime.
Java
A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language.2
Java Class Libraries
The collection of preexisting Java code that provides solutions to common programming problems.
platform independent
Java is extremely platform independent; unlike programs written in many other languages, the same Java program can be executed on many different operating systems, such as Windows, Linux, and Mac OS X.
The Java Programming environment (steps):
Type in a program as a Java class.
Compile the program file.
Run the compiled version of the program.
File extensions
The Java program files that you create must use the extension .java. When you compile a Java program, the resulting Java bytecodes are stored in a file with the same name and the extension .class.
Integrated Development Environments, or IDEs
Most Java programmers use what are known as Integrated Development Environments, or IDEs, which provide an all-in-one environment for creating, editing, compiling, and executing program files. Some of the more popular choices for introductory computer science classes are Eclipse, IntelliJ, NetBeans, jGRASP, DrJava, BlueJ, and TextPad. Your instructor will tell you what environment you should use.
Console Window
A special text-only window in which Java programs interact with the user.
The console window is a classic interaction mechanism wherein the computer displays text on the screen and sometimes waits for the user to type responses. This is known as console or terminal interaction. The text the computer prints to the console window is known as the output of the program. Anything typed by the user is known as the console input.
Class
A unit of code that is the basic building block of Java programs. Oracle has established the convention that class names always begin with a capital letter, which makes it easy to recognize them. Java requires that the class name and the file name match, so this program must be stored in a file called Hello.java.
basic form of a Java class
public class {
...
}
class header
The first line of the class is known as the class header. The word public in the header indicates that this class is available to anyone to use.
Method
Simple methods are like verbs: They command the computer to perform some action. Inside the curly braces for a class, you can define several different methods. At a minimum, a complete program requires a special method that is known as the main method. It has the following syntax: public static void main(String[] args) { ; ; ... ; }
Statement
An executable snippet of code that represents a complete command.
Each statement is terminated by a semicolon; it is used to terminate statements in the same way that periods terminate sentences in English.
String Literals (Strings):
In Java you specify a string literal by surrounding the literal text in quotation marks, as in
“This is a bunch of text surrounded by quotation marks.”
System.out.println
One of the simplest and most common statements is System.out.println, which is used to produce a line of output.
Each println statement produces a different line of output.
Escape Sequences:
The solution is to embed what are known as escape sequences in the string literals. Escape sequences are two-character sequences that are used to represent special characters. They all begin with the backslash character ().
System.out.println(“What "characters" does this \ print?”);
Identifier
A name given to an entity in a program, such as a class or method.
Identifiers must start with a letter, which can be followed by any number of letters or digits. The following are all legal identifiers:
first hiThere numStudents TwoBy4
The Java language specification defines the set of letters to include the underscore and dollar-sign characters (_ and $), which means that the following are legal identifiers as well:
two_plus_two _count $2donuts MAX_COUNT
Naming Conventions
AllMyChildren for a class name (each word starts with a capital) allMyChildren for a method name (starts with a lowercase letter, subsequent words capitalized) ALL_MY_CHILDREN for a constant name (all uppercase, with words separated by underscores Don’t hesitate to use long identifiers. The more descriptive your names are, the easier it will be for people (including you) to read your programs. Descriptive identifiers are worth the time they take to type. Java’s String class, for example, has a method called compareToIgnoreCase.
Comment
Text that programmers include in a program to explain their code. The compiler ignores comments. /* like this */ System.out.println("You win!"); // Good job!
Program Errors
Syntax errors occur when you misuse Java. They are the programming equivalent of bad grammar and are caught by the Java compiler.
Logic errors (bugs) occur when you write code that doesn’t perform the task it is intended to perform.
Runtime errors are logic errors that are so severe that Java stops your program from executing.
Decomposition
A separation into discernible parts, each of which is simpler than the whole.
With procedural programming languages like C, decomposition involves dividing a complex task into a set of subtasks. This is a very verb- or action-oriented approach, involving dividing up the overall action into a series of smaller actions. This technique is called procedural decomposition.
Java was designed for a different kind of decomposition that is more noun- or object-oriented. Instead of thinking of the problem as a series of actions to be performed, we think of it as a collection of objects that have to interact.
Iterative Enhancement or stepwise refinement
The process of producing a program in stages, adding new functionality at each stage. A key feature of each iterative step is that you can test it to make sure that piece works before moving on.
Static Methods
Java is designed for objects, and programming in Java usually involves decomposing a problem into various objects, each with methods that perform particular tasks. You will see how this works in later chapters, but for now, we are going to explore procedural decomposition. We will postpone examining some of Java’s details while we discuss programming in general. public static void () { ; ; ... ; }
Method Call
A command to execute another method, which causes all of the statements inside that method to be executed.
flow of control
The order in which the statements of a program are executed
type-safe language
Java is a type-safe language, which means that it requires you to be explicit about what kind of information you intend to manipulate and it guarantees that you manipulate the data in a reasonable manner. Everything that you manipulate in a Java program will be of a certain type, and you will constantly find yourself telling Java what types of data you intend to use.
Data Type
A name for a category of data values that are all related, as in type int in Java, which is used to represent integer values. A decision was made early in the design of Java to support two different kinds of data: primitive data and objects.
Primitive Types
There are eight primitive data types in Java: boolean, byte, char, double, float, int, long, and short. Four of these are considered fundamental: boolean, char, double, and int. The other four types are variations that exist for programs that have special requirements.
Expression
A simple value or a set of operations that produces a value. The simplest expression is a specific value, like 42 or 28.9. We call these “literal values,” or literals.
Evaluation
The process of obtaining the value of an expression.
The value obtained when an expression is evaluated is called the result.
Operator
A special symbol (like + or *) that is used to indicate an operation to be performed on one or more values.
The values used in the expression are called operands.
JShell
Starting with Java 9, the Java Development Kit now includes a tool called JShell that you can use to explore Java’s syntax and features. JShell is a program with a prompt where you can type arbitrary Java expressions. Once you press Enter, the expression you typed will immediately be evaluated and its result will be displayed. This kind of tool is often called a Read-Evaluate-Print Loop or REPL for short.
Read-Evaluate-Print Loop (REPL)
A program that prompts the user for an individual code expression, then executes the expression and displays its result immediately.
Literals:
The simplest expressions refer to values directly using what are known as literals. An integer literal (considered to be of type int) is a sequence of digits with or without a leading sign:
3 482 −29434 0 92348 +9812
remainder operator (%)
The remainder operator (%) is usually referred to as the “mod operator,” or simply “mod.” The mod operator lets you know how much was left unaccounted for by the truncating division operator.
Precedence
The binding power of an operator, which determines how to group parts of an expression.
unary
Another concept in arithmetic is unary plus and minus, which take a single operand, as opposed to the binary operators we have seen thus far (e.g., *, /, and even binary + and −)
Java Operator Precedence
unary operators
multiplicative operators
additive operators
assignment operators
Mixing Types
mixing values of different types and wanting to convert from one type to another. Java has simple rules to avoid confusion and provides a mechanism for requesting that a value be converted from one type to another.
Two types that are frequently mixed are ints and doubles. Java converts the int into a double and performs the computation entirely with double values
cast
Sometimes you want Java to go the other way, converting a double into an int. You can ask Java for this conversion with a cast. Think of it as “casting a value in a different light.”
Variable
`A memory location with a name and a type that stores a value.
Declaration
A request to set aside a new variable with a given name and type. Each variable is declared just once
uninitialized variables
So how do we get values into those cells? The easiest way to do so is using an assignment statement. The general syntax of the assignment statement is
= ;
height = 70;
incrementing
x = x + 1;
x += 1;
Assignment statement
We have been referring to the “assignment statement,” but in fact assignment is an operator, not a statement. When you assign a value to a variable, the overall expression evaluates to the value just assigned. That means that you can form expressions that have assignment operators embedded within them. Unlike most other operators, the assignment operator evaluates from right to left
String Concatenation
Combining several strings into a single string, or combining a string with other data into a new, longer string.
promote
The technical term is that Java promotes the integer into a double.
x = (int) (3.4 * 2.9); // now legal
Control Structure
A syntactic structure that controls other statements.
The for loop is the first example of a control structure that we will study.
For loop pattern
for (int = 1; <= n; ++) { ; ; ... ; }
Scope
The part of a program in which a particular declaration is valid.
Local Variable
A variable declared inside a method that is accessible only in that method.
Localizing Variables
Declaring variables in the innermost (most local) scope possible. In general, you will want to declare variables in the most local scope possible
Infinite Loop
A loop that never terminates.
Pseudocode
As you write more complex algorithms, you will find that you can’t just write the entire algorithm immediately. Instead, you will increasingly make use of the technique of writing pseudocode. English-like descriptions of algorithms. Programming with pseudocode involves successively refining an informal description until it is easily translated into Java.
Class Constant
A named value that cannot be changed. A class constant can be accessed anywhere in the class (i.e., its scope is the entire class). Constants are declared with the keyword final, which indicates the fact that their values cannot be changed once assigned, as in public static final = ; You can declare a constant anywhere you can declare a variable, but because constants are often used by several different methods, we generally declare them outside methods.
Parameters
Any of a set of characteristics that distinguish different members of a family of tasks. To parameterize a task is to identify a set of its parameters.
Formal Parameter
A variable that appears inside parentheses in the header of a method that is used to generalize the method’s behavior.
Actual Parameter
A specific value or expression that appears inside parentheses in a method call.
The word “argument” is often used as a synonym for “parameter,” as in “These are the arguments I’m passing to this method.” Some people prefer to reserve the word “argument” for actual parameters and the word “parameter” for formal parameters.
Method Overloading
The ability to define two or more different methods with the same name but different method signatures.
Method Signature
The name of a method, along with its number and type of parameters.
Return
To send a value out as the result of a method that can be used in an expression in your program. Void methods do not return any value. When Java encounters a return statement, it evaluates the given expression and immediately terminates the method, returning the value it obtained from the expression.
return type
You can tell whether or not a method returns a value by looking at its header. All the methods you’ve written so far have begun with public static void. The word void is known as the return type of the method.
The Math Class
Whenever you want to refer to something declared in another class, you use dot notation:
.
Object
A programming entity that contains state (data) and behavior (methods).
As we said in Chapter 1, classes are the basic building blocks of Java programs. But classes also serve another purpose: to describe new types of objects.
Class
A category or type of object. When it is used this way, a class is like a blueprint of what the object looks like. Once you’ve given Java the blueprint, you can ask it to create actual objects that match that blueprint. We sometimes refer to the individual objects as instances of the class. We tend to use the words “instance” and “object” interchangeably.
String objects
String objects are one of the most useful and most commonly used types of objects in Java, so they make a good starting point. They aren’t the best example of objects, though, because there are a lot of special rules that apply only to strings. You’ll notice that the type String is capitalized (as are the names of all object types in Java)
Index
An integer used to specify a location in a sequence of values. Java generally uses zero-based indexing (with 0 as the first index value, followed by 1, 2, 3, and so on).
Exception
A runtime error that prevents a program from continuing its normal execution.
We say that an exception is thrown when an error is encountered. When an exception is thrown, Java looks to see if you have written code to handle it If not, program execution is halted and you will see what is known as a stack trace or back trace. The stack trace shows you the series of methods that have been called, in reverse order.
Useful String methods
charAt(index); indexOf(text); length(); replace(s1, s2); substring(start, stop); toLowerCase()
Immutable Object
An object whose value cannot be changed. ex Strings
Console Input
Responses typed by the user when an interactive program pauses for input. When you refer to System.out, you are accessing an object in the System class known as the standard output stream, or “standard out” for short. There is a corresponding object for standard input known as System.in, but Java wasn’t designed for console input, and System.in has never been particularly easy to use for this purpose. Fortunately for us, there is an easier way to read console input: Scanner objects.
Constructor (Construct)
Most objects have to be explicitly constructed by calling a special method known as a constructor. A special syntax used to create and initialize an object. Objects in Java programs must be constructed before they can be used. Remember that a class is like a blueprint for a family of objects. Calling a constructor is like sending an order to the factory asking it to follow the blueprint to get you an actual object that you can manipulate.
Constructor syntax
In Java, constructors are called using the special keyword new, followed by the object’s type and any necessary parameters. For example, to construct a specific Scanner object, you have to pass information about the source of input. In particular, you have to provide an input stream. To read from the console window, pass it System.in: Scanner console = new Scanner(System.in); Once you’ve constructed the Scanner, you can ask it to return a value of a particular type. int n = console.nextInt();
Token
A single element of input (e.g., one word, one number).
By default, the Scanner uses whitespace to separate tokens.
Whitespace
Spaces, tab characters, and newline characters. hello there. "how are" "you?" all-one-token would be split into six tokens: hello there. "how are" "you?" all-one-token
Package
A collection of related Java classes. For example, the Scanner class is stored in a package known as java.util. Java programs don’t normally have access to a package unless they include an import declaration.
Import Declaration
A request to access a specific Java package. Java allows you to use an asterisk to import all classes from a package:
import java.util.*;