Professional Documents
Culture Documents
Register No :
Name :
Branch :
Semester :
CERTIFICATE
Certified that this is the bonafide record of work done by the above student in
the Laboratory
during the academic year
Page
S. No. Date Name of the Experiment Sign
No.
9. Multi-Threaded Application
Date:
AIM:
To develop a Java application for generating Electricity bill for domestic and commercial
consumers.
ALGORITHM:
1. Create a consumer class with the following members: consumer number, consumer name,
previous month reading, current month reading and type of EB connection (domestic or
commercial).
2. If the type of the EB connection is domestic, calculate the amount to be paid as follows:
• First 100 units - Rs.1 per unit
• 101-200 units - Rs.2.50 per unit
• 201 -500 units - Rs. 4 per unit
• 501 units - Rs.6 per unit
3. If the type of the EB connection is commercial, calculate the amount to be paid as follows:
• First 100 units - Rs. 2 per unit
• 101-200 units - Rs. 4.50 per unit
• 201 -500 units - Rs. 6 per unit
• 501 units - Rs. 7 per unit
4. Calculate the total amount based on the type of connection and print the result.
1
PROGRAM:
Main.java
import java.util.Scanner;
class EBBilling {
int consumer_no;
String consumer_name;
int previous_month_reading;
int current_month_reading;
int units_consumed;
int connection_type;
double amount;
consumer_name = inputs.next();
consumer_no = inputs.nextInt();
previous_month_reading = inputs.nextInt();
current_month_reading = inputs.nextInt();
connection_type = inputs.nextInt();
inputs.close();
double sum = 0;
2
if (connection_type == 1) {
if (i <= 100)
sum = sum + 1;
sum = sum + 4;
else
sum = sum + 6;
} else if (connection_type == 2) {
if (i <= 100)
sum = sum + 2;
sum = sum + 6;
else
sum = sum + 7;
else {
System.exit(0);
amount = sum;
3
public void domesticTariff() {
System.out.println("**************************");
System.out.println("****************************");
calculateBill();
System.out.println("****************************");
if (connection_type == 1) {
domesticTariff();
} else {
commercialTariff();
4
}
consumer.getDetails();
consumer.displayBill();
5
OUTPUT:
RESULT:
The Java program to generate the electricity bill for consumers was successfully executed.
6
2. CURRENCY, DISTANCE AND TIME CONVERTER
Date:
AIM:
To develop a Java application to implement currency converter, distance converter and time
converter using packages.
ALGORITHM:
1. Create three packages to represent the given converters ( currency, distance and time ).
2. Depending on the choice selected, implement the conversion routines by using
appropriate formulae.
3. Perform conversion for the following:
• Currency converter – Dollar, Euro and Yen to INR & vice versa
• Distance converter – Meter , Miles to KM & vice versa
• Time converter – Hours to minutes, seconds & vice versa
4. Print the result of the conversion as the output.
7
PROGRAM:
Currency.java
package com.ex2.converter;
import java.util.Scanner;
8
public static void userChoice() {
int choice = 0;
double money = 0;
System.out.println("CURRENCY CONVERTER");
System.out.println("******************");
choice = inputs.nextInt();
switch (choice) {
case 1:
money = inputs.nextDouble();
break;
case 2:
money = inputs.nextDouble();
break;
9
case 3:
money = inputs.nextDouble();
break;
case 4:
money = inputs.nextDouble();
break;
case 5:
money = inputs.nextDouble();
break;
case 6:
money = inputs.nextDouble();
break;
default:
System.out.println("\nInvalid Choice");
break;
inputs.close();
10
Distance.java
package com.ex2.converter;
import java.util.Scanner;
int choice = 0;
double distance = 0;
System.out.println("DISTANCE CONVERTER");
System.out.println("******************");
11
System.out.println("3. Kilometer to Miles");
choice = inputs.nextInt();
switch(choice){
case 1:
distance = inputs.nextDouble();
Meters");
break;
case 2:
distance = inputs.nextDouble();
Kilometers");
break;
case 3:
distance = inputs.nextDouble();
Miles");
break;
case 4:
12
distance = inputs.nextDouble();
Kilometers");
break;
default:
System.out.println("\nInvalid Choice");
break;
inputs.close();
Time.java
package com.ex2.converter;
import java.util.Scanner;
13
public static double SecsToHours(double seconds) {
int choice = 0;
double time = 0;
System.out.println("TIME CONVERTER");
System.out.println("**************");
choice = inputs.nextInt();
switch(choice){
case 1:
System.out.println("Enter Hours");
time = inputs.nextDouble();
break;
case 2:
System.out.println("Enter Minutes");
time = inputs.nextDouble();
break;
14
case 3:
System.out.println("Enter Hours");
time = inputs.nextDouble();
break;
case 4:
System.out.println("Enter Seconds");
time = inputs.nextDouble();
break;
default:
break;
inputs.close();
Main.java
package com.ex2;
import java.util.Scanner;
import com.ex2.converter.*;
int choice = 0;
15
System.out.println("CONVERTER");
System.out.println("*********");
choice = inputs.nextInt();
switch(choice){
case 1:
Currency.userChoice();
break;
case 2:
Distance.userChoice();
break;
case 3:
Time.userChoice();
break;
default:
System.out.println("Invalid Option");
break;
inputs.close();
16
OUTPUT:
17
RESULT:
The Java program to implement the currency, distance and time converter were successfully
implemented.
18
3. EMPLOYEE PAYROLL PROCESSING
Date:
AIM:
To write a Java program to create pay slips for employees by utilizing the concept of
inheritance.
ALGORITHM:
1. Create an Employee class with the following members : employee name, employee ID,
2. Inherit the classes - Programmer, Assistant Professor, Associate Professor and Professor
3. Add Basic Pay (BP) as the member of all the inherited classes with 97% of BP as DA, 10%
4. Generate pay slips for the employees with their gross and net salary.
19
PROGRAM:
Employee.java
package workforce;
import java.util.Scanner;
class Employee {
String emp_name;
String emp_id;
String address;
String mail_id;
String mobile_no;
int work_days_in_current_month;
int days_worked;
Scanner input;
Employee() {
getUserDetails();
work_days_in_current_month = input.nextInt();
20
staff_club_fund = (int) ((current_basic_pay / 100) * 0.1);
System.out.println("\n");
System.out.println("\n");
System.out.println("Name : ");
emp_name = input.next();
System.out.println("ID : ");
emp_id = input.next();
System.out.println("Address : ");
21
address = input.next();
System.out.println("EMail : ");
mail_id = input.next();
System.out.println("Mobile No :");
mobile_no = input.next();
System.out.println("\nBasic Pay of " + empType + " is " + basic_pay + " for " +
System.out.println("Current month the " + empType + " gets Rs. " + daily_pay + " as basic
pay / day");
System.out.println("\n\nEnter the number of days worked by the " + empType + " : ");
days_worked = input.nextInt();
System.out.println("\n\n");
System.out.println("*****************");
generatePaySlip();
} else {
22
Programmer.java
package workforce;
public Programmer() {
super();
computeProgrammerPay();
basic_pay = input.nextInt();
computeBasicPay("Programmer");
generatePaySlip();
displayPaySlip();
AsstProfessor.java
package workforce;
public AsstProfessor() {
super();
computeAssistantProfessorPay();
basic_pay = input.nextInt();
computeBasicPay("Assistant Professor");
generatePaySlip();
displayPaySlip();
23
AssocProfessor.java
package workforce;
public AssocProfessor() {
super();
computeAssociateProfessorPay();
basic_pay = input.nextInt();
computeBasicPay("Associate Professor");
generatePaySlip();
displayPaySlip();
Professor.java
package workforce;
public Professor() {
super();
computeProfessorPay();
basic_pay = input.nextInt();
computeBasicPay("Professor");
generatePaySlip();
displayPaySlip();
24
Main.java
import java.util.Scanner;
import workforce.*;
int choice;
System.out.println("\nEmployee Category");
System.out.println("*******************");
System.out.println("1. Programmer");
System.out.println("4. Professor");
System.out.println("5. Exit");
choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("Programmer Details");
System.out.println("******************");
new Programmer();
break;
case 2:
System.out.println("***************************");
new AsstProfessor();
break;
25
case 3:
System.out.println("***************************");
new AssocProfessor();
break;
case 4:
System.out.println("Professor Details");
System.out.println("*****************");
new Professor();
default:
System.exit(0);
break;
input.close();
26
OUTPUT:
27
RESULT:
The Java program to implement pay slip generation for employees through inheritance was
successfully executed.
28
4. ADT STACK WITH EXCEPTION HANDLING
Date:
AIM:
ALGORITHM:
2. Get the option from the user for performing array operations like : push, pop and display.
3. If the user option is push then check if the stack is full. If not enter the element on top of
4. If the user option is pop then check if the stack is empty. If not, remove the element
5. If the user option is display, then display all the elements present on the stack.
29
PROGRAM:
Operations.java
package stackfiles;
void pop();
void display();
Stack.java
package stackfiles;
int items[];
int capacity;
int position = 0;
this.capacity = capacity;
initStack();
items[i] = -1;
try {
items[position] = number;
position++;
30
System.out.println("\nThe element " + number + " is at position " + position);
display();
catch (ArrayIndexOutOfBoundsException e) {
int pop_element;
try {
items[position - 1] = -1;
position--;
display();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("\nStack is EMPTY");
System.out.println("\n");
System.out.println("STACK");
System.out.println("*****");
if (items[i] != -1)
System.out.println(items[i]);
System.out.println("*****");
31
Main.java
import java.util.Scanner;
import stackfiles.*;
int choice=0;
System.out.println("\n1.PUSH");
System.out.println("2.POP");
System.out.println("3.DISPLAY");
System.out.println("4.EXIT");
choice = input.nextInt();
switch (choice) {
case 1:
mystack.push(input.nextInt());
break;
case 2:
mystack.pop();
break;
case 3:
mystack.display();
break;
32
default:
System.exit(0);
break;
input.close();
33
OUTPUT:
34
RESULT:
The Java program to implement the ADT Stack using arrays was executed successfully.
35
5. STRING OPERATIONS USING ARRAYLIST
Date:
AIM:
ALGORITHM:
1. Create an array list to hold the string items entered by the user.
a. Append a string
c. Search a string
3. If the option is to append a string, place the string beneath the string already present in
4. If the option is to insert a string, then get the position and string from the user and place
5. If the option is to search the string, output the result whether the string is present in the
6. If the option is to display strings beginning with the alphabet, get the user option for the
beginning alphabet and display all the strings present within the array list starting with
36
PROGRAM:
StringOperations.java
import java.util.ArrayList;
ArrayList<String> wordList;
public StringOperations() {
wordList.add(aString);
displayList();
int cur_pos = 0;
else {
if (cur_pos == position - 1)
templist.add(aString);
templist.add(elememt);
cur_pos++;
37
}
wordList = templist;
displayList();
System.out.println("\n");
System.out.println("Contents of List");
System.out.println("****************");
int i = 0;
i++;
System.out.println("****************");
System.out.println("\n");
int cur_pos = 0;
if (element.equalsIgnoreCase(aString)) {
foundAt = cur_pos;
break;
} else
cur_pos++;
38
if (foundAt >= 0) {
System.out.println("\n");
} else
if (aLetter.equals(firstLetter))
System.out.println(element);
System.out.println("\n");
Main.java
import java.util.Scanner;
int choice = 0;
int position = 0;
while (choice != 6) {
System.out.println("List Operations");
System.out.println("***************");
39
System.out.println("1. Append a String");
System.out.println("5.Exit");
choice = inputs.nextInt();
switch (choice) {
case 1:
myStrings.addString(inputs.next());
break;
case 2:
stringInput = inputs.next();
position = inputs.nextInt();
myStrings.insertStringAt(position, stringInput);
break;
case 3:
stringInput = inputs.next();
myStrings.searchString(stringInput);
break;
case 4:
myStrings.displayStringsBeginWith(inputs.next());
break;
40
case 5:
default:
inputs.close();
System.exit(0);
break;
41
OUTPUT:
42
43
RESULT:
The Java program to perform string operations using ArrayList was successfully performed.
44
6. IMPLEMENTING ABSTRACT CLASSES
Date:
AIM:
To write a Java program to implement an Abstract Class and to extend it to create concrete
classes.
ALGORITHM:
1. Create an abstract class which contains the basic dimensional values of the shapes like
2. Extend the abstract shape class to create three concrete classes namely – Rectangle,
3. If the choice entered is rectangle, then compute the area of the rectangle based upon the
4. If the choice entered is triangle, then compute the area of the triangle based upon the
5. If the choice entered is circle, then compute the area of the circle based upon the formula
45
PROGRAM:
Shape.java
package shapes;
Rectangle.java
package shapes;
import java.util.Scanner;
System.out.println("\n");
length = inputs.nextDouble();
height = inputs.nextDouble();
46
Triangle.java
package shapes;
import java.util.Scanner;
System.out.println("\n");
length = inputs.nextDouble();
height = inputs.nextDouble();
Circle.java
package shapes;
import java.util.Scanner;
System.out.println("\n");
47
length = inputs.nextDouble();
Main.java
import shapes.*;
import java.util.Scanner;
int choice = 0;
do {
System.out.println("\n");
System.out.println("Area of Shapes");
System.out.println("**************");
System.out.println("1. Rectangle");
System.out.println("2. Triangle");
System.out.println("3. Circle");
System.out.println("4. Exit");
choice = inputs.nextInt();
48
switch (choice) {
case 1:
rectangle.printArea();
break;
case 2:
triangle.printArea();
break;
case 3:
circle.printArea();
break;
case 4:
inputs.close();
System.exit(0);
default:
System.out.println("Invalid choice");
break;
49
OUTPUT:
50
RESULT:
The Java program to implement an Abstract Class for finding the area of shapes was
implemented successfully.
51
7. USER DEFINED EXCEPTION HANDLING
Date:
AIM:
ALGORITHM:
2. Validate the name of the student using Regular Expressions. If the validation fails then
3. Validate the marks obtained by the student. If the mark is below 0 or above 100, then
4. If validation passes, print given messages for the student based on the range of marks
obtained.
52
PROGRAM:
StudentMarks.java
package exceptions;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
this.name = name;
this.marks = marks;
try {
53
if (matcher.matches())
else
} catch (ValidateName e) {
try {
if (isBetween(marks, 0, 49)) {
} else
} catch (ValidateMarks e) {
54
ValidateMarks.java
package exceptions;
@SuppressWarnings("serial")
public ValidateMarks() {
ValidateName.java
package exceptions;
@SuppressWarnings("serial")
public ValidateName() {
Main.java
import exceptions.*;
import java.util.Scanner;
String name;
int marks;
name = input.nextLine();
marks = input.nextInt();
55
StudentMarks aStudent = new StudentMarks(name, marks);
aStudent.nameValidation();
aStudent.marksValidation();
input.close();
56
OUTPUT:
57
RESULT:
The Java program for user defined exception handling to validate name and marks obtained
was successfully implemented.
58
8. FILE OPERATIONS IN JAVA
Date:
AIM:
ALGORITHM:
1. Get the fully qualified path of the file from the user as input.
2. Based on the path, check if the given file is present. If not display message stating the
3. If the file is present then display the following properties of the file:
59
PROGRAM:
UserFileHandler.java
import java.io.File;
File myFile;
int length = 0;
exists = myFile.exists();
readable = myFile.canRead();
writeable = myFile.canWrite();
else
return "";
if (exists) {
60
System.out.println("The File \"" + myFile.getName() + "\" Exists at Location - " +
myFile.getParent());
else if (readable)
else if (writeable)
} else
Main.java
import java.util.Scanner;
file_path = input.next();
new UserFileHandler(file_path).fileDetails();
input.close();
61
OUTPUT:
RESULT:
The Java program to implement basic file handling features to check the location, attributes,
file type & size of a given file was successfully implemented.
62
9. MULTI - THREADED APPLICATION
Date:
AIM:
ALGORITHM:
2. The first thread generates a random number within the range 0 to 1000, and prints it as
the output.
3. The second thread checks if the random number is even or not. If it is even, it calculates
4. The third thread checks if the random number is odd or not. If odd, it calculates the cube
63
PROGRAM:
RandomIntThread.java
package threads;
import java.util.Random;
int value;
while (true) {
try {
sleep(1000);
} catch (InterruptedException e) {
value = number.nextInt(1000);
if (value % 2 == 0)
new SquareThread(value).start();
else
new CubeThread(value).start();
SquareThread.java
package threads;
int number;
int square;
64
this.number = number;
CubeThread.java
package threads;
int number;
int cube;
this.number = number;
Main.java
import threads.*;
new RandomIntThread().start();
65
OUTPUT:
RESULT:
66
10. FINDING THE MAXIMUM VALUE USING GENERICS
Date:
AIM:
To write a java program to find the maximum value from the given array of elements by
using a generic function.
ALGORITHM:
1. Create a generic array that can hold different types of elements of similar types.
2. Populate the array with numbers and call the function to find the maximum element
4. Similarly populate the generic array with strings and call the same function to find the
67
PROGRAM:
Maximum.java
class Maximum {
T max = array[0];
if(element.compareTo(max) > 0)
max = element;
Main.java
max.maxFinder(numbers);
System.out.println("\n");
max.maxFinder(strings);
68
OUTPUT:
RESULT:
The Java program to find the maximum value from the given array of elements by using a
generic function was successfully implemented.
69
11. CALCULATOR USING EVENT-DRIVEN PROGRAMMING IN JAVA
Date:
AIM:
To create a calculator in Java using event driven programming for implementing standard /
scientific calculations
ALGORITHM:
1. Create a AWT application to produce a frame for displaying a graphics based calculator.
2. Populate the container with digits from 0 to 9 and with symbols like +, -, *, / , = and .
3. Based on the mouse input from the user, display the appropriate actions performed by
4. Perform the calculations and display the output when the ‘=’ button is clicked
5. The SCI/STD is used to toggle the calculator between the Scientific and Standard modes.
6. When the Scientific mode is enabled, display buttons like – sin, cos, tan and log, for
7. Similarly perform the clicked operation and display the result on the calculator window.
70
PROGRAM:
Main.java
import java.awt.*;
import java.awt.event.*;
Button n0, n1, n2, n3, n4, n5, n6, n7, n8, n9, point, equal;
TextField display;
String op_flag;
String data;
this.display = display;
n0 = new Button("0");
n0.setActionCommand("zero");
n0.addActionListener(this);
n1 = new Button("1");
n1.setActionCommand("one");
n1.addActionListener(this);
71
n2 = new Button("2");
n2.setActionCommand("two");
n2.addActionListener(this);
n3 = new Button("3");
n3.setActionCommand("three");
n3.addActionListener(this);
n4 = new Button("4");
n4.setActionCommand("four");
n4.addActionListener(this);
n5 = new Button("5");
n5.setActionCommand("five");
n5.addActionListener(this);
n6 = new Button("6");
n6.setActionCommand("six");
n6.addActionListener(this);
n7 = new Button("7");
n7.setActionCommand("seven");
n7.addActionListener(this);
n8 = new Button("8");
n8.setActionCommand("eight");
n8.addActionListener(this);
n9 = new Button("9");
n9.setActionCommand("nine");
n9.addActionListener(this);
72
point = new Button(".");
point.setActionCommand("point");
point.addActionListener(this);
equal.setActionCommand("equal");
equal.addActionListener(this);
plus.setActionCommand("plus");
plus.addActionListener(this);
minus.setActionCommand("minus");
minus.addActionListener(this);
multiply.setActionCommand("multiply");
multiply.addActionListener(this);
divide.setActionCommand("divide");
divide.addActionListener(this);
blank_1.setActionCommand("blank_1");
blank_1.addActionListener(this);
blank_2.setActionCommand("blank_2");
blank_2.addActionListener(this);
73
clear = new Button("C");
clear.setActionCommand("clear");
clear.addActionListener(this);
scientific.setActionCommand("scientific");
scientific.addActionListener(this);
add(n7);
add(n8);
add(n9);
add(divide);
add(n4);
add(n5);
add(n6);
add(multiply);
add(n1);
add(n2);
add(n3);
add(minus);
add(point);
add(n0);
add(equal);
add(plus);
add(blank_1);
add(clear);
add(blank_2);
add(scientific);
74
public String getDisplayText() {
return display.getText().toString();
display.setText(text);
display.setEditable(false);
setDisplay("");
data = "";
if (toAdvanced) {
plus.setLabel("sin");
plus.setActionCommand("sin");
minus.setLabel("cos");
minus.setActionCommand("cos");
multiply.setLabel("tan");
multiply.setActionCommand("tan");
divide.setLabel("log");
divide.setActionCommand("log");
} else {
plus.setLabel("+");
plus.setActionCommand("plus");
minus.setLabel("-");
minus.setActionCommand("minus");
multiply.setLabel("x");
75
multiply.setActionCommand("multiply");
divide.setLabel("/");
divide.setActionCommand("divide");
data = getDisplayText();
switch (e.getActionCommand()) {
case "zero":
setDisplay(data + "0");
break;
case "one":
setDisplay(data + "1");
break;
case "two":
setDisplay(data + "2");
break;
case "three":
setDisplay(data + "3");
break;
case "four":
setDisplay(data + "4");
break;
case "five":
setDisplay(data + "5");
break;
case "six":
setDisplay(data + "6");
break;
76
case "seven":
setDisplay(data + "7");
break;
case "eight":
setDisplay(data + "8");
break;
case "nine":
setDisplay(data + "9");
break;
case "point":
if (decimal == false) {
setDisplay(data + ".");
decimal = true;
break;
case "plus":
decimal = false;
op1 = data;
op_flag = "plus";
clearDisplay();
break;
case "minus":
decimal = false;
op1 = data;
op_flag = "minus";
clearDisplay();
break;
case "multiply":
decimal = false;
op1 = data;
op_flag = "multiply";
77
clearDisplay();
break;
case "divide":
decimal = false;
op1 = data;
op_flag = "divide";
clearDisplay();
break;
case "clear":
decimal = false;
clearDisplay();
break;
case "scientific":
if (flag_scientific) {
changeAdvanced(true);
flag_scientific = false;
} else {
changeAdvanced(false);
flag_scientific = true;
break;
case "sin":
op1 = data;
setDisplay(String.valueOf(Math.sin(Double.valueOf(op1))));
break;
case "cos":
op1 = data;
setDisplay(String.valueOf(Math.cos(Double.valueOf(op1))));
break;
78
case "tan":
op1 = data;
setDisplay(String.valueOf(Math.tan(Double.valueOf(op1))));
break;
case "log":
op1 = data;
setDisplay(String.valueOf(Math.log(Double.valueOf(op1))));
break;
case "equal":
decimal = false;
switch (op_flag) {
case "plus":
op2 = data;
clearDisplay();
dop1 = Double.parseDouble(op1);
dop2 = Double.parseDouble(op2);
result = String.valueOf(dresult);
setDisplay(result);
op_flag = "";
break;
case "minus":
op2 = data;
clearDisplay();
dop1 = Double.parseDouble(op1);
dop2 = Double.parseDouble(op2);
result = String.valueOf(dresult);
setDisplay(result);
79
op_flag = "";
break;
case "multiply":
op2 = data;
clearDisplay();
dop1 = Double.parseDouble(op1);
dop2 = Double.parseDouble(op2);
result = String.valueOf(dresult);
setDisplay(result);
op_flag = "";
break;
case "divide":
op2 = data;
clearDisplay();
dop1 = Double.parseDouble(op1);
dop2 = Double.parseDouble(op2);
result = String.valueOf(dresult);
setDisplay(result);
op_flag = "";
break;
80
class Calculator extends Frame {
TextField display;
public Calculator() {
setLayout(new BorderLayout());
add(display, BorderLayout.NORTH);
setVisible(true);
setSize(290, 300);
setResizable(false);
setTitle("Calculator");
setLocationRelativeTo(null);
addWindowListener(new WindowAdapter() {
dispose();
});
new Calculator();
81
OUTPUT:
RESULT:
An event driven programming for creating a standard / scientific calculator in Java was
successfully implemented.
82
12. MINI PROJECT – ONLINE JAVA QUIZ APPLICATION
Date:
AIM:
ALGORITHM:
1. Write a Swing based graphics application to create a container to display the components
3. Display each question and provide the user with multiple choice answers as responses to
the questions.
4. Similarly iterate through all the questions and store the user responses.
5. When all questions have been answered, create a new window to display the correct
responses to the questions along with the responses marked by the users.
6. On the same window, display the total quiz score obtained based on the number of correct
83
PROGRAM:
Quiz.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
JPanel panel;
JPanel panelresult;
JRadioButton choice1;
JRadioButton choice2;
JRadioButton choice3;
JRadioButton choice4;
ButtonGroup bg;
JLabel lblmess;
JButton btnext;
String[][] questions;
String[][] answers;
int qaid;
Quiz() {
initializedata();
setTitle("Programming Quiz");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(windowWidth, windowHeight);
84
Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
setResizable(false);
cont.setLayout(null);
cont.setBackground(Color.GRAY);
bg = new ButtonGroup();
bg.add(choice1);
bg.add(choice2);
bg.add(choice3);
bg.add(choice4);
lblmess.setForeground(Color.BLUE);
btnext.setForeground(Color.PINK);
btnext.addActionListener(this);
panel.setBackground(Color.LIGHT_GRAY);
panel.setLocation(10, 10);
panel.setSize(400, 300);
panel.add(lblmess);
panel.add(choice1);
panel.add(choice2);
panel.add(choice3);
panel.add(choice4);
85
panel.add(btnext);
cont.add(panel);
setVisible(true);
qaid = 0;
readqa(qaid);
if (btnext.getText().equals("Next Question")) {
if (qaid < 9) {
map.put(qaid, getSelection());
qaid++;
readqa(qaid);
} else {
map.put(qaid, getSelection());
btnext.setText("Show Answers");
new Report();
86
questions[0][3] = "javac JavaProgram.java";
questions[0][4] = "None";
questions[3][1] = "/...";
questions[3][2] = "//...";
questions[3][3] = "/*...";
questions[3][4] = "/*...*/";
87
questions[5][4] = "int num=(Integer)str_num";
questions[6][1] = "try";
questions[6][2] = "finally";
questions[6][3] = "thrown";
questions[6][4] = "catch";
88
answers[1][0] = "What is the range of short data type in Java?";
answers[3][1] = "//...";
answers[6][1] = "thrown";
89
public String getSelection() {
while (buttons.hasMoreElements()) {
if (temp.isSelected()) {
selectedChoice = temp.getText();
return (selectedChoice);
choice1.setText(questions[qid][1]);
choice2.setText(questions[qid][2]);
choice3.setText(questions[qid][3]);
choice4.setText(questions[qid][4]);
choice1.setSelected(true);
qaid = 0;
map.clear();
readqa(qaid);
btnext.setText("Next Question");
int count = 0;
90
for (int qid = 0; qid < qnum; qid++)
if (answers[qid][1].equals(map.get(qid)))
count++;
return count;
Report() {
setTitle("Result");
setSize(windowWidth, windowHeight);
Point center =
GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
setBackground(Color.WHITE);
setResizable(false);
addWindowListener(new WindowAdapter() {
dispose();
reset();
});
add(d);
setVisible(true);
91
int x = 10;
int y = 20;
y += 30;
y += 30;
y += 30;
if (y > 400) {
y = 20;
x = 450;
g.setColor(Color.BLUE);
92
QuizProgram.java
new Quiz();
93
OUTPUT:
94
RESULT:
The Java application for online quiz using Swing was implemented successfully.
95