Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
86 Cards in this Set
- Front
- Back
The variable names references an integer array with 20 elements. Write a for loop that prints each element of the array.
|
for( int i = 0; i < 20; i++)
System.out.println(names[i]); |
|
The variables numberArrayl and numberArray2 reference arrays that each have elements. Write code that copies the values in numberArrayl to numberArray2.
|
for( int i = 0; i < numberArray2.length; i++)
numberArray2[i] = numberArray1[i]; |
|
I. Write a statement that declares a String array initialized with the following string:
"Einstein", "Newton", "Copernicus", and "Kepler". II. Write a loop that displays the contents of each element in the array you declared in Part a. III. Write code that displays the total length of all the strings in the array you declared in Part a. |
String [ ] names = {"Einstein", "Newton", "Copernicus", "Kepler"};
for( int i = 0; i < 4; i++) System.out.println(names[i]); int intTotal = 0; for( int i = 0; i < 4; i++) intTotal = intTotal + names[i].length(); System.out.println(intTotal); |
|
I. In a program you need to store the populations of 12 countries.
a. Define two arrays that may be used in parallel to store the names of the countries and their populations. b. Write a loop that uses these arrays to print each country's name and its population. |
a. String [ ] names = new String[12];
int [ ] pop = new int[12]; b. for( int i = 0; i < 12; i++) System.out.println(names[i] + “ “ + pop[i]); |
|
In a program you need to store the identification numbers of 10 employees (as integers) and their weekly gross pay (as double values).
a. Define two arrays that may be used in parallel to store the 10 employee’s identification numbers and gross pay amounts. b. Write a loop that uses these arrays to print each of the employees' identification number and weekly gross pay. |
a. int [ ] id = new int[10];
double [ ] pay = new double[10]; b. for( int i = 0; i < 10; i++) System.out.println(id[i] + “ “ + pay[i]); |
|
a. Declare and create a two-dimensional int array named grades. It should have 30 rows and 10 columns.
b. Write code that calculates the average of all the elements in the grades array that you declared in a. |
a. int [ ] [ ] grades = new int [30] [10];
b. double totalGrades = 0; int totalStudents = 0; for( int i = 0; i < 30; i++) for( int j = 0; j < 10; j++) { totalGrades = totalGrades + grades [i] [j]; totalStudents = totalStudents + 1; } System.out.println(totalGrades/totalStudents); |
|
Look at the following array declaration:
int [ ] [ ] numberArray = new int [9] [11]; a. Write a statement that assigns 145 to the first column of the first row of this array. b. Write a statement that assigns 18 to the last column of the last row of this array. |
a. numberArray [0] [0] = 145;
b. numberArray [8] [10] = 18; |
|
The values variable references a two-dimensional double array with 10 rows and 20 columns. Write code that sums all the elements in the array and stores the sum in the variable total.
|
double total = 0;
for( int i = 0; i < 10; i++) for( int j = 0; j < 20; j++) total = total + values [i] [j]; |
|
An application uses a two-dimensional array declared as follows:
int [ ] [ ] days = new int [29] [5]; a. Write code that sums each row in the array and displays the results. b. Write code that sums each column in the array and displays the results. |
a. int totalRow = 0;
for( int col = 0; col < 5; col++) { totalRow = 0; for( int row = 0; row < 29; row++) totalRow = totalRow + days [row][col]; System.out.println(totalRow); } b. int totalCol; for( int row = 0; row < 29; row++) { totalCol = 0; for( int col = 0; col < 5; col++) totalCol = totalCol + days [row] [col]; System.out.println(totalCol); } |
|
In an inheritance relationship, what is the general class called?
|
superclass
|
|
In an inheritance relationship, what is the specialized class called?
|
subclass
|
|
This key word indicates that a class inherits ftom another class.
|
extends
|
|
A subclass does not have access to these superclass members.
|
private
|
|
This key word refers to an object's superclass.
|
super
|
|
In a subclass constructor, a call to the superclass constructor must appear where?
|
first statement
|
|
What is method overriding?
|
method in subclass has the same signature as method in superclass.
|
|
Write the first line of the definition for a Poodle class. The class should inherit from the Dog class.
|
public class Poodle extends Dog
|
|
Look at the following code which is the first line of a class definition:
public class Bear extends Animal In what order will the class constructors execute? |
Animal, then Bear
|
|
Write the statement that calls a superclass constructor and passes the arguments x, y, and z.
|
super(x, y, z);
|
|
A superclass has the following method:
public void setNumber (int n) { number = n; } Write a statement that can appear in a subclass that calls this method, passing 25 as an argument. |
setNumber(25);
|
|
Write the first line of the definition for a Stereo class. The class should inherit from the SoundSystem class and should implement the CDPlayable interface.
|
public class Stereo extends SoundSystem implements CDPlayable
|
|
Write an interface named Nameable that specifies the following methods:
public void setName (String n) public String getName () |
public interface Nameable
{ void setName(String n); String getName(); } |
|
The following if statement determines whether choice is equal to „Y‟ or „y‟
if(choice == „Y‟ || choice == „y‟) Rewrite this statement so it only makes one comparison and does not use the || operator . (Hint: Use either the toUpperCase or toLowerCase methods.) 2. Write a loop that counts the number of space characters that appear in the String object line |
if (Character.toUpperCase(choice) == ‘Y’)
String line; int totalSpace = 0; for (int i = 0; i < line.length(); i++) { if (Character.isSpaceChar(line.charAt(i))) totalSpace ++; } |
|
a. Write a loop that counts the number of space characters that appear in the String object line.
b. Shorter version |
a. String line;
char ch; int totalSpace = 0; for (int i = 0; i < line.length(); i++) { ch = line.charAt(i); if (Character.isSpaceChar(ch)) totalSpace ++; } b. String line; int totalSpace = 0; for (int i = 0; i < line.length(); i++) { if (Character.isSpaceChar(line.charAt(i))) totalSpace++; } |
|
Write a loop that counts the number of digits that appear in the String object line.
|
String line;
int totalDigit = 0; for (int i = 0; i < line.length(); i++) { if (Character.isDigit(line.charAt(i))) totalDigit ++; } |
|
Write a loop that counts the number of lowercase characters that appear in the String object line.
|
int totalLower = 0;
for (int i = 0; i < line.length(); i++) { if (Character.isLowerCase(line.charAt(i))) totalLower ++; } |
|
Look at the following string:
“cookies>milk>fudge:cake:ice cream” Write code using a StringTokenizer object that extracts the following tokens from the string and displays them: cookies, milk, fudge, cake, and ice cream. |
StringTokenizer token = new StringTokenizer (“cookies>milk>fudge:cake:ice cream”, “>:”);
while (token.hasMoreTokens()) System.out.println (token.nextToken()); |
|
Assume that d is a double variable. Write an if statement that assigns d to the int variable i, if the value in d is not larger than the maximum value for an int.
|
if ((int) d <= Integer.MAX_VALUE)
i = (int) d; |
|
______ variables are designed to hold only one value at a time.
|
Primitive
|
|
Arrays allow us to create a collection of like values that are _______.
|
indexed
|
|
An array can store any ________ but only one type of data at a time
|
type of data
|
|
An array is a list of _______.
|
data elements
|
|
An array is an object so it needs an ________.
|
object reference
int[] numbers; |
|
The array size must be a ____ number.
|
non-negative
|
|
An array is accessed by:
|
the reference name
a subscript that identifies which element in the array to access. |
|
The length of an array can be obtained via its ____ constant
|
length
ex: int size = temperatures.length; |
|
It is possible to get the size of an array from a user:
|
String input;
int numTests; int[] tests; input = JOptionPane.showInputDialog(null, "How many numbers do you have? "); numTests = Integer.parseInt(input); tests = new int[numTests]; |
|
Finding the Highest Value:
|
int [] numbers = new int[50];
int highest = numbers[0]; for (int i = 1; i < numbers.length; i++) { if (numbers[i] > highest) highest = numbers[i]; } |
|
Finding the Lowest Value:
|
int lowest = numbers[0];
for (int i = 1; i < numbers.length; i++) { if (numbers[i] < lowest) lowest = numbers[i]; } |
|
Summing Array Elements:
|
int total = 0; // Initialize accumulator
for (int i = 0; i < units.length; i++) total += units[i]; |
|
Averaging Array Elements:
|
double total = 0; // Initialize accumulator
double average; // Will hold the average for (int i = 0; i < scores.length; i++) total += scores[i]; average = total / scores.length; |
|
To use the class, the import statement:
|
import java.util.Array;
|
|
The return type of the method must be declared as an array of the right type.
|
public static double[] getArray()
{ double[] array = { 1.2, 2.3, 4.5, 6.7, 8.9 }; return array; } |
|
Arrays have a final field named ______.
|
length
|
|
String objects have a method named ______.
|
length
|
|
An array’s length is a field
|
You do not write a set of parentheses after its name.
|
|
A String’s length is a method
|
You do write the parentheses after the name of the String class’s length method.
|
|
By using the same subscript, you can build relationships between data stored in two or more arrays:
|
String[] names = new String[5];
String[] addresses = new String[5]; |
|
An ______ is an object that describes an unusual or erroneous situation
|
exception
|
|
Exceptions are thrown by a program, and may be _____ and _______ by another part of the program
|
caught
handled |
|
A program can be separated into a _____ execution flow and an _______ execution flow
|
normal
exception |
|
Java has a ______ set of exceptions and errors that can occur during execution
|
predefined
|
|
VERY IMPORTANT!!!!!!!!!!
The relationship between a superclass and an inherited class is called an _______. |
“is a” relationship
|
|
We can _______ the capabilities of a class
|
extend
|
|
Inheritance involves a _______ and a subclass
|
superclass
|
|
The subclass is based on, or _______ from, the superclass.
|
extended
|
|
The relationship of classes can be thought of as parent ______ and _________.
|
classes and child classes
|
|
The subclass inherits fields and methods from the superclass without any of them being _______.
|
rewritten
|
|
The Java keyword, _______, is used on the class header to define the subclass.
|
extends
ex: public class FinalExam extends GradedActivity |
|
Members of the superclass that are marked _____
-are not inherited by the subclass |
private
|
|
Members of the superclass that are marked _______.
-are inherited by the subclass |
public
|
|
Constructors are ________.
|
not inherited
|
|
When a subclass is instantiated, the _______ _______ constructor is executed first.
|
superclass default
|
|
The _______ keyword refers to an object’s superclass.
|
super
|
|
The subclass method overrides the superclass method.
This is known as _________. |
method overriding
|
|
The class StringTokenizer is in the _____ package.
|
java.util
|
|
the constructor method:
|
public StringTokenizer (String theString)
StringTokenizer tokens = new StringTokenizer(line) |
|
Constructor for a tokenizer that will use the characters in the string delimiters as separators when finding tokens in theString:
|
StringTokenizer tokens = new StringTokenizer(line, “,-”)
|
|
Returns true if there are more tokens in theString; returns false otherwise.
|
public boolean hasMoreTokens()
|
|
Returns the next token from theString
|
public String nextToken()
|
|
Returns the number of tokens remaining to be returned by nextToken
|
public int countTokens()
|
|
In each of the following statements us in the StringTokenizer class, how many tokens would be returned?
String line = “Ed’s ID# is 234-61-9876!”; a. StringToken tokens = new StringTokenizer(line, “-”) b. StringToken tokens = new StringTokenizer(line, “#-”) c. StringToken tokens = new StringTokenizer(line, “#’-”) |
a. 3
b. 4 c. 5 |
|
An _______ is an object that describes an unusual or erroneous situation
|
exception
|
|
Exceptions are thrown by a program, and may be caught and handled by _________.
|
another part of the program
|
|
A program can be separated into a normal execution flow and an _______ execution flow
|
exception
|
|
If an exception is ignored by the program, the program will ___________ and produce an appropriate message
|
terminate abnormally
|
|
To process an exception when it occurs, the line that throws the exception is executed within a _______.
|
try block
|
|
A ______ is followed by one or more catch clauses, which contain code to process an exception
|
try block
|
|
Each catch clause has an associated exception type and is called an _________
|
exception handler
|
|
A try statement can have an optional clause following the catch clauses, designated by the reserved word _______
|
finally
|
|
The statements in the finally clause _____ are executed
|
always
|
|
When working with GUI, the two sets classes that we focus on are ______ classes
|
AWT and Swing
|
|
AWT is ___________.
|
the Abstract Windowing Toolkit (AWT)
|
|
Swing is a library of classes that provide an __________.
|
improved alternative for creating GUI applications and applets
|
|
The Swing classes are part of the _______ package.
|
javax.swing
|
|
In the main method, two constants are declared to set the window size:
|
final int WINDOW_WIDTH = 350,
WINDOW_HEIGHT = 250; |