Storing and Operating On Data
Storing and Operating On Data
Wonders
withIT N
Write a program to take user input and store it. Variables and Data Types
Use Types of Operators
operators to calculate simple interest
VARIABLES AND DATA TYPES scan
to know
Java provides a number of pre-defined classes that perform specific operations. All
these classes are grouped under different libraries. To use the pre-defined classes in
your program, you must include the libraries to which those classes belong. Libraries
can be imported within a program using the import keyword. The import statement
must appear at the beginning of a program.
2. Create the basic structure of the program. To do this, type the code as shown below:
import java.util.Scanner;
public class simple_Interest
si =(p rt)/100
amt si + p
For performing these calculations, you require the following values from the user:
int pi
float rot; (rota ot h5ud)
int t
float si;
float amt;
o
Operating
Storing and
48
g
a a varia
variable, it should be first declared. For example:
To u s e
i n t t ; /*This statement is used to declare the variable i.e. specifying
variable name and data
type*/ n Cotswar0% Sn
t-0 T h i s statement is used to initialise the variable i . e . allocating
value to the vari able*/
int t-0
For declaring a variable, the computer memory needs to know the name of the variable and the type
ofbedata or
stored
intormaton
in
the variable
variable. Java has
a
willstore. A data type specifies the type and size of data that can
some pre-defined
data types, which are called primitive data
types
Some of the basic primitive data types in Java are:
int: This data type is used to define variables that store integer values, for example, 3, 4, 5, etc.
p and t in the task above are of int data type as they are used to store integer values.
.float:This data type is used to define variables that store floating point numbers i.e. they can
take decimal values, for example, 2.43, 7.56, etc.rot, si and amt are the float data types in this
program as all these variables can include decimal values.
double: This data type is used to define variables that store BIG floating point numbers, for
example, 85342.4433.
char: This data type stores single character
value, for example, a, b, C, D, etc.
boolean: This data type is used to store either true or false.
float amt;
Scanner input =new Scanner (System. in);
Here, object of the Scanner class 'input' is created. This object is used to take input from the
users for the values of the principal, rate of interest and time variables.
S. statement to display the message, followed by the statement
Add the System.out.printin with
nextintmethod ofthe Scanner class to obtain the principal amount from the user. This value ic
stored in thevariablepas shown below:
nextint() is a built-in method of the scanner class. This is used to take the integer data
type input from the user. When the program executes, nextint method is use
capture user input.
IT Nugget
You can print the output using System.out.print also. The difference between print
and println is that println moves the cursor to the next line after the output, while
print keeps the cursor on the same line.
6. Add System.out.println statements followed by the scanner object statements to obtain the rate
of interest and time from the user, and store them in
variables rot and t, respectively as shown
below:
p-input.nextInt ();
System. out .println ("Enter the Rate of Interest : ") ;
rot=input.nextFloat () ;
System. out .print ln ("Enter the Number of years :") ;
t-input. next Int ();
Here, for the rot variable, the nextFloat method of the Scanner class is used, since rot is of the
float data type.
7. Save your program as Simple_Interest.java by clicking the File menu and then clicking Save.
scan to know
more
Or TYPES OF OPERATORRS
Note, the values have been captured and stored in variables. The next step is to
perform the calculations for simple interest and store the resultant value.
2.Add thefollowing line of code to calculate simple interest and store the resultant value in
variable si:
Datu
on
Note that has been used in place of x in the formula,* is an arith metic operator used in Java
programs.
3. Add the following line of code to calculate the amount and store the resultant value in variable
amt:
IT Nugget
Some other operators used in Java are:
Greater than or
the variable.
to
equal
Figure 2: Relational Operators
import java.util.Scanner ;
public class Simple_Interest
Stc
on Da
52 Storing and
Operating
int p
float rot;
int t
float si;
float amt;
6. Compile and execute the program. To do this, write the following code on commant prompt.
javac Simple_Interest . java
java Simple_Interest
The output shows the simple interest and the total amount for the values entered.
Enter the Principal Amount:
100
Enter the Rate of Interest :
20.5
Enter the Number of years :
IT Nugget
The command-line argument is another method of taking input from the user. Using this
method the argument i.e. input value is passed at the time of executing the java program.
Once the program has been compiled sucessfully, the argument is passed at the
time of execution. For example, to pass the three values in the above program using
command as:
Command line argument, enter the
javac simple_Interest
20.5 5
ava Simple_Interest 100
using string array of main method.
nese values are stored in args[land processed
NOTEPAD
Skill Achieved
Concept Learned
Declaring variables Variables are the placeholders for
Initialising Variables storing data for future use in a
Taking user input and storing it program.
Data input from the user needs
ldentifying operators to bestored for future use in the
Using arithmetic operators in a program.
programn
A data type should be specified
at the time of declaration of a
variable.
Operators are used to perform
operations on the variables.
Mathematical calculations can be
performed using various arithmetic
operators.
to know
Scan
more
WORKOUT
C. 23.3 d. interest
54 Storing and Operating on D