JAVA QP Programs Solutions

Embed Size (px)

Citation preview

  • 8/22/2019 JAVA QP Programs Solutions

    1/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 1

    This File contains only codes or Programs for Java Paper most of the questions I have been able

    to cover.

    Only revised Syllabus from May 2009 onwards

    Year : May 2009Q1) b )

    import java.util.*;

    public class Recursion

    {

    double exp(double a,int b)

    {

    if(b0)

    return (a*(exp(a,b-1)));

    else

    return 1;

    }

    public static void main(String a[])

    {

    Recursion r= new Recursion();

    Scanner sc= new Scanner(System.in);

    double b,rs;int pow;

    System.out.print("Enter base :");

    b= sc.nextDouble();

    System.out.print("Enter power :");

    pow= sc.nextInt();

    rs= r.exp(b,pow);

    System.out.print("Result :"+rs);

    }

    }

  • 8/22/2019 JAVA QP Programs Solutions

    2/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 2

    Year : May 2009

    Q2 ) a) GUI for Calculator

    import java.awt.*;

    import javax.swing.*;

    import java.awt.event.*;

    class Calculator extends JFrame implements ActionListener

    {

    JMenuBar mb = new JMenuBar();

    JMenu file = new JMenu("File");

    JMenuItem mexit = new JMenuItem("Exit");

    JTextField t = new JTextField(20);

    JButton bspace= new JButton("BackSpace");

    JButton bce= new JButton("CE");

    JButton bc= new JButton("C");

    JLabel l1 = new JLabel("");

    JLabel l2 = new JLabel("");

    JLabel l3 = new JLabel("");

    JLabel l4 = new JLabel("");

    JLabel l5 = new JLabel("");

    JLabel l6 = new JLabel("");JLabel l7 = new JLabel("");

    JLabel l8 = new JLabel("");

    JLabel l9 = new JLabel("");

    JButton b1 = new JButton("1");

    JButton b2 = new JButton("2");

    JButton b3 = new JButton("3");

    JButton b4 = new JButton("4");

    JButton b5 = new JButton("5");

    JButton b6 = new JButton("6");JButton b7 = new JButton("7");

    JButton b8 = new JButton("8");

    JButton b9 = new JButton("9");

    JButton b0= new JButton("0");

    JButton bslash = new JButton("/");

  • 8/22/2019 JAVA QP Programs Solutions

    3/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 3

    JButton bsqrt= new JButton("sqrt");

    JButton b1byx= new JButton("1/x");

    JButton bminus = new JButton("-");

    JButton bplus= new JButton("+");

    JButton bpercent = new JButton("%");JButton bequal= new JButton("=");

    JButton bmultiply = new JButton("*");

    JButton bplusminus= new JButton("+/-");

    JButton bpoint = new JButton(".");

    JPanel p = new JPanel();

    JPanel pbutton = new JPanel();

    JPanel pbuttons = new JPanel(new GridLayout(4,5,5,5));

    GridBagLayout gb = new GridBagLayout();

    GridBagConstraints cs = new GridBagConstraints();

    Calculator(String s)

    {

    super(s);

    setBounds(400,400,400,230);

    setResizable(false);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container c = getContentPane();

    c.setLayout(new BorderLayout());

    pbutton.setLayout(gb);

    cs.gridx=0;

    cs.gridy=0;

    cs.insets = new Insets(5,5,5,5);

    gb.setConstraints(bspace,cs);

    pbutton.add(bspace);

    cs.gridx=1;

    cs.gridy=0;cs.insets = new Insets(5,5,5,5);

    gb.setConstraints(l1,cs);

    pbutton.add(l1);

    cs.gridx=2;

    cs.gridy=0;

  • 8/22/2019 JAVA QP Programs Solutions

    4/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 4

    cs.insets = new Insets(5,5,5,5);

    gb.setConstraints(l2,cs);

    pbutton.add(l2);

    cs.gridx=3;

    cs.gridy=0;cs.insets = new Insets(5,5,5,5);

    gb.setConstraints(l3,cs);

    pbutton.add(l3);

    cs.gridx=4;

    cs.gridy=0;

    cs.insets = new Insets(5,5,5,5);

    gb.setConstraints(l4,cs);

    pbutton.add(l4);

    cs.gridx=5;

    cs.gridy=0;

    cs.insets = new Insets(5,5,5,5);

    gb.setConstraints(l5,cs);

    pbutton.add(l5);

    cs.gridx=7;

    cs.gridy=0;

    cs.insets = new Insets(5,5,5,5);

    gb.setConstraints(l6,cs);

    pbutton.add(l6);cs.gridx=8;

    cs.gridy=0;

    cs.insets = new Insets(5,5,5,5);

    gb.setConstraints(l7,cs);

    pbutton.add(l7);

    cs.gridx=9;

    cs.gridy=0;

    cs.insets = new Insets(5,5,5,5);

    gb.setConstraints(l8,cs);

    pbutton.add(l8);cs.gridx=10;

    cs.gridy=0;

    cs.insets = new Insets(5,5,5,5);

    gb.setConstraints(l9,cs);

    pbutton.add(l9);

  • 8/22/2019 JAVA QP Programs Solutions

    5/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 5

    cs.gridx=13;

    cs.gridy=0;

    cs.insets = new Insets(5,5,5,5);

    gb.setConstraints(bce,cs);

    pbutton.add(bce);cs.gridx=12;

    cs.gridy=0;

    cs.insets = new Insets(5,5,5,5);

    gb.setConstraints(bc,cs);

    pbutton.add(bc);

    pbuttons.add(b7);

    pbuttons.add(b8);

    pbuttons.add(b9);

    pbuttons.add(bslash);

    pbuttons.add(bsqrt);

    pbuttons.add(b4);

    pbuttons.add(b5);

    pbuttons.add(b6);

    pbuttons.add(bmultiply);

    pbuttons.add(b1byx);

    pbuttons.add(b1);

    pbuttons.add(b2);

    pbuttons.add(b3);

    pbuttons.add(bminus);pbuttons.add(bpercent);

    pbuttons.add(b0);

    pbuttons.add(bplusminus);

    pbuttons.add(bpoint);

    pbuttons.add(bplus);

    pbuttons.add(bequal);

    p.setLayout(new BorderLayout());

    p.add(t,BorderLayout.NORTH);

    p.add(pbutton,BorderLayout.SOUTH);

    setJMenuBar(mb);file.add(mexit);

    mb.add(file);

    c.add(p,BorderLayout.NORTH);

    c.add(pbuttons,BorderLayout.CENTER);

    t.setText("0");

  • 8/22/2019 JAVA QP Programs Solutions

    6/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 6

    //t.setHorizontalAlignment(JTextField.RIGHT);

    t.setEditable(false);

    setSize(400,400);

    setVisible(true);

    mexit.addActionListener(this);

    pack();

    }

    public void actionPerformed(ActionEvent ae)

    {

    if(ae.getSource()==mexit)

    {

    System.exit(0);

    }

    }

    public static void main(String args[])

    {

    Calculator cal = new Calculator("CALCULATOR");

    }

    }

  • 8/22/2019 JAVA QP Programs Solutions

    7/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 7

    Year : May 2009

    Q3)b)

    Series :- 1+x+ x2+.

    Solution

    import java.util.*;public class Series

    {

    int x,n;

    int pow(int a,int b)

    {

    if(b==0)

    return 1;

    else

    return (a*pow(a,(b-1)));

    }

    void show()

    {

    Scanner sc= new Scanner(System.in);

    System.out.print("\n Enter value of X:");

    x=sc.nextInt();

    System.out.print("\n Enter end limit n:");

    n=sc.nextInt();

    int d=0;

    for(int i=0;i

  • 8/22/2019 JAVA QP Programs Solutions

    8/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 8

    Year : May 2009

    Q4)b) : Passing Parameter to Applet

    /*

    */

    import java.applet.Applet;

    import java.awt.*;

    public class DemoParam extends Applet

    {

    String n;

    int year;

    public void init()

    {

    n = getParameter("Name");

    year = Integer.parseInt(getParameter("Year"));

    }

    public void paint(Graphics g)

    {

    g.drawString("Name of College: "+n, 10, 20);

  • 8/22/2019 JAVA QP Programs Solutions

    9/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 9

    g.drawString("Year of Establishment: "+year, 10, 40);

    }

    }

  • 8/22/2019 JAVA QP Programs Solutions

    10/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 10

    Year : May 2009

    Q6)b)

    Table printing by thread

    Sol:

    class Table

    {

    int no;

    Table(int t)

    {

    no = t;

    }

    }

    public class Multitable extends Table implements Runnable

    {

    public Multitable(int c)

    {

    super(c);

    }

    public synchronized void run()

    {

    printTable();

    }

    public void printTable()

    {

    System.out.println("Table of : "+no+" ");

    for(int i=1;i

  • 8/22/2019 JAVA QP Programs Solutions

    11/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 11

    {

    Multitable mt = new Multitable(5);

    Thread t1 = new Thread(mt);

    t1.start();

    Multitable mt1 = new Multitable(7);Thread t2 = new Thread(mt1);

    t2.start();

    Multitable m = new Multitable(13);

    Thread t3 = new Thread(m);

    t3.start();

    }

    }

  • 8/22/2019 JAVA QP Programs Solutions

    12/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 12

    Dec 2009

    Q2)a) Copy Contents of One File to another

    Solution:

    import java.io.*;

    public class Copyfile{

    public static void main(String argd[])

    {

    try

    {

    InputStream is= new FileInputStream("abc.docx");

    OutputStream os= new

    FileOutputStream("abc1.docx");

    int c;

    while((c=is.read())!=-1)

    {

    os.write((char)(c));

    }

    os.close();

    is.close();

    }

    catch(Exception e)

    {

    }}

    }

  • 8/22/2019 JAVA QP Programs Solutions

    13/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 13

    Dec 2009

    Q2)b) Refer May 09 Q6)b

    Q3)b) Runtime Unchecked Exceptions

    import java.util.*;

    public class MyException{

    String a[];

    public MyException()

    {

    a=new String[2];

    try

    {

    Scanner sc= new Scanner(System.in);

    for(int i=0;i

  • 8/22/2019 JAVA QP Programs Solutions

    14/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 14

    }

    }

    public static void main(String a[])

    {

    MyException e = new MyException();}

    }

  • 8/22/2019 JAVA QP Programs Solutions

    15/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 15

    Dec 2009

    Q6)b) Serialization

    // Q6 ) b

    import java.io.*;

    import java.util.*;

    class MyObject implements Serializable

    {

    String s;

    Date d;

    public MyObject()

    {

    s = "Pantoji";

    d = new Date();

    }

    public String toString()

    {

    return "String = "+s+"\t Date = "+d;

    }

    }

    public class Serial{

    public static void main(String args[])

    {

    MyObject mo = new MyObject();

    System.out.println("Before Serialization : "+mo.toString());

    try

    {

    // Object Serialization : Writing onto the File

    FileOutputStream fs = newFileOutputStream("MyObject.dat");

    ObjectOutputStream os = new

    ObjectOutputStream(fs);

    os.writeObject(mo);

    os.close();

  • 8/22/2019 JAVA QP Programs Solutions

    16/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 16

    }

    catch(IOException e)

    {

    System.out.println(e);

    }

    try

    {

    // Object Serialization : Reading from the File

    FileInputStream fis = new

    FileInputStream("MyObject.dat");

    ObjectInputStream ois = new

    ObjectInputStream(fis);

    mo = (MyObject) ois.readObject();

    ois.close();

    }

    catch(IOException e)

    {

    e.printStackTrace();

    }

    catch(ClassNotFoundException cfe)

    {

    cfe.getMessage();

    }System.out.println("After deserialization: "+mo.toString());

    }

    }

  • 8/22/2019 JAVA QP Programs Solutions

    17/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 17

    Dec 2009

    Q7)a) Sorting names

    // Command line argument

    public class Sort

    {public static void main(String args[])

    {

    int i,j,l;

    l=args.length;

    String temp,arr[];

    arr= new String[l];

    for(i=0;i

  • 8/22/2019 JAVA QP Programs Solutions

    18/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 18

    Dec 2009

    Q7)b) Wrapper Class

    public class Wrap

    {public static void main(String args[])

    {

    Integer i =Integer.valueOf(43);

    System.out.println("i = "+i);

    Integer i1= new Integer(43);

    byte b = i1.byteValue();

    System.out.println("byte = "+b);

    short s = i1.byteValue();

    System.out.println("short = "+s);

    double d = i1.doubleValue();

    System.out.println("double = "+d);

    float f = i1.floatValue();

    System.out.println("float = "+f);

    }

    }

  • 8/22/2019 JAVA QP Programs Solutions

    19/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 19

    May 2010

    Q1)b : Multithreading

    class Counter

    {

    int count=15;

    public int getCount()

    {

    return count;

    }

    }

    public class GetCounter implements Runnable

    {

    Counter c = new Counter();

    public static void main(String args[])

    {

    GetCounter gc = new GetCounter();

    Thread t1 = new Thread(gc);

    Thread t2 = new Thread(gc);

    Thread t3 = new Thread(gc);

    t1.setName("Thread-1 ");

    t2.setName("Thread-2 ");t3.setName("Thread-3 ");

    t1.start();

    t2.start();

    t3.start();

    }

    public void decrement()

    {

    if(c.count>0)

    {c.count = c.count - 1;

    System.out.println("Counter value for

    :"+Thread.currentThread().getName()+" "+c.getCount());

    try

    {

  • 8/22/2019 JAVA QP Programs Solutions

    20/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 20

    Thread.sleep(1000);

    }catch(InterruptedException e)

    {

    e.printStackTrace();

    }}

    else

    {

    }

    }

    public synchronized void run()

    {

    for(int i=0;i

  • 8/22/2019 JAVA QP Programs Solutions

    21/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 21

    May 2010

    Q3)b :Counting objects

    //Asked for 10 marks ( just 30Lines = 1 Page )

    public class ObjectCount

    {private static int count;

    private int x;

    public ObjectCount(int y)

    {

    x=y;

    count++;

    }

    public static void showCount()

    {

    System.out.println("No of Objects Created = "+count);

    }

    public void show()

    {

    System.out.println("\n Value in the object is ="+x);

    }

    public static void main(String a[])

    {

    ObjectCount o1= new ObjectCount(10);

    showCount();ObjectCount o2= new ObjectCount(20);

    showCount();

    ObjectCount o3= new ObjectCount(30);

    showCount();

    o1.show();

    o2.show();

    o3.show();

    }

    }

  • 8/22/2019 JAVA QP Programs Solutions

    22/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 22

    May 2010

    Q4)a)Creating a GUI ( five Swing Control and Layout Managers )

    import java.awt.*;

    import javax.swing.*;

    public class PrinterGui extends JFrame{

    JPanel p1,p2,p3,p4,p5;

    JLabel l1;

    JButton b_ok,b_cancel,b_setup,b_help;

    JRadioButton Sel, All, App;

    ButtonGroup bg1;

    JComboBox Quality;

    JCheckBox chk1,chk2,chk3,chk4;

    public PrinterGui()

    {

    l1=new JLabel("Printer:EPSon EPL-7000 ");

    b_ok= new JButton("OK");

    b_cancel=new JButton("Cancel");

    b_setup= new JButton("Setup");

    b_help= new JButton("Help");

    Sel = new JRadioButton("Select ");

    All = new JRadioButton("All ");

    App = new JRadioButton("Applet");

    bg1 = new ButtonGroup();bg1.add(Sel);

    bg1.add(All);

    bg1.add(App);

    Quality = new JComboBox();

    Quality.addItem("High");

    Quality.addItem("Medium");

    Quality.addItem("Low");

    chk1= new JCheckBox("Image");

    chk2= new JCheckBox("Text");

    chk3= new JCheckBox("Code");chk4= new JCheckBox("Print to File");

    p1= new JPanel ();

    p2= new JPanel();

    p3=new JPanel();

    p4= new JPanel();

  • 8/22/2019 JAVA QP Programs Solutions

    23/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 23

    p5 = new JPanel();

    p1.setLayout(new BorderLayout());

    p2.add(l1);

    p3.add(b_ok);

    p3.setLayout(new BoxLayout(p3,BoxLayout.Y_AXIS));p3.add(b_cancel);

    p3.add(b_setup);

    p3.add(b_help);

    p4.add(Quality);

    p4.add(chk4);

    p5.setLayout(new GridLayout(3,2,5,5));

    p5.add(chk1);

    p5.add(chk2 );

    p5.add(chk3 );

    p5.add(Sel );

    p5.add(All );

    p5.add(App );

    p1.add(p2,BorderLayout.NORTH);

    p1.add(p3,BorderLayout.EAST);

    p1.add(p4,BorderLayout.SOUTH);

    p1.add(p5,BorderLayout.CENTER);

    getContentPane().add(p1);

    setVisible(true);

    setSize(600,400);this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public static void main(String args[])

    {

    PrinterGui p1= new PrinterGui();

    }

    }

  • 8/22/2019 JAVA QP Programs Solutions

    24/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 24

    Dec 2010

    Q2)a)

    import java.awt.*;

    import java.applet.*;

    /*

    */

    public class AppReverse extends Applet

    {

    int s,x;

    public void init()

    {

    s=Integer.parseInt(getParameter("No"));

    x=0;

    int d,t;

    t=s;

    while(t>0)

    {

    d=t%10;

    x=x*10+d;

    t=t/10;

    }

    }

    public void paint(Graphics g){

    g.drawString("\n Reverse of "+s+" = "+x, 10, 20);

    }

    }

  • 8/22/2019 JAVA QP Programs Solutions

    25/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 25

    Dec 2010

    Q2)b) Refer Dec 2009 Q3)b

    Q3)b Two Thread Printing ( Joint Effort by me and Tushar Fulmali )

    class Student

    {int roll [];

    float per [];

    boolean b;

    public Student()

    {

    roll=new int[]{1, 2, 3, 4, 5};

    per=new float[]{40, 50, 60, 70, 80};

    b=true;

    }

    public synchronized void printRoll()

    {

    for(int i=0;i

  • 8/22/2019 JAVA QP Programs Solutions

    26/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 26

    {

    if(b)

    {

    try

    {// System.out.println ("Waiting.."+i+

    " BOOL: "+b);

    wait();

    }

    catch(Exception e )

    {

    System.out.println(e);

    }

    }

    System.out.println("Per = "+per[i]);

    b=true;

    notify();

    }

    }

    }

    class RollThread extends Thread

    {

    Student s;

    public RollThread(Student s){

    this.s=s;

    this.start();

    }

    public void run()

    {

    s.printRoll();

    }

    }

    class PerThread extends Thread{

    Student s;

    public PerThread(Student s)

    {

    this.s=s;

  • 8/22/2019 JAVA QP Programs Solutions

    27/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 27

    this.start();

    }

    public void run()

    {

    s.printPer();}

    }

    public class PrintThread

    {

    public static void main(String args[])

    {

    System.out.println("\n**Press Ctrl+C to Stop**\n");

    Student s= new Student();

    new RollThread(s);

    new PerThread(s);

    }

    }

  • 8/22/2019 JAVA QP Programs Solutions

    28/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 28

    Dec 2010

    Q4)a ) Thread Priority

    class testMulti implements Runnable

    {

    testMulti(){

    Thread cur = Thread.currentThread();

    Thread t = new Thread (this, "New thread");

    t.start();

    try

    {

    for (int cnt = 10; cnt > 0; cnt --)

    {

    System.out.println ("Parent Thread : "+cnt);

    Thread.sleep(1000);

    }

    }

    catch (InterruptedException e)

    {

    System.out.println ("Interrupted");

    }

    System.out.println ("exiting main thread");

    }

    public void run ()

    {

    try

    {

    for (int i = 5; i > 0; i--)

    {

    System.out.println ("Child Thread : " + i);

    Thread.sleep(2000);

    }}

    catch (InterruptedException e)

    {

    System.out.println ("child interrupted");

    }

  • 8/22/2019 JAVA QP Programs Solutions

    29/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 29

    System.out.println ("exiting child thread");

    }

    public static void main (String args[])

    {

    new testMulti();

    }

    }

    class FooRun implements Runnable

    {

    public void run(String s)

    {

    String name;

    name = s;

    System.out.println("Name: "+name);

    }

    public void run()

    {

    for(int i=0;i

  • 8/22/2019 JAVA QP Programs Solutions

    30/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 30

    t1.setName("BBB");

    t2.setName("CCC");

    t.start();

    t.setPriority(9);t1.start();

    t1.setPriority(7);

    t2.start();

    t2.setPriority(9);

    t2.yield();

    fr.run("student");

    }

    }

  • 8/22/2019 JAVA QP Programs Solutions

    31/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 31

    Dec 2010

    Q5)a)

    class A

    {

    public void ShowA(){

    System.out.println("In Show method of A class");

    }

    }

    interface Ainter

    {

    void disp();

    }

    interface Binter extends Ainter

    {

    void show();

    }

    public class B extends A implements Binter

    {

    public void disp()

    {

    System.out.println("\n Display method overriding");

    }

    public void show(){

    System.out.println("\n Show method overriding");

    }

    public static void main(String args[])

    {

    B obj = new B();

    obj.disp();

    obj.show();

    obj.ShowA();

    }}

  • 8/22/2019 JAVA QP Programs Solutions

    32/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 32

    Dec 2010

    Q5)b) Refer May 2010 Q4)a)

    May 2011

    Q2)a) Thread Intercommunication

    public class ThreadA

    {

    public static void main(String args[])

    {

    ThreadB b = new ThreadB();

    b.start();

    synchronized(b)

    {

    try

    {

    System.out.println("Waiting for b to

    complete...");

    b.wait();

    }

    catch(InterruptedException e)

    {

    }

    System.out.println("Total is : "+b.total);

    }}

    }

    class ThreadB extends Thread

    {

    int total;

    public void run()

    {

    synchronized(this)

    {

    for(int i=0;i

  • 8/22/2019 JAVA QP Programs Solutions

    33/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 33

    notify();

    }

    }

    }

  • 8/22/2019 JAVA QP Programs Solutions

    34/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 34

    May 2011

    Q2)b) Refer May 2009 Q7)b

    Q4)a) Refer May 2010 Q4)a

    Q5)a) Refer Dec 2009 Q3)b

    Q5)b) Refer Dec 2009 Q6)bQ6)a) Mouse Events in Applet

    /*

    */

    import java.applet.*;

    import java.awt.*;

    import java.awt.event.*;

    public class MouseEvent1 extends Applet implements MouseListener

    {

    public void init()

    {

    this.addMouseListener(this);

    }

    public void paint (Graphics g)

    {

    g.drawString("Right Click Red Color \n",50,50);

    g.drawString("Left Click Yellow Color \n",50,70);

    }public void start()

    {

    System.out.println("Start Method ");

    }

    public void stop()

    {

    System.out.println("Stop Method ");

    }

    public void destroy()

    {System.out.println("Destroy Method ");

    }

    public void mouseExited(MouseEvent e)

    {

    System.out.println(" Mouse Exited");

  • 8/22/2019 JAVA QP Programs Solutions

    35/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 35

    super.setBackground(Color.CYAN);

    }

    public void mouseClicked(MouseEvent e)

    {

    System.out.println(" Mouse Clicked");}

    public void mouseEntered(MouseEvent e)

    {

    System.out.println(" Mouse Entered");

    super.setBackground(Color.PINK);

    }

    public void mousePressed(MouseEvent e)

    {

    System.out.println(" Mouse Pressed");

    if(e.getButton()==e.BUTTON1)

    {

    super.setBackground(Color.RED);

    System.out.println("Color Changed ");

    }

    if(e.getButton()==e.BUTTON3)

    {

    System.out.println("Color abdh ");

    super.setBackground(Color.YELLOW);

    }}

    public void mouseReleased(MouseEvent e)

    {

    System.out.println(" Mouse Released");

    }

    }

  • 8/22/2019 JAVA QP Programs Solutions

    36/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 36

    May 2011

    Q7)a) Final Keyword

    // May 11 ) q7 ) a

    class A

    {final void meth()

    {

    System.out.println("This is a final method.");

    }

    }

    class B extends A

    {

    void meth() { // ERROR! Can't override.

    System.out.println("Illegal!");

    }

    final class C

    {

    }

    class D extends C // Error cannot extends

    {

    }

    public class Final1

    {

    final int a=1;public void mod()

    {

    a++; // Cannot modify Error

    }

    }

  • 8/22/2019 JAVA QP Programs Solutions

    37/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 37

    May 2011

    Q6)b) Custom Exception

    public class OverDraftException extends Exception

    {

    String msg;

    public OverDraftException()

    {

    msg="Hi Siddhiraj ! Your account does not have sufficient amount";

    }

    public OverDraftException(String str)

    {

    msg = str;

    }public String getMessage()

    {

    return msg;

    }

    }

    class BankAcc

    {

    int bal;

    BankAcc(int b)

    {

    bal = b;

    }

    public void withdraw(int amt)

    {

    try

    {

    if((bal-amt)

  • 8/22/2019 JAVA QP Programs Solutions

    38/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 38

    }

    }

    public class BankImpl

    {

    public static void main(String args[]){

    BankAcc a1 = new BankAcc(1000);

    a1.withdraw(800);

    }

    }

  • 8/22/2019 JAVA QP Programs Solutions

    39/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 39

    May 2012

    Q1)b ) Cricketer Class

    // May 2012 Q1 . B ( 10 marks )

    import java.io.*;

    public class Cricketer{

    String name;

    int age;

    float batavg,bolavg;

    public void accept()

    {

    String temp="";

    BufferedReader br = new BufferedReader(new

    InputStreamReader(System.in));

    System.out.print("\n Enter the Name of Cricketer :");

    try

    {

    temp=br.readLine();

    name = temp;

    System.out.print("\n Enter the Age of "+name+"

    :");

    temp=br.readLine();

    age=Integer.parseInt(temp);

    System.out.print("\n Enter the Batting Average of"+name+" :");

    temp=br.readLine();

    batavg=Float.parseFloat(temp);

    System.out.print("\n Enter the Bowling Average of

    "+name+" :");

    temp=br.readLine();

    bolavg=Float.parseFloat(temp);

    }

    catch(Exception e )

    {System.out.println(e);

    }

    }

    public void Disp()

    {

  • 8/22/2019 JAVA QP Programs Solutions

    40/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 40

    System.out.println(name

    +"\t"+age+"\t"+batavg+"\t"+bolavg);

    }

    public static void sortBatAvg ( Cricketer obj[],int n)

    {for(int i=0;i

  • 8/22/2019 JAVA QP Programs Solutions

    41/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 41

    for(int i=0;i

  • 8/22/2019 JAVA QP Programs Solutions

    42/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 42

    May 2012

    Q5)a) Swing Application

    // Program for Displaying the contents of a Selected File

    // may 2012 )Q5 ) a ) ( 10 marks ), Solve Karun majja ali

    import javax.swing.*;import java.io.*;

    import java.awt.*;

    import java.awt.event.*;

    public class FileDisp extends JFrame implements ActionListener

    {

    JPanel p1;

    JTextArea t1;

    JButton b1;

    JLabel l1;

    JFileChooser chooser ;

    JScrollPane jsp;

    public FileDisp()

    {

    super("This is the Testing of ");

    p1 = new JPanel();

    t1 = new JTextArea(20,30);

    b1= new JButton ( "Select ");

    b1.addActionListener(this);l1= new JLabel ("Text of File ");

    jsp= new JScrollPane(t1);

    p1.add(l1);

    p1.add(jsp);

    p1.add(b1);

    setVisible(true);

    setSize(600,600);

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    getContentPane().add(p1);

    JFileChooser chooser = new JFileChooser();}

    public void actionPerformed(ActionEvent e)

    {

    String path="";

    System.out.println("\n Action Evenrt mein aa gaya ");

  • 8/22/2019 JAVA QP Programs Solutions

    43/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 43

    if(b1.equals(e.getSource()))

    {

    String str1="";

    JFileChooser chooser = new JFileChooser();

    int returnVal = chooser.showOpenDialog(this);if(returnVal ==

    JFileChooser.APPROVE_OPTION)

    {

    System.out.println("You chose to open this

    file: " + chooser.getSelectedFile().getPath());

    try

    {

    FileInputStream fstream = new

    FileInputStream(chooser.getSelectedFile().getPath());

    DataInputStream dr= new

    DataInputStream(fstream);

    System.out.println("\n File opened

    Successfully ");

    BufferedReader br = new

    BufferedReader (new InputStreamReader(dr));

    str1=br.readLine();

    while(str1!=null)

    {

    t1.append(str1);System.out.println(str1);

    t1.append("\n");

    str1=br.readLine();

    }

    }

    catch(Exception e2)

    {

    System.out.println(e2);

    }

    }}

    }

    public static void main(String args[])

    {

    FileDisp obj = new FileDisp();

  • 8/22/2019 JAVA QP Programs Solutions

    44/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ]

    Contact : [email protected] Page 44

    }

    }

  • 8/22/2019 JAVA QP Programs Solutions

    45/45

    Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem

    IV)(Refer Questions from Q.papers)

    May 2012

    Q6)a) Mouse Listener Interface

    Refer May 2011) Q6)a

    Best of

    luck

    Suggestions are welcomed.