review1_1227

advertisement
Midterm Review
Tami Meredith
Primitive Data Types
byte, short, int, long
 Values without a decimal point, ..., -1, 0, 1, 2, ...
float, double
 Values with a decimal point, 3.14, .333, 99.9, ...
char
 Alphabetic symbols, 'a', 'b', '%', ...
boolean
 Truth values: true, false
void
 The empty set, nothing, no values
Complex/Composite Types
 Arrays
 Ordered, fixed length, groups of identically typed data
 Strings
 Special type that is very like an array of char
 Classes
 User defined collections of other types and methods
 As an OOPL, Java relies heavily on the use of classes
Exercise
 Define (in Java) a variable to store the following data.
Where necessary, define any needed classes or arrays.
1) The weight of Jessica's new daughter (in kg).
2) 10 possible baby names.
3) A blanket colour, as a set of 3 integer RGB values.
4) A birth date (day, month, year).
Solution
The weight of Jessica's new daughter (in kg).
double weight;
2) 10 possible baby names.
String[] names = new String[10];
3) A blanket colour, as a set of 3 integer RGB values.
int[] colour = new int[3];
4) A birth date (day, month, year).
public class Date {
int day, month, year;
}
Date d = new Date();
1)
Variable Manipulation
 After defining variables we assign them values
 We use these values in computation to create new
values
 It is critical that we know what every variable stores at
all points in time
Exercise
1.
2.
3.
4.
5.
6.
7.
int i, j = 1, k;
double a = j;
k = squareRoot(64);
for (i = 0; i < k; i = i + 2) {
j = i * i;
System.out.println("j is " + j");
}
A.
B.
C.
D.
E.
How many times are lines 5 and 6 executed?
What are the values of i, j, k, and a after lines 1, 2, and 3.
What are their values after line 7?
What are their values at the END of each loop iteration?
What is printed out?
You may use a table if you wish.
Solution
1. int i, j = 1, k;
2. double a = j;
3. k = squareRoot(64);
4. for (i = 0; i < k; i = i + 2) {
5. j = i * i;
6. System.out.println("j is "+j);
7. }
Prints:
j is 0
j is 2
j is 4
j is 6
Line
i
j
k
a
1
?
1
?
n/a
2
?
1
?
1.0
3
?
1
8
1.0
Loop 1
0, 2 0
8
1.0
Loop 2
2, 4 4
8
1.0
Loop 3
4, 6 8
8
1.0
Loop 4
6, 8 36 8
1.0
After 7
8
1.0
36 8
Programming
 We program algorithms with only a few basic
elements:
1. Assignments and expressions – to manipulate data
and memory
2. Conditional (if) statements - to make choices
3. Loops - to repeat things
4. Blocks - to group things
5. Method calls - to use code that is in another place
We combine these things to create programs
Syntax
 if (condition) statement
 if (condition) statement1 else statement2
 do statement while (condition);
 while (condition) statement
 for (preloop ; condition ; postbody) statement
 { statement1 statement2 ... statementn }
 variable = expression;
 method(arguments);
Exercise
 An array called nums contains 20 integers
 Determine the number of positive and negative
integers in the array and store these in variables you
must define called p and n
int[] nums = new int[20];
getData(nums);
// Your code here
Solution
int[] nums = new int[20];
getData(nums);
int p = 0, n = 0, i;
for (i = 0; i < 20; i = i + 1)
{
if (nums[i] < 0)
{
n = n + 1;
}
else if (nums[i] > 0)
{
p = p + 1;
}
}
Solution (ICK!)
int[] nums = new int[20];
getData(nums);
int p,n,i;
for (p=n=i=0;i<nums.length;i++)
if (nums[i]<0) n++;
else if (nums[i]>0) p++;
Exercise
 An integer, n, is prime if it is not evenly divisible by
any of 2, 3, 4, 5, ... , n-1
 if n % m equals zero, n is evenly divisible by m
 write code to determine if an integer "n" is prime and
print either "Prime" or "Not prime" to the screen
int n = keyboard.nextInt();
// Your code here
Solution
int n = keyboard.nextInt();
int m;
boolean prime = true;
for (m=2; m < n; m++) {
if ((n % m) == 0) {
prime = false;
}
}
if (prime == true) {
System.out.println("Prime");
} else{
System.out.println("Not Prime");
}
Methods
 Methods are named blocks of code that
 Take input as parameters
 Generate a return value
Format:
public staticopt return-type name (parameters) {
statement(s)
}
An Example
public static char last (String str) {
int len = str.length();
return (str.charAt(len – 1));
}
 Method name: last
 Required Input: A string
 Return type: character
 Return value: The last character in the input string
Exercise
 Write a method that counts the number of spaces in a
string. Your method should be public and static.
Solution
 Write a method that counts the number of spaces in a string. Your
method should be public and static.
public static int spaces (String s) {
int i, num = 0;
char c;
for (i = 0; i < s.length(); i++) {
c = s.charAt(i);
if (c == ' ') {
num = num + 1;
}
}
return (num);
}
Exercise
 It is common in computing to keep track of time as
the number of seconds since midnight. Write a
method that takes an array of 3 integers (hours,
minutes, seconds) and returns it as the number of
seconds since midnight.
E.g.,
If time = {4, 5, 6 }
seconds = (4 * 60 * 60) + (5 * 60) + 6
Solutions
public static int sinceMidnight(int[] t) {
int s;
s = t[0] * 60 * 60;
s = s + (t[1] * 60);
s = s + t[2];
return (s)
}
public static int sinceMidnight (int[] t) {
return ((t[0]*3600)+(t[1]*60)+t[2]);
}
Exercise
 Write a method called equals that takes two arrays of
integers as parameters and returns true if the arrays
are the same and false otherwise.
Solution
public static boolean equals (int[] a1, int[] a2) {
if (a1.length != a2.length)
return (false);
for (int i = 0; i < a1.length; i++) {
if (a1[i] != a2[i])
return (false);
}
return (true);
}
A final exercise
Write a complete Java program that counts the number
of lines in a file. Here is an example of its use:
File? arrays.java
arrays.java: 22 lines
The Midterm
 Tracing code (fill in the table)
 Writing Variable Definitions
 Find the errors:
 syntax >> error when you "Build File"
 algorithm >> does not "work" properly
 Write code to ...
 Write a complete program to ...
The Usuals
 Nothing = Zero – don't leave questions blank, ANYTHING is








better than nothing!
There are no penalties for incorrect answers
SHOTGUN approach – write anything and everything you think
is possibly relevant
Pseudo code, point-form, flow charts ... If you don't know the
Java, answer it some other way
For T/F, Multiple-Choice, Matching, etc. GUESS if you have to –
no negative scoring!
Copy parts of the textbook that you think might work, don't
worry about perfection, just about getting a small part right
Leave lots of space, add stuff later when you think of it
Write code in pencil, bring an eraser
Answer questions in any order – do easiest stuff first
Download