/* CSC15 lab5: Sample Solutions */ 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(double r) { return 2*r*3.14; } /* 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. */ public int area(int width, int height) { return width*height; } /* 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) { int answer; double temp; // temporary fahrenheit temperature temp = Celcius*(9.0/5) + 32; answer = (int) (temp + 0.5); return answer; // or: // return (int) (Celcius*(9.0/5) + 32.5); } /* 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, int r2) { if (dist(x1,y1,x2,y2) <= r1+r2) return true; else return false; } /* 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. */ public boolean envelop(int x1, int y1, int r1, int x2, int y2, int r2) { return dist(x1,y1,x2,y2) + r1 < r2; } } // end of someMethods class /* **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. public class lab5 { public static void main(String[] args) { someMethods M = new someMethods(); System.out.println( M.circumference(3.4) ); System.out.println( M.area(3,4) ); System.out.println( M.Fahrenheit(20.5) ); System.out.println( M.overlap(1,2,3,4,5,6) ); System.out.println( M.envelop(6,5,4,3,2,1) ); } } */ /* Part II: In part II of this assignment, you are asked to move the functions concerning circles above into a class. For this I also did the following: I created an instance of the someMethods class above inside the circle class, so I can call the dist method without rewriting it: */ class circle { someMethods M = new someMethods(); 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) { x = newx; y = newy; } // 2. Similarly, write a function that sets (or changes) the radius public void setradius(int newr) { r = newr; } // 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. public double circumference() { return 2*r*3.14; } // 4. Write the overlap function, this time, the other circle is // passed in as a parameter: public boolean overlap(circle other) { int x2, y2, r2; // properties of "other" circle x2 = other.getx(); y2 = other.gety(); r2 = other.getr(); return M.dist(x,y,x2,y2) < r + r2; } // 5. Write your envelop function similarly: public boolean envelop(circle other) { return M.dist(x,y,other.getx(),other.gety()) + r < other.getr(); } } /* 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)); } }