|
|
- What is the first index position of a string?
- The first position in a string is denoted by index position 0.
- In the .substring(int, int) method:
- What does the first explicit parameter represent? The first element position within the String that is to be returned.
- What does the second explicit parameter represent? Return up to - but not including, this element position.
- What element position is the first element position in a string? Position 0 is the first element position in a String.
- Using the .substring() method, write the code to do the following for the String variable testString.
- return just the first character testString.substring(0, 1)
- return just the last character testString.substring(testString.length() - 1)
- remove the first character from the string testString.substring(1)
- remove the last character from the string testString.substring(0, testString.length() - 1)
- Evaluate
the following statements:
- String quote = “study very hard”;
- System.out.println(quote.substring(4, 9));
- Identify
the object(s), method(s), parameter(s)
- objects - quote, System.out
- method - substring(), println()
- parameters - 4, 9, quote, output from quote.substring(4, 9), System.out
- 4 & 9 are explicit parameters passed to the substring() method
- quote is an implicit parameter also passed to the substring() method
- the output from quote.substring(4, 9) is an explicit parameter passed to the println() method
- System.out is an implicit also passed to the println() method
- Is there an implicit
parameter? If yes, what is it?
- yes - quote is an implicit parameter to the substring() method
- System.out is an implicit parameter to the println() method
- Is
there an explicit parameter(s)? If yes, what is it?
- yes - 4 & 9 are explicit paramters for the substring() method
- quote.substring(4,9) is an explicit parameter for the println() method
- What will be the output of these statements?
- y ver
- Remember that when you start counting, being with 0, so s is position 0, t is position 1, and y is position 4.
- Continue counting to position 9 which is the y in very.
- With
the substring, the first position is inclusing meaning it is included
in the output, but the last position is exclusive, meaning it is not
included.
- Evaluate
the following statement:
- String output = "Value" + 7/3;
- What will be the output of this statement?
- Value2
- Both
7 and 3 are integer values. In integer division the result is
always an integer, no remainder. 7 divided by 3 is 2 with a
remainder of 1. The one is discarded giving you just 2. No
space is indicated after the string Value so the result is Value2.
- How is input
received from the console? What object needs to be declared?
How is integer data accepted? double data accepted? string data
accepted?
- The Scanner object is used to receive input from the console.
- Scanner in = new Scanner();
- integer data - nextInt()
- double data - nextDouble()
- string data
- nextLine() - read in all tokens (words) up to the carriage return line feed (enter key)
- next() - read in the next token (word) up to the space
- 5.1 - 5.4: If statements, comparing values, boolean expressions
- What comparison operator is used
- To compare 2 integer values? ==
- To compare 2 double values? ==
- When
should you NOT use the == comparison operator? What should you use instead?
- When
you are comparing values where the decimal values after a certain point
are not important. For example if you have a repeating decimal
and you are only concerned with the first 3-4 decimal places. You
would then do the following:
- if Math.abs(value1 - value2) > epsilon where epsilon is .001 or whatever decimal place you want to test up to
- When
you are comparing objects, such a Strings. When comparing 2
string values you would use the .equals(), .equalsIgnoreCase(), or
.compareTo() methods. When comparing other objects, you would use
the .equals() methods that are written specifically for those objects.
- Can you use the == comparison operator to compare 2 floating point or double values?
- No.
The computer can take a floating point/double value out to the
nth decimal place, while we humans are only willing to go so far.
If the numbers are not exactly equals, clear out to the nth
decimal, the computer will return false. Instead, choose a close
enough value.
- For
instance assume you have 2 variables, x & y, both of data type
double and for your purposes they are equal if they are same to the 3rd
decimal place. You would then write your comparison as:
- if ((Math.abs(x - y) < .001)
- subtract
the 2 values and take the absolute value of the result (so you don't
have to worry about whether x is greater or y is greater and a negative
result), then compare the result to your close enough value, in this
case .001. If the answer is true, then the numbers are considered
equal, if false then unequal.
- What is the purpose of an IF statement?
- An IF statement determines whether a program will carrry out actions depending upon a given condition.
- Evaluate
the following statement:
- if ( x == 12 || 8 )
- Is
this a valid statement? Will it successfully compile?
- No, it is not a valid statement. It will generate a compiler error.
- Why or why not?
- x cannot be both an integer and a boolean, it must be one or the other.
- The condition x == 12 implies that x is an integer datatype, while the condition 8 implies that x is a boolean data type.
- Evaluate the following statement:
- if (!input.equals("R") || !input.equals("T"))
- System.out.println("Input is: " + input);
- What will be printed if input is equal to "R"?
- Input is R
- The
if statement says if it is NOT equal to R OR it's not equal to T.
input is equal to R which equates the first portion to false.
input is NOT equal to T which equates the second portion to true.
In an or condition if either side is true the result is true, and
the statement is printed.
- What will be printed if input is equal to "T"?
- What will be printed if input is equal to "Q"?
- Evaluate
the following statement, assuming that quote1 and quote2 are both data type String.
- quote1.compareTo(quote2)
- What
will be returned if the two String objects are equal?
- What
will be returned if quote1 is alphabetically before quote2?
- -1 indicates quote1 is alphabetically before quote2
- What
will be returned if quote2 is alphabetically before quote1?
- 1 indicates quote 2 is alphabetically before quote1
- Assuming P is true, Q is false and R is true, what will be the result of:
- P && Q false
- P || Q true
- !(P && R) false
- !(P || R) false
- Write the code to
test gender of an employee printing "Male" if the employee has a gender
of M and "Female" if the employee has a gender of F. Print "Unknown"
if neither M or F. The employee gender can be retrieved through the
get method getGender(). The gender value returned is data type char.
- If (employee.getGender() == 'M')
- {
- System.out.println("Male");
- }
- else if (employee.getGender() == 'F')
- {
- System.out.println("Female"):
- }
- else
- {
- System.out.println("Unknown");
- }
- Write
the code to test the age of an employee. Use the getAge() method to
retrieve the age which returns an integer value. Print the following:
- If the age is less than 25 print "youngster"
- If the age is greater than or equal to 25 and less than 35 print "Generation X".
- If the age is greater than or equal to 35 and less than 45 print "Generation Y".
- If the age is greater than or equal to 45 print "Baby Boomer".
- If (employee.getAge() < 25)
- {
- System.out.println("youngster");
- }
- else if (employee.getAge() < 35)
- {
- System.out.println("Generation X"):
- }
- else if (employee.getAge() < 45)
- {
- System.out.println("Generation Y"):
- }
- else
- {
- System.out.println("Baby Boomer");
- }
- 5.5 Code Coverage
- How are unit tests in Test Driven Development generally developed?
- They can be developed:
- prior to the class
- after the class
- in parallel with the class
- When should they be developed?
- They
SHOULD be developed prior to the class. This ensures that you are
not writing the test case to pass what you have written - potentially
giving you a false positive.
- 6.1 - 6.4: Loops & Sentinel Values
- What is a sentinel value?
- A special value designated by the programmer to terminate a loop.
- Evaluate the following statements:
- boolean stopLoop = false;
- int ctr = 0;
- while (stopLoop)
- {
- if
(ctr++ > 12)
- {
- System.out.println(ctr);
- stopLoop
= true;
- }
- }
- How many times will this loop execute?
- It
will never execute. The variable stopLoop is set equal to false.
The loop condition says to execute as long as the variable
stopLoop is equal to true. Since it is initialized to false, it
never executes.
- What will be the output from this loop?
- There is no output from the loop.
- Describe the four types of loops. When would each be used?
- While()
- continue processing as long as the condition is true, used when
there is a set condition that will eventually become false
- Do
While() - process the loop statements once and then continue processing
as long as the condition is true, used when the statements needs to be
executed at least once
- For
- continue processing as long as the condition is true, incrementing as
specified, used when processing a set number of time
- Enhanced for Loop - used to iterate through the objects in a collection such as an ArrayList.
- Can a for loop be rewritten as a while loop? As a do/while loop?
- while loop - yes
- do/while
loop - sometimes, a do/while always executes the code at least once and
then checks the condition. If this is the intent of the for loop
then it can be rewritten as a do/while loop.
- What are the four parts of a loop?
- initialization of the loop control variable
- condition to be tested
- update of the loop control variable
- body of the loop
- Write the loop statement to print the value for each element in an array of type String named dogs.
- Write the statement using the for loop.
- for (int i = 0; i < dogs.length; i++)
- {
- System.out.println(dogs[i]);
- }
- Write the statement using the enhanced for loop.
- for (String tmpDog: dogs)
- {
- System.out.println(tmpDog);
- }
- 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 input;
- double numberTotal = 0;
- int counter = 0;
- Scanner in = new Scanner(System.in);
- boolean keepLooping = true;
- while (keepLooping)
- {
- System.out.println("Enter a number, enter -99.99 to exit");
- input = in.nextDouble();
- if (input != -99.99)
- {
- numberTotal = numberTotal + input;
- counter++;
- }
- }
- if (counter > 0)
- {
- System.out.println("average = " + numberTotal / counter);
- }
- else
- {
- System.out.printlnt("No entries made");
- }
- 6.5: Random numbers & simulations
- Evaluate
the following code:
- Random generator = new Random();
- int newNumber = 5 + generator.nextInt(10);
- What possible values will the random number generater return based upon the above explicit paramter of 10?
- The random generator will return 0 through 9
- What possible values will the integer variable newNumber contain after these statements execute?
- Because the value 5 is added to the randomly generated numbers, the possible values are 5 through 14.
- Write the statements 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 ran = new Random();
- int roll = 1 + ran.nextInt(6);
- ***The
random number generator will generate a value from 0 inclusive to 6
exclusive or values from 0 to 5. You have to add 1 to the result
to get the values 1 through 6.
- 7.3: Wrappers & Autoboxing
- Describe wrapper and autoboxing. What are the wrapper classes for int, double, boolean, and char.
- Wrapper
classes are used to represent primitive values when an object is
required. It converts a primitive value into an object.
- int - Integer
- double - Double
- boolean - Boolean
- char - Character
- Autoboxing is a shortcut for converting a primitive value into an object.
- How do you convert a String variable to an integer using the wrapper class?
- Integer.parseInt(string variable name)
- How do you convert a String variable to a double using the wrapper class?
- Double.parseDouble(string variable name)
- 7.1 - 7.8: Arrays & Array Lists
- Evaluate
the following statement, assuming that testArray is defined with a length of 10 and a data type of double.
- testArray[10] = 104.89;
- What are the boundaries of the array?
- The lower boundary is 0 and the upper boundary is 9.
- Is the double value being inserted within the boundaries?
- No. [10] asks that it be inserted into position 11 which is outside the boundaries.
- Will this statement compile? If no, why not?
- Yes. The statements meet the syntax requirements of the language.
- Will an error be produced at run time? if yes, what error?
- Yes - an out of bounds error will be generated.
- An error will not be produced at compilation, it is not until the code is executed or ran that an error will result.
- Describe an array verses
and ArrayList. What are the advantages and disadvantages of both?
How is a copy made of an array or ArrayList? What type of
class is an ArrayList? What methods are available with an
ArrayList?
- An array is a static collection. When it is declared the number of elements the array is able to contain is identified.
- Advantage - can access the elements directly.
- Disadvantage
- cannot increase the size, would have to create a new array and then
copy the items from the old array to the new larger array.
- An ArrayList is a dynamic collection. A size is not defined when the ArrayList is declared.
- Advantage - no limitation on size, has methods for adding/removing, will automatically shift elements when adding/removing
- Disadvantage - cannot access elements directly, always have to use a method
- Methods available:
- Write the following declaration statements:
- Declare an array of 100 Coin objects with a name of tmpCoin.
- Coin [] tmpCoin = new Coin[100];
- Declare an array of 50 int values with a name of dogs.
- int [] dogs = new int[50];
- Declare an array of 75 Rat objects with a name of compRats.
- Rat [] compRats = new Rat[75];
- Declare a 4 element integer array with a name of scores, and assign the whole numbers 1 through 4 to the array elements.
- int [] scores = {1, 2, 3, 4};
- Declare an ArrayList of data type String and a name of employees.
- ArrayList<String> employees = new ArrayList<String>();
- Declare an ArrayList of Paycheck objects with a name of annualPay.
- ArrayList<Paycheck> annualPay = new ArrayList<Paycheck>();
How do you return the length of a - How do you know how many elements within an array have been populated?
- This
cannot be determined by the length - the length only tells you how many
possible elements are in an array. To determine how many have
been populated you can:
- Use a loop to check each element of an array
- Use a nextPos type counter to keep track of what elements have been populated and where the next element can be inserted.
- 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.
- Write the code using a for loop.
- for (int i = 0; i < arrayListName.size(), i++)
- Write the code using a while loop.
- int i = 0;
- while (i < arrayListName.size())
- {
- arrayListName.add(i);
- i++;
- }
- Write the code using a do/while loop.
- int i = 0;
- {
- arrayListName.add(i);
- i++;
- }
- while (i < arrayListName.size())
What is the purpose of the enhanced for loop? - An enhanced for loop is designed to interate (loop) through each element in a collection (array or array list).
- 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.
- Declare a two-dimensional array that has 3 rows and 4 columns of integer values with a name of testScores.
- int [] [] testScores = new int [3] [4];
- Write a loop to populate each element within the array with a value of 0.
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- }
- }
|
|
|