Sample Code
public class Meth { final double PI = 3.1415; Meth() { int r = 4; System.out.println("The Area of a circle radius " + r + " is " + area(r)); System.out.println("The Circumference of a circle radius " + r + " is " + circumference(r)); } double area(int r) { return PI*r*r; } double circumference(int r) { return 2*PI*r; } public static void main (String args []) { new Meth(); } }
Exercises
- Write a method that accepts the length and width of a rectangle and returns the perimeter of the rectangle
- Write a method that accepts the base and height of a triangle and returns the area of the triangle
- Write a method that accepts three integers as paramaters and returns the average of the integers.
- Write a method that accepts an integer array as a parameter and returns the average of the values of that array.
- Write a method that accepts an integer array as a parameter and returns the minium value in that array
- Write a method that returns the hypotenuse of a triangle when the other two sides are int a and int b. (Remember: hypotenuse squared equals a squared plus b squared)
- The scalar product of u=(u1,u2,u3) and v=(v1,v2,v3) is defined to be u1v1+u2v2+u3v3. Write a method that accepts two int arrays as parameters and returns an int representing the scalar product of those two arrays
- If A = (a1,a2, …an) and B = (b1,b2, …bn) then the vector sum of the two arrays A + B = (a1+b1, a2+b2, … , an+bn). Write a method that accepts two arrays as parameters and returns an array representing the vector sum of those two arrays.
- The Euclidean distance between two points A = (a1,a2, …an) and B = (b1,b2, …bn) is defined as sqrt((a1-b1)2 + (a2-b2)2 +… + (an-bn)2). Write a method that accepts two int arrays representing A and B as parameters and returns a double representing the Euclidean distance between them.