Worksheet 4
For each of the problems write a Java code to perform the required task.
Course Title: Object Oriented Programming using Java
Lab: Method
1. Write a java method named cube() that accepts an integer and returns the cube the
number.
2. Write a method named triangle() that computes the area of a triangle using its two formal
parameters h and w, where h is the height and w is the length of the bases of the triangle.
3. Write a method named fibonacci() that accepts an integer number from the user and
prints Fibonacci series of that number.
4. Write a method named sum() that accepts a number and computes the sum of the
numbers between 1 and the number.
5. Write a method named factorial() that accepts number n from key board and computes n!
(Factorial of n). Use this method in another method called facto() that computes the
following mathematical computation. 1 + 1/1! +1/2! + 1/3! + 1/4! + …. 1/n!
6. Write a Java class named Shape that overloads the method area() to calculate and print
the area of a circle, triangle and rectangle by accepting the necessary parameters from
keyboard. (For circle radius should be double, for triangle base and height should be float
and for rectangle width and height should be integer)
7. Write a method called isPrime() that accepts a number and checks whether the number is
prime of not.
Note: you are expected to declare a method, method definition, and call the method in main
method for all questions.