Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

AAYUSHI MAURYA-02

Advanced Java Programming (AJP)


Practical No. 1: Write a program to demonstrate the use of
AWT components.

X. Program Code-

1. Design an applet/application to demonstrate the use of Radio Button and


Checkbox.

Ans -

1|Page
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
2. Design an applet/application to create form using Text Field, Text Area,
Button and Label.

Ans -

2|Page
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
XI. Result (Output of Code):

1)

2)

3|Page
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
XII. Practical Related Questions:

1. State the difference between CheckBox and RadioButton.

Ans –

Radio button Checkbox


It is used when only one option to be Checkbox allows one or many
selected out of several available options. options to be selected.
It is a single control unit. It is a multiple control unit.
Radio button is presented as a small circle Checkbox is presented as a small
on the screen. square box on the screen.
Radio button have only 2 states namely- Checkbox have 3 states namely-
True & False. Checked, unchecked &
indeterminate.
It is used when you want to limit the users It is used when you want to allow user
choice to just one option from the range to select multiple choices.
provided.

2. Write the use of setEnabled() method.

Ans -
The code setEnabled(false), disables the TextField it is not selectable and
user can’t copy data from it and also user cannot able edit or copy ,it is just
used for preventing purpose from being edited,but it will still look the same
as TextFeild without setEnabled() method.

4|Page
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
3. Draw the life cycle of an Applet.
Ans-

5|Page
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
XIII. Exercise:

1. Develop a program using Label to display message “Welcome to Java”

Ans-

6|Page
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
2. Develop a program to select multiple languages known to user. (e. g Marathi,
Hindi, English, Sanskrit).
Ans-

7|Page
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
3. Write a program to create three Buttons with Caption OK, RESET and
CANCEL
Ans-

8|Page
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
Practical No. 2: Write a program to design a form using the

components List and Choice.

X. Program Code:

1. Write Java Program to show following output.


Ans-

9|Page
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
XII. Practical Related Questions:

1) Write the name of components used in following output


Ans-
Components that are used-
a) List
b) Label
2) State the difference between List and Choice control
Ans-
 A Choice is displayed in a compact form that requires you to pull it down to see the
list of available choices. Only one item may be selected from a Choice.Choice is the
act of picking or deciding between two or more possibilities.Only one item may be
selected from a choice.
 A List may be displayed in such a way that several List items are visible. A List
supports the selection of one or more List items.A list is any enumeration of a set of
items. A List supports the selection of one or more list items.

3) Write the use of getSelectedItem() and getSelectedIndex() for List.


Ans-
 getSelectedIndex()
returns the index of the currently selected item.
 getSelectedItem()
gets a representation of the current choice as a string

10 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
XIII. Exercise:
1) Develop an applet/ application using List components to add
names of 10 different cities.

Ans-

11 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

2) Develop applet / application to select multiple names of news papers


Ans-

12 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

13 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
Practical No. 3: Write a program to design simple calculator
with the use of Grid Layout
X. Program Code:

1. Write java Program to Demonstrate Grid of 5* 5


Ans-

14 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
2. Write a program to display The Number on Button from 0 to 9.
Ans-

15 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
XI. Result (Output of Code):

1.

2.

16 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
XII. Practical Related Questions-

1. Give name of default Layout for Different container


Ans-
Default layout of frame and windows is BorderLayout
And D0efault layout of panel and applet is FlowLayout.

2. List the names of BorderLayout regions.


Ans-
names of BorderLayout regions are-
i. East
ii. West
iii. South
iv. North
v. Center
3. Write the default horizontal and vertical gap in FlowLayout

Ans-

5Unit is the default horizontal and vertical gap in FlowLayout .

4. Write the use of Insets in border layout


Ans-
If you want to leave a small space between the container that holds your
components and the window that contains it. To do this, override the
getInsets() method that is defined by the container
Insets(int top, int left, int bottom, int right)

The values passed in top, left, bottom and right specify the amount of
space between the container and its enclosing window

17 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
XIV. Exercise:
1. Write a program to generate following output

Ans-

18 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
2. Write a program to generate following output using Border Layout

Ans-

19 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
Practical No. 4: Use of CardLayout to write a program to create
a two- level card deck that allows the user to select an

operating system.
X. Program Code:
1. Write java Program to Demonstrate Grid of 5* 5
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CardLayoutExample extends JFrame implements
ActionListener
{
CardLayout card;
JButton b1, b2, b3;
Container c;
CardLayoutExample()
{
c=getContentPane();
card=new CardLayout(40,30);
c.setLayout(card);
b1=new JButton("Apple");
b2=new JButton("Boy");
b3=new JButton("Cat");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c.add("a",b1);c.add("b",b2);c.add("c",b3);
}
public void actionPerformed(ActionEvent e)
{
card.next(c);
}
public static void main(String[] args)
{
CardLayoutExample cl=new CardLayoutExample();

20 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
cl.setSize(400,400);
cl.setVisible(true);
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
/*
<applet code= "CardLayoutExample.java" width=200 height=200>
</applet>
*/
Ans-

21 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XII. Practical Related Questions


1. State difference between GridLayout and GridBagLayout.
Ans-
GridLayout class lays all components in a rectangular grid like structure of
container. The container is divided into an equal sized rectangles and each
component is placed inside a rectangle.
The GridBagLayout class is a flexible layout manager that aligns
components vertically and horizontally, without requiring that the
components be of the same size. Each GridBagLayout object maintains a
dynamic, rectangular grid of cells, with each component occupying one or
more cells, called its display area.
2. Explain constructor of GridbagLayout.
Ans-
Java.awt.GridBagLayout is declared using the following syntax.
public class GridBagLayout extends Object implements LayoutManager2,
Serializable
GridBagLayout() is the constructor of GridBagLayout which helps in
creating a grid bag layout manager. Firstly, we create the object for
GridBagLayout class with the help of a no-argument constructor
GridBagLayout obj = new GridBagLayout();
setLayout(obj);

22 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
XIII. Exercise
1. Write Java program to display following output.

Ans-

23 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

2. Write Java Program to display following output.

24 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

Ans-

25 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

26 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
Practical No. 5: Write a program using AWT to create a menu bar
where menu bar contains menu items such as File, Edit, View and
create a submenu under the File menu: New and Open.
X. Program Code:
1. Write a program which creates Menu of different colors and disable menu
item for Black color.
Ans-

27 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XI. Result (Output of Code):

28 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
XII. Practical Related Questions

1. Write the use of setEnabled() method


Ans-
setEnabled() Enables or disables the given add-on. This function must
usually be called in the context of a user action, such as the click handler for
a button. The browser may also ask the user to confirm the change
2. Write the procedure to assign shortcut key to the Menu Item.
Ans-
First, create the JMenuBar object that will hold the menus. Next, construct
each menu that will be in the menu bar. In general, a menu is constructed by
first creating a JMenu object and then adding JMenuItems to it. After the
menus have been created, add them to the menu bar. The menu bar, itself,
must then be added to the frame by calling setJMenuBar( ). Finally, for each
menu item, you must add an action listener that handles the action event fired
when the menu item is selected.
3. Write a syntax and use of addSeparator() method
Ans-
public void addSeparator() - This will append a new separator to the end of
the menu. It is used to create a dividing line between two components.
More specifically, it is mainly used to create dividing lines between menu
items in a JMenu.

29 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)
XIII. Exercise
1. Find errors in following program and display output as shown below.
Ans-

30 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

31 | P a g e

You might also like