Professional Documents
Culture Documents
Devang Final
Devang Final
PRACTICAL FILE
(Session Jan -June 2024)
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
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.
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?
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.
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
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
Viva
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
void show() {
System.out.println(" roll no= "+rno);
System.out.println(" name= "+name);
}
}
class Result extends Student
{ float m1,m2;
Results
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.4 Results
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);
}
}
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?
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.
RESULT:
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?
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);
}
}
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?
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.
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?
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.
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?
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.
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?
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.
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?
frame.add(choice);
Devang Chouhan 31 2100BTCSE11490
frame.add(button);
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?
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
}
}}
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?
public SVVV() {
Devang Chouhan 39 2100BTCSE11490
frame = new Frame("Calculator");
textField = new TextField();
textField.setBounds(50, 50, 300, 30);
textField.setEditable(false);
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);
}
void nextChar() {
ch = (++pos < expression.length()) ? expression.charAt(pos) : -1;
}
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('+')) {
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();
}
Results
b1.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e){
t1.setText(t1.getText().concat("1"));
} });
4.5
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?
import javax.swing.*;
{
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);
}
}
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?
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);
}
}
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.
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);
}
}
}
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?
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?
class SVVV {
SVVV() {
JFrame f = new JFrame("Swing Menu Demo");
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);
}