/* CSC15 lab5: Functions Part I: For the first part of this lab and assignment you are to write a number of functions in a single class and use them in a program. Use the examples I did in class, and the following sample program, as guide: // This program illustrates different styles of functions in a class: class someFunctions // class that contains some functions { // the following function return the absolute value of integers public int abs(int x) { if (x < 0) return -1 * x; else return x; } // the following is a "boolean" function that computes exclusive or public boolean xor(boolean A, boolean B) { if (A) return !B; else return B; } // for example, xor(true,true) returns false, but (true || true) is true. // The following function computes a simple mathematical formula: (x*x)+1 public int f2(int x) { int answer; // internal variable answer = (x*x) + 1; return answer; // type of value returned must match declaration (int) // the entire body can also be replaced by return (x*x)+1; } // f2 // BE SURE YOU UNDERSTAND: f2(3) is the same as 10, so "f2(3);" doesn't //make sense as a statement - it'd be like "10;" // A function that calls another function: public int f3(int z) { int x; x = f2(z) + 1; // can also write this.f2(z) return x; } // in the above, since f2 returns an integer, f2(z) can be used in place of // any integer expression, so "return f2(z);" makes sense, just as "return 3;" // does. The value returned by f3 is one plus whatever is returned by // f2 when called withe the same parameter. } // end of someFunctions class // The "main" function is inside a special "public class". Be sure to note // the manner in which an "instance" of a class has to be created before // functions can be called on it. public class usefunctions { public static void main(String[] args) { int x; // variable to hold result returned by some functions someFunctions samples; // samples is of type someFunctions samples = new someFunctions(); // makes new object from template System.out.println(samples.abs(-4)); System.out.println(samples.xor(false,false)); x = samples.f2(3); System.out.println(x); // should print 10 System.out.println(samples.f3(3)); // should print 11 } } // public class Using the above examples as guide, you are to write a class called "someMethods" that contains the following functions: class someMethods { // ... 0. Write a function circumference, that returns the circumference of a circle given (as a parameter) the radius. The function "header" should look like public double circumference(int x, int y, double r) where x,y is the center of circle and r is the radius. Use 3.1415927 for pi. 1. Write a function called area, that returns the area of a rectangle (not one particular rectangle, any rectangle!). You'll have to determine what and how many parameters should be appropriate. 2. Write a function to convert a Celcius temperature to Fahrenheit. Furthermore, the Celcius temperature is given as a double, but the Fahrenheit value that's returned should be an integer that's rounded off. given Celcius C, Fahrenheit F = C*(9.0/5) + 32. But you'll need to figure out the rounding-off part: public int Fahrenheit(double Celcius) 3. Here's a function that computes the distance between two points (x1,y1) and (x2,y2): public double dist(int x1, int y1, int x2, int y2) { double answer; // value to be returned int dx, dy; // differences between x and y coordinates dx = (x1-x2); dy = (y1-y2); answer = Math.sqrt(dx*dx + dy*dy); return answer; } 3a. Now, use this function to determine if two circles overlap. Two circles overlap if the distance between their center points is less than the sum of the radii. Note this function should return true or false: public boolean overlap(int x1, int y1, int r1, int x2, int y2, double r2) 3b. Write a variation of the above function, call it envelop - that returns true if the circle indicated by x1,y1 and r1 is completely contained inside the second circle. You do the math this time. **Write another, public class and main method that demonstrates how each of the methods that you defined can be called. Use the example above as a guide. ---------- Part II: In part II of this assignment, you are asked to move the functions concerning circles above into a class. Create a different file for part II. Here's the skeleton of the class class circle { private int x; private int y; private int r; // radius // function to return the values of x, y and r: public int getx() { return x; } public int gety() { return y; } public int getr() { return r; } // 1. Write a function sets the current position (x,y) to the // new values passed in: public void setpos(int newx, int newy) // 2. Similarly, write a function that sets (or changes) the radius // 3. Now write a function that returns the circumference - does // it still need parameters? no - because the relevant info // is now inside the object. Look at the main function I wrote // below for hints to how to write this. // The following function compares the size of "this" circle // with that of another circle passed in as a parameter. The // size of a circle is just its radius: public boolean justasbig(circle other) { if (r == other.getr()) return true; else return false; } // 4. Write the overlap function, this time, the other circle is // passed in as a parameter: public boolean overlap(circle other) // 5. Write your envelop function similarly: } Your program above must work with the following code, which you're NOT allowed to change: */ public class lab5 // DON'T TOUCH { public static void main(String[] args) { // create two circle objects: circle mycircle = new circle(); circle yourcircle = new circle(); mycircle.setpos(200,300); mycircle.setradius(50); yourcircle.setpos(220,300); yourcircle.setradius(60); System.out.println(mycircle.circumference()); System.out.println(yourcircle.circumference()); System.out.println(mycircle.overlap(yourcircle)); System.out.println(mycircle.envelop(yourcircle)); } }