King Fahd University of Petroleum & Minerals ICS102: Introduction to Computing
Information and Computer Science Department Spring 2009 (Term 082)
Lab11: Static Methods & Variables
Objectives
Designing and implementing Java programs that deal with: (* denotes an important point)
1. * Static methods
a. Invoking non-static method within a static method
b. Math class methods: pow, abs, min, max, round, ceil, floor, and sqrt
2. * Static variables
a. Default initialization
b. Math class constants (static & final): E, and PI
3. Wrapper Classes … class types (e.g., Double) corresponds to primitive types (e.g.,
double)
a. Boxing … primitive type value primitive class object (e.g., double Double)
b. Unboxing … primitive class object primitive type value (e.g., Double double)
c. NO no-argument constructor (e.g., Double num1 = new Double() does NOT exit)
Summary
User Defined Method Syntax
[modifiers] return_type method_name ([parameter_list])[throws_clause]
{
[statement_list]
}
KFUPM, ICS Department 2/4 Spring 2009 (Term 082)
ICS102: Introduction to Computing Lab08: Static Methods and Variables
Static Methods
public static double square (double a) {
return (a * a);
}
The exit method of the System class:
System.exit(0);
The max method of the Math class:
Math.max(-9,12);
Static Variables
private static int round = 0;
The PI constant (static & final) of the Math class:
Math.PI;
KFUPM, ICS Department 3/4 Spring 2009 (Term 082)
ICS102: Introduction to Computing Lab08: Static Methods and Variables
Exercises
Exercise 1: (Geometry.java)
Write the following static methods that compute the volume and surface of a sphere with radius r; a
cylinder with circular base with radius r and height h; and a cone with circular base with radius r and
height h. Place these methods in appropriate class. Define (PI) as a static variable equals to 3.14
then use it in the methods.
public static double sphereVolume(double r)
public static double sphereSurface(double r)
public static double cylinderVolume(double r, double h)
public static double cylinderSurface(double r, double h)
public static double coneVolume(double r, double h)
public static double coneSurface(double r, double h)
Now, prompt the user for the values of r and h, call the 6 methods, and print the result.
Exercise 2: (Converter.java)
Write the implementation of the following 2 static methods:
public static int fromDecimalToBinary(int number)
public static int fromBinaryToDecimal(int number)
The method fromDecimalToBinary should convert the passed number from a decimal system to a
binary system. Conversion to binary system is achieved by iterative division by 2 and getting the
remainder of each iteration. For example, 1010 (pronounce 10 in decimal) = 10102 (pronounce 1010
in binary) and 1610 = 100002.
The method fromBinaryToDecimal should convert an input number from binary system to decimal
system. Converting to decimal system achieved by multiplying each binary digit with its
corresponding weight, and then adding the produced partial sums. For example, 10102 = 1010 and
100002 = 1610.
Now, test the above 2 methods by converting 15510, 60010, and 99910 from the decimal system to the
binary system and 1100112, 1110002, 101010102 from the binary system to the decimal system.
Then, print the output of the conversion to the screen.
Exercise 3: (Leap.java)
Write a predicate method:
public static Boolean isLeapYear(int year)
KFUPM, ICS Department 4/4 Spring 2009 (Term 082)
ICS102: Introduction to Computing Lab08: Static Methods and Variables
That tests whether a year is a leap year, that is, a year with 366 days. Leap years are necessary to
keep the calendar synchronized with the sun, because the earth revolves around the sun once every
365.25 days. Actually, that figure is not entirely precise, and for all dates after 1582 the Gregorian
correction applies.
Usually years that are divisible by 4 are leap years, for example 1996. However, years that are
divisible by 100 (for example, 1900) are not leap years, but years that are divisible by 400 are leap
years (for example, 2000).
Now, test the method with a set of years taken from the user until a negative number is entered.
Exercise 4: (Sum.java)
Write the following 2 static methods:
public static int ComputeOddSum(int input)
public static int ComputeEvenSum(int input)
The method ComputeOddSum find the sum of all odd numbers less than input.
The method ComputeEvenSum find the sum of all even numbers less than input.
Now, test these 2 methods by prompting the user to input a number each time until a negative
number is entered.