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


1.

 class Monitor
{
    int size;
    String company;

    public Monitor(int s, String c){
        size = s;
        company = c;
    }

    public void set(int s, String c){
        size = s;
        company = c;
    }

    public void prn_Monitor(){
        System.out.println("Monitor Size(Inches):" + size + "  company:" + company);
    }
}   


class Keyboard
{
    int num_keys;
    String company;

    public Keyboard(int n, String c){
        num_keys = n;
        company = c;
    }

    public void set(int n, String c){
        num_keys = n;
        company = c;
    }

    public void prn_Keyboard(){
        System.out.println("Num Keys:" + num_keys + "  company:" + company);
    }
}   


class CPU
{
    int hd;
    int mm;
    double cpu_speed;
   
    public CPU(int h, int m, double speed){
        hd = h;
        mm = m;
        cpu_speed = speed;
    }

    public void set(int h, int m, double speed){
        hd = h;
        mm = m;
        cpu_speed = speed;
    }

    public void prn_CPU(){
        System.out.println("Hard Disk size(GB):" + hd + "  Main Memory(MB):" + mm + "  CPU Speed(GHz):"+ cpu_speed);
    }
}   


public class MyPC{
    CPU my_cpu;
    Monitor my_mon;
    Keyboard my_kbd;
   
    public MyPC(CPU c, Monitor m, Keyboard k){
        my_cpu = new CPU(c.hd, c.mm, c.cpu_speed);
        my_mon = new Monitor(m.size, m.company);
        my_kbd = new Keyboard(k.num_keys, k.company);
    }   
       
   
    void PrintConfiguration(){
        my_cpu.prn_CPU();
        my_mon.prn_Monitor();
        my_kbd.prn_Keyboard();
    }

    public static void main(String args[]){
        CPU c = new CPU(80, 512, 3);
        Monitor m = new Monitor(15, "Samsung");
        Keyboard k = new Keyboard(107, "HCL");

        MyPC pc = new MyPC(c,m,k);
       
        pc.PrintConfiguration();
    }
}   



2.

 static void ChkPoint(Rectangle r, Point p)
 {
        boolean b;
                                                                                                                            
        b = ( (r.A.x < p.x) && (r.A.x + length > p.x) && (r.A.y < p.y) && (r.A.y + width > p.y) );
                                                                                                                            
        System.out.println("Is inside: " + b);
 }



3.

class l3_a3{
   
    static int getOneCnt(byte b)
    {
        int num=0;

        num += b & 0x01;
        num += (b & 0x02) >> 1;
        num += (b & 0x04) >> 2;
        num += (b & 0x08) >> 3;
        num += (b & 0x10) >> 4;
        num += (b & 0x20) >> 5;
        num += (b & 0x40) >> 6;
        num += (b & 0x80) >> 7;
           
        return num;
    }   

    static boolean isEven(byte b)
    {
        return  (b&0x01) == 0;
    }   

    static boolean isPowTwo(byte b)
    {
        return  (b&(b-1)) == 0 ;
    }

   
    public static void main(String args[]){
        byte b=50,e=20,p=31;

        System.out.println("Ones cnt of " + b + " is "+getOneCnt(b));
        System.out.println(e + " isEven: " + isEven(e));
        System.out.println(p + " isPowTwo: " + isPowTwo(p));
    }
}