Saturday, 7 February 2015

Balagurusamy Solutions

/* 4.1
 * The following code results in compile time error. Identify the error
 */

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg4_1;

/**
 *
 * @author Azeem
 */

public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        int   x  = 123456;
        float f  = 100.12;
        System.out.println("Float Value = " + f);
    }   
}

/* Floating point numbers are treated as doubl-precision quantities. To force
   them to be in single-precision mode, we must append f or F to the numbers
   float f = 100.12F
   or
   float f = 100.12f
*/




/* 4.2
 * The following code results in compile time error. Identify the error
 */


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg4_2;

/**
 *
 * @author Azeem
 */

public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        int y;
        if (x > 10)
        {
            y = x;
        }
        System.out.println("Value of Y = " + y);
    }   
}

/* Variable x is not defined
 */




/* 4.3
 * What modification should be done to the following code so that the value of
 * the variable pi is not modifiable
 */


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg4_3;

/**
 *
 * @author Azeem
 */

public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        /* When we define symbolic constatnt they cannot be assigned any other
         * value.
         * Defining pi as symbolic constants
         */

        final float PI = 3.14F;
        System.out.println("Value of PI = " + PI);
    }   
}




/* 4.4
 * The following code results in compile time error while storing the value of int
 * variable to a byte variable. Identify the problem with the code and provide the
 * solution
 */



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg4_4;

/**
 *
 * @author Azeem
 */

public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
       int  i = 1234;
       /* Byte is only 1 byte whereas int is 4 byte.
        * So there will be loss of data
        * Hence in order to preserve the data we need to type case it to
        * either long, float or double
        */

       long b = (long)i;
       System.out.println("Value of Byte Variable b = " + b);
    }
}




/* 4.5
 * Identify the error in the following code:
 */

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg4_5;

/**
 *
 * @author Azeem
 */

public class Scope {

    /**
     * @param args the command line arguments
     *
/
    public static void main(String[] args) {
        int m = 10;
        {
            int m = 20;
        }
    }
   
}

/* Variable m is already defined (multiple definition of m) in method Scope
 */




/* 5.1
 * In the following code the expected value is 8, but it prints the value 2.What
 * would you modify in the code to obtain the expected value ?
 */

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg5_1;

/**
 *
 * @author Azeem
 */

public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        int i = 42 + 45 -48 - 5;
        int j = 5 + 5 - 8 + 2;
        /* Using division sign instead of modulus
         */

        int ans = i / j;
        System.out.println("Value of Ans = " + ans);
    }   
}
 
 




/* 5.3
 * The following code results in compilation error. Debug the code and rectify
 * the problem
 */

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg5_3;

/**
 *
 * @author Azeem
 */

public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        /* Using arithmetic operators properly
         */

        int ans = (2  * (5 - 8) * (5 - 5) + 10) * 2;
        System.out.println("Value of Ans = " + ans);
    }   
}



/* 5.4
 * In the code give below what should be changed to obtain the value of 40.0 for
 * x
 */


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg5_4;

/**
 *
 * @author Azeem
 */

public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        /* rint returns the closest integer value in the argument
         * Replacing it with floor which gives the largest integer that is less
         * than or equal to the argument.
         */

        double x = Math.floor(40.6);
        double y = Math.abs(40.6);
       
        System.out.println("Value of X is = " + x + " and Y is = " + y);
    }   
}




/* 6.1
 * Determine whether the following are true or false:
 * (a) When if statements are nested, the last else gets associated with the nearest
 * if without an else

 *  Ans: True
 * (b) One if can have more than one else clause
 *  Ans: False
 * (c) A switch statement can always be replaced be a series of if..else
 *    statement
 *  Ans: True
 * (d) A switch expression can be of any type
 *  Ans: False
 * (e) A program stopes its execution when a break statement is encountered
 *  Ans: False
 *
/




/* 6.3
 * Find errors, if any, in each of the following segments
 */

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg6_3;

/**
 *
 * @author Azeem
 */

public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
       
        /* Declaring and initializing variables
         */

        int x = 10;
        int y = 5;
        int z = 8;
        /* A conditional operator '==' must be used instead of an
         * assignment operator '=' in conditional statemens
         */

        if (x + y == z && y > 0){
         /* Statements
           */  

        }
      
        /* Declaring and initializing variables
        */

        int a = 10;
        int b = 9;
        int c = 8;
        int code = a + b;
       
        /* If statement ends because of semicolon ';'. Thats fine but else statement
         * should immediately follow 'if' statements or it may give a compilation error
         * Statements a = b + c and a = 0 should end with a semicolon
         */

        if (code > 1)
            a = b + c;
        else
            a = 0;  
       
       /* Declaring and initializing variables
        */

        int p = 10;
        int q = 9;
       
        /* If has no parenthesis which gives compilation error
         */

        if ((p < 0) || (q < 0)){
        /* Statements
         */

        }
    }   
}

/* 6.6
 * Write a program to find the number of and sum of all integers greater than
 * 100 and less than 200 that are divisible by 7
 */


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg6_6;

/**
 *
 * @author Azeem
 */

public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
       
        /* Initializing variables
         */

        int index = 0;
        int sum   = 0;
        int count = 0;
       
       
        for (index = 100; index <= 200; index++){

            /* If the value of index is divisible by 7 then
             * Increase count and add that number to sum
             */

            if (index % 100 == 0){
                count++;
                sum = sum + index;
            }
        }
       
        /* Print out the result
         */

        System.out.println("The number of integers dvisible by 7 greater than "
                + "100 but less than 200 is " + count);
        System.out.println("The sum of number of integers divisible by 7 greater"
                + " than 100 but less than 200 is " + sum);
    }   
}



/* 6.7
 * A set of two linear equations with two unknown x1 and x2 is given below:
 * ax1 + bx2 = m
 * cx1 + dx2 = n
 * The set has a unique solution
 * x1 = (md - bn) / (ad - cd)
 * x2 = (na - mc) / (ad - cd)
 * provided the denominator ad - cd is not equal to zero
 * Write the program that will read the values of constants a, b, c, d, m and n and
 * compute the values of x1 and x2.
 * An appropriate message shoud be printed if ad - cb = 0
 */


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg6_7;
import java.util.Scanner;

/**
 *
 * @author Azeem
 */

public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
       
        /* Declaring variables
         */

        int x1, x2;
        int a, b, c, d, m, n;

        /* Scanning data from user
         */

        Scanner in = new Scanner(System.in);
        System.out.println("Enter the value of a");
        a = in.nextInt();
        System.out.println("Enter the value of b");
        b = in.nextInt();
        System.out.println("Enter the value of c");
        c = in.nextInt();
        System.out.println("Enter the value of d");
        d = in.nextInt();
        System.out.println("Enter the value of m");
        m = in.nextInt();
        System.out.println("Enter the value of n");
        n = in.nextInt();
       

        /* Checking if denominator is zero
         */

        if ( (a * d) - (c * b) == 0){
            System.out.println("Error: ad - cb = 0");
        }
        else{
            x1 = ((m * d) - (b * n)) / ((a* d) - (c*d));
            x2 = ((n * a) - (m* c)) / ((a* d) - (c*d));
        
            /* Printing out results
             */

            System.out.println("The value of x1 = " + x1);
            System.out.println("The value of x2 = " + x2);
        }
    }
}



/* 6.9
 * Admission to a professional course is subject to the following conditions:
 * (a) Marks in mathematics         >= 60
 * (b) Marks in physics             >= 50
 * (c) Marks in chemistry           >= 40
 * (d) Marks in all three subjects  >= 200
 *                 ( or )
 * Total in mathematics and phhysics  >= 150. Given the marks in three subjects,
 * write a program to process the applications to list the eligible candidates
 */


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg6_9;
import java.util.Scanner;
/**
 *
 * @author Azeem
 */

public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
       
        /* Declaring variables
         */

        int physics;
        int chemistry;
        int mathematics;
        int total;
        int total_math_phy;
       
        /* Declaring scanner
         */

        Scanner in = new Scanner (System.in);
       
        /* Taking input from user
         *
/
        System.out.println("Enter marks for physics");
        physics = in.nextInt();
       
        System.out.println("Enter marks for chemistry");
        chemistry = in.nextInt();
       
        System.out.println("Enter marks for mathemetics");
        mathematics = in.nextInt();
   
        /* Calculating total
         */

        total = physics + chemistry + mathematics;
        total_math_phy = physics + mathematics;

        /* Checking if candidate is eligible or not
         */

        if (((physics >= 50) && (chemistry >= 40) &&  (mathematics >= 60) && (total >= 20))
                      ||   (total_math_phy >= 150)){
            System.out.println("The candidate is eligible");
        }
        else{
            System.out.println("The candidate is not eligible");
        }   
    }  
}



/* 6.13.a
 * Write a program that will read the value of x and evaluate the following function
 *                 {     1     for x > 0
 *  y       =      {     0     for x = 0
 *                 {    -1     for x < 0
 * (a) nested if statements
 */

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg6_13;
import java.util.Scanner;
/**
 *
 * @author Azeem
 */

public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        int x;
        int y;
        Scanner in = new Scanner(System.in);
       
        System.out.println("Enter the value of x");
        x = in.nextInt();
       
        y = 1;
        if ( x <= 0){
            y = 0;
            if ( x < 0){
                    y = -1;
                }
            }
        System.out.println("y = " + y);
        }       
    }



/* 6.13.b
 * Write a program that will read the value of x and evaluate the following function
 *                 {     1     for x > 0
 *  y       =      {     0     for x = 0
 *                 {    -1     for x < 0
 *  (b) else if statements

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg6_13_b;
import java.util.Scanner;
/**
 *
 * @author Azeem
 */

public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        int x;
        int y;
        Scanner in = new Scanner(System.in);
        x = in.nextInt();
       
        /* Using if else statement
         */

        if ( x > 0){
            y = 1;
        }
        else if ( x < 0){
            y = -1;
        }
        else{
            y = 0;
        }
        System.out.println("y = " + y);
    }   
}



/* 6.13.b
 * Write a program that will read the value of x and evaluate the following function
 *                 {     1     for x > 0
 *  y       =      {     0     for x = 0
 *                 {    -1     for x < 0
 *  (c) conditional operator ?:.

package pkg6_9_c;
import java.util.Scanner;
/**
 *
 * @author Azeem
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int x;
        int y;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the value of x");
        x  = in.nextInt();
       
        y = (x > 0) ?(1): ((x < 0) ? (-1): (0));
       
        System.out.println("y = " + y);
    }
}