Lab Session 3: Basics of Java Programming Language (2) Objective
Lab Session 3: Basics of Java Programming Language (2) Objective
Lab Session 3: Basics of Java Programming Language (2) Objective
Objective
Pre-lab Exercise
1|Page
obj2.x = 0;
obj2.cal(2, 3);
System.out.println(obj1.x + " " + obj2.y);
}
}
2|Page
Math & Data and Time Class
Methods in Math class are used to perform basic operations such as the elementary
exponential, logarithm, square root, and trigonometric functions.
As such, the java.lang.Math class provides us access to many of these functions. The
table below lists some of the more common among these.
Method Description Arguments
Abs Returns the absolute value of the argument Double, float, int, long
Round Returns the closed int or long (as per the argument) double or float
Ceil Returns the smallest integer that is greater than or equal to Double
the argument
Floor Returns the largest integer that is less than or equal to the Double
argument
Min Returns the smallest of the two arguments Double, float, int, long
max Returns the largest of the two arguments Double, float, int, long
Java provides the Date class available in java.util package; this class encapsulates the
current date and time.
The Date class supports two constructors
Date(): this constructor initializes the object with the current date and time.
Date(long millisec): this constructor accepts an argument that equals the number
of milliseconds that have elapsed since midnight, January 1, 1970.
import java.util.Date;
public class DateDemo {
3|Page
Important Methods
boolean after(Date date) : Tests if current date is after the given date.
boolean before(Date date) : Tests if current date is before the given date.
int compareTo(Date date) : Compares current date with given date. Returns 0 if
the argument Date is equal to the Date; a value less than 0 if the Date is before
the Date argument; and a value greater than 0 if the Date is after the Date
argument.
long getTime() : Returns the number of milliseconds since January 1, 1970, 00:00:00
GMT represented by this Date object.
void setTime(long time) : Changes the current date and time to given time.
import java.util.*;
import java.text.*;
4|Page
In-lab Exercise
7. Write a Java program to accept a float value of number and return a rounded float
value.
8. Calculate the value of 2.5 raised to the power of 3, using the pow method.
9. (Displaying an integer reversed) Write the following method to display an integer in
reverse order:
public static void reverse(int number)
For example, reverse (3456) displays 6543. Write a test program that prompts the user to
enter an integer and displays its reversal.
10. Write a Java program to display the dates in the specified formats.
Sample Format:
Current date: 2021/08/03
Current date: 03-08-2021
Post-lab Exercise
11. (Math: approximating the square root) Implement the sqrt method. The square root of a
number, num, can be approximated by repeatedly performing a calculation using the
following formula:
nextGuess = (lastGuess + (num / lastGuess)) / 2
When nextGuess and lastGuess are almost identical, nextGuess is the approximated
square root.
The initial guess can be any positive value (e.g., 1). This value will be the starting value for
lastGuess. If the difference between nextGuess and lastGuess is less than a very small
number, such as 0.0001, you can claim that nextGuess is the approximated square root
of num. If not, nextGuess becomes lastGuess and the approximation process continues.
5|Page