1. Define constructor? Explain constructor overloading with suitable example.
More than one constructor with different signature in a class is called constructor overloading.
public class Student{
int roll, marks;
String name;
Student(int r, String n, int m){
roll = r;
name = n;
marks = m;
}
Student(int r, int m, String n){
roll = r;
name = n;
marks = m;
}
void displaInfo(){
System.out.println(roll+"\t"+name+"\t"+marks);
}
public static void main(String args[]){
Student s1 = new Student(1, "Ram", 56);
Student s2 = new Student(2, 89, "Shyam");
System.out.println("Roll \t Name \t Marks");
s1.displaInfo();
s2.displaInfo();
}
}
2. How class is different then object ? Write a program to create Employee class with data members employee id, employee name, and salary. Also put necessary member function for reading and display objects. Create two objects of employee class and then read and display their details.
Classes and objects are the fundamental components of OOP's. A class is an entity that determines how an object will behave and what the object will contain. In other words, it is a blueprint or a set of instruction to build a specific type of object.
import java.util.Scanner; class Employee{ int id, salary; String name; public void getData(){ Scanner in = new Scanner(System.in); System.out.println("\nEnter the Following Info of Employee"); System.out.print("Employee ID : "); id = in.nextInt(); System.out.print("Employee Name : "); name = in.next(); System.out.print("Employee Salary : "); salary = in.nextInt(); } public void displayData(){ System.out.println("\n***Employee Details***"); System.out.println("ID = "+id); System.out.println("Name = "+name); System.out.println("Salary = "+salary); } public static void main(String args[]){ Employee em1 = new Employee(); Employee em2 = new Employee(); em1.getData(); em1.displayData(); em2.getData(); em2.displayData(); } }
3. Write a program to create a form first number second number, and result, and two buttons and add subtract. Handle the events such that clicking on and button computes addition and clicking on subtract performs subtraction.
Solution:import java.awt.*; import javax.swing.*; import java.awt.event.*; class Calc extends JFrame implements ActionListener{ JLabel l1, l2, l3; JTextField t1, t2, t3; JButton b1, b2; Calc(){ super("Handling Action Event"); l1 = new JLabel("Enter First Number"); l2 = new JLabel("Enter Second Number"); l3 = new JLabel("Result"); t1 = new JTextField(10); t2 = new JTextField(10); t3 = new JTextField(10); b1 = new JButton("Add"); b2 = new JButton("Subtract"); setLayout(new FlowLayout(FlowLayout.LEFT,90,10)); add(l1); add(t1); add(l2); add(t2); add(l3); add(t3); add(b1); add(b2); b1.addActionListener(this); b2.addActionListener(this); setSize(300, 300); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent ae){ int x, y, z; x=Integer.parseInt(t1.getText()); y=Integer.parseInt(t2.getText()); if(ae.getSource() == b1){ z = x + y; }else{ z = x - y; } t3.setText(String.valueOf(z)); } public static void main(String args[]){ new Calc(); } }
4. What are different types of exceptions? Explain use of try.....catch and finaly with example.
Solution:Exceptions are the unwanted errors or bugs or events that restrict the normal execution of a program. In such cases we get a system generated error message. An error message is displayed on the screen. There are mainly two types of exceptions: They are :
- Built-In Exception
- User-Defined Exception
- Arithmetic Exception
- ArrayIndexOutOfBoundsException
- ClassNotFoundException
- FileNotFoundException
- IOException etc.
Use of Try ... Catch and Finally
- A try block is used to enclose the code that might throw an exception. It must be used within the method. Java try block must be followed by either catch or finally block.
- Java catch block is used to handle the Exception. It must be used after the try block only.
- Java finally block is a block that is used to execute important code such as closing connection, stream etc. Java finally block is always executed whether exception is handled or not. Java finally block follows try or catch block.
public class TestFinally{ public static void main(String args[]){ try{ int x = 20/0; System.out.println("Quoitent = "+x); } catch(ArithmeticException e){ System.out.println("e.getMessage"); } finally{ System.out.println("Finally block is always executed"); } System.out.println("Rest of the Codes .... "); } }
5. Write down the the steps for writing client and server program by using UDP?
SolutionUDP is a Datagram, It is a connection less ,independent , self contained protocol. Bulk Data are send and receive as packets from using UDP. Steps to write client and server program by using UDP are as follows:
Steps for Writing Client Program
Step1: Get Datagram Socket
DatagramSocket ds=new DatagramSocket(port)
Step2: Send Request
DatagramPacket p=new DatagramPacket(byte[],length, inetaddress,port)
ds.send(p)
Step3: Get Response
p=new DatagramPacket(buf,buf.length)
ds.receive(p)
Step4: Display Response
String s=new String(p.getData());
System.out.println(“Server:”+s);
Step 5: Close the Socket
ds.close();
Steps for Writing Server Program
Step1: Get Datagram Socket
DatagramSocket ds=new DatagramSocket(port)
Step2: Receive Request
byte[] buf=new byte[256]
DatagramPacket p=new DatagramPacket(buf,buf.length)
ds.receive(p)
Step3: Send Response
String s=new String(“Hello Client”);
buf=s.getBytes()
ds.send(p)
Step 4: Close the Socket
ds.close();
5. Write a Java Swing Program to design the following menu bar.
Solution:
import javax.swing.*; class MenuDemo extends JFrame{ JMenuBar mb; JMenu file, edit, format; JMenuItem open, save, save_as, exit, undo, redo; MenuDemo(){ setTitle("Menu Bar"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mb=new JMenuBar(); file=new JMenu("File"); edit=new JMenu("Edit"); format=new JMenu("Format"); open=new JMenuItem("Open"); save=new JMenuItem("Save"); save_as=new JMenuItem("Save As"); exit=new JMenuItem("Exit"); undo=new JMenuItem("Undo"); redo=new JMenuItem("Redo"); file.add(open); file.add(save); file.add(save_as); file.add(exit); edit.add(undo); edit.add(redo); mb.add(file); mb.add(edit); mb.add(format); setJMenuBar(mb); setSize(300, 200); setVisible(true); } public static void main(String args[]){ new MenuDemo(); } }
Post a Comment