0% found this document useful (0 votes)
223 views

Worksheet 03 - Variables & Constants

The document discusses variables, data types, and constants in Java. It defines variables as named storage locations that can be manipulated during a program's execution. It describes how to declare and initialize variables of different data types like int, double, float, etc. It also discusses default initial values for variables and constants using the final keyword. Examples are provided to declare variables, initialize them, and write simple programs to calculate perimeter, tax amount, etc. using variables and constants.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
223 views

Worksheet 03 - Variables & Constants

The document discusses variables, data types, and constants in Java. It defines variables as named storage locations that can be manipulated during a program's execution. It describes how to declare and initialize variables of different data types like int, double, float, etc. It also discusses default initial values for variables and constants using the final keyword. Examples are provided to declare variables, initialize them, and write simple programs to calculate perimeter, tax amount, etc. using variables and constants.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Purwanchal Vidyamandir

SESSION- 2021-22
STUDY MATERIAL
SUBJECT- COMPUTER
APPLICATIONS

CLASS- IX
TOPIC: VALUES AND DATA TYPES

Variables:
Variables represent named storage locations, whose values can be manipulated during program
run. For instance, to store name of a student and marks of a student during a program run, we
require named storage locations.
Variables, called as symbolic variables, serve the purpose.
Declaration of a Variable
The declaration of a variable generally takes the following form
type variablename;
where type is any Java data type and variablename is the name of the variable. A variablename
is an identifier. Thus all rules of identifier naming apply to the name of a variable. Followine
declaration creates a variable age of int type :
int age ;
Following are some more examples of variable declarations:
double pival ;
float res ;
The above declaration creates a variable namely pival of type double and a variable namely res
of type float.
All above given definitions are simple definitions. A simple definition consists of a type specifier
followed by a variable-name. When more than one identifier of a type is being defined, a
comma-separated list of identifiers may follow the type specifier. For example,
double salary, wage;
int month, day, year ;
long distance, area;
Initialization of Variables
A first value (initial value) may be specified in the definition of a variable. A variable with a
declared first value is said to be an initialised variable. Java supports two forms of variable
initialization at the time of variable definition :
int val = 1001;
Here val is initialized with a first value of 1001. (This type of initialization can take place either
inside a method or inside a class ; but in initial class the declaration should also include keyword
static e.g., static int val - 1001 ;) Following are some more examples of initialized variables :
double price = 214.70, discount = 0.12;
float x = 0.27;
long val = 25L;
Now consider the following example programs which declares variables of different datatypes
and prints their values.

Write a program to print details about you i.e., your name and age using variables.
public class First
{
void display( )
String name = "Rohan" ;
int age = 16 ;
System.out.printin ("My name is " + name) ;
System.out.printin ("My age is " + age) ;
}
Output produced is :
My name is Rohan
My age is 16

Write a program to print sum of three given numbers 1221, 2332, 3443.
public class Test {
void myMethod() (
int num1, num2, num3, sum ;
numl = 12213;
num2 = 2332;
num3 = 3443;
sum = num1 + num2 + num3 ;
System.out.printin ( "3 given numbers are : ") ;
System.out.printin ( numl );
System.out.printin ( num2 ) ;
System.out.printin ( num3 );
System.out.printin ( "Sum = " + sum );
}}
Output produced is :
3 given numbers are :
1221
2332
3443
Sum = 6996
Write a program to find the perimeter of a triangle with sides measuring 10 cm, 14 cm and
15 cm.
public class Calc
{
publlic void compute()
{
int sidel = 10, side2 = 14, side3 = 15;
int perimeter;
perimeter = sidel + side2+ side3 ;
System.out.printin("Perimeter of triangle with sides 10 cm, 14 cm and 15 cm is" +
perimeter + "cm");
}}
Output produced is :
Perimeter of triangle wi th sides 10 cm, 14 cm and 15 cm is 39 cm
Initial values of Variables
Every variable in a program must have a value before it is used in an expression
Each class variable, instance variable, or array component is initialized with a default
value when it is created:
Type Initial value
byte. 0(Zero) of byte type
short 0(Zero) of short type
int 0
tong 0L
float. 0.0f
double 0.0d
char null character i.e.'\u0000'
boolean false
All reference types null
Please note that the local variables i.e, the variables we define inside a method are not
initialised by detault. We give them initial value explicitly.

Constants
Often in a program we want to give a name to a constant value. For example, we might have a
fixed tax rate of 0.030 for goods and a tax rate of 0.020 for services. These are constants,
because their value is not going to change when the program is executed. It is convenient to
give these constants a name.
This can be done as follows:
final double TAXRATE = 0.25;
The keyword final makes a variable as constant i.e., whose value can not be changed during
program execution. Consider the following program that declares and uses two constants.
class CalculateTax {
public void Calc () NOTE
final double GOODS_TAX = 0.030;
final double SERVICE_TAX = 0.020;
…..
}}
The reserved word final tells the compiler that the value will not change. The names of
constants follow the same rules as the names for variables. (Programmers sometimes use all
capital letters for constants; but that is a matter of personal style, not part of the language).
Once declared constants, their value cannot be modified e.g. after declaring constant
GOODS_TAX, if we issue a statement like:
GOODS_TAX = 0.050; // error
it will cause an error, as the value of constants cannot be modified.
Advantages of Constants
There are two big advantages to using constants:
1. They make our program easier to read and check for correctness.
2. If a constant needs to be changed (for instance, a new tax law changes the rates) all we need
to do is change the declaration. We don't have to search through our program for every
occurrence of a specific number.

Write a program to calculate tax for a taxable income of rs.3,10,000, if tax rate is fixed at
2.2 %.
public class Calculate {
vold calcTax () {
final double taxrate = 2.2;
double income-310000, tax = 0;
tax = income * taxrate/ 100;
System.out.println ("Tax payable is Rs." + tax);
}}
Output produced is:
Tax payable is Rs. 6820.0
Remember, in Java, wherever we use multiple data types in single expression, Java will convert
the types of participant values to make them of same data type. The process of type
conversion requires us to understand expressions and before that operators. Hence, we shall
talk about Tyve Conversion in details in coming chapter where we talk about
operators, expressions and of course Type Conversion.
PURWANCHAL VIDYAMANDIR
SESSION: 2021 - 2022
CLASS: IX
SUBJECT: COMPUTER APPLICATIONS
TOPIC: VALUES AND DATA TYPES
1. Answer the following:
a) What is an identifier ? What is the identifier forming rule of Java ?
b) What are literals? How many types of integer literals are available in Java?
c) What kind of program elements are the following:
13, 'a', 4.38925, main()?
d) Which escape sequences represent the newline and null character?
e) What is meant by floating constant in Java? How many ways can a floating
constant be represented into?
2. Write true/false [give reason(s)]:
a) An identifier can start with the underscore "_" character.
b) An identifier can begin with a digit or letter.
c) All special characters are allowed in Java identifiers.
3. Solve the following programs in Java:
a) Write a program to determine the length of the wooden strip required to frame a
photograph of length and breadth 32 cm and 21 cm respectively.
b) A rectangular piece of land measures 0.7 km by 0.5 km. Each side is to be
fenced with 4 rows of wires.
Write a program to determne the length of the wire needed.
c) A student's school is at a distance of 5 km 350 m from her house. She travels 1
km 70 m on foot and the rest by bus. Write a program to determine how much
distance she travels by bus.
(Hint: Use constant, 1 km- 1000 meters)
4. Find out the errors and rewrite the code

Public class payment


{
Int pg-read;
int amt;
compute()
{
pg-read=134+217+80+145;
amt=pg-read * 60:
System.out println("/tFor"+pg-read+' pages, amount payable is Rs."+amt)
}
}

You might also like