0% found this document useful (0 votes)
7 views8 pages

Storing and Operating On Data

Uploaded by

Soumya R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Storing and Operating On Data

Uploaded by

Soumya R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Storing and Operating on Data

Wonders

withIT N

Vicky: Monika! I want to submit an article


with IT section of the school
on
'Programming with Java' for the Wonders
creating a program
magazine. In support of my article, I am considering
Monika: why don't you make
learnt in Maths
a
program for calculating simple interest which
recently? we have
Vicky: You are
right, Monika. It will be interesting for everyone to know how
programming can be related to Math's calculations.
To create an application program to calculate simple In This Chapter
interest using Java, you need to:

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

Calculation of simple interest requires different values. These values can


more
User vary as per
requirement, thus you need to first write a program to take the input from the
user and store these
input values to perform calculations on them.
1. Import the Scanner class to capture user input. To do this type the following code:
import java.util.Scanner ;

Storing and Operating on Data


47
Here, the import java.util.Scanner statement is used to perform input functions. The gure
Scanner
class is a pre-defined Java class, which contains
objects that help to take the input from the
Scanner class belongs to the util library. user
IT Nugget

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

public static void main (String args [] )

As we know, the formula to calculate simple interest is:

si =(p rt)/100
amt si + p

For performing these calculations, you require the following values from the user:

Principal, rate of interest and time.


the
You need to store these values to be used in the future for calculation. You also need to store
resultant value of calculation. For this, you need to declare variables. A variable is a named storage

location in memory that can store any value.

3. Declare these variables in the main function as shown below:

public static void main (String args [] )

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*/

Both these statements can be combined together.

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.

Are You Aware?


When the variable is initialised with float data type within the source
code, the
value should end with for F. For example, float interest=10.3F;

Seonhah Scoun YRn (gsem,


aloa eec
4. Write the following statement within the main method body to take the input from the
user:

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:

Storing and Operating on Data


49
Scanner input=new Scanner (System. in)
System.out.println (" Enter the Principal Amount: ");
p-input.nextInt ()

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.

Resource: The sample code is available in the Simple_Interest,java file.

1. Ensure the Simple_Interest.java file is open in notepad.

2.Add thefollowing line of code to calculate simple interest and store the resultant value in
variable si:

Datu

on

Storing and Operating


t-input.nextInt ();
si- (p*rot *t) /100; /*calculat ion of simple interest*/

Note that has been used in place of x in the formula,* is an arith metic operator used in Java
programs.

Anoperator is a symbol that tells the compiler to perform the


specific logical or mathematical operation. Using operators, you Operator Description
can perform operations on variables. addition

Arithmetic operators perform mathematical calculations. subtraction


Some of the arithmetic operators used by Java are as follows: multiplication
The modulo modulus operator is used to determine
or
the division
remainder when dividing two numbers.
% modulo
For example, b = 10 % 3.
Figure 1: Arithmetic operators
In this statement, the variable, b, contains the value 1. This is because dividing 10 by 3 gives the
quotient, 3, and the remainder 1.

3. Add the following line of code to calculate the amount and store the resultant value in variable
amt:

(p*rot *t) /100; /*calcul at ion of simple


si=
interest* /
amt=si+p;/*calculation of total amount */

IT Nugget
Some other operators used in Java are:

Relational Operators: The relational


Operator Description
operators compare two or more
Equal to
expressions.
(Figure 2). They give the result as either Not equal to
true or false. Less than
Assignment Operator: The assignment Greater than
operator (=) assigns a value to a variable.
Less than
It is used to modify the current value of equal to
or

Greater than or
the variable.
to
equal
Figure 2: Relational Operators

Storing and Operating on Data


51
Logical Operators: The logical
operators (&&)
expressions to obtain a single relational and (||) are used when
gi
which is used to comparing two
result. The logical operator
(!) corresponds to
inverse the resultant value. NOT
and yields true if (&&) corresponds to the
both the
expressions logical AND
and yields
true if any of the
are true. (I) corresponds to the logical ORoperation
expressions is true. operation
Increment and Decrement
work as shortcuts Operators: There are two special operators that
to
quickly increase or decrease the values
The increment of a variable.
The
operator++ is used to increase the value of a variable
These
decrement operator- is used to decrease the byy1.
value of a variable by 1.
operators can be used in
combination with an assignment
operator.
4. Write the
following statements to display the calculated
simple interest and total amount:
amt si+p;/* calculation of
total amount*/
System.out.println ("\n Simple Interest is: +si);
System.out.println("\n Total Amount is: "+amt);

Notice, When displaying the text


the variable value, + message it is encased in double
sign is used. quotes. Whereas, when displaying
When foperator is used with
two numeric
type operands, it works as arithmetic operator.
example: an For
int val =7+8 ;/*Here value of variable val is
15*/
int x,Y, val ; x=7; y=8 ; val=x+Y ; /*Here value of variable val is 15*/
When +
operator is used with either of the two operands not being a numeric operator, it works as
a concatenation operator. As concatenation operator, it appends the value of
value of first second operand to the
operand. For example:
int x=7;

System.out.println ("Hello " +x) ;/* This displays Hello 7"*/


System.out.println(x+" Hello") ; /* This displays 7
Hello" */
Here, the operator fetches the value of variable and appends it
+
to the other operand. Remember,it
does not add rather appends the value.

5. Save your program as


Simple_Interest.java by clicking the File tab and then clicking Save.
The final program is given below:

import java.util.Scanner ;
public class Simple_Interest

public static void main (String args [] )

Stc
on Da
52 Storing and
Operating
int p
float rot;
int t
float si;
float amt;

Scanner input=new Scanner (System. in)


System.out.println (" Enter the Principal Amount: "
p-input.next Int );
System.out.println ("Enter the Rate of Interest: ") i
rot=input.nextPloat ();
System.out.println ("Enter the Number of years: ") ;
t-input.next Int ()
si- (p*rot *t) /100; /*calculation
of simple interest */
amt-si+P:*calculation of total amount*/
System.out.println ("\n Simple Interest is: "+S1) ;
System.out.println ("\n Total Amount is: "+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 :

Simple Interest is: 102.5

Total Amount is: 202.5

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

Storing and Operating on Data 53


TECH TERMS
Variables: Variables are named storage location in memory that can store
any value.
Data Type: Data type specifies the type and size of data that is stored in a variable.
Operator: Operator is a symbol that tells the compiler to
perform the specific logical or
mathematical operation.
Arithmetic Operators: Arithmetic
operators perform mathematical calculations.
Assignment Operator: Assignment operator assigns a value to a variable.
Relational Operators: Relational
operators compare two or more expressions.
Logical Operators: Logical operators are 1, && and ||. They mean
NOT, AND and OR,
respectively.

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

Multiple Choice Questions


1. Which one of the following options is an example of char data type?
a. 2

C. 23.3 d. interest
54 Storing and Operating on D

You might also like