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;
38 Cards in this Set
- Front
- Back
1. Write a for loop that displays the following set of numbers:
0, 10, 20, 30, 40, 50 . . . 1000. |
for(int i = 0; i <= 1000; i += 10)
System.out.print(i + “,”); |
|
2. Write a loop that asks the user to enter a number. The loop should iterate 10 times and keep a running total of the numbers entered.
|
Scanner k = new Scanner(System.in);
int total = 0; for(int i = 0; i < 10; i ++){ System.out.println(“Enter a number: “); total += k.nextInt(); } |
|
3. Write a for loop that calculates the total of the following series of numbers:
1/30 + 2/29 + 3/28 + 4/27 + . . . 30/1 |
double total = 0;
for(int i = 1; i < 31; i ++){ total += (i / (31 – i)); } |
|
4. Write a nested loop that displays 10 rows of ‘#’ characters. There should be 15 ‘#’ characters in each row.
|
for(int i = 0; i < 10; i ++){
for(int j = 0; j < 15; j++){ System.out.print(“#”); } System.out.println(); } |
|
5. Covert the while loop in the following code segment to a for loop:
int count = 0; while (count < 50) { System.out.println(“count is “ + count); count++; } |
for(int i = 0; i < 50; i++)
System.out.println(“count is “ + i); |
|
In regards to fields and methods, what happens when one class inherits from another?
|
You're allowed to use all the methods
|
|
Explain what a reference alias is:
|
2 reference variables that refer to the same object
|
|
Explain what a static field is (a field is a variable):
|
Static fields are created in memory one time. Variable that is shared among off classes. All objects share the same value when you have a static field
|
|
Explain what a static modifier on method headers means
|
Allows you to call the method without creating an object. You do not have to call the method through the object.
|
|
Explain the null keyword and why it’s used:
if(object != null) |
Reference variable does not hold value or doesn’t point to an object. Used to see if it in fact does point to the object.
|
|
polymorphism:
|
Reference variable can point to an object of its declared type or any object that extends from that object
|
|
super() reference:
|
the one the class extends from. Refers from the class directly above
|
|
Describe "is-a" relationship
|
a relationship that only goes one way.
ex: a thumb is a finger but a finger is not a thumb. |
|
Inheritance is the process of deriving:
|
a new class from an existing one.
also to reuse existing software |
|
wrapper classes do:
|
they "wrap" the primitive data type into an object of that class.
ex: Integer n = new Integer (15); |
|
explain how does a try/catch works:
|
Each catch clause on a try statement
handles a particular kind of exception that may be thrown within the try block. |
|
Why would you use try/catch blocks:
|
to guard against and handle a run-time error
|
|
What is an advantage of an ArrayList:
|
ArrayList don’t have a set limit or size
|
|
What is an disadvantage of an ArrayList:
|
ArrayList only hold objects
|
|
Write the code to loop through and output all the elements of a given ArrayList:
|
for(int i =0; i < staff.size(); i++)
REMEMBER ARRAYLIST USES .SIZE ARRAY USES .LENGTH |
|
ArrayList<Player> band = new ArrayList<Player>();
Player p1 = new Player(“Joe”, 12, “Center”); Write the code to add the Player object (p1) to the ArrayList. |
band.add(p1);
|
|
Given that there is an ArrayList named list that contains 5 Player objects, write the code to call and output the getPoints() method of the third element in list. The getPoints() method accepts no parameters and returns an int value.
|
System.out.println(list.get(2).getPoints());
|
|
Given that there is an ArrayList named staff that contains Employee Objects, write the code that will loop through staff, calling and outputting the toString() method for each Employee object.
|
for(int i =0; i < staff.size(); i++){
System.out.println(staff.get(i).toString()); } |
|
ArrayList list = new ArrayList();
int num = 14; Write the code to add the value contained in num to the ArrayList. Remember that ArrayLists can only hold objects. |
list.add(new Integer(num));
|
|
Complete method getTotalPoints, which returns the total points of a given set of objects.
//precondition: list is a non-empty ArrayList containing Player objects. //postcondition: returns the total of the int values stored in the points field of the Player objects contained inside the ArrayList. //the Player class contains method getPoints() which returns the point field of the Player object. |
public static double getTotalPoints (ArrayList list){
double total = 0.0; for(int i = 0; i < list.size(); i++){ total += list.get(i).getPoints(); } Return total; } |
|
Create a program (driver) that generates fifty random numbers in the 1 – 100 (inclusive) range. Your program should store those numbers into an array. The program should then create a chart showing how often a range of numbers is represented (see chart below). For example, if three of the fifty random numbers are between 11 and 20, there should be three * marks in the 11-20 row.
|
import java.util.Random;
public class Rainfall { public static void main(String[] cheese){ Random rand = new Random(); int[] numbers = new int[50]; String[] output = {"", "", "", "", "", "", "", "", "", ""}; for(int i = 0; i < numbers.length; i++){ numbers[i] = rand.nextInt(100) + 1; } for(int i = 0; i < numbers.length; i++){ int index = (numbers[i] - 1) / 10; output[index] += "*"; } for(int i = 0; i < output.length; i++){ System.out.println((i*10 + 1) + "-" + (i*10 + 10) + ": " + output[i]); } } } |
|
The Boolean Expression: !(A || B) is equivalent to which of the following?
a. A != B b. !A || B c. A && B d. !A || !B e. !A && !B |
E. !A && !B
|
|
The Boolean Expression: !((A < B) && (C > D)) is equivalent to which of the following?
a. (A < B) || (C > D) b. (A >= B) && (C <= D) c. (A >= B) || (C <= D) d. (A > B) && (C < D) e. (A > B) || (C < D) |
C. (A >= B) || (C <= D)
|
|
Write the code that creates an object of the Scanner class. Use that object to take a String variable name, and set it equal to what the user types into the keyboard.
|
Scanner k = new Scanner(System.in);
|
|
Based on the following code, what data type does the determineHeight method return?
double armLength = 18.3; double legLength = 20.5; int bodyLength = 13; int age = 15; double height = determineHeight(armLength, legLength, bodyLength, age); |
double armLength = 18.3;
double legLength = 20.5; int bodyLength = 13; int age = 15; |
|
Queues
Where are new elements added From where are elements removed |
Back
Front |
|
Given an implementation, determine the order in which elements are removed in a queue:
|
First In First Out - FIFO
|
|
Stacks
Where are new elements added From where are elements removed top or front |
top or front
top or front Last In First Out-LIFO |
|
what does this do:
.hasNext() |
gives us next value
|
|
what does this do:
.add(e) |
add and element to stack or queue
|
|
what does this do:
peek() |
looks at top element w/o removing it from stack
|
|
what does this do:
pop() |
used to remove the object at the top of this stack and returns that object as the value of this function.
|
|
what does this do:\
push() |
This method pushes an item onto the top of this stack
|