Monday, April 30, 2018

Class X (ICSE) - Another example for explicit conversion & important tip


CLASS - X (ICSE)

COMPUTER  APPLICATION



Write a program to calculate a gross salary if basic salary is given of an employee. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary.

class GrossSalary
{
      public static void main(String[] args) {
              
            float bs=10000.0f;
            float da,hra,gr_sal;
            da=(float)0.4*bs;
            hra=(float)0.2*bs;
            gr_sal=bs+da+hra;
           
            System.out.println("Basic salary is : "+bs);
            System.out.println("Dearness allowance is : "+da);
            System.out.println("Hra  is : "+hra);
            System.out.println("Gross salary is : "+gr_sal);

      }

}


Note: In the above example, you will notice that I have written (float) before 0.4*bs; and before 0.2*bs; because when we simply multiply 0.4*bs or 0.2*bs , we will get an answer in decimal but this answer will be considered as of double data type by default, and we want to store that value into float type variable, this will not be possible without explicit conversion.

Hence, explicit conversion is done in this example.


                                                  IMPORTANT  TIP

if you don't want to convert it explicitly, then you can take all the variables in double, as you know explicit conversion is needed only when we want to convert the higher data type into lower one.

class GrossSalary
{
      public static void main(String[] args) {
              
            double bs=10000.0;
            double da,hra,gr_sal;
            da=0.4*bs;
            hra=0.2*bs;
            gr_sal=bs+da+hra;
           
            System.out.println("Basic salary is : "+bs);
            System.out.println("Dearness allowance is : "+da);
            System.out.println("Hra  is : "+hra);
            System.out.println("Gross salary is : "+gr_sal);

      }

}


Output:
Basic salary is : 10000.0
Dearness allowance is : 4000.0
Hra  is : 2000.0
Gross salary is : 16000.0

Monday, April 23, 2018

Class X (ICSE) Explicit conversion explanation through example



CLASS - X (ICSE)
COMPUTER  APPLICATION


Concept to remember: In case of division always remember that if numerator and denominator are integer then answer would be without fractional part. 

But either numerator or denominator is real constant then it will divide thoroughly by giving the fractional part also. 

Example: 3/4 = 0
                3.0/4 = 0.75
                3/4.0 = 0.75 
                3.0/4.0 = 0.75

//Program to calculate average of four numbers such that if users enter 2, 3, 4, 5 then the result will be 3.5
package com.mohan;

class Pattern
{
      public static void main(String[] args) {
                 int num1 = 2 ,num2 = 3, num3 = 4, num4 = 5;
                 float avg;
                 avg=(float)(num1+num2+num3+num4)/4;  //Explicit conversion

            System.out.println(avg);
      }

}


Output: 3.5

Explanation: As you know program runs line by line,
                  int num1 = 2 ,num2 = 3, num3 = 4, num4 = 5; this line will store a number 2,3,4,5 in variable num1,num2,num3 and num4 respectively.


                 avg=(float)(num1+num2+num3+num4)/4; In this line sum of four numbers is dividing by 4.
To divide it completely we need to convert either numerator or denominator with a fractional part such as 4.0 but in this line we are not writing 4.0 so we are converting the numerator into real constant by converting it explicitly.


Hence in the above program if you wants to divide thoroughly, you can write 
                  avg=(float)(num1+num2+num3+num4)/4;  //In this you are converting numerator into real constant
                            or
                  avg=(num1+num2+num3+num4)/4.0;       // In this denominator is in real constant

But,
 if you are writing it as                   avg=(num1+num2+num3+num4)/4.0;   
 then output will be 3.0 because avg is of float type


Saturday, April 21, 2018

Class X (ICSE) Type conversions- implicit & explicit



CLASS - X (ICSE)
COMPUTER  APPLICATION


Type Conversion
®     When different types of variables and constants are mixed up in an expression, they are converted in one common type implicitly by the compiler. This is called type conversion.
®     Java facilitates the type conversion in two forms:

Implicit type conversion:- 
®     An implicit type conversion is a conversion performed by the compiler without programmer’s intervention.
®     The java compiler converts all operands upto the data type of the largest data type’s operand, which is called type promotion.
®     Example:
int a=10; 
float f=a; 
®     Program
          class ExampleOfImplicit{
            public static void main(String args[]){
              int a=10; 
              float f=a; 
                System.out.println(a); 
                System.out.println(f); 
             }
             }
            Output:
            10
10.0


Explicit type conversion :- 
®     An explicit type conversion is user-defined that forces an expression to be of specific data type.
®     It is also known as type casting.
®     Example:float x=3.4f;
     int y=(int)x;   
      
   
®     Program
          class ExampleOfExplicit{
            public static void main(String args[]){
              float f=10.5f; 
               int a=(int)f;                                 //int a=f; (compile time error)
               System.out.println(f); 
               System.out.println(a); 
            }
           }
Output:
 10.5
 10
®     If you will do typecast then it means you are doing forcefully as you can see int y=(int)x; here data is lost but no error.
®     Always remember you can do typecast but java usually don’t allow it by itself.
                                

 Conversions that compiler do itself (implicit conversions)

*      byte to short, int, long, float or double
*      short to int, long, float or double
*      char to int, long, float or double
*      int to long, float or double
*      long to float or double
*      float to double

Friday, April 20, 2018

Class X (ICSE) Variables and Comments





CLASS - X (ICSE)
COMPUTER  APPLICATION




Variables
®     A named memory location, whose contains can be changed with in program execution is known as variable.
®     It is a combination of "vary + able" that means its value can be changed.
®     When you run any program, it consumes memory in RAM till its execution in process.
®     Examples:
Ø  int counter;
Ø  double temp;
Ø  String name;
Ø  int[] ages;
Ø  char letters[]
®     String is a predefined class not a primitive data type.

Types of Variable
There are three types of variables in java
1.      local variable
2.      instance variable
3.      static variable

1) Local Variable

®     A variable which is declared inside the method is called local variable.

2) Instance Variable or Object Variable

®     A variable which is declared inside the class but outside the method is called instance variable.
®     It is  not declared as static.

3) Static variable or Class Variable

®     A variable that is declared as static is called static variable.
®     It cannot be local.

    Example of types of variables

class TypesOfVariables{    
    
    int data=50;                                  //instance variable 
    static int m=100;                         //static variable 
    void method(){ 
    int n=90;                                     //local variable 
    } 
 }

Comments

1.     Block or multiple-line comment:
®     Begin with /* and terminate with */ that spans multiple lines.

2.     Line comment:
®     Begin with // and terminate at the end of the line.

3.     Documentation comment:
®     Begin with /** and terminate with */ that spans multiple lines.
®     They are generally created using the automatic documentation generation tool

Class X (ICSE) Data types



CLASS - X (ICSE)

COMPUTER  APPLICATION



Data Types
®      A type identifies a set of values (and their representation in memory) and a set of operations that transform these values into other values of that set.
®     Java is strongly typed language.
®     Types of data types
Ø  Primitive type (or Intrinsic Data Type)
Ø  Non-Primitive type(Reference/Object or Composite Data Type)

Primitive Data Types
®     Once a primitive data type has been declared its type can never change, although in most cases its value can change.
®     These eight primitive type can be put into four groups
Data type
Default value
Default size
boolean
false
1 bit
char
‘\u0000’
2 byte
byte
0
1 byte
short
0
2 byte
int
0
4 byte
long
0L
8 byte
float
0.0f
4 byte
double
0.0d
8 byte

Data Type
Range 
boolean
Reserves 8 bits and only uses  bit
char
0(‘\u0000’) to 65,535(‘\uffff’)
byte
-128 to 127 ( -27 to 27-1)
short
(-215 to 215-1 )
int
(-231 to 231-1 )
long
 (-263 to 263-1 )
float
-3.4E+38 to  +3.4E+38
double
-1.7E+308 to +1.7E+308


Non-primitive Data Type
®     A reference data type is used to refer to an object.
®     A reference variable is declare to be of specific and that type can never be change.
®      class, arrays, interface etc.
®     Eg: String, Scanner, Random, int[], String[] etc
®     Reference variables store addresses