Presentation is loading. Please wait.

Presentation is loading. Please wait.

Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.

Similar presentations


Presentation on theme: "Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with."— Presentation transcript:

1 Midterm Review Tami Meredith

2 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

3 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

4 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).

5 Solution 1) 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();

6 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

7 Exercise 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. } A. How many times are lines 5 and 6 executed? B. What are the values of i, j, k, and a after lines 1, 2, and 3. C. What are their values after line 7? D. What are their values at the END of each loop iteration? E. What is printed out? You may use a table if you wish.

8 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 Lineijka 1?1?n/a 2?1?1.0 3?18 Loop 10, 2081.0 Loop 22, 4481.0 Loop 34, 6881.0 Loop 46, 83681.0 After 783681.0

9 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

10 Syntax if (condition) statement if (condition) statement 1 else statement 2 do statement while (condition); while (condition) statement for (preloop ; condition ; postbody) statement { statement 1 statement 2... statement n } variable = expression; method(arguments);

11 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

12 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; }

13 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++;

14 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

15 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"); }

16 Methods Methods are named blocks of code that Take input as parameters Generate a return value Format: public static opt return-type name (parameters) { statement(s) }

17 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

18 Exercise Write a method that counts the number of spaces in a string. Your method should be public and static.

19 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); }

20 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

21 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]); }

22 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.

23 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); }

24 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

25 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...

26 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 ppt "Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with."

Similar presentations


Ads by Google