JAVA GTU PEPAR SOLUTION
(b) Declare a class called employee having employee_id and employee_name as members. Extend class employee to have a subclass called salary having designation and monthly_salary as members. Define following:
- Required constructors
- A method to find and display all details of employees drawing salary more than Rs. 20000/-.
- Method main for creating an array for storing these details given as command line arguments and showing usage of above methods.
Question 1
(a) Explain final and super by giving examples.class H { final void show() { System.out.println("Final Method. Cant Override"); } } final class E extends H { //void show() //{ // Can Not Override Final Method from H //} } //class J extends E //{ // Can Not Subclass Of E //} public class FinalKeywordSimpleExampleH2E { public static void main(String args[]) { H h = new H(); h.show(); } }
class employee { int employee_id; String employee_name; employee(int employee_id, String employee_name) { this.employee_id = employee_id; this.employee_name = employee_name; } } class salary extends employee { String designation; double monthly_salary; salary(int employee_id, String employee_name,String designation,double monthly_salary) { super(employee_id,employee_name); this.designation = designation; this.monthly_salary = monthly_salary; } void show() { System.out.println("Employee Details : "); System.out.println("Id = "+this.employee_id); System.out.println("Name = "+this.employee_name); System.out.println("Designation = "+this.designation); System.out.println("Salary = "+this.monthly_salary); } } class SuperKeywordSimpleEampleH2E { public static void main(String args[]) { salary s = new salary(1,"Shruti Patel","HR Manager",30000); s.show(); } }
(b) Declare a class called employee having employee_id and employee_name as members. Extend class employee to have a subclass called salary having designation and monthly_salary as members. Define following:
- Required constructors
- A method to find and display all details of employees drawing salary more than Rs. 20000/-.
- Method main for creating an array for storing these details given as command line arguments and showing usage of above methods.
class employee { int employee_id; String employee_name; } class salary extends employee { String designation; double monthly_salary; salary() {} salary(int employee_id,String employee_name,String designation,double monthly_salary) { this.employee_id = employee_id; this.employee_name = employee_name; this.designation = designation; this.monthly_salary = monthly_salary; } void findDataAbove20000(String arr[][]) { for(int i=0;i<arr.length;i++) { if( (Double.parseDouble(arr[i][3]) ) > 20000 ) { System.out.println("Employee Details : "); System.out.println("Id = "+arr[i][0]); System.out.println("Name = "+arr[i][1]); System.out.println("Designation = "+arr[i][2]); System.out.println("Salary = "+arr[i][3]); } } } } public class employeeClassExampleInJavaH2E { public static void main(String args[]) { salary s1[] = new salary[4]; String dataArr[][]; s1[0] = new salary(1,"Jaydip-Panchal","Finance-Manager",18000); s1[1] = new salary(2,"Pratik-Patel","HR-Manager",29000); s1[2] = new salary(3,"Arpan-Patel","Project-Manager",25000); s1[3] = new salary(); try { s1[3].employee_id = Integer.parseInt(args[0]); s1[3].employee_name = args[1]; s1[3].designation = args[2]; s1[3].monthly_salary = Double.parseDouble(args[3]); //System.out.println(args[0]+args[1]+args[2]+args[3]); } catch(NumberFormatException e) { System.out.println("Exception : "+ e); } dataArr = new String[s1.length][4]; for(int i=0;i<s1.length;i++) { dataArr[i][0] = String.valueOf(s1[i].employee_id); dataArr[i][1] = s1[i].employee_name; dataArr[i][2] = s1[i].designation; dataArr[i][3] = String.valueOf(s1[i].monthly_salary); } s1[3].findDataAbove20000(dataArr); } }
Question 2
(a) Explain method overriding and method overloading with the help of examples.class H2EA { int a,b; H2EA(int i,int j) { a=i; b=j; } void inside() { System.out.println("Inside H2EA"); } int Multiplication() { return a*b; } } class H2EB extends H2EA { int c; H2EB(int i,int j,int k) { super(i,j); c=k; } void inside() { System.out.println("Inside H2EB"); } int Multiplication(int x) { return x*c; } } class methodOverloadingAndOverridingDifferenceH2E { public static void main(String args[]) { H2EB Obj = new H2EB(5,6,7); Obj.inside(); // Method Overriding System.out.println("Multiplication = "+Obj.Multiplication()); // Method Overloading System.out.println("Multiplication = "+Obj.Multiplication(8)); // Method Overloading } }(b) Write a method for computing xy by doing repetitive multiplication. x and y are of type integer and are to be given as command line arguments. Raise and handle exception(s) for invalid values of x and y. Also define method main. Use finally in above program and explain its usage.class powerExampleInJavaH2E { public static void main(String args[]) { int x,y,mul; try { x = Integer.parseInt(args[0]); y = Integer.parseInt(args[1]); mul = 1; for(int i=0;i<y;i++) mul = mul*x; System.out.println(x+"^"+y+" : " + mul); } catch(NumberFormatException e) { System.out.println("Exception : "+e); } } }(b) Explain package and interface by giving examples.// Package Example class packageExampleImplementH2E { public static void main(String args[]) { int x; help2engg.packageExampleH2E pe = new help2engg.packageExampleH2E(); pe.show(); pe.setMember(15); x = pe.getMember(); System.out.println("Member of Inside Package : "+x); } } // The file packageExampleH2E.java must be in help2engg folder package help2engg; public class packageExampleH2E { String msg = "Inside Package Of Help2Engg"; int x; public void show() { System.out.println("Message From Inside Package : "+msg); } public void setMember(int x) { this.x = x; } public int getMember() { return x; } }//interface Example interface interfaceClass { void interfaceMethod(int p); } class interfaceImplement implements interfaceClass { public void interfaceMethod(int p1) { System.out.println("Interface Class Called with "+p1); } public void nonInterfaceMethod(int p1) { System.out.println("Interface Implement Class Called with "+p1); } } class interfaceSimpleExampleH2E { public static void main(String args[]) { interfaceImplement IIO = new interfaceImplement(); IIO.interfaceMethod(15); IIO.nonInterfaceMethod(15); } }Question 3
(a) Explain inner class and working of concatenation operator + by giving examples.//Inner Class Example class outer { int o_x = 52; void test() { Inner inner = new Inner(); inner.show(); } class Inner { void show() { System.out.println("Show : X of Outer Class : "+o_x); } } } class innerClassExampleH2E { public static void main(String args[]) { outer outObj = new outer(); outObj.test(); } }//concatenation operator + public class concatenationOperatorExampleH2E { public static void main(String args[]) { String str1 = "Welcome to : "; String str2 = "Help2Engg"; System.out.println(str1+str2); System.out.println("GTU's No.1 Website is : "+str2); } }Question 4
(a) Explain various methods called during execution cycle of the applet.
import java.awt.*; import java.applet.*; /* <applet code="AppletLifeCycle" width=300 height=300> </applet> */ public class AppletLifeCycle extends Applet { String msg1,msg2,msg3,msg4; // called first public void init() { // Initialization msg1="init section begins"; } // called second public void start() { // start or resume execution msg2="start section"; } // called when applet stopped public void stop() { // suspends execution msg3="stop section"; } // called when applet public void destroy() { // execute shutdown activities msg4="destroy section"; } public void paint(Graphics g) { // redisplay content of window g.drawString("Demo for basic methods of applet",20,40); g.drawString(msg1,20,80); g.drawString(msg2,20,90); g.drawString(msg3,20,100); g.drawString(msg4,20,120); } }(b) Write a program to create a frame with exit capabilities. Handle events for mouse pressed, mouse released, mouse clicked and mouse dragged by displaying appropriate message describing the event at the coordinates where the event has taken place.
import java.awt.*; import java.awt.event.*; public class HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E extends Frame { String msg = ""; int mouseX=30; int mouseY=30; public HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E() { addMouseListener(new MyMouseAdapter(this)); addMouseMotionListener(new MyMouseMotionAdapter(this)); addWindowListener(new MyWindowAdapter()); } public void paint(Graphics g) { g.drawString(msg,mouseX,mouseY); } public static void main(String[] args) { HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E f = new HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E(); f.setSize(new Dimension(300,300)); f.setTitle("HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E"); f.setVisible(true); } } class MyMouseAdapter extends MouseAdapter { HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E f1; public MyMouseAdapter(HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E f1) { this.f1 = f1; } public void mousePressed(MouseEvent me) { f1.mouseX = me.getX(); f1.mouseY = me.getY(); f1.msg = "Mouse Down at "+f1.mouseX+","+f1.mouseY; f1.repaint(); } public void mouseClicked(MouseEvent me) { f1.mouseX = me.getX(); f1.mouseY = me.getY(); f1.msg = "Mouse Clicked at "+f1.mouseX+","+f1.mouseY; f1.repaint(); } public void mouseReleased(MouseEvent me) { f1.mouseX = me.getX(); f1.mouseY = me.getY(); f1.msg = "Mouse Released at "+f1.mouseX+","+f1.mouseY; f1.repaint(); } } class MyMouseMotionAdapter extends MouseMotionAdapter { HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E f1; public MyMouseMotionAdapter(HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E f1) { this.f1 = f1; } public void mouseDragged(MouseEvent me) { f1.mouseX = me.getX(); f1.mouseY = me.getY(); f1.msg = "Mouse Dragged at "+f1.mouseX+","+f1.mouseY; f1.repaint(); } } class MyWindowAdapter extends WindowAdapter { public void windowClosing(WindowEvent we) { System.exit(0); } }(b) Write a complete program to create a frame for providing GUI to implement a stack for storing integer numbers. There are two buttons called PUSH & POP and a text field. Clicking of button PUSH pushes the number entered in the text field onto the stack. The click of button POP pops an element from the stack and displays that in the text field.
import java.awt.*; import java.awt.event.*; import javax.swing.*; class stackUsingJFrameInJavaPushPopExampleH2E { JLabel lbl; JButton pop,push; JTextField text; String stackArr[] = new String[10]; int top=-1; CopyOfstackUsingJFrameInJavaPushPopExampleH2E() { JFrame frm = new JFrame("Stack Implementation In Frame - Help2Engg"); frm.setLayout(new FlowLayout()); frm.setSize(300,300); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); push = new JButton("PUSH"); pop = new JButton("POP"); text = new JTextField(12); push.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { ++top; if(top < 10) { stackArr[top] = text.getText(); lbl.setText("Push Element : "+text.getText()); } else lbl.setText("Stack is Full"); } } ); pop.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { if(top >= 0) { lbl.setText("Pop Element : "+stackArr[top]); --top; } else lbl.setText("Stack is Empty"); } } ); frm.add(text); frm.add(push); frm.add(pop); lbl = new JLabel("Press a Button"); frm.add(lbl); frm.setVisible(true); } public static void main(String args[]) { SwingUtilities.invokeLater(new Runnable() { public void run() { new CopyOfstackUsingJFrameInJavaPushPopExampleH2E(); } } ); } }Question 5
(a) It is required to have total two threads, both capable of acting as a produce as well as a consumer. If first thread acts as a producer then, the second thread becomes the consumer and vice-versa. They communicate with each other through a buffer, storing one integer number. One of the threads initiates the communication by sending 1 to the other thread. The second thread, on receiving 1 sends 2 to the first thread. On receiving 2, the first thread sends three integer numbers, one by one to the second thread. The second thread consumes the numbers by displaying them. Both threads terminate after that. Note that both threads must be capable of initiating the communication. Write complete multi-threaded program to meet above requirements.class CI { int n; boolean valueSet = false; synchronized int recieve() { while(!valueSet) try { wait(); } catch(InterruptedException e) { System.out.println("InterruptedException caught"); } System.out.println("Recieve : " + n); valueSet = false; notify(); return n; } synchronized void send(int n) { while(valueSet) try { wait(); } catch(InterruptedException e) { System.out.println("InterruptedException caught"); } this.n = n; valueSet = true; System.out.println("Send : "+n); notify(); } } class Producer implements Runnable { CI ci; Producer(CI ci) { this.ci = ci; new Thread(this,"Producer").start(); } public void run() { int i=0; while(true) { ci.send(i++); } } } class Consumer implements Runnable { CI ci; Consumer(CI ci) { this.ci = ci; new Thread(this,"Consumer").start(); } public void run() { while(true) { ci.recieve(); } } } class multithreadedProgramProducerConsumerExampleH2E { public static void main(String args[]) { CI ci = new CI(); new Producer(ci); new Consumer(ci); System.out.println("Press Control-C to Stop."); } }(b) Explain utility class Hashtable and instanceof operator by giving examples.// utility class Hashtable Example import java.io.*; import java.util.*; class utilityClassHashtableExampleH2E { public static void main(String args[]) throws IOException { Hashtable<String,Integer> ht = new Hashtable<String,Integer>(); ht.put("Jaydip",88); ht.put("Arpan",89); ht.put("Praik",90); System.out.println("The Students names : "); Enumeration e = ht.keys(); while(e.hasMoreElements()) System.out.println(e.nextElement()); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter Student Name : "); String name = br.readLine(); name = name.trim(); Integer mark = ht.get(name); if(mark != null) { int mv = mark.intValue(); System.out.println(name + " mark : "+mv); } else System.out.println("Student Not Found"); } }// Instanceof Operator Example class A { int i,j; } class B { int i,j; } class C extends A { int k; } class D extends A { int k; } class instanceofOperatorExampleH2E { public static void main(String args[]) { A a = new A(); B b = new B(); C c = new C(); D d = new D(); if(a instanceof A) System.out.println("a is instance of A"); if(b instanceof B) System.out.println("b is instance of B"); if(c instanceof C) System.out.println("c is instance of C"); if(d instanceof D) System.out.println("d is instance of D"); System.out.println(); A ob; ob = d; System.out.println("ob is now refers to d"); System.out.println(); if(ob instanceof D) System.out.println("ob is instance of D"); ob = c; System.out.println(); System.out.println("ob is now refers to C"); System.out.println(); if(ob instanceof D) System.out.println("ob can cast to D"); else System.out.println("ob can not cast to D"); } }