CSC15 Midterm Exam Review Guide Solutions will be posted day before exam Exam format will be similar to the quizzes and pseudoquiz. Some of the questions will be very similar to those I asked you before, but there will also be a few that require you to device an original solution. I also always put one particularly challenging question on the exam. General concepts to be convered: Basics: Difference between statements and expressions Variable declarations, use of variables Use of int, double, String, boolean, AND objects as expressions if-else constructs, including nested if-else overall structure of Java programs (where main goes, etc) Type casting between double and int types - and how to round off. Functions (methods): functions that return values and functions that do not How functions are called How parameter passing works How and why variables declared inside a function are local to the function Classes and objects How objects are created and used. Declaration of variables inside classes : how and why Difference between calling a function from inside the same class and from another class Difference between private (internal) and public (external interface) components of classes The constructor method of a class Methods that take other objects as arguments. Exam will NOT cover: while loops Input/Output except System.out.println The most important things to study are: A. Lecture notes B. Sample programs and notes on the class homepage C. past programming assignments, quizzes Practice Problems: YOU SHOULD WRITE OUT ON PAPER ALL FUNCTIONS FIRST. Remember, you'll be asked to write code on paper during the exam! Do not fool yourself. Write the program by looking at your notes and other programs first. Then, try to write it again, WITHOUT looking at your notes. 1. complete the following program, following instructions in comments: Write the following simple statements a. Declare integer variables x and y, b. Assign 2*x to y b2. swap the values of x and y c. assign the character 'y' to the variable z declared above. d. Write an if-else statement that determines which one of three integer variables (x,y,z) contains the smallest value. e. Assume integer variables x, y and double variable z has been declared. You want to divide x by y, but you want the result to be a floating point (double) value, so you can assign it to z. Write one or more statements to do so. 2. In the following class does not contain any variables. Write the following functions in this class, then complete the main function in the second class below. Remember: determine what parameters and what return value these functions need to have first. Since there are no variables in the class, these functions(methods) don't share variables with any other functions (all their variables are local). class theClass { // write a function f that takes an integer x and return an integer // representing 3*x + 1. public int f(int x) ... (I won't always give you the first line) // write a function s that takes an integer x and returns no value. // the function's code should first change the value of x to x+1, then // print out the new value of x. e.g., s(3) should print 4. // Write a function that takes a double between 0 and 1 and return the // corresponding integer between 0 and 100 (think if it as a percentage // value). For example, f(0.5) should return 50, assuming f is the // name of the function. Hint: multiply by 100.0, then type cast to int. } // the class // complete the following main function: public class whatever { public static void main(String[] args) { // declare variables x, and y, and assign them 3 and 4 respectively // declare a boolean variable b; // create an instance of the class theClass so you can call functions // on it. Remember this is a two-step process. // call function s on x; -- note this is a statement, since s returns void // print out the value of x. Why did it print 3? Wasn't it changed to // 4 inside the function s?! You need to understand what's happening // here (local variables). // Print out the final values of x, y, indicating which is which. } } 3. Review the object-orientation examples. Assume you build PCs for people. Write a class to represent a PC. The class should encapsulate the followoing information (variables). String cputype - such as "pentium4" or "athlon" double ghz - e.g, 2.4 gigahertz int RAM - e.g, 512 megabytes int drivespace - e.g, 80 gigs String gpu - such as "Nvidia" or "ATI" class PC { String cputype; ... 3a. Write a constructor function for the class. By default, all PC's will have 256 megs of RAM and a 60 gig hard drive. However, the constructor should accept parameters to determine the type and speed of the CPU and the graphics card. public PC(... 4. Write "accessor methods" that return the values of each of the variables in the class. 5. Write a function called 'changegpu' that changes the graphics card. public void changegpu(String newgpu) Sometimes I'll give you the first line, like above, but sometimes I won't 6. Write a function called 'upgrade' that doubles the ammount of RAM in the PC, and triples the disk space. 7. Write a function called 'betterthan' that takes another PC object as a parameter and determines if 'this' PC is better than the other PC. One PC is better than another if it has more memory AND a faster processor. Otherwise, it's not better. Your function should return a boolean. (look in other programs (e.g., footballteam) for similar examples). 8. Write a public class with a main method that creates two PC objects and demonstrate how each of the methods can be called.