Sei sulla pagina 1di 49

# Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge

, Bandra E ), Revision 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 Dedicated to My Parents and Teachers of Hiray College Hope you Guys find this useful in Java . This version includes questions with solutions Year : May 2009 Q1) b )Write a java program that enables the user to enter base and exponent through keyboard.In that write a recursive method power (base,exponent ). That when invoked returns baseexponent For eg , power (3,4) =3*3*3*3. Assume that exponent is an integer greater that or equal to 1 import java.util.*; public class Recursion { double power (int a,int b) { if(b==1) return a; else return (a*pow(a,(b-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.power(b,pow); System.out.print("Result :"+rs); } }

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

Page 1

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 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("");
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com Page 2

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1

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("/"); 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);

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

Page 3

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 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; 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);
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 4

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

Page 5

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

Page 6

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 Year : May 2009 Q3)b)Calculate Sum of 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 Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 Year : May 2009 Q4)b) : Passing Parameter to Applet embedded in html /*<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); g.drawString("Year of Establishment: "+year, 10, 40); } }

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

Page 8

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1

Year : May 2009 Q6)b) Program to explain multithreading with use of multiplication tables. Three threads must be defined . Each one must create one multiplication table , they are 5 table, 7 table , 13 table 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 9

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 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 10

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 Dec 2009 Q2)a) Write a java program to copy contents of one file to another and display the contents to console. 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 11

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 Dec 2009 Q2)b) Refer May 09 Q6)b Q3)b) Write a java Program to demonstrate any one type of Exception( Runtime Unchecked Exceptions) Solution: 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 )
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 12

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 { System.out.println(e2); } } public static void main(String a[]) { MyException e = new MyException(); } }

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

Page 13

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 Dec 2009 Q4 ) b ) What is interface . Explain with suitable java program Refer Dec 2010 Q3)a. Q6)b) What is Serialization ? Explain with suitable java Program 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 14

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 } 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 15

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 Dec 2009 Q7)a) Write a java program to arrange Minal , Pooja , Shama , Geeta ,Neeta , Mohan in ascending order Solution // I have done for command line arguments where any no of strings will be sorted 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 16

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 Dec 2009 Q7)b) Program to explain 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 17

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

Page 18

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 try { 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 19

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 May 2010 Q3)b :Write a program to count no of objects created for class using static member method Solution 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 20

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 May 2010 Q4)a)Create the following GUI using Swing

Solution: 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() { super("Printer"); 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");
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com Page 21

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

Page 22

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 Dec 2010 Q2)a) Write a applet that takes 4 digit number as a parameter & prints the number in reverse 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 23

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 Dec 2010 Q2)b) Write a program to accept 5 no from user and demonstrate arithmetic , numberformat , arrayoutofbounds excpetion. Solution: Refer Dec 2009 Q3)b

Q3)b Write a program to create two threads using thread class , name the threads & let the first thread print roll no and second print Percentage obtaimned Solution ( 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<5;i++) { if(!b) { try { // System.out.println ("Waiting.."+i+ " BOOL: "+b); wait(); } catch(Exception e ) { System.out.println(e); } }
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 24

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 System.out.println("Roll = "+roll[i]); b=false; notify(); } } public synchronized void printPer() { 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("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();
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 25

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 } } class PerThread extends Thread { Student s; public PerThread(Student s) { this.s=s; 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 26

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

Page 27

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 System.out.println ("child interrupted"); } 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);
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 28

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 Thread t2 = new Thread(fr); t.setName("AAA"); 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 29

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 Dec 2010 Q5)a)

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

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 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 Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 Dec 2010 Q5)b) What is Swing ? Explain with suitable program any 5 swing components to create sophisticated GUI Solution Refer May 2010 Q4)a) Q6 ) b) WAP to create table CAR ( chasis no (primary key), model , mf_date, price ) & insert 5 records then update price of each record two times & then delete the table Solution ( New Included not in previous doc, for all programs of jdbc ) import java.sql.*; public class CarTable { Connection conn; PreparedStatement pstmt; Statement stmt; ResultSet rs; String str; public CarTable() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn= DriverManager.getConnection("jdbc:odbc:MyDsn"); stmt= conn.createStatement(); } catch(Exception e) { System.out.println(e); } } public void Create() { try { str= "Create TABLE Car (chasis integer, Model char(10),Mf_date datetime, Price float)"; boolean b=stmt.execute(str); System.out.println("Table Created Successfully "); }
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 32

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 catch(Exception e) { System.out.println(e); } } public void Destroy() { try { str= "drop TABLE Car"; boolean b=stmt.execute(str); System.out.println("Table Dropped Successfully "); } catch(Exception e) { System.out.println(e); } } public void disp() { try { str="Select * from car "; ResultSet rs=stmt.executeQuery(str); ResultSetMetaData rsmd = rs.getMetaData(); for(int i=1;i<=rsmd.getColumnCount();i++) { System.out.print(rsmd.getColumnName(i)+"\t"); } System.out.println("\n-------------------------------------------------------"); while(rs.next()) { for(int i=1;i<=rsmd.getColumnCount();i++) { System.out.print(rs.getString(i)+"\t"); } System.out.println(); } }
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 33

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 catch(Exception e ) { System.out.println("In Select "+e); } } public void update() { try { pstmt=conn.prepareStatement("Update car set Price = 2*Price"); int f=pstmt.executeUpdate(); if(f>0) System.out.println(f+" Rows Updated"); } catch(Exception e ) { System.out.println("In Update "+e); } } public void insert() { try { pstmt=conn.prepareStatement("insert into car values (?,?,?,?)"); for(int i=1;i<=5;i++) { pstmt.setInt(1,i); pstmt.setString(2,"AA"+i); pstmt.setString(3,"7-"+i+"-13"); pstmt.setFloat(4,(i*10000)); int f=pstmt.executeUpdate(); if(f>0) System.out.println("Row Inserted"); } } catch(Exception e ) { System.out.println("In insert "+e);
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 34

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 } } public void delete() { try { pstmt=conn.prepareStatement("delete from car"); int f=pstmt.executeUpdate(); if(f>0) System.out.println(f+ " Rows Deleted"); } catch(Exception e ) { System.out.println("In insert "+e); } } public static void main(String hehehe[]) { try { CarTable obj =new CarTable(); System.out.println("Creating Table "); obj.Create(); System.out.println("Inserting 5 values in Table "); obj.insert(); System.out.println("Displaying values in Table "); obj.disp(); System.out.println("Updating Price in table"); obj.update(); System.out.println(" Table after Updating Price"); obj.disp(); System.out.println(" Deleting values in table "); obj.delete(); System.out.println(" Displaying after delete "); obj.disp(); System.out.println(" Removing Table from Database "); obj.Destroy(); } catch(Exception e )
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 35

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 { System.out.println("In main "+e); } } }

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

Page 36

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 May 2011 Q2)a) Write a program to Demonstrate Thread Intercommunication Solution: 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; } notify(); } } }
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 37

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 May 2011 Q2)b) Wrapper Classes Solution Refer May 2009 Q7)b Q4)a) (GUI Using 5 components ) Solution : Refer May 2010 Q4)a Q5)a) WAP in java to demonstrate any two java unchecked Runtime Exception Subclasses Solution Refer Dec 2009 Q3)b Q5)b) WAP in java to show Serialization and Deserialization Solution Refer Dec 2009 Q6)b Q6)a) Write a applet program to handle mouse events /* <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 ");
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 38

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 } 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"); 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) {
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 39

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 System.out.println(" Mouse Released"); } }

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

Page 40

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 May 2011 Q7)a) Write a java Code to demonstrate use of Final Keyword 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 41

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 Dec 2011 Q6)b ) What is Exception ?Explain steps to create your own Exception with suitable example Solution: Custom Exception Definition 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; } } Implementation of Exception 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) {
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 42

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 System.out.println(od.getMessage()); } System.out.println("Balance: "+bal); } } 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 43

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 May 2012 Q1)b ) Design a cricketer Class cricketer with name, age, batting average , bowling average.Create an array of cricketer objects and sort it on batting average Solution :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 44

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 { 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 45

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 } 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 46

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 May 2012 Q5)a) Design a swing application to select a file and display its contents in a text area. On click of select button , program should display the file dialog , and on click of OK Button , the contents of selected file should be displayed in text Area Solution 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="";
Created By : Siddhiraj Ramesh Pantoji [ Hiray College, Bandra(East) ] Contact : siddhirajpantoji@gmail.com

Page 47

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 System.out.println("\n Action Evenrt mein aa gaya "); 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 48

Java Paper Program Solutions from May 2009 to May 2012 (Mumbai Univ) (Siddhiraj Pantoji , Hiray Collge , Bandra E ), Revision 1 May 2012 Q6)a) What is Mouse Listener Interface? Write an applet to set the background color to yellow on left click of mouse and to red on right click of mouse . Solution Refer May 2011) Q6)a

Best of luck
Suggestions are welcomed. Thanks to my friends and faculties. THANK YOU , MISSION MCA TEAM

Page 49