Comp111-Final Exam Flashcards
What is a sentinel value?
A constant used in a loop, so that when that value is received as input, the loop terminates.
boolean stopLoop = false; int ctr = 0; while (stopLoop){ if(ctr++ > 12){ System.out.println(ctr); } }
- how many times will this loop execute?
- What will be the output from this ?
- 0 times
- nothing, null
Describe the four types of loops. When would each be used? Know how to write each type of loop.
while - pretest loop, as the condition is tested before the execution of the body. If false, it never runs. initialize while(condition){ body update }
for - ideally used for counting, especially in the case of an array. During each step of the loop, the body is executed, & the update is to increment the variable.
for(initialize; condition; update){
body
}
do/while - used when loop needs to run an indeterminate amount of times, where the body needs to execute before the condition is tested. variable do{ body update } while(condition);
enhanced for - used to traverse through the elements of an array or ArrayList.
for(dataType variable : collection){
statement;
}
Can a for loop be rewritten as a while loop? As a do/while loop?
- Yes
- Yes
What are the four parts of a loop?
initialization, condition, body, update
Write the while loop statement to print the value for each element in an array of type String named Dogs.
int i = 0; while(i < Dogs.length){ System.out.println(Dogs[i]); i++; }
Write the for loop statement to print the value for each element in an array of type String named Dogs.
for(int i = 0; i < Dogs.length - 1; i++){
System.out.println(Dogs[i]);
}
Write the enhanced for loop statement to print the value for each element in an array of type String named Dogs.
for(String dog : Dogs){
System.out.println(dog);
}
Write the code segment to prompt a user for entry of a double value until a sentinel value of -99.99 is entered. After the sentinel value, compute the average of the numbers entered.
double sent = 99.99; double i = 0; int count = 0; while(i < sent){ System.out.println("Enter value: "); Scanner scanDbl = new Scanner(System.in); double inDbl = scanDbl.nextDouble();
if((i + inDbl) > sent){ System.out.println("too large"); }else if(((I + inDbl) <= sent){ i = i + inDbl; if(i == sent){ System.out.println("max value reached"); System.out.println(“average is: “ + (sent / (double)count)); return; } } }
Evaluate the following code: Random generator = new Random(); int newNumber = 5 + generator.nextInt(10); o What possible values will the random number generater return based upon the above explicit paramter of 10? o What possible values will the integer variable newNumber contain after these statements execute?
- Random number between 0 & 9
- Random number between 5 & 14
Write the statement to generate a random number that represents a roll of a die. Where each side of the die is represented by the numbers 1 through 6, the 1 is number 1, the 2 is number 2, etc.
Random generator = new Rnadom(); int d = 1 + generator.nextInt();
• Describe wrapper and autoboxing. What are the wrapper classes for int, double, boolean, and char?
Wrapper Class is because Arraylist cannot take in primitive data types. Autoboxing is the process of changing primitives to a Wrapper Class. Integer, Double, Boolean, & Character.
• How do you convert a String variable to an integer using the wrapper class?
valueOf() to get an int.
• How do you convert a String variable to a double using the wrapper class?
valueOf() to get an double
• Evaluate the following statement, assuming that testArray is defined with a length of 10 and a data type of double.
testArray[10] = 104.89;
o What are the boundaries of the array?
o Is the double value being inserted within the boundaries?
o Will this statement compile? If no, why not?
o Will an error be produced at run time? if yes, what error?
- 10 array elements available, 0-9
- No
- Yes, because it is correct in respect to syntax.
- No, as the array position 10 is out of bounds.
• Describe an array verses and ArrayList.
o What are the advantages and disadvantages of both?
Array is a fixed length data structure. Whereas Arraylist is a variable length. Array is useful when the amount needed for the structure is known, but it cannot be adjusted. ArrayLists can grow & shrink as needed, and come with built in methods.
• What type of class is an ArrayList?
Collection Class
• What methods are available with an ArrayList?
size(), get(), add(), indexOf(), contains(), set(), remove(), sort(), clear()
• Write the following declaration statements:
o Declare an array of 100 Coin objects with a name of tmpCoin.
o Declare an array of 50 int values with a name of dogs.
o Declare an array of 75 Rat objects with a name of compRats.
o Declare a 4 element integer array with a name of scores, and assign the whole numbers 1 through 4 to the array elements.
o Declare an ArrayList of data type String and a name of employees.
o Declare an ArrayList of Paycheck objects with a name of AnnualPay.
- Coin [] tmpCoin = new Coin[100];
- int [] dogs = new int[50];
- Rats [] compRats = new Rats[75];
- int [] scores = new int[4];
scores = {1, 2, 3, 4}; - ArrayList employees = new ArrayList();
- ArrayList AnnualPay = new ArrayList();
• How do you return the length of a
o String?
o array?
o ArrayList?
- String.length()
- array.getLength();
- ArrayList.size();
• How do you know how many elements within an array have been populated?
Establish an int variable to count the elements that have been added to the array. You can then compare it to the array.getLength().
• Write the code to populate an ArrayList with the numbers 0 through 85, where each element is the array list is assigned 1 number. The first element is assigned 0, the second assigned 1, and so on.
o Write the code using a for loop.
o Write the code using a while loop.
o Write the code using a do/while loop.
- int sent = 85; for(int i = 0; i <= sent; i++){ ArrayList.add(i); i++; } - int sent = 85; int i = 0; while(i <= sent){ ArrayList.add(i); i++; } - int sent = 85; int i = 0; do{ ArrayList.add(i); i++; }while(i <= sent);
• What is the purpose of the enhanced for loop?
For traversing all elements of an array or ArrayList.
• Assuming you have an array list of data type string named dogs, write an enhanced for loop to read the entire array list and print out the content of each array element.
for(String dog: dogs){
System.out.println(dog);
}
• Declare a two-dimensional array that has 3 rows and 4 columns of integer values with a name of testScores.
int row = 3;
int col = 4;
int[][] testScores = new int[row][col];
• Write a loop to populate each element within the array with a value of 0.
int v = 0; for(int i = 0; i < row; i++){ for(int j = 0; i < col; j++){ int[i][j] = v; } }
• What is an actor class?
A class that performs a certain task, like Scanner or Random
• What does an actor class model, a noun or a verb?
verb
What is a utility class?
Utility are classes that provide services, like Math
What is cohesion
Is a measure of the degree to which a class models simple concepts.
When designing a class is high or low cohesion desired?
High cohesion is desired
What is coupling?
Coupling is a measure of the degree to which a class depends on other classes to work properly.
What is a package?
Packages group classes with similar purposes into a structure that is more manageable for programmers.
• When designing a package (group of classes) is high or low coupling desirable?
low coupling
• What is an immutable class?
A class that defines no mutators
What is a mutator class?
A class that changes the state of an object
What is a side effect?
Any kind of modification that is observable outside of the method.
What is a precondition?
Must be true before a method executes, enforced by stat validation outside the method.
What is a postcondition?
What is true after the method executes.