Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CSCI 161 Introduction to Computer Science Fall 2014 Lab 7: Writing Static Methods Instructor: David Chiu Due: Beginning of Next Lab Important: In this lab, you will be writing loops. You will inevitably write some infinite loops that will cause your BlueJ environment to freeze. When this happens, hold down the ctrl key and press d . Switch partners between every problem! Instructions for Part I 2 Create a New Project named Lab7. Inside this new project, create two files: Lab7Methods.java and TestMethods.java. 2 The Lab7Methods.java file should contain only the static methods that you will be writing (no main() in this file) 2 The TestMethods.java file should contain a single static method, main(). For The code in main(), here are some ground rules: – The code in main() shall contain only test (dummy) code to validate the correctness of the methods you implement. – You are not allowed to have any loops (or any other loops for that matter), if-statements, or any other control-flow statements in main(). – main() should just contain a series of calls to your methods inside the Lab7Methods class. – Recall that to call a static method from within different class, use: ClassName.methodName(). Part I 2 Each of the following problems should be solved using at least one static method (i.e., you might write supporting methods to help solve a subproblem). 2 Problem 0: Write a method called public static int getPositiveNumber() that takes no inputs, and asks the user to enter a positive integer, N . If N is negative or zero, the method will repeatedly ask for N until N becomes positive. Then return N . This method can use System.out.print() to prompt for an answer, and Scanner methods to get the user’s input. 2 Problem 1: Write a static method, public static boolean isPrime(int num) to determine whether the given number is prime. In TestMethods.java: ask the user to enter a positive integer N , and call Lab7Methods.isPrime(N). to determine if N is prime. Wait a second, you just created a method guaranteed to get a positive integer from the user.. use it! 2 Problem 2: Write a static method, public static void printPrimes(int num) to print out the first num prime numbers. Again, didn’t you just write a method you can reuse that simplifies writing printPrimes()?. In TestMethods.java: Like the previous problem, first call public static int getPositiveNumber() to retrieve a positive number for N . Then call Lab7Methods.printPrimes(N). 2 Problem 3: Write a recursive method rPow that inputs base ≥ 0 and exp ≥ 0, and returns baseexp . You are not allowed to use Math.pow(), or any loops to solve this problem. 1 2 Problem 4: Write a method, createRandomArray(int num), that creates an array of num doubles, initializing all elements in this array to a random number between 0 and 100. you should call Math.random() to get a number in [0, 1). This random array is returned by this method. 2 Problem 5: Write another method called printArray that prints the contents of any given array of doubles. Each element in the content must be separated by a comma. There should not be a comma after the last element is printed. 2 Problem 6: Write a method called resetArray that inputs an array of doubles and resets all of its elements to 0. Recall that, unlike other input parameters, any modifications you make to an array parameter are persistent! Because of this, this method does not return any values. 2 Problem 7: A vector dot product is an algebraic operation on two equal-length vectors of numbers. The dot product of two vectors A = (a0 , a1 , a2 , ..., aN −1 ) and B = (b0 , b1 , b2 , ..., bN −1 ) is: A·B = N −1 X ai bi i=0 Notice a vector can be represented with an array. Create and initialize two arrays that contain random N numbers a piece. If the sizes of the arrays are not equal, then your method should return an undefined number. In Java, an undefined number is the constant Double.NaN (Not-a-Number). Again, to test this method, you can call createRandomArray() to generate two arrays of doubles to input into this method. Part II: Refactoring Code In CS, to refactor code means to improve it in terms of readability and/or efficiency. The linear regression homework you just completed is an excellent candidate for refactoring. Particularly, these are the obvious places where you could have extracted out a clear subroutine as a static method: 2 getData(): A method to populate both your arrays, x[..] and y[..]. 2 getMean(): A method to compute the mean given an array of doubles. 2 getSlope(): A method to compute the slope m of the regression, given x[..] and y[..]. 2 getIntercept(): A method to compute the intercept b of the regression, given x[..] and y[..]. 2 predict(): A method to make a prediction ŷ given x, m, and b. 2 getR2(): A method to compute the correlation r2 . 2 Recall that methods can call each other to simplify your code. For instance, note that you need the mean to compute the slope and the intercept. Instead of copying-and-pasting the code to compute the mean into both methods, you should be calling getMean(). 2 Name this class LinearRegressionRefactored. 2 Although the functionality of this refactored code does not change from Homework 4, the code itself should look a lot cleaner and be much more intuitive to read. 2 Part III: Submitting Your Lab After you have completed the lab, please do the following to submit your work. 2 Zip up all necessary files. 2 Rename the zip file to LastName1 LastName2 Lab7.zip, where the LastNames are the respective last names of the authors. 2 Submit it on Moodle. You may submit as often as you’d like before the deadline. I will grade the most recent copy. 3