0% found this document useful (0 votes)
3 views9 pages

Summer Java

The document outlines key features of Java, including its platform independence, object-oriented nature, security, and robustness. It also discusses concepts such as static imports, exceptions, type casting, and differences between AWT and Swing. Additionally, it includes example programs demonstrating exception handling, garbage collection, GUI components, JDBC, and socket programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Summer Java

The document outlines key features of Java, including its platform independence, object-oriented nature, security, and robustness. It also discusses concepts such as static imports, exceptions, type casting, and differences between AWT and Swing. Additionally, it includes example programs demonstrating exception handling, garbage collection, GUI components, JDBC, and socket programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

a) List any four features of Java.

1. Platform Independent: Java code can run on any machine that


has a Java Virtual Machine (JVM).
2. Object-Oriented: Everything in Java is an object, following
concepts like inheritance and polymorphism.
3. Secure: Java provides a secure environment by running code
within a "sandbox" and avoiding explicit pointers.
4. Robust: It emphasizes compile-time and runtime error checking
and has strong memory management (Garbage Collection).
b) Define static import. Give its syntax.
Definition: Static import allows members (fields and methods)
defined in a class as static to be used in Java code without
qualifying them with the class name.
Syntax:
import static [Link];
(Or to import all static members: import static
[Link].*;)
c) Define exceptions. List its types.
Definition: An exception is an unwanted or unexpected event that
occurs during the execution of a program (at runtime) and disrupts
the normal flow of instructions.
Types:
1. Checked Exceptions: Checked at compile-time (e.g.,
IOException).
2. Unchecked Exceptions: Checked at runtime (e.g.,
ArithmeticException).
d) Define type casting. List its types.
Definition: Type casting is the process of converting a value from
one primitive data type to another.
Types:
1. Widening Casting (Implicit): Converting a smaller type to a
larger type size (e.g., int to double).
2. Narrowing Casting (Explicit): Converting a larger type to a
smaller type size (e.g., double to int).
e) Write any two differences between AWT and Swing.
Feature
AWT (Abstract Window Toolkit)
Swing
Platform
Platform Dependent (Heavyweight)
Platform Independent (Lightweight)
Components
Uses OS native components
Provides its own set of components
f) Define proxy server, reserved socket.
Proxy Server: An intermediary server that separates
end-user clients from the destinations they browse,
providing security, administrative control, and
caching services.
Reserved Socket: These are well-known ports
(ranging from 0 to 1023) reserved by the system for
specific standard services like HTTP (80), FTP (21),
and Telnet (23).
g) Write any four methods of Statement.
executeQuery(String sql): Used for SELECT
statements.
executeUpdate(String sql): Used for INSERT,
UPDATE, or DELETE statements.
execute(String sql): Used when the query might
return more than one result set.
close(): Used to immediately release a Statement
object's database and JDBC resorces.
Q2 (a) Program: Exception if age < 18
import [Link].*;
class AgeException extends Exception {
AgeException(String msg) {
super(msg);
}
}
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter age: ");
int age = [Link]();
try {
if (age < 18) {
throw new AgeException("Age is less than 18");
}
[Link]("Valid age");
} catch (AgeException e) {
[Link]([Link]());
}
}
}
Q2 (b) Garbage Collection in Java
Garbage Collection is the process of automatically
removing unused objects from memory.
Managed by JVM
Frees heap memory
No manual deallocation needed
[Link]() can request GC (not guaranteed)
Q2 (c) Login Form Program
import [Link].*;
import [Link].*;
class LoginForm extends Frame implements ActionListener {
TextField t1, t2;
Label l;
LoginForm() {
setLayout(new FlowLayout());
add(new Label("Username"));
t1 = new TextField(15);
add(t1);
add(new Label("Password"));
t2 = new TextField(15);
[Link]('*');
add(t2);
Button b = new Button("Login");
add(b);
l = new Label();
add(l);
[Link](this);
setSize(300,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if([Link]().equals("admin") && [Link]().equals("123")) {
[Link]("Login Successful");
} else {
[Link]("Login Failed");
}
}
public static void main(String[] args) {
new LoginForm();
}
}
Q3 (a) Constructor with Types
Constructor = special method used to initialize objects.
Types:
[Link] Constructor
[Link] Constructor

class Demo {
int x;

Demo() { x = 0; } // default
Demo(int a) { x = a; } // parameterized
}

Q3 (b) Two Threads (Even & Odd)

class A extends Thread {


public void run() {
for(int i=2;i<=50;i+=2) {
[Link]("Even: " + i);
try { [Link](100); } catch(Exception e){}
}
}
}

class B extends Thread {


public void run() {
for(int i=1;i<=50;i+=2) {
[Link]("Odd: " + i);
try { [Link](100); } catch(Exception e){}
}
}
}
public class Test {
public static void main(String[] args) {
new A().start();
new B().start();
}
}

}
Q3 (d) JComboBox Program
import [Link].*;
import [Link].*;
class ComboDemo {
public static void main(String[] args) {
JFrame f = new JFrame();
String states[] = {"MH","GJ","RJ","UP","MP"};
JComboBox cb = new JComboBox(states);
JTextField t = new JTextField(10);
[Link](e -> {
[Link]((String)[Link]());
});
[Link](cb);
[Link](t);
[Link](new [Link]());
[Link](300,200);
[Link](true);
}
}
Q4 (a) Add Two Numbers (GridLayout)
import [Link].*;
import [Link].*;
import [Link].*;
class AddDemo {
public static void main(String[] args) {
JFrame f = new JFrame();
[Link](new GridLayout(4,2));
JTextField t1 = new JTextField();
JTextField t2 = new JTextField();
JTextField t3 = new JTextField();
JButton b = new JButton("Add");
[Link](new JLabel("Num1"));
[Link](t1);
[Link](new JLabel("Num2"));
[Link](t2);
[Link](b);
[Link](t3);
[Link](e -> {
int a = [Link]([Link]());
int c = [Link]([Link]());
[Link](""+(a+c));
});
[Link](300,200);
[Link](true);
}
}
Q4 (c) Read from URL
import [Link].*;
import [Link].*;
class URLDemo {
public static void main(String[] args) throws Exception {
URL u = new URL("[Link]
BufferedReader br = new BufferedReader(
new InputStreamReader([Link]())
);
String line;
while((line = [Link]()) != null) {
[Link](line);
}
}
}
Q4 (d) Types of JDBC Drivers
1. Type 1 – JDBC-ODBC Bridge
2. Type 2 – Native API
3. Type 3 – Network Protocol
4. Type 4 – Thin Driver (most used)
Q4 (e) KeyListener Program
import [Link].*;
import [Link].*;
class KeyDemo extends Frame implements KeyListener {
TextField t;
KeyDemo() {
t = new TextField();
add(t);
[Link](this); Q4 (b) Package in Java
setSize(300,200); Package = group of related classes.
setVisible(true); Create:
} package mypack;
public void keyTyped(KeyEvent e) { public class A {}
[Link]([Link]()); Access:
} import mypack.A;
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public static void main(String[] args) {
new KeyDemo();
}
}
Q5 (a) Display employees of deptno 10 (PreparedStatement)
import [Link].*;
class EmpDemo {
public static void main(String[] args) {
try {
Connection con = [Link](
"jdbc:mysql://localhost:3306/test", "root", "");
PreparedStatement ps = [Link](
"SELECT * FROM employee WHERE deptno=?");
}
[Link](1, 10); }
ResultSet rs = [Link]();
[Link]());
while ([Link]()) { [Link]("Capacity: " +
[Link](// vi) Capacity
[Link]("eno") + " " +
[Link]();
[Link]("name") + " " +
// v) Remove all
[Link]("salary") + " " +
[Link]("Vector: " + v);
[Link]("deptno"));
// iv) Display
} [Link](obj);
[Link](); Object obj = [Link]();
} catch (Exception e) {");
[Link](e);[Link]("Enter element to remove:
} Scanner sc = new Scanner([Link]);
} // iii) Remove element by user
} [Link](2, "Hello");
Points: // ii) Add string at 3rd position
PreparedStatement avoids SQL injection
[Link](5.5f);
Precompiled query → faster
[Link](20.5);
Uses ? parameter [Link](10.5);
[Link](20);
Q5 (c) Life Cycle of Thread [Link](10);
Thread life cycle has following states:
// i) Add elements
1. New → Thread object created
Vector v = new Vector();
2. Runnable → Ready to run (start())
public static void main(String[] args) {
3. Running → CPU executing thread
class VectorDemo {
4. Blocked/Waiting → Waiting for resource or sleep
import [Link].*;
5. Terminated → Execution completed
Q5 (b) Vector Program
Flow:
New → Runnable → Running → Waiting/Blocked → Running →
Terminated
Q6 (a) Interface + Class + Result Program
interface Exam {
int sports_wt = 20;
}
class Student {
int rollNo;
String name;
int m1, m2, m3; }
Student(int r, String n, int a, int b, int c) { }
rollNo = r; [Link]();
name = n; [Link]("Result: " + res);
m1 = a; String res = [Link]();
m2 = b; [Link](num);
m3 = c; int num = [Link]();
} [Link]("Enter number: ");
} Scanner sc = new Scanner([Link]);
class Result extends Student implements Exam {
DataInputStream([Link]());
Result(int r, String n, int a, int b, int c) {
DataInputStream in = new
super(r, n, a, b, c); DataOutputStream([Link]());
} DataOutputStream out = new
void calPercentage() { Socket s = new Socket("localhost", 1234);
int total = m1 + m2 + m3 + sports_wt; public static void main(String[] args) throws Exception {
double per = total / 4.0; class Client {
[Link]("Percentage: " + per);
import [Link].*;
} import [Link].*;
void display() { import [Link].*;
[Link](rollNo + " " + name);
Client
} }
public static void main(String[] args) { }
Result r = new Result(1, "Ram", 70, 80, 90);
[Link]();
[Link](); [Link]("Odd");
[Link](); else
} [Link]("Even");
} if (num % 2 == 0)
Q6 (b) JTree Folder Structure int num = [Link]();
import [Link].*; DataOutputStream([Link]());
import [Link].*; DataOutputStream out = new
import [Link];
DataInputStream([Link]());
class TreeDemo {
DataInputStream in = new
public static void main(String[] args) {
Socket s = [Link]();
JFrame f = new JFrame();
ServerSocket ss = new ServerSocket(1234);
File rootFile = new File("C:/");
public static void main(String[] args) throws Exception {
DefaultMutableTreeNode root =
new DefaultMutableTreeNode([Link]());
class Server {

for (File file : [Link]()) {


import [Link].*;

[Link](new DefaultMutableTreeNode([Link]()));
import [Link].*;

}
Server

JTree tree = new JTree(root);


Q6 (c) Socket Programming (Client-Server)

[Link](new JScrollPane(tree));
[Link](300,400);
[Link](true);
}
}

You might also like