Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 64

SHRI VAISHNAV VIDYAPEETH VISHWAVIDYALAY

SHRI VAISHNAV INSTITUTE OF INFORMATION TECHNOLOGY


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

PRACTICAL FILE
(Session Jan -June 2024)

Advanced Java Programming


Course Name : Advanced Java Programing
Course Code : BTIT407N
Semester / Year : 2nd year / IV Sem.
Section : CSE-B
Submitted To : Prof. Pallavi Kala
Submitted By : Devang Chouhan
Enroll.No. : 22100BTCSE11490

Devang Chouhan 1 2100BTCSE11490


Experiment : 1

Title: Write a program in java to demonstrate class and object

Outcome: The outcome of the experiment is to demonstrate the usage of classes and objects in Java
programming.

Objectives:
1. Understand the concept of classes and objects in Java.
2. Create instances (objects) of a class and access their properties.
3. Learn how to define class variables and methods.
Nomenclature:
 Class: A blueprint or template that defines the variables and methods common to a certain
type of objects.
 Object: An instance of a class that represents a specific entity or concept.

Solution:
The program defines two classes: A and Anuj. Class A has two instance variables a and b, a get
method to set the values of a and b, and a show method to display the values of a and b. The
Radhika class contains the main method where an object ob1 of class A is created, and the get
method is called to set the values of a and b. Finally, the show method is called to display the
values of a and

Code/ Pseudo Code


class A {
int a;
float b;
void get (int a1, float b1) {
a=a1;
b=b1;
}
void show()
{ System.out.println("a=
"+a); System.out.println("b=
"+b);
}
}
class DevendraLavvanshi {
public static void main(String[] args)
{ A ob1=new A();
ob1.get(11,2.1f);
ob1.show();
}
}

Devang Chouhan 2 2100BTCSE11490


}
}
Results

Quiz & Viva Questions

Quiz:
Which of the following best describes a class in Java?
(a) A function that performs a specific action.
(b) A keyword used to declare variables.
(c) A blueprint or template that defines the variables and methods of objects.
(d) An object that represents a specific entity or concept.

What is an object in Java?


(a) A special type of variable used to store data.
(b) A loop construct used for iteration.
(c) A keyword used to define classes.
(d) An instance of a class that represents a specific entity or concept.

What are instance variables in Java?


(a) Variables defined within a method.
(b) Variables declared inside a loop.
(c) Variables that hold the state or characteristics of an object.
(d) Variables used for mathematical calculations.

Viva
6.1.1 Explain the concept of classes and objects in Java.
6.1.2 How do you define a class in Java?
6.1.3 How do you create an object of a class?

Devang Chouhan 3 2100BTCSE11490


Experiment : 2

Title: Write a program in java to demonstrate Inheritance

Outcome: The outcome of this experiment is to demonstrate the concept of inheritance in Java, where the
"Programmer" class inherits the properties and methods of the "Employee" class.

Objectives:
1.2 Understand the concept of inheritance in Java.
1.3 Demonstrate the use of inheritance to extend the functionality of a base class.
1.4 Illustrate how derived classes can inherit properties and methods from the base class.

Nomenclature:
 Employee class: The base class that represents an employee and has a "salary" property.
 Programmer class: The derived class that extends the Employee class and adds a "bonus"
property.

Code/ Pseudo Code

class
Employee{ float
salary=40000;
}
class DevendraLavvanshiextends Employee
{ int bonus=10000;
public static void main(String args[])
{ DevendraLavvanship=new Chirag();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}

Results

Devang Chouhan 4 2100BTCSE11490


Quiz & Viva Questions

Quiz:
What is inheritance in Java?
(a) A mechanism to reuse code and create new classes from existing classes
(b) A way to override methods in the base class
(c) A technique to hide data members of a class
(d) A method to create multiple objects of a class

What is the base class in the given code?


(a) Employee
(b) Programmer
(c) float salary
(d) int bonus

What is the output of the program?


(a) Programmer salary is: 40000.0, Bonus of Programmer is: 10000
(b) Programmer salary is: 10000, Bonus of Programmer is: 40000.0
(c) Programmer salary is: 50000.0, Bonus of Programmer is: 10000
(d) Programmer salary is: 10000, Bonus of Programmer is: 50000.0

Viva

What is inheritance and why is it useful in object-oriented programming?


Explain the relationship between the "Employee" class and the "Programmer" class in terms of
inheritance.
How does the "Programmer" class inherit the "salary" property from the "Employee" class?

Devang Chouhan 5 2100BTCSE11490


Experiment : 3

Title: Write a program in java to demonstrate Single level Inheritance


Outcome: After running the program, the output will display the roll number, name, marks of two subjects,
and the average marks of the student.
Objective
Demonstrate single-level inheritance in Java.
Illustrate the concept of extending a class to inherit its properties and methods.
Understand the relationship between the base class (Student) and the derived class (Result).
Nomenclature, theory with self-assessment questionnaire:
1.2 Nomenclature:
1.2.1 Student class:
1.2.1.1 rno - Roll number (integer)
1.2.1.2 name - Name of the student (String)
1.2.1.3 get(int no, String nm) - Method to set the roll number and name
1.2.2 Result class (extends Student):
1.2.2.1 m1 - Marks of subject I (float)
1.2.2.2 m2 - Marks of subject II (float)
1.2.2.3 getMarks(float mr1, float mr2) - Method to set the marks of the subjects
1.2.3 DevendraLavvanshiclass:
1.2.3.1 main(String args[]) - The main method where program execution starts

Solution: This Java program demonstrates single-level inheritance. Inheritance is a feature in object-oriented
programming that allows a class to inherit properties and methods from another class. In single-level
inheritance, a derived class extends a single base class. The given program consists of three classes: Student,
Result, and Chirag

Code/ Pseudo Code


class Student
{ int rno;
String name;
void get(int no,String nm) {
rno=no;
name=nm;

void show() {
System.out.println(" roll no= "+rno);
System.out.println(" name= "+name);
}
}
class Result extends Student
{ float m1,m2;

Devang Chouhan 6 2100BTCSE11490


void getmarks(float mr1,float mr2) {
m1=mr1;
m2=mr2;
}
void showmarks() {
System.out.println(" marks of I subject : "+m1);
System.out.println("\n marks of II subject : "+m2);
}
float showresult() {
return((m1+m2)/2);
}
}
class DevendraLavvanshi {
public static void main(String args[])
{
Result rs=new Result();
rs.get(10,"DevendraLavvanshi ");
rs.getmarks(80f,90f);
rs.show();
rs.showmarks();
System.out.println(" result of student is "+rs.showresult());
}
}

Results

Devang Chouhan 7 2100BTCSE11490


Quiz & Viva Questions
1.2.4 Quiz:
What is the purpose of single-level inheritance in Java?
a) To create multiple objects
b) To reuse code and extend the functionality of a class
c) To encapsulate data and methods
d) To implement polymorphism

In the given program, which class is the base class?


a) Result
b) Student
c) AnujVyas
d) None of the above
1.3 Viva
1.3.1 Demonstrate single-level inheritance in Java.
1.3.2 Illustrate the concept of extending a class to inherit its properties and methods.
1.3.3 Understand the relationship between the base class (Student) and the derived class
(Result).

Devang Chouhan 8 2100BTCSE11490


Experiment :4
1. Title: Write a program in java to demonstrate Multi level Inheritance
2. Outcome: The outcome of the experiment is to demonstrate the usage of classes and multiple inheritance
in Java programming.
3. Objectives:
1. Understand the concept of multi-level inheritance in Java.
2. Learn how to create an inheritance hierarchy with multiple levels.
3. Demonstrate the reuse of code and extension of functionality using multi-level inheritance.

4. Nomenclature, theory with self-assessment questionnaire:


4.1 Nomenclature:
 Vehicle class:
o drive() - Method that represents the action of driving a vehicle.
 Car class (extends Vehicle):
o accelerate() - Method that represents the action of accelerating a car.
 SportsCar class (extends Car):
o turbocharge() - Method that represents the action of turbocharging a sports car.
 AnujVyas class:
o main(String[] args) - The main method where program execution starts.

4.2 Solution:
The program defines two classes: A and Chirag. Class A has two instance variables a and b, a get
method to set the values of a and b, and a show method to display the values of a and b. The
Radhika class contains the main method where an object ob1 of class A is created, and the get
method is called to set the values of a and b. Finally, the show method is called to display the
values of a and

4.3 Code/ Pseudo Code


class A {
int a;
float b;
void get (int a1, float b1) {
a=a1;
b=b1;
}
void show()
{ System.out.println("a=
"+a); System.out.println("b=
"+b);
}
}
Devang Chouhan 9 2100BTCSE11490
class DevendraLavvanshi{
public static void main(String[] args)
{ A ob1=new A();
ob1.get(11,2.1f);
ob1.show();
}
}

4.4 Results

5. Quiz & Viva Questions


5.1 Quiz:
6.1.1 What is multi-level inheritance?
a) Inheritance of properties and methods from multiple classes
b) Inheritance of properties and methods from a single class
c) Inheritance of properties and methods between unrelated classes
d) None of the above
6.1.2 In the given program, which class is the base class?
a) Car
b) Vehicle
c) SportsCar
d) None of the above
6.1.3 What is the purpose of multi-level inheritance in Java?
a) To create multiple objects
b) To reuse code and extend the functionality of a class
c) To encapsulate data and methods
d) To implement polymorphism
6.2 Viva
6.2.1 What is multi-level inheritance? Provide an example.
6.2.2 Explain the concept of inheritance hierarchy in the given program.
6.2.3 How does multi-level inheritance promote code reusability?

Devang Chouhan 10 2100BTCSE11490


Experiment : 5
1. Title: Write a program in java to demonstrate Constructor
2. Outcome: The outcome of the experiment is to demonstrate the usage of classes and constructor in Java
programming.
3. Objectives:
 To demonstrate the use of constructors in Java.
 To initialize the state of objects during their creation.
 To showcase the concept of constructor overloading.
4. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
 Student: A class representing a student.
 rollNumber: An instance variable to store the roll number of the student.
 name: An instance variable to store the name of the student.
 display(): A method to display the roll number and name of the student.
 DevendraLavvanshi: The main class that contains the main method to execute the
program.

4.2 Solution:
 Student class: It has a parameterized constructor that takes the roll number and name as input
and initializes the instance variables. It also has a method display() to print the student's
information.
 AnujVyas class: It creates objects of the Student class using the constructor and calls the
display() method to print the student information.

4.3 Code/ Pseudo Code
class Student {
int rollNumber;
String name;
public Student(int rollNumber, String name)
{ this.rollNumber = rollNumber;
this.name = name;
}
void display() {
System.out.println("Roll Number: " + rollNumber);
System.out.println("Name: " + name);
}
}

Devang Chouhan 11 2100BTCSE11490


class DevendraLavvanshi{
public static void main(String[] args) {
// Create objects of Student class using constructors
Student student1 = new Student(101, "John Doe");
Student student2 = new Student(102, "Jane Smith");

// Display student information


student1.display();
System.out.println();
student2.display();}}
4.4 Results

5. Quiz & Viva Questions


6.2 Quiz:

What is a constructor in Java?


a) A method used to initialize objects of a class.
b) A method used to perform mathematical calculations.
c) A method used to modify the state of an object.
d) A method used to create new instances of a class.

What is the purpose of a constructor?


a) To allocate memory for an object.
b) To initialize the instance variables of an object.
c) To define the behavior of an object.
d) To create multiple instances of a class.

6.3 Viva
 What is the difference between a constructor and a method?
 Can a class have multiple constructors?
 What is the role of the ‘this’ keyword in a constructor?

Devang Chouhan 12 2100BTCSE11490


Experiment : 6
1. Title: Write a program in java to demonstrate Abstract Class
2. Outcome: The outcome of the experiment is to demonstrate the usage of Abstract class in Java
programming.
3. Objectives:
 To understand the concept of abstract classes in Java.
 To demonstrate the usage of abstract methods and concrete methods within an abstract class.
 To showcase the inheritance and polymorphism features in Java.
4. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
 Animal: An abstract class representing an animal.
 Dog: A concrete subclass of Animal representing a dog.
 Cat: A concrete subclass of Animal representing a cat.
 sound(): An abstract method in the Animal class to be implemented by its subclasses.
 sleep(): A non-abstract method in the Animal class that provides a common behavior.

4.2 Solution:
The program defines an abstract class Animal with an abstract method sound() and a non-
abstract method sleep(). It also includes two concrete subclasses Dog and Cat, which extend
the Animal class and provide their own implementations of the sound() method. The
AnujVyas class creates objects of the Dog and Cat classes, invokes the sound() and sleep()
methods, and demonstrates polymorphic behavior.

4.3 Code/ Pseudo Code


abstract class Animal
{ String name;
abstract void sound();
void sleep() {
System.out.println(name + " is sleeping.");
}}
class Dog extends Animal
{ Dog(String name) {
this.name = name;
}

Devang Chouhan 13 2100BTCSE11490


void sound() {
System.out.println(name + " is barking.");
}}
class Cat extends Animal
{ Cat(String name) {
this.name = name;
}
void sound() {
System.out.println(name + " is meowing.");
}}
class DevendraLavvanshi{
public static void main(String[] args)
{ Animal dog = new Dog("Buddy");
dog.sound();
dog.sleep();
Animal cat = new Cat("Whiskers");
cat.sound();
cat.sleep();}}
4.4 Results

5. Quiz & Viva Questions


6.4 Quiz:
Can you instantiate an object of an abstract class directly in Java?
a) Yes
b) No
c) Only if it has a default constructor
d) Only if all its methods are implemented
What is the purpose of an abstract method in an abstract class?
a) To provide a default implementation
b) To force the subclasses to implement it
c) To prevent instantiation of the abstract class
d) To override methods in the subclasses
6.5 Viva
 What is the difference between an abstract class and an interface?

Devang Chouhan 14 2100BTCSE11490


 Can an abstract class have both abstract and non-abstract methods?
Experiment : 7
1. Title: Write a program in java to demonstrate Interface.
2. Outcome: The outcome of the experiment is to demonstrate the usage of Interface in Java programming.
3. Objectives:
 To understand the concept of interfaces in Java.
 To demonstrate the usage of interfaces to define contracts and common behavior.
 To showcase polymorphism through interface implementations.

4. Nomenclature, theory with self-assessment questionnaire:


4.1 Nomenclature:
 Vehicle: An interface representing a vehicle.
 Car: A class that implements the Vehicle interface, representing a car.
 Bike: A class that implements the Vehicle interface, representing a bike.
 start(): A method defined in the Vehicle interface to start a vehicle.
 stop(): A method defined in the Vehicle interface to stop a vehicle.
4.2 Solution:
The program defines an interface Vehicle with two abstract methods, start() and stop(). It also
includes two classes, Car and Bike, which implement the Vehicle interface and provide their
own implementations of the methods.
4.3 Assumptions: N/A
4.4 Dependencies: N/A
4.5 Code/ Pseudo Code
interface Vehicle
{ void start();
void stop();
}
class Car implements Vehicle
{ public void start() {
System.out.println("Car started.");
}
public void stop()
{ System.out.println("Car
stopped.");
}}
class Bike implements Vehicle
{ public void start() {
System.out.println("Bike started.");
}

public void stop()


{ System.out.println("Bike
Devang Chouhan 15 2100BTCSE11490
stopped.");

Devang Chouhan 16 2100BTCSE11490


}}
Class DevendraLavvanshi{
public static void main(String[] args)
{ Vehicle car = new Car();
car.start();
car.stop();
Vehicle bike = new Bike();
bike.start();
bike.stop();
}}
4.6 Results

5. Quiz & Viva Questions


6.6 Quiz:
Can a class implement multiple interfaces in Java?
a) Yes
b) No
c) Only if the interfaces have the same methods
d) Only if the interfaces have different names
What is the purpose of an interface in Java?
a) To provide a default implementation of methods
b) To define a contract for classes to implement
c) To prevent inheritance of a class
d) To create objects directly
6.7 Viva
 What is the difference between an abstract class and an interface?
 Can an interface have variables in Java?
 Can a class extend an interface in Java?

Devang Chouhan 17 2100BTCSE11490


Experiment : 8
1. Title: Write a program in java to demonstrate AWT.
2. Outcome: The outcome of the experiment is to demonstrate the usage of AWT in making GUI in Java
programming.
3. Objectives:
 To understand the concept of interfaces in Java.
 To demonstrate the usage of interfaces to define contracts and common behavior.
 To showcase polymorphism through interface implementations.

4. Nomenclature, theory with self-assessment questionnaire:


4.1 Nomenclature:
 Frame: A class representing a window in a GUI application.
 Label: A class representing a text label in a GUI application.
 TextField: A class representing a text input field in a GUI application.
 Button: A class representing a button in a GUI application.
4.2 Solution:
The program uses AWT to create a simple GUI application. It creates a frame, labels, text fields,
and a button. The components are added to the frame, and the layout is set to ‘FlowLayout’. The
frame's size is set, and it is made visible.

4.3 Code/ Pseudo Code


import java.awt.*;
public class DevendraLavvanshi{
public static void main(String arg[])
{
Frame f = new Frame("DevendraLavvanshiCHouahn");
Label l1 = new Label("First");
Label l2 = new Label("Second");
TextField t1 = new TextField(10);
TextField t2 = new TextField(10);
Button b = new Button("SWAP");
f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.setLayout(new FlowLayout());
f.add(b);
f.setSize(500, 600);

Devang Chouhan 18 2100BTCSE11490


f.setVisible(true);

Devang Chouhan 19 2100BTCSE11490


}}

RESULT:

5. Quiz & Viva Questions


6.8 Quiz:
What does AWT stand for?
a) Abstract Window Toolkit
b) Advanced Widget Technology
c) Application Window Toolkit
d) All Window Tools

Which class represents a text input field in AWT?


a) TextField
b) InputField
c) TextInput
d) TextEntry

6.9 Viva
 What are the key classes and interfaces in AWT?
 How can you add components to a frame in AWT?
 What is the purpose of setting the layout in AWT?
 How can you handle events in AWT?

Devang Chouhan 20 2100BTCSE11490


Experiment : 9
1. Title: Write a program in java to demonstrate Button in AWT.
2. Outcome: The outcome of the experiment is to demonstrate the usage of Button in making GUI with
AWT package in Java programming.
3. Objectives:
 To understand the usage of buttons in AWT-based GUI applications.
 To create a button and add it to a frame.
 To position and size the button within the frame.
4. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
 Frame: A class representing a window in a GUI application.
 Button: A class representing a button component in AWT.
4.2 Solution:
The program demonstrates the creation of a button in an AWT-based GUI application. It creates a
frame, adds a button to it with the label "Click Me", sets the position and size of the button, and
makes the frame visible.’

4.3 Code/ Pseudo Code

import java.awt.*;
public class DevendraLavvanshi{
public static void main(String[] args){
Frame frame = new Frame("DevendraLavvanshi AWT Program");
Button btn = new Button("Click Me : DevendraLavvanshi ");
btn.setBounds(100,100,200,50);
frame.add(btn);

frame.setLayout(null);
frame.setSize(500,600);
frame.setVisible(true);
}
}

Devang Chouhan 21 2100BTCSE11490


4.4 Results

5. Quiz & Viva Questions


6.10 Quiz:
Which class is used to create a button in AWT?
a) Button
b) AWTButton
c) GuiButton
d) AWTComponent

How do you set the position and size of a button within a frame?
a) Using the setBounds() method
b) Using the setPosition() and setSize() methods
c) Using the setLocation() and setDimension() methods
d) Using the setPositionAndSize() method

6.11 Viva
 How can you handle button clicks or events in AWT?
 What layout managers are available in AWT for arranging components?
 How does the setBounds() method work in AWT?

Devang Chouhan 22 2100BTCSE11490


Experiment : 10
1. Title: Write a program in java to demonstrate Label in AWT.
2. Outcome: The outcome of the experiment is to demonstrate the usage of Label in making GUI with
AWT package in Java programming.
3. Objectives:
 To understand the usage of labels in AWT-based GUI applications.
 To create labels and add them to a frame.
 To position and size the labels within the frame.
4. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
 Frame: A class representing a window in a GUI application.
 Label: A class representing a label component in AWT.

4.2 Solution:
The program demonstrates the creation of labels in an AWT-based GUI application. It creates a
frame, adds two labels to it, sets the position and size of the labels, and makes the frame visible.

4.3 Code/ Pseudo Code


import java.awt.*;
public class DevendraLavvanshi{
public static void main(String args[]) {
Frame f = new Frame("DevendraLavvanshi ");
Label l1, l2;
l1 = new Label("First Label.");
l2 = new Label("Second Label.");
l1.setBounds(50, 100, 100, 30);
l2.setBounds(50, 150, 100, 30);
f.add(l1);
f.add(l2);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}
}

Devang Chouhan 23 2100BTCSE11490


4.4 Results

5. Quiz & Viva Questions


6.12 Quiz:
Which class is used to create labels in AWT?
a) Label
b) AWTLabel
c) GuiLabel
d) AWTComponent

How do you set the position and size of a label within a frame?
a) Using the setBounds() method
b) Using the setPosition() and setSize() methods
c) Using the setLocation() and setDimension() methods
d) Using the setPositionAndSize() method

6.13 Viva
 How can you change the text of a label dynamically in AWT?
 What layout managers are available in AWT for arranging components?
 How does the setBounds() method work in AWT?

Devang Chouhan 24 2100BTCSE11490


Experiment : 11
1. Title: Write a program in java to demonstrate TextField in AWT.
2. Outcome: The outcome of the experiment is to demonstrate the usage of TextField in making GUI with
AWT package in Java programming.
3. Objectives:
 To understand the usage of text fields in AWT-based GUI applications.
 To create a text field and add it to a frame.
 To set the position and size of the text field within the frame.
4. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
 Frame: A class representing a window in a GUI application.
 Text Field: A class representing a text input component in AWT.

4.2 Solution:
The program demonstrates the creation of labels in an AWT-based GUI application. It creates a
frame, adds two labels to it, sets the position and size of the labels, and makes the frame visible.

4.3 Code/ Pseudo Code


import java.awt.*;
public class Chirag{
public static void main(String args[]) {
Frame f = new Frame("Text Field
Demo"); TextField tf = new TextField();
tf.setBounds(50, 50, 200, 30);
f.add(tf);
f.setSize(400, 200);
f.setLayout(null);
f.setVisible(true);
}
}

Devang Chouhan 25 2100BTCSE11490


4.4 Results

5. Quiz & Viva Questions


6.14 Quiz:
Which class is used to create text fields in AWT?
a) TextField
b) AWTTextField
c) GuiTextField
d) AWTComponent

How do you set the position and size of a text field within a frame?
a) Using the setBounds() method
b) Using the setPosition() and setSize() methods
c) Using the setLocation() and setDimension() methods
d) Using the setPositionAndSize() method

6.15 Viva
 How can you restrict the input in a text field to a specific format or range?
 What events can be associated with a text field in AWT?
 How does the getText() method work in AWT text fields?

Devang Chouhan 26 2100BTCSE11490


Experiment : 12
1. Title: Write a program in java to demonstrate Text Area in AWT.
2. Outcome: The outcome of the experiment is to demonstrate the usage of Text Area in making GUI with
AWT package in Java programming.
3. Objectives:
 To understand the usage of text area in AWT-based GUI applications.
 To create a text area and add it to a frame.
 To set the position and size of the text area within the frame.
4. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
 Frame: A class representing a window in a GUI application.
 TextArea: A class representing a multi-line text input component in AWT.

4.2 Solution:
The program demonstrates the creation of a text area in an AWT-based GUI application. It creates a
frame, adds a text area to it, sets the position and size of the text area, and makes the frame visible.

4.3 Code/ Pseudo Code


import java.awt.*;
public class SVVV {
public static void main(String args[])
{ Frame f = new Frame("Text Area
Demo"); TextArea ta = new TextArea();
ta.setBounds(50, 50, 300, 200);
f.add(ta);
f.setSize(400, 300);
f.setLayout(null);
f.setVisible(true);
}
}

Devang Chouhan 27 2100BTCSE11490


4.4 Results

5. Quiz & Viva Questions


6.16 Quiz:
Which class is used to create text areas in AWT?
a) TextArea
b) AWTTextField
c) GuiTextField
d) AWTComponent

How do you set the position and size of a text field within a frame?
a) Using the setBounds() method
b) Using the setPosition() and setSize() methods
c) Using the setLocation() and setDimension() methods
d) Using the setPositionAndSize() method

6.17 Viva
 How can you retrieve the text entered in a text area in AWT?
 How can you set the initial text content of a text area in AWT?
 What methods can be used to modify the appearance of a text area, such as its font, color,
or alignment?
 How does the append() method work in AWT text areas?

Devang Chouhan 28 2100BTCSE11490


Experiment : 13
1. Title: Write a program in java to demonstrate Checkbox in AWT.
2. Outcome: The outcome of the experiment is to demonstrate the usage of Checkbox in making GUI with
AWT package in Java programming.
3. Objectives:
 To understand the usage of text area in AWT-based GUI applications.
 To create a checkbox and add it to a frame.
 To set the position and size of the checkbox within the frame.
4. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
 Frame: A class representing a window in a GUI application.
 Checkbox: A class representing a checkbox component in AWT.

4.2 Solution:
The program demonstrates the creation of checkboxes in an AWT-based GUI application. It
creates a frame, adds checkboxes to it, sets their position and size, and makes the frame visible.
Additionally, it sets the title of the frame using the setTitle() method.

4.3 \Code/ Pseudo Code


import java.awt.*;
public class SVVV{
public static void main(String[] args) {
Frame frame = new Frame("SVVV "); // Set frame title
Checkbox checkbox1 = new Checkbox("Checkbox 1");
checkbox1.setBounds(100, 100, 100, 30);
Checkbox checkbox2 = new Checkbox("Checkbox 2");
checkbox2.setBounds(100, 150, 100, 30);
frame.add(checkbox1);
frame.add(checkbox2);
frame.setLayout(null);
frame.setSize(400, 300);
frame.setVisible(true);
}
}

Devang Chouhan 29 2100BTCSE11490


4.4 Results

5. Quiz & Viva Questions


6.18 Quiz:
Which class is used to create CheckBox in AWT?
a) TextArea
b) AWTTextField
c) Checkbox
d) AWTComponent

How do you set the position and size of a text field within a frame?
a) Using the setBounds() method
b) Using the setPosition() and setSize() methods
c) Using the setLocation() and setDimension() methods
d) Using the setPositionAndSize() method

6.19 Viva
 How can you check if a checkbox is selected or not in AWT?
 How can you add an action listener to a checkbox to perform some action when it is
clicked?
 Can multiple checkboxes be selected at the same time in AWT?
 How does the setSelected() method work in AWT checkboxes?

Devang Chouhan 30 2100BTCSE11490


Experiment : 14
1. Title: Write a program in java to demonstrate Choice in AWT.
2. Outcome: The outcome of the experiment is to demonstrate the usage of Choice in making GUI with
AWT package in Java programming.
3. Objectives:
 To understand the usage of choice in AWT-based GUI applications.
 To create a dropdown menu for selecting options using the Choice class..
 To add options to the Choice using the add() method.
 To retrieve the selected option from the Choice component.
4. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
 Choice: A class representing a dropdown menu component in AWT.
 ActionListener: An interface used to handle action events.
4.2 Solution:
The program demonstrates the usage of the Choice component in AWT to create a dropdown
menu for selecting options. It adds the Choice to a frame, adds options to it, and attaches an
ActionListener to a button to display the selected option when clicked.

4.3 Code/ Pseudo Code


import java.awt.*;
import
java.awt.event.*; public
class SVVV {
public static void main(String[] args) {
Frame frame = new Frame("SVVV "); // Set frame title
Choice choice = new Choice();
choice.setBounds(100, 100, 100, 30);
choice.add("Option 1");
choice.add("Option 2");
choice.add("Option 3");
Button button = new Button("Show Selected");
button.setBounds(100, 150, 100, 30);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{ String selectedOption =
choice.getSelectedItem();
System.out.println("Selected Option: " + selectedOption);
}
});

frame.add(choice);
Devang Chouhan 31 2100BTCSE11490
frame.add(button);

Devang Chouhan 32 2100BTCSE11490


frame.setLayout(null);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
4.4 Results

5. Quiz & Viva Questions


6.20 Quiz:
Which class is used to create a dropdown menu in AWT?
a) ComboBox
b) Choice
c) Dropdown
d) AWTCombo

How can you add options to a Choice component?


a) Using the add() method
b) Using the setOptions() method
c) Using the setItems() method
d) Using the addOption() method

6.21 Viva
 How can you retrieve the selected option from a Choice component in AWT?
 Can a Choice component allow multiple options to be selected at the same time?
 What is the purpose of the addActionListener() method in AWT buttons?
 How can you handle the button click event in AWT?

Devang Chouhan 33 2100BTCSE11490


Experiment : 15
6. Title: Write a program in java to demonstrate List in AWT.
7. Outcome: The outcome of the experiment is to demonstrate the usage of List in making GUI with AWT
package in Java programming.
8. Objectives:
 To understand the usage of the List component in AWT-based GUI applications.
 To create a selectable list of items using the List class.
 To add items to the List component.
9. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
 List: A class representing a selectable list component in AWT.
9.2 Solution:
The program demonstrates the usage of the List component in AWT to create a selectable list of
items. It creates a frame, creates a List component, adds items to it, and adds the list to the frame.

9.3 Code/ Pseudo Code


import java.awt.*;
public class MyList
{
public static void main(String[] args){
Frame frame = new Frame("My First AWT program"); // Set frame title

List list = new List(5); // Create a List component with 5 visible rows
list.setBounds(100, 100, 200, 200); // Set position and size of the list
list.add("List Item one"); // Add items to the list
list.add("List Item two");
list.add("List Item three");
list.add("List Item four");
list.add("List Item five");
frame.add(list); // Add the list to the frame

frame.setLayout(null);
frame.setSize(500, 600); // Set size of the frame
frame.setVisible(true); // Make the frame visible
}
}}

Devang Chouhan 34 2100BTCSE11490


9.4 Results

10. Quiz & Viva Questions


6.22 Quiz:

Which class is used to create a selectable list of items in AWT?


a) Choice
b) ComboBox
c) List
d) Dropdown

How many visible rows are set for the list in the provided code?
a) 3
b) 4
c) 5
d) 6

6.23 Viva
 How can you retrieve the selected option from a Choice component in AWT?
 Can a Choice component allow multiple options to be selected at the same time?
 What is the purpose of the addActionListener() method in AWT buttons?
 How can you handle the button click event in AWT?

Devang Chouhan 35 2100BTCSE11490


Experiment : 16
1. Title: Write a program in java to demonstrate Mouse Listner in AWT.
2. Outcome: The program demonstrates Mouse Listener in AWT by creating a frame with a label. The
label is registered with a MouseListener, which allows it to capture mouse events. When the mouse is
clicked, pressed, released, entered, or exited, the corresponding MouseListener methods are invoked, and
the relevant information is printed to the console.
3. Objectives:
 Understand the concept of Mouse Listener in AWT.
 Learn how to register Mouse Listeners for capturing mouse events.
 Handle mouse events such as mouse clicking, pressing, releasing, entering, and exiting.
 Retrieve information about the mouse coordinates during the events.
4. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
 Frame: The top-level container that represents a window in AWT.
 Label: A component used for displaying text or images.
 MouseListener: An interface that defines the mouseClicked(), mousePressed(),
mouseReleased(), mouseEntered(), and mouseExited() methods for handling mouse
events.
 addMouseListener(): A method to register a MouseListener for a component.
 getX(): A method to retrieve the x-coordinate of the mouse cursor.
 getY(): A method to retrieve the y-coordinate of the mouse cursor.
 MouseEvent: A class that encapsulates information about mouse events.
4.2 Solution:
The program creates a frame with the title "Key Listener Demo". It creates a text field using the
TextField class. The position and size of the text field are set using the setBounds() method. A
KeyListener is registered for the text field using an anonymous inner class that overrides the
keyTyped(), keyPressed(), and keyReleased() methods. These methods are invoked when a key is
typed, pressed, or released, respectively. The relevant information, such as the key character or
key code, is retrieved from the KeyEvent object and printed to the console. The text field is added
to the frame, and the layout is set to null to manually position the component. Finally, the size
and visibility of the frame are set.

4.3 Code/ Pseudo Code


import java.awt.*;
import
java.awt.event.*;

public class SVVV


{ private Frame frame;
private Label label;

Devang Chouhan 36 2100BTCSE11490


public MouseListenerDemo() {
frame = new Frame("Mouse Listener Demo");
label = new Label();
label.setBounds(100, 100, 200, 30);
// Registering the label with a MouseListener
label.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
// Invoked when the mouse button is clicked
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Clicked at (" + x + ", " + y + ")");
}
public void mousePressed(MouseEvent e) {
// Invoked when the mouse button is pressed
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Pressed at (" + x + ", " + y + ")");
}
public void mouseReleased(MouseEvent e) {
// Invoked when the mouse button is released
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Released at (" + x + ", " + y + ")");
}
public void mouseEntered(MouseEvent e) {
// Invoked when the mouse enters the component
System.out.println("Mouse Entered");
}
public void mouseExited(MouseEvent e) {
// Invoked when the mouse exits the component
System.out.println("Mouse Exited");
}
});
frame.add(label);
frame.setLayout(null);
frame.setSize(400, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
new SVVV();
}}}

Devang Chouhan 37 2100BTCSE11490


4.4 Results

5. Quiz & Viva Questions


6.24 Quiz:
 What is Mouse Listener in AWT?
 Which interface defines the methods for handling mouse events?
 How can you register a MouseListener for a component in AWT?
 What are some common mouse events that can be handled using Mouse Listener?
 How can you retrieve the mouse coordinates during a mouse event?

Devang Chouhan 38 2100BTCSE11490


Experiment : 17
1. Title: Write a program in java to demonstrate Calculator in AWT.
2. Outcome: The program demonstrates a simple calculator using AWT. It creates a frame with a text field
for displaying the input and output. The calculator supports basic arithmetic operations such as addition,
subtraction, multiplication, and division. Buttons are used to input numbers and operators, and an equals
button is used to evaluate the expression. The expression is evaluated using a recursive descent parser.
3. Objectives:
 Understand the concept of a calculator using AWT.
 Learn how to create buttons and text fields in AWT.
 Implement action listeners to handle button clicks.
 Use an expression parser to evaluate arithmetic expressions.
 Display the evaluated result in the text field.
4. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
 Frame: A top-level window in AWT.
 TextField: A component used for displaying text.
 Button: A component used for buttons.
 ActionListener: An interface that defines the actionPerformed() method for
handling button clicks.
 addActionListener(): A method to register an action listener for a button.
 eval(): A method that uses a recursive descent parser to evaluate arithmetic expressions.
 parse(), parseExpression(), parseTerm(), parseFactor(): Methods used in the
expression parser.
4.2 Solution:
The program creates a frame with the title "Calculator". It creates a text field for displaying the
input and output of the calculator. Numeric buttons (0-9), operator buttons (+, -, *, /), and an
equals button (=) are created using the Button class. The numeric buttons are registered with an
action listener that appends the corresponding digit to the text field. The operator buttons append
the operator to the text field. The equals button evaluates the expression using the eval() method,
which uses a recursive descent parser. The result is displayed in the text field. The frame layout is
set to null, and the frame size is set to accommodate the calculator. The frame is made visible.

4.3 Code/ Pseudo Code


import java.awt.*;
import
java.awt.event.*;

public class SVVV


{ private Frame frame;
private TextField textField;

public SVVV() {
Devang Chouhan 39 2100BTCSE11490
frame = new Frame("Calculator");
textField = new TextField();
textField.setBounds(50, 50, 300, 30);
textField.setEditable(false);

Button[] numberButtons = new Button[10];


for (int i = 0; i < 10; i++) {
numberButtons[i] = new Button(String.valueOf(i));
}

Button addButton = new Button("+");


Button subtractButton = new Button("-");
Button multiplyButton = new Button("*");
Button divideButton = new Button("/");
Button equalsButton = new Button("=");

addButton.setBounds(50, 100, 50, 50);


subtractButton.setBounds(120, 100, 50, 50);
multiplyButton.setBounds(190, 100, 50, 50);
divideButton.setBounds(260, 100, 50, 50);
equalsButton.setBounds(330, 100, 50, 50);

ActionListener numberListener = new ActionListener()


{ public void actionPerformed(ActionEvent e) {
Button button = (Button) e.getSource();
String buttonText = button.getLabel();
String currentText = textField.getText();
textField.setText(currentText + buttonText);
}
};

for (int i = 0; i < 10; i++)


{ numberButtons[i].setBounds(50 + (i * 70), 170, 50,
50);
numberButtons[i].addActionListener(numberListener);
}

addButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "+");
}
});
Devang Chouhan 40 2100BTCSE11490
subtractButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "-");
}
});

multiplyButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "*");
}
});

divideButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "/");
}
});

equalsButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e) {
String expression = textField.getText();
String result = evaluateExpression(expression);
textField.setText(result);
}
});

frame.add(textField);
for (int i = 0; i < 10; i++) {
frame.add(numberButtons[i]);
}
frame.add(addButton);
frame.add(subtractButton);
frame.add(multiplyButton);
frame.add(divideButton);
frame.add(equalsButton);

frame.setLayout(null);
frame.setSize(400, 300);
frame.setVisible(true);
}

Devang Chouhan 41 2100BTCSE11490


private String evaluateExpression(String expression)
{ try {
return String.valueOf(eval(expression));
} catch (Exception ex)
{ return "Error";
}
}

private double eval(final String expression)


{ return new Object() {
int pos = -1, ch;

void nextChar() {
ch = (++pos < expression.length()) ? expression.charAt(pos) : -1;
}

boolean eat(int charToEat)


{ while (ch == ' ') {
nextChar();
}
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}

double parse() {
nextChar();
double x = parseExpression();
if (pos < expression.length()) {
throw new RuntimeException("Unexpected: " + (char) ch);
}
return x;
}

double parseExpression() {
double x = parseTerm();
while (true) {
if (eat('+')) {

Devang Chouhan 42 2100BTCSE11490


x += parseTerm();
} else if (eat('-')) {
x -= parseTerm();
} else {
return x;
}
}
}

double parseTerm()
{ double x =
parseFactor(); while
(true) {
if (eat('*')) {
x *= parseFactor();
} else if (eat('/')) {
x /= parseFactor();
} else {
return x;
}
}
}

double parseFactor()
{ if (eat('+')) {
return parseFactor();
}
if (eat('-')) {
return -parseFactor();
}

double x;
int startPos = this.pos;
if (eat('(')) {
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') {
while ((ch >= '0' && ch <= '9') || ch == '.') {
nextChar();
}
x = Double.parseDouble(expression.substring(startPos, this.pos));
} else {
Devang Chouhan 43 2100BTCSE11490
throw new RuntimeException("Unexpected: " + (char) ch);
}
return x;
}
}.parse();
}

public static void main(String[] args) {


new SVVV();
}}

Results

5. Quiz & Viva Questions


6.25 Quiz:
 How does the calculator handle arithmetic operations?
 What is the purpose of the eval() method?
 How are the numeric buttons registered with an action listener?
 What happens when the equals button is clicked?
 How is the result displayed in the text field?

Devang Chouhan 44 2100BTCSE11490


Experiment : 18
1. Title: Write a program in java to demonstrate Dialer in AWT.
2. Outcome: The program demonstrates Item Listener in AWT by creating a frame with a checkbox. The
checkbox is registered with an ItemListener, which allows it to capture item state changes. When the
checkbox is checked or unchecked, the itemStateChanged() method of the ItemListener is invoked,
and the corresponding message is printed to the console.
3. Objectives: The objective of the program is to demonstrate how to create a dialer GUI using AWT
components and handle user interactions with event listeners. It showcases the basic usage of AWT
buttons, labels, text fields, and event handling mechanisms.
4. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
 Frame: It represents the main window of the application.
 Label: It displays a text label for the dialer.
 TextArea: It provides a multi-line text input area for displaying the entered number.
 TextField: It provides a single-line text input field for displaying the dialing status.
 Button: It represents the buttons for the dialer's numeric keypad and the "DIAL" button.
 ActionListener: It handles the action events generated by the buttons.
 WindowAdapter: It handles the window closing event to exit the program.
4.2 Solution:
The Dialer program utilizes AWT components to create a dialer-like GUI. It consists of a Frame
as the main window, a Label to display the dialer label, a TextArea to display the entered phone
number, a TextField to display the dialing status, and Buttons to represent the numeric keypad
and the "DIAL" button.
ActionListener is implemented to handle button click events. When a digit button is clicked, the
corresponding digit is appended to the text area. Clicking the "DIAL" button concatenates the
entered number with a dialing message displayed in the text field.

4.3 Code/ Pseudo Code


import java.awt.*;
import java.awt.event.*;
public class SVVV{
public static void main(String[] args)
{ Frame f = new Frame("SVVV ");
Label l = new Label("SVVV Dialer");
TextArea t1 = new TextArea();
TextField t2 = new TextField();
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");

Devang Chouhan 45 2100BTCSE11490


Button b5 = new Button("5");
Button b6 = new Button("6");
Button b7 = new Button("7");
Button b8 = new Button("8");
Button b9 = new Button("9");
Button b10 = new Button("0");
Button b11 = new Button("DIAL!!");
l.setAlignment(Label.CENTER);
l.setBounds(0,30,300,30);
f.add(l);
t1.setBounds(20,60,260,60);
f.add(t1);
b1.setBounds(50,150,60,60);
f.add(b1);
b2.setBounds(120,150,60,60);
f.add(b2);
b3.setBounds(190,150,60,60);
f.add(b3);
b4.setBounds(50,220,60,60);
f.add(b4);
b5.setBounds(120,220,60,60);
f.add(b5);
b6.setBounds(190,220,60,60);
f.add(b6);
b7.setBounds(50,290,60,60);
f.add(b7);
b8.setBounds(120,290,60,60);
f.add(b8);
b9.setBounds(190,290,60,60);
f.add(b9);
b10.setBounds(70,360,160,60);
f.add(b10);
b11.setBounds(70,430,160,60);
f.add(b11);
t2.setBounds(30,500,240,40);
f.add(t2);

b1.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e){
t1.setText(t1.getText().concat("1"));
} });

Devang Chouhan 46 2100BTCSE11490


b2.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e){
t1.setText(t1.getText().concat("2"));
} });
b3.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e){
t1.setText(t1.getText().concat("3"));
} });
b4.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e){
t1.setText(t1.getText().concat("4"));
} });
b5.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e){
t1.setText(t1.getText().concat("5"));
} });
b6.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e){
t1.setText(t1.getText().concat("6"));
} });
b7.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e){
t1.setText(t1.getText().concat("7"));
} });
b8.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e){
t1.setText(t1.getText().concat("8"));
} });
b9.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e){
t1.setText(t1.getText().concat("9"));
} });
b10.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e){
t1.setText(t1.getText().concat("0"));
} });
b11.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e){
t2.setText(t1.getText().concat(" Dialing your number!!"));
}
});

Devang Chouhan 47 2100BTCSE11490


f.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e1)
{
System.exit(0);
} });
f.setLayout(null);
f.setVisible(true);
f.setSize(300,550);
f.setBackground(Color.lightGray);
}
}
4.4 Results

4.5

Devang Chouhan 48 2100BTCSE11490


Quiz
Which AWT component is used to display a label for the dialer?
a) TextArea
b) TextField
c) Button
d) Label
What is the purpose of ActionListener in the Dialer program?
a) To handle window closing events
b) To display the dialing status
c) To append digits to the text area
d) To create the numeric keypad buttons
Which event is triggered when a button on the dialer is clicked?
a) KeyPress event
b) MouseClick event
c) ActionEvent
d) WindowClosing event

What happens when the "DIAL" button is clicked in the Dialer program?
a) The program exits
b) The dialing status is cleared
c) The entered number is displayed in the text area
d) The dialing process starts
Which layout manager is used in the Dialer program?
a) BorderLayout
b) FlowLayout
c) GridLayout
d) CardLayout

5. Viva
1. Explain the purpose of the Dialer program.
2. How does the Dialer program use AWT components to create the GUI?
3. Describe the role of ActionListener in the Dialer program.
4. How does the program handle button click events?
5. What is the function of the TextArea component in the Dialer program?
6. Which class is used to handle the window closing event?
7. What is the size of the Frame in the Dialer program?
8. Which color is used to set the background of the Frame?
9. Can you explain the purpose of the WindowAdapter class?
10. How does the program exit gracefully when the window is closed?

Devang Chouhan 49 2100BTCSE11490


Experiment : 19
1. Title: Write a program in java to demonstrate Frame in Swing.
2. Outcome: The program creates a Swing frame, which is a top-level container that can be used to hold
other Swing components. It demonstrates the use of the JFrame class to create a graphical user interface
window in Java Swing.
3. Objectives: The objective of the program is to create a Swing frame using the JFrame class. The
program creates a basic frame with a specified size and displays it on the screen.
4. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
4.1.1 Frame: Frame is component that contains all other component of GUI.
4.2 Solution:
The program creates a frame with the title "SubMenu Demo". It creates a MenuBar and a top-
level Menu called "Menu". Inside the top-level menu, it creates two MenuItem objects ("Item 1"
and "Item 2"), and a SubMenu called "Sub Menu". The sub-menu contains two MenuItem objects
("Sub Item 1" and "Sub Item 2"). The program adds the menu items and sub-menu to the top-
level menu using the add() method. Finally, it sets the menu bar for the frame, sets the layout to
null, sets the frame size, and makes the frame visible.
4.3 Assumptions: N/A
4.4 Dependencies: Java Swing package
4.5 Code/ Pseudo Code

import javax.swing.*;

public class SVVVJFrame

{
public static void main(String[] args) {
JFrame f = new JFrame("Welcome to JAVA SWING Frame");
f.setSize(400, 500);
f.setLayout(null);
f.setVisible(true);
}
}

Devang Chouhan 50 2100BTCSE11490


4.6 Results

5. Quiz & Viva Questions


6.26 Quiz:

What is the purpose of the AnujVyasJFrame program?


a) To demonstrate event handling in Swing
b) To create a basic Swing frame
c) To showcase layout managers in Swing
d) To implement a graphical user interface using AWT

Which package is imported to use the JFrame class?


a) java.awt
b) java.util
c) javax.swing
d) javax.awt

What is the size of the frame created in the program?


a) 300x400 pixels
b) 400x500 pixels
c) 500x600 pixels
d) 600x700 pixels

6.27 Viva
 How is the size of the frame determined in the program?
 What is the purpose of setting the layout to null?
 Can you mention some other layout managers in Swing?

Devang Chouhan 51 2100BTCSE11490


Experiment : 20
1. Title: Write a program in java to demonstrate Label in Swing.
2. Outcome: The program creates a Swing frame and adds two labels to it. The labels display the specified
text "My First Label" and "My Second Label" at specific positions within the frame. The frame is
displayed on the screen with a size of 300x300 pixels.
3. Objectives: The objective of the program is to create a Swing frame with two labels using the JLabel
class. The program demonstrates the usage of JLabel to display text in a graphical user interface.
4. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
4.1.1 Label: Label is a component used to display text or an image on a graphical user
interface. It is represented by the JLabel class, which is a subclass of JComponent.
4.2 Solution:
The program creates a frame with two label with Swing package.
4.3 Assumptions: N/A
4.4 Dependencies: Java Swing package
4.5 Code/ Pseudo Code

import javax.swing.*;
class SVVV {
public static void main(String args[])
{ JFrame f = new JFrame("SVVV ");
JLabel l1 = new JLabel("My First
Label"); l1.setBounds(50, 50, 100, 30);
JLabel l2 = new JLabel("My Second Label.");
l2.setBounds(50, 100, 100, 30);
f.add(l1);
f.add(l2);
f.setSize(300, 300);
f.setLayout(null);
f.setVisible(true);
}
}

Devang Chouhan 52 2100BTCSE11490


4.6 Results

5. Quiz & Viva Questions


6.28 Quiz:
Which package is imported to use the JLabel class?
a) java.awt
b) java.util
c) javax.swing
d) javax.awt

How many labels are added to the frame in the program?


a) One
b) Two
c) Three
d) Four

6.29 Viva
 What is the role of the JLabel class in the program?
 How are the positions of the labels specified within the frame?
 Can you modify the program to change the size and position of the labels?
 Discuss any additional methods or properties of JLabel that you are aware of.

Devang Chouhan 53 2100BTCSE11490


Experiment :21
1. Title: Write a program in java to demonstrate TextField in Swing.
2. Outcome: The program creates a Swing frame and adds two labels to it. The labels display the specified
text "My First Label" and "My Second Label" at specific positions within the frame. The frame is
displayed on the screen with a size of 300x300 pixels.
3. Objectives:
3.1 To create a Swing-based graphical user interface.
3.2 To understand the usage of JTextField to accept user input.
3.3 To demonstrate the positioning of components within a JFrame window.
4. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
4.1.1 JFrame: A top-level container that represents a window in Swing.
4.1.2 JTextField: A component used for user input of text.
4.1.3 setBounds(int x, int y, int width, int height): Sets the position and size of a component.
4.1.4 add(Component comp): Adds a component to a container.
4.2 Solution:
The program creates a JFrame window and adds two JTextField components to it, allowing the
user to enter a username and password. The setBounds method is used to position and size the
text fields within the window. Finally, the window is displayed using setVisible(true).

4.3 Code/ Pseudo Code

import javax.swing.*;
class SVVV
{
public static void main(String args[])
{
JFrame f= new JFrame("SVVV ");
JTextField t1=new JTextField("Username");
t1.setBounds(50,100, 200,30);
JTextField t2=new JTextField("Password");
t2.setBounds(50,150, 200,30);
f.add(t1);
f.add(t2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
}

Devang Chouhan 54 2100BTCSE11490


4.4 Results

5. Quiz & Viva Questions


6.30 Quiz:
What is the purpose of using JTextField in Swing?
a) To display text labels
b) To accept user input of text
c) To create buttons
d) To display images

Which method is used to set the position and size of a component in Swing?
a) setSize(int width, int height)
b) setBounds(int x, int y, int width, int height)
c) setPosition(int x, int y)
d) setLocation(int x, int y)
6.31 Viva
 How does Swing differ from AWT in Java?
 Can you explain the purpose of the setVisible method in Swing?
 How would you modify the program to add a button for submitting the entered username
and password?

Devang Chouhan 55 2100BTCSE11490


Experiment :22
1. Title: Write a program in java to demonstrate Button in Swing.
2. Outcome: The program demonstrates the usage of a Swing button in creating a graphical user interface.
It creates a JFrame window and adds a JButton labeled "Click Me" to it. The button is positioned using
the setBounds method, and the window is displayed using setVisible(true).
3. Objectives:
3.1 To create a Swing-based graphical user interface.
3.2 To understand the usage of a JButton to perform actions when clicked.
3.3 To demonstrate the positioning of components within a JFrame window.

4. Nomenclature, theory with self-assessment questionnaire:


4.1 Nomenclature:
4.1.1 JFrame: A top-level container that represents a window in Swing.
4.1.2 JButton: A component used for button actions.
4.1.3 setBounds(int x, int y, int width, int height): Sets the position and size of a component.
4.1.4 add(Component comp): Adds a component to a container.
4.2 Solution:
T he program creates a JFrame window and adds a JButton labeled "Click Me" to it. The button is
positioned using the setBounds method. When the button is clicked, no action is performed as no
event handling is implemented..

4.3 Code/ Pseudo Code


import javax.swing.*;
class SVVV {
public static void main(String[] args) {
JFrame f = new JFrame("Swing Button Demo");
JButton button = new JButton("Click Me");
button.setBounds(100, 100, 100, 40);
f.add(button);
f.setSize(300, 200);
f.setLayout(null);
f.setVisible(true);
}
}

Devang Chouhan 56 2100BTCSE11490


4.4 Results

5. Quiz & Viva Questions


6.32 Quiz:
What is the purpose of using JButton in Swing?
a) To display text labels
b) To accept user input of text
c) To create buttons
d) To display images

Which method is used to set the position and size of a component in Swing?
a) setSize(int width, int height)
b) setBounds(int x, int y, int width, int height)
c) setPosition(int x, int y)
d) setLocation(int x, int y)
6.33 Viva
 How does Swing differ from AWT in Java?
 Can you explain the purpose of the setVisible method in Swing?
 How can you handle button click events in Swing?

Devang Chouhan 57 2100BTCSE11490


Experiment : 23
1. Title: Write a program in java to demonstrate Button in Swing.
2. Outcome: The program demonstrates the usage of Swing menu components in creating a graphical user
interface. It creates a JFrame window and adds a JMenuBar with multiple JMenu and JMenuItem
components. The menu structure includes a "File" menu with "New," "Open," "Save," and "Exit" items,
an "Edit" menu with "Cut," "Copy," and "Paste" items, and a "View" menu with "Zoom In" and "Zoom
Out" items.
3. Objectives:
3.1 o create a Swing-based graphical user interface with menus.
3.2 To understand the usage of JMenuBar, JMenu, and JMenuItem components.
3.3 To demonstrate the structure and hierarchy of menus in a Swing application.
4. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
4.1.1 JFrame: A top-level container that represents a window in Swing.
4.1.2 JMenuBar: A horizontal container for organizing menus in Swing.
4.1.3 JMenu: A component used to create a menu item group within a menu bar.
4.1.4 JMenuItem: A component representing an item within a menu.
4.1.5 add(Component comp): Adds a component to a container.
4.2 Solution:
T The program creates a JFrame window and adds a JMenuBar to it. The menu bar contains
multiple JMenu components representing different menu categories. Each JMenu contains
multiple JMenuItem components representing the items within the menu. The menu structure is
created using add methods to add components to their parent containers. The window is displayed
using setVisible(true).

4.3 Code/ Pseudo Code


import javax.swing.*;

class SVVV {
SVVV() {
JFrame f = new JFrame("Swing Menu Demo");

JMenuBar mb = new JMenuBar();

JMenu fileMenu = new JMenu("File");


JMenu editMenu = new JMenu("Edit");
JMenu viewMenu = new JMenu("View");

Devang Chouhan 58 2100BTCSE11490


JMenuItem newItem = new JMenuItem("New");
JMenuItem openItem = new JMenuItem("Open");
JMenuItem saveItem = new JMenuItem("Save");
JMenuItem exitItem = new JMenuItem("Exit");

JMenuItem cutItem = new JMenuItem("Cut");


JMenuItem copyItem = new JMenuItem("Copy");
JMenuItem pasteItem = new JMenuItem("Paste");

JMenuItem zoomInItem = new JMenuItem("Zoom In");


JMenuItem zoomOutItem = new JMenuItem("Zoom Out");

fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(exitItem);

editMenu.add(cutItem);
editMenu.add(copyItem);
editMenu.add(pasteItem);

viewMenu.add(zoomInItem);
viewMenu.add(zoomOutItem);

mb.add(fileMenu);
mb.add(editMenu);
mb.add(viewMenu);

f.setJMenuBar(mb);

f.setSize(300, 200);
f.setLayout(null);
f.setVisible(true);
}

public static void main(String[] args) {


newSVVV();
}
}

Devang Chouhan 59 2100BTCSE11490


4.4 Results

5. Quiz & Viva Questions


6.34 Quiz:
What is the purpose of using JMenuBar in Swing?
a) To display text labels
b) To accept user input of text
c) To create menus
d) To display images

How can you add items to a menu in Swing?


a) Using addMenu()
b) Using addItem()
c) Using add(JMenuItem item)
d) Using add(JMenu menu)
6.35 Viva
 Can you add multiple JMenus to a JMenuBar?
 How can you handle events when a menu item is clicked in Swing?
 Is it possible to add submenus within menu.

Devang Chouhan 60 2100BTCSE11490


Experiment : 24
1. Title: Write a program in java to demonstrate database connectivity with sql.
2. Outcome: The outcome of this program is to demonstrate the connectivity between a Java program and
a SQL database. It allows the Java program to interact with the database by executing SQL queries and
retrieving results.
3. Objectives:
 Establish a connection between Java and a SQL database.
 Execute SQL queries from the Java program.
 Retrieve and display the results obtained from the database.
4. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
 SQL: Structured Query Language, a programming language used for managing
and manipulating relational databases.
 JDBC: Java Database Connectivity, an API that provides Java programs with a standard way
to interact with databases.
 Connection: Represents a connection to a specific database.
 Statement: Represents an SQL statement to be executed.
 ResultSet: Represents the result of a database query.

4.2 Code/ Pseudo Code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class SVVV {
public static void main(String[] args) {
String MySQLURL = "jdbc:mysql://localhost:3306/web?useSSL=false";
String databseUserName = "root";
String databasePassword = "123456";
Connection con = null;
try {
con = DriverManager.getConnection(MySQLURL, databseUserName,
databasePassword);
if (con != null) {
System.out.println("Database connection is successful !!!!");
}
} catch (Exception e) {
e.printStackTrace();
} }}

Devang Chouhan 61 2100BTCSE11490


5. Quiz & Viva Questions
6.36 Quiz:
What is JDBC?
a) Java Database Compiler
b) Java Database Connectivity
c) Java Data Collection
d) Java Database Class

What does the Class.forName() method do in database connectivity?


a) Loads the JDBC driver
b) Establishes a connection to the database
c) Executes an SQL query
d) Retrieves data from the ResultSet
6.37 Viva
 How does JDBC facilitate database connectivity in Java?
 What are the steps involved in establishing a connection to a SQL database using JDBC?
 How can you execute an SQL query and retrieve the results in JDBC?
 How do you handle exceptions in JDBC?

Devang Chouhan 62 2100BTCSE11490


Experiment : 25
1. Title: Write a program to demonstrate JSP in java..
2. Outcome: The outcome of this program is to display the "Hello World,SVVV " message on a web page
using JSP (JavaServer Pages).
3. Objectives:
 Demonstrate the use of JSP to generate dynamic content on a web page.
 Print the "Hello World" message using JSP scripting elements.
4. Nomenclature, theory with self-assessment questionnaire:
4.1 Nomenclature:
 JSP: JavaServer Pages. It is a technology used to create dynamic web pages that can
generate HTML, XML, or other types of documents.
 Scripting Elements: In JSP, scripting elements allow you to embed Java code within the
page. The code inside the scripting elements is executed when the page is requested.
4.2 Assumptions: N/A
4.3 Dependencies: Web Server , JSP API
4.4 Code/ Pseudo Code
<html>
<head>
<title>Hello World JSP</title>
</head>
<body>
<h1><% out.println("Hello World"); %></h1>
</body>
</html>

Devang Chouhan 63 2100BTCSE11490


4.5 Result:

5. Quiz & Viva Questions


6.38 Quiz:
What is the purpose of JSP?
a. To style web pages
b. To generate dynamic content
c. To define the layout of a web page
d. To handle client-side interactions
How is Java code embedded in JSP
pages?
a. Using <java> tags
b. Using <script> tags
c. Using JSP scripting elements
d. Using CSS classes
6.39 Viva
 What is the difference between JSP and servlets?
 Can we use JSP without a web server?
 How does a JSP page get converted into a servlet?

Devang Chouhan 64 2100BTCSE11490

You might also like