Vous êtes sur la page 1sur 45

Java Mumbai University Q.

Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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 2009 Q1) b ) import java.util.*; public class Recursion { double exp(double a,int b) { if(b<0) return(1/exp(a,-b)); else if(b>0) 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); } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 1

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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("/");
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 2

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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;
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 3

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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);
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 4

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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");
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 5

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) //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"); } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 6

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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<=n;i++) d=d+pow(x,i); System.out.println("Sum of Series = "+d); } public static void main(String a[]) { Series s= new Series(); s.show(); } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 7

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) Year : May 2009 Q4)b) : Passing Parameter to Applet

/*<applet code="DemoParam" width=400 height=400>

<param name="Name" value="Hiray College"> <param name="Year" value="2001">

</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);
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 8

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) g.drawString("Year of Establishment: "+year, 10, 40); } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 9

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

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<=10;i++) { System.out.print(" "+(i*no)); } System.out.println("_______________________"); } public static void main(String args[])
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 10

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) { 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(); } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 11

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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) { } } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 12

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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<5;i++) { System.out.print("Enter no "+(i+1)+" :"); a[i]=sc.next(); //b[i]=Integer.parseInt(a[i]); } } catch(ArrayIndexOutOfBoundsException e ) { System.out.println(e); } try { int x=5/0; } catch(ArithmeticException e1 ) { System.out.println(e1); } try { int i=Integer.parseInt("1a"); } catch(NumberFormatException e2 ) { System.out.println(e2);
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 13

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) } } public static void main(String a[]) { MyException e = new MyException(); } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 14

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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 = new FileOutputStream("MyObject.dat"); ObjectOutputStream os = new ObjectOutputStream(fs); os.writeObject(mo); os.close();
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 15

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) } 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()); } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 16

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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<l;i++) arr[i]=args[i]; for(i=0;i<l;i++) { for(j=i+1;j<l;j++) { if(arr[i].compareTo(arr[j])>0) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } System.out.println("\n Arranged order is :"); for(i=0;i<l;i++) System.out.print(arr[i]+"\t"); } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 17

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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); } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 18

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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 {
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 19

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) Thread.sleep(1000); }catch(InterruptedException e) { e.printStackTrace(); } } else { } } public synchronized void run() { for(int i=0;i<5;i++) { decrement(); } } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 20

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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(); } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 21

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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();
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 22

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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(); } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 23

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) Dec 2010 Q2)a) import java.awt.*; import java.applet.*; /*<applet code="AppReverse.class" width=400 height=400> <param name="No" value="1234"> </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); } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 24

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) Dec 2010 Q2)b) Refer Dec 2009 Q3)b Q3)b Two Thread Printing class Student {

( Joint Effort by me and Tushar Fulmali )

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<5;i++) { if(!b) { try { // System.out.println ("Waiting.."+i+ " BOOL: "+b); wait(); } catch(Exception e ) { System.out.println(e); } } System.out.println("Roll = "+roll[i]); b=false; notify(); } } public synchronized void printPer() { for(int i=0;i<5;i++)
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 25

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) { 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;
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 26

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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); } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 27

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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"); }
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 28

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

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<5;i++) { System.out.println("Run By : -"+Thread.currentThread().getName()); } } } public class TestThread { public static void main(String args[]) { FooRun fr = new FooRun(); Thread t = new Thread(fr); Thread t1 = new Thread(fr); Thread t2 = new Thread(fr); t.setName("AAA");
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 29

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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"); } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 30

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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(); } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 31

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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<100;i++) { total += i; }
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com Page 32

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) notify(); } } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 33

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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)b Q6)a) Mouse Events in Applet /* <applet code="MouseEvent1.class" width=400 height=400> </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");
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 34

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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"); } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 35

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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 } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 36

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

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)<500) throw new OverDraftException(); else bal = bal - amt; } catch(OverDraftException od) { System.out.println(od.getMessage()); } System.out.println("Balance: "+bal);
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com Page 37

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) } } public class BankImpl { public static void main(String args[]) { BankAcc a1 = new BankAcc(1000); a1.withdraw(800); } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 38

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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() {
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 39

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) System.out.println(name +"\t"+age+"\t"+batavg+"\t"+bolavg); } public static void sortBatAvg ( Cricketer obj[],int n) { for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(obj[i].batavg>obj[j].batavg) { Cricketer t= obj[i]; obj[i]=obj[j]; obj[j]=t; } } } } public static void main( String args[]) { int n=0; String t=""; System.out.print("\n Enter the no of Cricketers :"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { t= br.readLine(); } catch( Exception e) { System.out.println(e); } n=Integer.parseInt(t); Cricketer obj[]= new Cricketer[n]; for(int i=0;i<n;i++) { obj[i]= new Cricketer(); }
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 40

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) for(int i=0;i<n;i++) { obj[i].accept(); } System.out.println("Name \t Age \t Bat Avg \t Bol Avg"); for(int i=0;i<n;i++) { obj[i].Disp(); } sortBatAvg(obj,n); System.out.println("After Sorting :"); System.out.println("Name \t Age \t Bat Avg \t Bol Avg"); for(int i=0;i<n;i++) { obj[i].Disp(); } } }

Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 41

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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 ");
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 42

Java Mumbai University Q. Paper (Programs) from May 2009 to May 2012 ( for MCA- Sem IV)(Refer Questions from Q.papers) 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();
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 43

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 : siddhirajpantoji@gmail.com

Page 44

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.
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com Page 45

Vous aimerez peut-être aussi