Solutions to the Practice Questions#
Data Types#
Data types allow the correct amount of space to be allocated for the variable in RAM and allow the static environment to know which operators/methods are valid.
Many correct answers including:
String myName = "Rosanna";
Many correct answers including:
short daysFebLeapYear = 29;
Many correct answers including:
double pi = 3.1415926535897932384626433;
The value of pi is not finite, even in Mathematics, hence the value is not exact.double dBigNum = Integer.MAX_VALUE – 1;
This value will be exact sincedouble
s have about 15 significant digits. If the value was declared as afloat
, then it would not be exact.a.
banana
is 7
b.jasper
is 3.0 (Note: 3 is incorrect)int[] oddNums = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
double[] nums = new double[20];
The largest valid index is 19.String firstNameJavaInventor = "James"; String lastNameJavaInventor = "Gosling"; String nameJavaInventor = firstNameJavaInventor + " " + lastNameJavaInventor; System.out.println("Original inventor of Java: " + nameJavaInventor);
StringBuilder javaInventor = new StringBuilder("James"); javaInventor.append(" Gosling"); System.out.println("Original inventor of Java: " + javaInventor);
(theChar >= 'A' && theChar <= 'Z')
In the case of my name “ROSANNA”
(theChar == 'R' || theChar == 'O' || theChar == 'S' || theChar == 'A' || theChar == 'N')
if Statement#
a. no b. yes c. yes
d. yes e. no f. yes
g. yes h. yes i. yes
a. 1., 2. Evaluates to true, 3., 4., 5.
Output:
10
11
b. 1., 2. Evaluates to true, 3., 4., 8.
Output:
10
Memory diagrams are the same for both a) and b). Indices are not stored in memory, but are in the diagram for clarity.
for-each Loop#
1., 2., 3., 4., 5., 2., 3., 4., 5., 2., 3., 4., 5., 2., 3., 4., 5., 2., 6.
Output:20&22&30&26&DONE
n
is an array of integersvalue
is the iterator, the name used to move through the array.value
will take on each item in the array and this value is set when execution returns to the top of the loop
int[] odds11To29 = {11, 13, 15, 17, 19, 21, 23, 25, 27, 29};
int answer = 0;
for (int oddNum : odds11To29){
answer = answer + oddNum;
}
System.out.println("The sum of the odd numbers from 11 to 29 is:");
System.out.println(answer);
The sum of the odd numbers from 11 to 29 is:
200
String[] stringList = {"kiwi", "apple", "orange", "watermelon"};
for (String item : stringList){
System.out.println(item.charAt(0));
}
k
a
o
w
String[] stringList = {"kiwi", "apple", "orange", "watermelon"};
for (String item : stringList){
System.out.println(item.charAt(item.length() - 1));
}
i
e
e
n
while Loop#
1., 2. true, 3., 4., 5., 2. true, 3., 4., 5., 2. true, 3., 4., 5., 2. true, 3., 4., 5., 2. false, 6.
Output:25&12&6&3&1&DONE
n
is the control variable for the while loop.An infinite loop.
int answer = 0;
int oddNum = 11;
while (oddNum < 30){
answer = answer + oddNum;
oddNum = oddNum + 2;
}
System.out.print("The sum of the odd numbers from 11 to 29 is:");
System.out.println(answer);
The sum of the odd numbers from 11 to 29 is:200
String[] stringList = {"kiwi", "apple", "orange", "watermelon"};
int index = 0;
while (index < stringList.length){
System.out.println(stringList[index].charAt(0));
index = index + 1;
}
k
a
o
w
String[] stringList = {"kiwi", "apple", "orange", "watermelon"};
int index = 0;
while (index < stringList.length){
String item = stringList[index];
System.out.println(item.charAt(item.length() - 1));
index = index + 1;
}
i
e
e
n
Methods#
void
public static int sumOf2(int num1, int num2){
public static String embedInInts(String aString, int number){
public static double secantSlope(double x1, double y1, double x2, double y2){ return (y2 - y1) / (x2 - x1); //rise over run }
double secSlopePoints = secantSlope(4, 5, 3, 12);
Formal parameters:
x1
,y1
,x2
,y2
(from question 4 code)
Actual parameters: 4, 5, 3, 12System.out.println(secSlopePoints);
or if you want a specific number of decimal places, say 4, to print:
System.out.printf("%.4f", secSlopePoints);
/** * Prints aChar the given number of times to standard output. No * newline is added. * * @param num - the number of times to print the char * @param aChar - the char to print */ public static void printNumChars(int num, char aChar){ while (num > 0){ System.out.print(aChar); num = num - 1; } }//printNumChars
/** * Makes a String that is num copies of aChar. * * @param num - the number of copies of the character * @param aChar - the char to copy * * NOTE: if the number is less than or equal to zero, the * empty String is returned. */ public static String stringNumChar(int num, char aChar){ String answer = ""; //start empty while (num > 0){ answer = answer + aChar; num = num - 1; } return answer; }//stringNumChar
Congratulations! You are now a Java programmer!