Page 124 - CA_Blue( J )_Class9
P. 124
Program 2 Write a program to import a package and use its class.
1 import java.util.*;
2 public class Cal {
3 public static void main()
4 {
5 Calendar cr = Calendar.getInstance();
6 System.out.println("Today is: " + cr.getTime());
7 }
8 }
When you execute the preceding program, the current date of the computer will be returned, as shown:
Today is: Thu Nov 07 12:29:31 IST 2024
6.2.2 Advantages of Packages
Advantages of using packages are:
• Packages allow us to create multiple classes with the same name in different packages.
• Packages increase the reusability of code as a class in one package can be used in another package.
• Packages make the management of classes easy.
The four basic steps in working with a computer are Input, Store, Process, and Output. The "Input" step is used
to accept data from the user. There are four main ways to input data in Java: through initialisation, by passing
parameters, using the Scanner class, and through the InputStreamReader class.
Let us discuss these in detail.
6.3 INITIALISATION
We can assign an initial value to a variable before using the variable in the program. This is required as some
programs require one or more fixed values for execution. This is also known as direct input or hard-coded input.
There are two ways to initialise a variable:
1. The value is assigned at the time of declaration.
Example: if we want to take 6 in length and 2 in breadth, then we can write:
int length=6, breadth=2;
2. The value is assigned after declaring the variable.
Example: Using the same variables as above:
int length, breadth;
length=6;
breadth=2;
122 Touchpad Computer Applications-IX

