Lab 2  Solutions,  ESC101, 2004-2005 Semester-II


1.

        Consider the statement  tot = tot + 1/n1;
        Here in the expression 1/n1, both numerator and denominator are integers. So final result is also integer.
But the actual value of 1/n1 is 0.xxx. Since it is integer division the fractional part is truncated and we get only 0.
        To overcome this make either of the numerator or denominator as a float value.  Then the statement becomes  tot = tot + 1.0/n1;
        Similarly we have to make the changes in the statement
tot = tot + 1/n1.  Now it becomes tot = tot + 1.0/n2.
        Note that in the above statement we also changed n1 to n2.


2.

public class Lab02
{
        static String format(String date)
        {
                int mm;
                String month = "JanFebMarAprMayJunJulAugSepOctNovDec";
                String op;
                                                                                                                            
                mm = Integer.parseInt(date.substring(3,5));
                                                                                                                            
                op = date.substring(0,2) + ", " + month.substring((mm-1)*3,(mm-1)*3+3) + " " + date.substring(6,10);
                                                                                                                            
                return op;
        }
                                                                                                                            
                                                                                                                            
        public static void main(String[] args)
        {
                System.out.println(format("06-01-2005"));
        }
}


3.

        static double finalval(double pv, double interest, int yrs)
        {
                return pv * Math.pow(1+interest/100, yrs);
        }


4.

        static void prnval1(double d)
        {
                String ds = d + "";
                                                                                                                            
                System.out.println(ds.substring(0,ds.indexOf('.')));
                System.out.println(ds.substring(ds.indexOf('.')+1,ds.length()));
        }


5.

        static void prnval2(double d)
        {
                int val1;
                String ds;
                                                                                                                            
                ds = d + "0";
                                                                                                                            
                val1 = (int)Math.floor(d); /* or ds.substring(0, ds.indexOf('.') */
                                                                                                                            
                System.out.println(val1/1000);
                val1 = val1 % 1000;
                                                                                                                            
                System.out.println(val1/100);
                val1 = val1 % 100;
                                                                                                                            
                System.out.println(val1/10);
                val1 = val1 % 10;
                                                                                                                            
                System.out.println(val1);
                                                                                                                            
                                                                                                                            
                System.out.println(ds.substring(ds.indexOf('.')+1, ds.indexOf('.')+2));
                System.out.println(ds.substring(ds.indexOf('.')+2, ds.indexOf('.')+3));
        }