[Go to site: main page, start]

0% found this document useful (0 votes)
8 views25 pages

Java Assignment2

The document contains multiple Java programs demonstrating various functionalities, including a bouncing ball animation using Applet, simple forms with user input using Swing, a calculator, a digital clock, and exception handling examples. Each program is structured with relevant imports, class definitions, and methods to achieve specific tasks, such as user interaction and error handling. The document serves as a comprehensive guide for Java programming concepts and practical applications.

Uploaded by

djalok7261972
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)
8 views25 pages

Java Assignment2

The document contains multiple Java programs demonstrating various functionalities, including a bouncing ball animation using Applet, simple forms with user input using Swing, a calculator, a digital clock, and exception handling examples. Each program is structured with relevant imports, class definitions, and methods to achieve specific tasks, such as user interaction and error handling. The document serves as a comprehensive guide for Java programming concepts and practical applications.

Uploaded by

djalok7261972
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

Jabha Assignment 2

Java Applet
1. Create a java Program to perform bouncing ball animation and take two
button Start and Stop and when user press a start button animation Start and
user press stop button animation stop.
import [Link];

import [Link].*;

import [Link].*;

public class BouncingBall extends Applet implements Runnable, ActionListener

{
int x = 50, y = 50; // Ball position

int dx = 2, dy = 2; // Ball movement direction

Thread t;

boolean running = false;

Button start, stop;

public void init()


{

setLayout(new FlowLayout());

start = new Button("Start");

stop = new Button("Stop");

add(start);

add(stop);

[Link](this);
[Link](this);

public void paint(Graphics g)


{

[Link]([Link]);

[Link](x, y, 40, 40); // Draw ball

public void run()

while(running)

x = x + dx;

y = y + dy;

if(x <= 0 || x >= getWidth() - 40)

dx = -dx;

if(y <= 0 || y >= getHeight() - 40)


dy = -dy;

repaint();

try

[Link](20);

catch(Exception e) {}
}

public void actionPerformed(ActionEvent e)


{

if([Link]() == start)

if(t == null || !running)

running = true;

t = new Thread(this);

[Link]();

if([Link]() == stop)

running = false;
}

}
}

<html>

<body>
<applet code="[Link]" width="400" height="300">

</applet>

</body>

</html>

Java Swing
2. Write a java swing Program to create simple form (username, and Password)
and when user Pross a button show all information on screen.

import [Link].*;

import [Link].*;
import [Link].*;

public class SimpleForm extends JFrame implements ActionListener


{

JLabel l1, l2, result;


JTextField t1;

JPasswordField p1;

JButton b1;

SimpleForm()
{

setTitle("Login Form");

setLayout(new FlowLayout());

l1 = new JLabel("Username:");

l2 = new JLabel("Password:");

t1 = new JTextField(15);

p1 = new JPasswordField(15);

b1 = new JButton("Submit");

result = new JLabel("");

add(l1);
add(t1);

add(l2);
add(p1);

add(b1);
add(result);

[Link](this);

setSize(300,200);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

public void actionPerformed(ActionEvent e)

{
String username = [Link]();

String password = new String([Link]());

[Link]("Username: " + username + " Password: " + password);

public static void main(String args[])

{
new SimpleForm();

3. write a java swing Program to create simple form (name, email, gender, city )
and when user Press a button show all information. on screen.
import [Link].*;

import [Link].*;
import [Link].*;

public class SimpleForm2 extends JFrame implements ActionListener

JLabel l1, l2, l3, l4, result;

JTextField t1, t2;

JRadioButton male, female;

JComboBox city;

JButton submit;

ButtonGroup bg;

SimpleForm2()

{
setTitle("Simple Form");

setLayout(new FlowLayout());

l1 = new JLabel("Name:");

t1 = new JTextField(15);

l2 = new JLabel("Email:");

t2 = new JTextField(15);

l3 = new JLabel("Gender:");

male = new JRadioButton("Male");

female = new JRadioButton("Female");

bg = new ButtonGroup();
[Link](male);

[Link](female);

l4 = new JLabel("City:");
String c[] = {"Rajkot", "Ahmedabad", "Surat", "Bhavnagar"};

city = new JComboBox(c);

submit = new JButton("Submit");

result = new JLabel("");

add(l1);

add(t1);

add(l2);

add(t2);
add(l3);

add(male);

add(female);
add(l4);

add(city);
add(submit);

add(result);

[Link](this);

setSize(350,250);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public void actionPerformed(ActionEvent e)
{

String name = [Link]();


String email = [Link]();

String gender = "";

if([Link]())

gender = "Male";

else if([Link]())

gender = "Female";

String cityname = [Link]().toString();

[Link]("Name: " + name + ", Email: " + email +


", Gender: " + gender + ", City: " + cityname);

public static void main(String args[])

{
new SimpleForm2();

4. write a java swing program to perform basic mathematical operation


(calculator).

import [Link].*;

import [Link].*;

import [Link].*;
public class Calculator extends JFrame implements ActionListener
{

JTextField t1, t2, result;


JButton add, sub, mul, div;

Calculator()

setTitle("Simple Calculator");

setLayout(new FlowLayout());

t1 = new JTextField(10);

t2 = new JTextField(10);

result = new JTextField(10);

add = new JButton("Add");

sub = new JButton("Sub");

mul = new JButton("Mul");


div = new JButton("Div");

add(new JLabel("Number 1"));

add(t1);

add(new JLabel("Number 2"));

add(t2);

add(add);

add(sub);

add(mul);
add(div);

add(new JLabel("Result"));

add(result);

[Link](this);

[Link](this);

[Link](this);

[Link](this);

setSize(300,200);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

public void actionPerformed(ActionEvent e)

int a = [Link]([Link]());
int b = [Link]([Link]());

int r = 0;

if([Link]() == add)

r = a + b;

if([Link]() == sub)

r = a - b;

if([Link]() == mul)

r = a * b;
if([Link]() == div)
r = a / b;

[Link]([Link](r));

public static void main(String args[])

new Calculator();

5. write a java swing program to enter 5 subjects marks and calculate total,
percentage and grade (take appropriate components)

import [Link].*;

import [Link].*;

import [Link].*;

public class Marksheet extends JFrame implements ActionListener


{

JTextField s1, s2, s3, s4, s5;

JTextField total, per, grade;

JButton calc;

Marksheet()

setTitle("Student Marksheet");

setLayout(new FlowLayout());
s1 = new JTextField(10);
s2 = new JTextField(10);

s3 = new JTextField(10);
s4 = new JTextField(10);

s5 = new JTextField(10);

total = new JTextField(10);

per = new JTextField(10);

grade = new JTextField(10);

calc = new JButton("Calculate");

add(new JLabel("Subject 1"));

add(s1);

add(new JLabel("Subject 2"));

add(s2);

add(new JLabel("Subject 3"));


add(s3);

add(new JLabel("Subject 4"));


add(s4);

add(new JLabel("Subject 5"));

add(s5);

add(calc);
add(new JLabel("Total"));
add(total);

add(new JLabel("Percentage"));

add(per);

add(new JLabel("Grade"));

add(grade);

[Link](this);

setSize(300,300);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e)


{

int m1 = [Link]([Link]());
int m2 = [Link]([Link]());

int m3 = [Link]([Link]());

int m4 = [Link]([Link]());
int m5 = [Link]([Link]());

int t = m1 + m2 + m3 + m4 + m5;

double p = t / 5.0;

String g;
if(p >= 75)

g = "A";
else if(p >= 60)

g = "B";
else if(p >= 50)

g = "C";

else if(p >= 35)

g = "D";

else

g = "Fail";

[Link]([Link](t));

[Link]([Link](p));

[Link](g);

public static void main(String args[])

{
new Marksheet();

}
}

6. write a java swing program using Jmenu to display following many.


New

New window

Open…

Save

Save As…

Page setup…
Print…

Exit
import [Link].*;

public class JMenuExample

public static void main(String[] args)

JFrame f = new JFrame("JMenu Example");

JMenuBar mb = new JMenuBar();

JMenu m = new JMenu("File");

JMenuItem i1 = new JMenuItem("New");

JMenuItem i2 = new JMenuItem("New window");


JMenuItem i3 = new JMenuItem("Open…");

JMenuItem i4 = new JMenuItem("Save");

JMenuItem i5 = new JMenuItem("Save As…");


JMenuItem i6 = new JMenuItem("Page setup…");

JMenuItem i7 = new JMenuItem("Print…");


JMenuItem i8 = new JMenuItem("Exit");

[Link](i1);

[Link](i2);

[Link](i3);

[Link](i4);

[Link](i5);

[Link](i6);
[Link](i7);

[Link](i8);

[Link](m);
[Link](mb);

[Link](400,300);

[Link](JFrame.EXIT_ON_CLOSE);

[Link](true);

7. write a java program in swing to take two jtextfield and jframe and one
button (Convert) to Convert dollar to rupees (first textfield user input dollar
value and second textfield Calculate it's ruppes value)

import [Link].*;
import [Link].*;

import [Link].*;

public class DollarToRupee extends JFrame implements ActionListener

JTextField t1, t2;

JButton b;

DollarToRupee()

setTitle("Dollar to Rupees Converter");

setLayout(new FlowLayout());

add(new JLabel("Dollar:"));
t1 = new JTextField(10);

add(t1);

add(new JLabel("Rupees:"));
t2 = new JTextField(10);

add(t2);

b = new JButton("Convert");

add(b);

[Link](this);

setSize(300,150);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e)


{

double dollar = [Link]([Link]());


double rupees = dollar * 83; // Example conversion rate

[Link]([Link](rupees));
}

public static void main(String args[])

new DollarToRupee();

}
}

8. write a java swing program to show digital clock.

import [Link].*;
import [Link].*;

import [Link];

import [Link];

public class DigitalClock extends JFrame

JLabel clock;

DigitalClock()

clock = new JLabel();


[Link](new Font("Arial", [Link], 30));

[Link]([Link]);

add(clock);

setTitle("Digital Clock");

setSize(300,150);

setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

while(true)

Date d = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");


[Link]([Link](d));

try

{
[Link](1000);

catch(Exception e){}

public static void main(String args[])

new DigitalClock();

Exception Handling

(Built in Exception)
9. write a java program to handle arithmetic exception handling.

public class ArithmeticExceptionDemo


{

public static void main(String args[])

{
int a = 10;

int b = 0;

int c;

try

{
c = a / b;

[Link]("Result = " + c);


}

catch(ArithmeticException e)
{

[Link]("Arithmetic Exception Occurred");

[Link]("Program continues...");

10. write a java program to handling arrayindexoutofbound exception


handling.
public class ArrayExceptionDemo

public static void main(String args[])

int a[] = {10, 20, 30, 40, 50};

try

[Link]("Element = " + a[7]);

catch(ArrayIndexOutOfBoundsException e)
{

[Link]("Array Index Out Of Bounds Exception Occurred");

}
[Link]("Program continues...");

}
}

11. write a java program to handling null pointed exception Handling.

public class NullPointerExceptionDemo

public static void main(String args[])

String s = null;

try

[Link]("Length = " + [Link]());

}
catch(NullPointerException e)

[Link]("Null Pointer Exception Occurred");


}

[Link]("Program continues...");

12. write a java Program to handling negative number exception handling.

import [Link];

public class NegativeNumberExceptionDemo

{
public static void main(String args[])

{
Scanner sc = new Scanner([Link]);

try

[Link]("Enter a number: ");

int num = [Link]();

if(num < 0)

throw new Exception("Negative Number Not Allowed");

[Link]("You entered: " + num);


}

catch(Exception e)

{
[Link]("Exception: " + [Link]());

[Link]("Program continues...");

}
}

(user defined Exception)

13. write a java Program to handle exception if amount is negative number.

class NegativeAmountException extends Exception

{
NegativeAmountException(String msg)

{
super(msg);

}
}

public class UserDefinedExceptionDemo

public static void main(String args[])

int amount = -500;

try

if(amount < 0)
{

throw new NegativeAmountException("Amount cannot be negative");

[Link]("Amount = " + amount);


}

catch(NegativeAmountException e)

{
[Link]("Exception: " + [Link]());

[Link]("Program continues...");

}
14. write a java Program enter three subject marks, total, percentage to the
handle exception if students mark is < 90

import [Link];

class MarksException extends Exception

{
MarksException(String msg)

{
super(msg);

public class StudentMarks


{

public static void main(String args[])

Scanner sc = new Scanner([Link]);

try
{

[Link]("Enter marks of Subject 1: ");

int m1 = [Link]();

[Link]("Enter marks of Subject 2: ");


int m2 = [Link]();

[Link]("Enter marks of Subject 3: ");

int m3 = [Link]();
if(m1 < 90 || m2 < 90 || m3 < 90)
{

throw new MarksException("Marks should be 90 or above");


}

int total = m1 + m2 + m3;

double per = total / 3.0;

[Link]("Total = " + total);

[Link]("Percentage = " + per);

catch(MarksException e)

[Link]("Exception: " + [Link]());


}

Connectivity
15. write a java Program to create connectivity Program of java with access
database and perform following operation on it

-> Connect data

-> insert data into database

-> update data into database


-> delete data from database

-> Display data from database

You might also like