COMPUTER APPLICATIONS Ch: 3 Values and Data...
Transcript of COMPUTER APPLICATIONS Ch: 3 Values and Data...
![Page 1: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/1.jpg)
1 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
COMPUTER APPLICATIONS
Ch: 3 Values and Data types
➢ Syllabus:
Introduction,
Tokens, Constants, Literals,
Data Types
Identifiers
Variables
Punctuators
Data Type Conversions
ACSII codes
➢ Introduction
Data types are the basis of any programming language. Without data type,
programming is not possible.
Java provide you various in-built data type. These in-built data types are
called primitive data types. You can create your own data types in Java
programs by using primitive data types.
➢ Tokens
Tokens are the various Java program elements which are identified by the
compiler. A token is the smallest element of a program that is meaningful to
the compiler. Tokens supported in Java include keywords, variables,
constants, special characters, operations etc.
Different types of tokens are:
• Reserved words or keywords
• Identifiers
![Page 2: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/2.jpg)
2 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
• Literals
• Punctuators
• Operators
Note: for more details about tokens open the following link
https://www.youtube.com/watch?v=L17kre7RVtc&feature=youtu.be
➢ Constants
Constants are the fixed values that do not change during the execution of
the program. Constants are also called literals.
Figure:
Java supports various types of constants.
Integer Constants
These are the sequence of digits. There are three types-decimal, octal,
hexadecimal.
• Decimal constants:
Example of some valid decimal constants:
120, 375, -120, 4562
Example of some invalid decimal constants:
(i) 1,20 (ii) 1 60 (iii) 145 A
No space, comma and no alphabet or special character is permitted between
digits.
![Page 3: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/3.jpg)
3 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
• Octal constants:
It contains combination of digits from 1 to 7.
Example:
0, 47, 324, 76342
• Hexadecimal constants:
It contains combination of digits from 1 to 9 and alphabets from A to F
Example:
7FA, 2DG, B5A, 89
Real Constants
These are the numbers containing fractional part.
These are also called floating point constants.
Example:
1.236 , -2.45, 39.27, -45.02
Single character Constants
A single character constant contains only single character enclosed in single
quotes.
Example:
‘A’, ‘7’, ‘P’
String Constants
A string constant is sequence of character enclosed in double quotes.
Example:
“Hello”, “Pramod Rajput”, “My favourite subject is Computer”
Special character, blank spaces, digits are allowed in string constants.
Note: for more details about literals open the following link
https://www.youtube.com/watch?v=L17kre7RVtc&feature=youtu.be
![Page 4: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/4.jpg)
4 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
➢ Literals
Literals is a programming language term, which means that what you type is
what you get.
If you type 4 you will get a 4.
Some special cases of literals are:
Numbers, characters, strings and Boolean literals.
Number Literals
There are several integer literals. For example, 4 is int literal.
You can force a smaller number to long by appending the letter l or L to that
number. For example, 5L is a long integer.
Floating point literals for example 3.14114
You can force a number to the type float by appending the letter f or F to
that number. For example, 5.43F.
Boolean Literals
Boolean literals consist of the keywords, true and false. These keywords can
be used anywhere you need a test.
Character Literals
Character literals are expressed by a single character surrounded by single
quotes.
Example:
‘P’, ‘5’, ‘#’
String Literals
A combination of character is string. String literals consist of a sequence of
characters inside double quotes.
Example:
“My name is Pramod”, “2020” etc.
“” is an empty string.
➢ Data types
A programmer while writing programs has to deal with various type of data.
![Page 5: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/5.jpg)
5 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
Primitive or Intrinsic Data types
Primitive data types are in-built data types. Java support eight types of
primitive data types.
These are byte, short, int, long, float, double, char and boolean
![Page 6: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/6.jpg)
6 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
• Integer
Integer are whole numbers such as 5, 7, -12,56, -9 etc. No fractional part is
used in integer.
(i) Byte
A byte is a primitive data type that uses eight bits to represents a
number. It is ranging from number -128 to 127
(ii) Integers
Integers are those positive or negative whole numbers whose value
varies from -2147483648 to 2147483647.
The type int is a primitive data type that uses four bytes to represent
whole numbers.
(iii) Short
Short integers are those positive or negative whole numbers whose
value varies from -32768 to 32767.
The type short is a primitive data type that uses two bytes to
represent whole numbers.
(iv) Long
The type long is a primitive data type that uses eight bytes to
represent whole numbers.
A bit is a binary digit that can have value of one or zero. A byte is
group of eight bits.
• Floating Point Constants
Float data type:
Fractional number is floating point number.
![Page 7: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/7.jpg)
7 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
Example:
5.0, 12.45, -24.654, 0.321 etc.
We can write floating point number in the following format also:
Standard format:
91360000 ------→ 9.136 x 107
Exponent format:
9.136 x 107 ------→ 9.136E7
In this exponent from left part of E is called mantissa i.e. 9.136
In this exponent from right part of E is called exponent i.e. 7
Double:
Type double is primitive data type.
The type float provides 7 digits of accuracy whereas double provides 15
significant digits.
Numeric overflow
By applying arithmetic operator to value of a given data type, one could
obtain a result that falls outside of data type range values and hence cannot
be represents with same primitive data type. Such a situation is called
overflow.
Example:
byte a=125;
byte b=a+3; // numeric overflow
Data type range value of byte is -128 to 127, by adding 3 into byte data
type value i.e. 125, it is now 128, so it is overflow situation.
![Page 8: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/8.jpg)
8 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
• Character Types
The type char is primitive data type that uses two bytes to store character.
Character constant varies between 0 to 255.
Each character is assigned a particular value according to ASCII (American
Standard Code for Information Interchange) codes.
Special Printing Characters
Character can be written by writing the character in single quotes. But some
characters are written in java program by using escape (\).
Escape character changes the meaning of character which follow its.
• String
String is a series of characters. These characters may be alphabets, digits,
blank spaces etc.
The string is enclosed within double quotes.
Example:
“My name is Pramod Rajput”, “Surat”, “Blue” etc.
![Page 9: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/9.jpg)
9 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
• Boolean Data
The type boolean is used to test a particular condition during the execution
of the program. It works on true or false values. All comparison operators
return boolean values.
Note: for more details about data types open the following link
https://www.youtube.com/watch?v=-ABEZ1GTAOM&feature=youtu.be
➢ Identifiers
An identifier is a name given to a package, class, interface, method or
variable. An identifier is a sequence of one or more characters.
Naming rules for identifiers:
• Identifier may consist one or more than one letter, number and
underscore.
• The first character of the identifier must be a letter of the alphabet
(upper or lowercase) or an underscore ('_').
• Identifier names are case-sensitive.
• Try to use meaningful name.
• They must not be a keyword
• No blank spaces are allowed in an identifier name.
• Use of special character is not allowed.
• An Identifier can be any length.
➢ What is a Variable?
A variable is quantity which varies during execution of program. A variable is
the name of location where information is stored. Declaration of variable is
essential before using it in java program.
Syntax:
datatype variableName;
Example:
int value;
double salary;
int age;
![Page 10: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/10.jpg)
10 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
Assign value to a variable
The value to a variable can be assigned using = sign.
Syntax:
datatype variableName;
variableName = value;
Example:
int value; // two lines initialization
value=19;
datatype variableName=value;
Example:
double salary= 45000; //single line initialization
Default values are assigned to variables are as follows:
Initialize constant
A variable store a value that can change as a program executes, while a
constant store a value that cannot be changed.
Syntax:
final datatype variableName=value;
final int sales=17;
![Page 11: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/11.jpg)
11 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
Declaring multiple variables
You can declare multiple variables in java.
Example:
int a, b, c;
Each variable is separated by comma.
Note: for more details about identifiers open the following link
https://www.youtube.com/watch?v=L17kre7RVtc&feature=youtu.be
➢ Java Reserved Words
Java reserve words are those words which cannot not be used as variable
name.
Figure:
![Page 12: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/12.jpg)
12 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
➢ Punctuators
A punctuator is a token that has syntactic and semantic meaning to a
compiler but the exact meaning depending upon the context.
Punctuators also known as separators.
There are nine punctuators in java
Punctuators that terminates a statement in java is called statement
terminator. Semicolon (;) is a statement terminator in java.
Note: for more details about punctuators open the following link
https://www.youtube.com/watch?v=L17kre7RVtc&feature=youtu.be
➢ Data Type Conversions
When you assign a value of one data type to another, the two type might
not compatible to each other. If the data types are compatible, then java
will perform the conversions automatically known as Automatic type
conversions and if not then they need to be converted explicitly.
Widening or Automatic or Implicit Type Conversions
Implicit conversion take palace when two data types are automatically
converted.
Byte --> short --> int --> long --> float --> double
For example
int a=100;
long b=a; // implicit conversion
Narrowing or Explicit Type Conversions
If we want to assign a value of larger data type to a smaller data type we
perform explicit type conversion.
double --> float --> long --> int --> short --> byte
char and number are not compatible with each other.
![Page 13: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/13.jpg)
13 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
double d= 100.24;
long a= (long) d; // explicit conversion
Note: for more details about data type conversions open the following link
https://www.youtube.com/watch?v=w2dKvQpm67E&feature=youtu.be
➢ ASCII Codes
There are 128 values in ASCII codes.
Note: for more details about ASCII code open the following link
https://www.youtube.com/watch?v=NEdrPb4D3Bg&feature=youtu.be
*********
➢ Java programs with Input method
1. Write a java program to calculate the area and circumference
of circle using input method Area= πr2 and Circumference= 2πr.
import java.io.*;
class pr1
{
public static void main(String args[]) throws IOException
{
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(isr);
System.out.println("Enter the radius of circle");
String s1= br.readLine();
double r= Double.parseDouble(s1);
double area, cf;
![Page 14: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/14.jpg)
14 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
area=3.14*r*r;
cf=2*3.14*r;
System.out.println("The area of circle is="+area);
System.out.println("The circumference of circle is="+cf);
}
}
2. Write a java programs to find out area of triangle using input
method.
area of triangle=1/2*base*height
import java.io.*;
class pr2
{
public static void main(String args[]) throws IOException
{
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(isr);
System.out.println("Enter the base of triangle");
String s1= br.readLine();
double b= Double.parseDouble(s1);
System.out.println("Enter the height of triangle");
String s2= br.readLine();
double h= Double.parseDouble(s2);
double area;
area=1.0/2*b*h;
System.out.println("The area of triangle is="+area);
}
}
![Page 15: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/15.jpg)
15 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
3. Write a java programs to find out volume of cuboid using input
method.
Volume of cuboid=l*b*h
import java.io.*;
class pr3
{
public static void main(String args[]) throws IOException
{
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(isr);
System.out.println("Enter the length of cuboid");
String s1= br.readLine();
double l= Double.parseDouble(s1);
System.out.println("Enter the breadth of cuboid");
String s2= br.readLine();
double b= Double.parseDouble(s2);
System.out.println("Enter the height of cuboid");
String s3= br.readLine();
double h= Double.parseDouble(s3);
double volume;
volume=l*b*h;
System.out.println("The volume of cuboid is="+volume);
}
}
![Page 16: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/16.jpg)
16 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
4. Write java program for swapping of two numbers using third
(temporary) variable with input method.
Note: if inputs are a=2 and b=5
Then output will be a=5 and b=2
Swapping means interchanging the values of variables
import java.io.*;
class pr4
{
public static void main(String args[]) throws IOException
{
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(isr);
System.out.println("Enter the first number");
String s1= br.readLine();
int a= Integer.parseInt(s1);
System.out.println("Enter the second number");
String s2= br.readLine();
int b= Integer.parseInt(s2);
int c;
c=a;
a=b;
b=c;
System.out.println("Number after swapping are");
System.out.println("a="+a);
System.out.println("b="+b);
}
}
![Page 17: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/17.jpg)
17 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
5. Write java program for swapping of two numbers without
using third variable with input method.
import java.io.*;
class pr5
{
public static void main(String args[]) throws IOException
{
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(isr);
System.out.println("Enter the first number");
String s1= br.readLine();
int a= Integer.parseInt(s1);
System.out.println("Enter the second number");
String s2= br.readLine();
int b= Integer.parseInt(s2);
b=b-a;
a=a+b;
b=a-b;
System.out.println("Number after swapping are");
System.out.println("a="+a);
System.out.println("b="+b);
}
}
*********
Worksheet 1
1. What is token?
2. Give the examples of Hexadecimal constant?
3. How many bytes memory is occupied by int and long datatype?
4. How many bytes memory is occupied by double and char datatype?
5. What is the range of short datatype?
6. What is the range of int datatype?
![Page 18: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/18.jpg)
18 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
7. What is numeric overflow?
8. What is the syntax for variable declaration?
9. What is the default value of char datatype?
10. What is full form of ASCII
Solution of worksheet 1
1. Tokens are the various java program elements which are identified by
the compiler.
2. 7FA, A4, 3F
3. int 4 bytes and long 8 bytes
4. double 8 bytes and char 2 bytes
5. -32768 to 32767
6. -231 to 231-1
7. By applying arithmetic operator to value of a given data type, one
could obtain a result that falls outside of data type range values and
hence cannot be represents with same primitive data type. Such a
situation is called overflow.
Example:
byte a=125;
byte b=a+3; // numeric overflow
8. datatype variableName;
Ex: int a;
9. \u0000
10. American Standard Code for Information Interchange.
*********
Worksheet 2
1. Write a java programs to find out cube of a number using input
method.
2. Write a java programs to find out area of rectangle using input
method.
area of rectangle=length*breadth
3. Write a java programs to find out volume of sphere using input
method.
Volume of sphere= 4/3*πr3
![Page 19: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/19.jpg)
19 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
Solution of worksheet 2
1.
import java.io.*;
class pramodrajput
{
public static void main(String args[]) throws IOException
{
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(isr);
System.out.println("Enter the number");
String s1= br.readLine();
int n= Integer.parseInt(s1);
int cube=n*n*n;
System.out.println("Cube of number is="+cube);
}
}
2.
import java.io.*;
class pramodrajput
{
public static void main(String args[]) throws IOException
{
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(isr);
System.out.println("Enter the breadth of rectangle");
String s1= br.readLine();
double b= Double.parseDouble(s1);
System.out.println("Enter the length of rectangle");
String s2= br.readLine();
![Page 20: COMPUTER APPLICATIONS Ch: 3 Values and Data typesmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/Ch-3-IX... · Data types are the basis of any programming language. Without](https://reader034.fdocuments.in/reader034/viewer/2022042221/5ec75a761ef01d61b25384fa/html5/thumbnails/20.jpg)
20 | P a g e I X - C o m p u t e r A p p l i c a t i o n s
double l= Double.parseDouble(s2);
double area;
area=l*b;
System.out.println("The area of rectangle is="+area);
}
}
3.
import java.io.*;
class pramodrajput
{
public static void main(String args[]) throws IOException
{
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(isr);
System.out.println("Enter the radius of sphere");
String s1= br.readLine();
double r= Double.parseDouble(s1);
double volume;
volume=4.0/3*3.14*r*r*r;
System.out.println("The volume of sphere is="+volume);
}
}
**********