Back to tyco.paawanchaudhary.com
Download the Advanced Java Lab Manual from here

Index:-

Question Bank - Unit Test 1

Experiment No. 1
Experiment No. 1 - Questions
Experiment No. 1 - Program Code
Experiment No. 1 - Output
Experiment No. 2
Experiment No. 2 - Questions
Experiment No. 2 - Program Code
Experiment No. 2 - Output
Experiment No. 3
Experiment No. 3 - Questions
Experiment No. 3 - Program Code
Experiment No. 3 - Output
Experiment No. 4
Experiment No. 4 - Questions
Experiment No. 4 - Program Code
Experiment No. 4 - Output
Experiment No. 5
Experiment No. 5 - Questions
Experiment No. 5 - Program Code
Experiment No. 5 - Output
Experiment No. 6
Experiment No. 6 - Questions
Experiment No. 6 - Program Code
Experiment No. 6 - Output
Experiment No. 12
Experiment No. 12 - Questions
Experiment No. 12 - Program Code
Experiment No. 12 - Output
Experiment No. 13
Experiment No. 13 - Questions
Experiment No. 13 - Program Code
Experiment No. 13 - Output
Experiment No. 14
Experiment No. 14 - Questions
Experiment No. 14 - Program Code
Experiment No. 14 - Output


Disclaimer:- Students are advised not to copy blindly from here, this website should only be used as a reference and not for completion. I am not responsible for the accuracy of the content provided on this site. This website is in no way related to any Educational Institute or Board, it has been created by a student to provide help and give guidance to other students.

Question Bank - Unit Test 1






EXPERIMENT No. 1

Aim:- To develop a program illustrating the use of the following AWT components in an applet- TextField, TextArea, Button and Label.

Questions:-
Q1. What are AWT components?
Ans. Java's Abstract Windowing Toolkit provides many of the user interface objects we find in the Windows environment. These are called "Components" of the Java AWT.

Q3. What is a container?
Ans. A generic Abstract Window Toolkit (AWT) conatiner object is a component that can contain other AWT components. An applet is a container. Other containers include windows, frames, dialogs, and panels. Containers may contain other containers.

Q5. In what way TextField and TextArea are different?
Ans. A textfield is a one-row rectangle with a certain width that allows a user to enter a series of characters. A textarea is a multi-row rectangle with scrollbars that allows a user to enter multiple lines of characters.

Q9. What is the use of the keyword “new” used in AWT programming?
Ans. "new" keyword is used to intialize block of memory available to store this object into and store the object there. Thus, it is used for using the various components of AWT

Q12. How to avoid the use of default Layout in a container?
Ans. Each Container object has a layout manager associated with it. A layout manager is an instance of any class that implements the LayoutManager interface. The layout manager is set by the setLayout( ) method. If no call to setLayout( ) is made, then the default layout manager is used. Whenever a container is resized (or sized for the first time), the layout manager is used to position each of the components within it.

Q13. What is meant by Controls?
Ans. Controls are components that allow a user to interact with your application in various ways - for example a commonly used control is the push button.

Program:-

/*===============================================
 * Program for Expt1:- Components
 * Name: Paawan Chaudhary
 * Roll No: 12
 * Class: TYCO - Ist Shift
=================================================*/
import java.awt.*;
import java.applet.*;
public class Expt1ComponentDemo extends Applet
{
	TextField txtField;
	TextArea txtArea;
	Button bttn;
	Label name, about, gender, course;
	Checkbox java, oracle, php ;
	CheckboxGroup g;
	Checkbox male, female;
	public void init()
	{
		txtField = new TextField();
		txtArea = new TextArea();
		bttn = new Button("Submit");
		name = new Label("Name Please :");
		about = new Label("About Yourself :");
		gender = new Label("Gender :");
		course = new Label("Courses :");
		java = new Checkbox("Java", null, true);
		oracle = new Checkbox("Oracle");
		php = new Checkbox("PHP");
		g = new CheckboxGroup();
		male = new Checkbox("Male", g, true);
		female = new Checkbox("Female", g, false);
		setLayout(new GridLayout(6,2));
		add(name);
		add(txtField);
		add(about);
		add(txtArea);
		add(gender);
		add(male);
		add(female);
		add(course);
		add(java);
		add(oracle);
		add(php);
		add(bttn);
	}
}
/*<APPLET CODE ="Expt1ComponentDemo.class" HEIGHT=100 WIDTH=200>
</APPLET> */
Output:-
Expt1 Output
Download the .doc file for printing the Output.


EXPERIMENT No. 2

Aim:- To develop a program demonstrating the use of Grid layout in an Applet.

Questions:-
Q2. How much is the default horizontal and vertical gap in FlowLayout?
Ans. The default horizontal and vertical gap in FlowLayout is of 5 units.

Q3. What is the default alignment of buttons in Flow Layout?
Ans. The default alignment of buttons in FlowLayout is left to right until no more buttons fit on the same line. Each line is centered.

Q5. Which GridBagLayout variable defines the external padding (border) around the component in its display area?
Ans. GridBagConstraints.insets specifies the component's external padding, the minimum amount of space between the component and the edges of its display area.

Q7. What is the default Layout Manager for the Dialog class in Java?
Ans. The BorderLayout is the default LayoutManager for Dialog class.


Program:-

/*===============================================
 * Program for Expt2:- Layout
 * Name: Paawan Chaudhary
 * Roll No: 12
 * Class: TYCO - Ist Shift
=================================================*/
import java.awt.*;
class BorderLayoutDemo extends Frame
{
  Button n,s,c,e,w;
  BorderLayoutDemo()
  {
   BorderLayout b=new BorderLayout(30,30);
   setLayout(b);
   n=new Button("North");
   s=new Button("South");
   w=new Button("West");
   e=new Button("East");
   c=new Button("Center");
  
   add(n,BorderLayout.NORTH);
   add(s,BorderLayout.SOUTH);
   add(w,BorderLayout.WEST);
   add(e,BorderLayout.EAST);
   add(c,BorderLayout.CENTER);
  }
 public static void main(String args[])
  {
   BorderLayoutDemo b=new BorderLayoutDemo();
   b.setSize(300,300);
   b.setVisible(true);
   }
} 

Output:-
Expt2 Output
Download the .doc file for printing the Output.


EXPERIMENT No. 3

Aim:- To develop a program to set the given string in a desired font and color.(Font: Ariel, Bold and point size 20,color:pink)

Question nos according to batches:-
HOD Ma'am:- 1, 3, 5, 7, 8
Other:- Any 5, bt i m nt sure...:-P

Questions:-
Q1. Write the Font class and Color class of AWT?
Ans. i) java.awt.Font
The Font class represents fonts. The font is specified by its name, style and point size.
ii) java.awt.Color
The Color class is used to encapulate colors in the default RGB color space

Q3. How to obtain the information about a font?
Ans. The information about a Font can be accessed through the getAttributes method. A Font objects's attributes include its name, size, transform, and font features such as weight and posture.

Q5. How to create different components (such as buttons) in different colors?
Ans. A Component object is responsible for remembering the current foreground and background colors. Thus, we can give different colors for each component to create different components in different colors.

Q7. How to set the background color of an Applet as the current color?
Ans. For using the background color as the current color, it can be obtained by calling the getBackground() method.

Q8. Write the use of the setBackground() and setForeground() method?
Ans. i) The setBackground() method is used to set the background color of an applet's window.
ii) The setForeground() method is used to set the the foreground color (the color in which text is shown, for example)


Program:-

/*===============================================
 * Program for Expt3:- Fonts
 * Name: Paawan Chaudhary
 * Roll No: 12
 * Class: TYCO - Ist Shift
=================================================*/
import java.applet.*;
import java.awt.*;
public class Expt3SampleFont extends Applet
{
	Font f;
	Color c;
	public void init()
	{
		f = new Font("Times New Roman", Font.ITALIC, 14);
		setFont(f);
		c = new Color(255,0,0);//red
		setForeground(c);
	}
	public void paint(Graphics g)
	{
		g.drawString("Java Programming - TYCO SSPP", 4, 20);
	}
}
/*<APPLET CODE ="Expt3SampleFont.class" HEIGHT=100 WIDTH=200>
</APPLET> */
Output:-
Expt3 Output
Download the .doc file for printing the Output.


EXPERIMENT No. 4

Aim:- To develop a program to create a menu with various menu items and submenu items.

Questions:-
Q1. What are Menubar, Menu and MenuItems?
Ans. MenuBar, Menu, MenuItem are AWT classes.
Menu bar:- A menubar is an AWT component and contains a collection of menus. It is set for a particular window.
Menu:- A menu is also an AWT component and contains a collection of menu items. Menus are added to the menubar.
Menu item:- A menu item is also an AWT component and is added to the menu under different menu categories.

Q2. Write the procedure to create a MenuBar and to add menus to it.
Ans. Procedure to create a MenuBar and add Menus to it:-
i)Creating a menu bar:-
MenuBar mbar= new MenuBar();
ii)Setting menubar for the window:
window.set MenuBar (mbar); //Here window is an object of frame.
iii)Creating Menus:-
Menu fileMenu= new Menu (“File”);
iv)Adding individual Menus to the MenuBar:-
mbar.add(fileMenu);
mbar.add(editMenu);

Q4. How to make a particular Menu Item disable from selecting?
Ans. A particular Menu item can be disabled from selecting by the disable(); method
e.g.:- fileMenu.Disable();

Q7. Explain the various constructors of the class Menu.
Ans. Following are the constructors for Menu:-
i) Menu() throws HeadlessException
ii) Menu(String optionName) throws HeadlessException
iii) Menu(String optionName, boolean removable) throws HeadlessException
Here, optionName specifies the name of the menu selection. If removable is true, the menu can be removed and allowed to float free. Otherwise, it will remain attached to the menu bar.

Q8. What do you understand by Popup Menu?
Ans. PopupMenu works just like Menu, but produces a menu that can be displayed at a specific location. PopupMenu provides a flexible, useful alternative for some types of menuing situations.


Output:-
Expt 4 Output
Download the .doc file for printing the Output.


EXPERIMENT No. 5

Aim:- To develop a program to handle events.

Questions:-
Q1. What is an event?
Ans. An event is an object that describes a change in the state of a source. It can be generated as a consequence of a person interacting with the elements in a graphical user interface. Some of the activities that cause events to be generated are pressing a button, entering a character via the keyboard, selecting an item in a list, clicking the mouse etc.

Q3. What interface is extended by AWT event listeners?
Ans. All AWT Event Listeners extend the java.util.EventListener interface.

Q4. What class is the top of the AWT event hierarchy?
Ans. AWTEvent class is the highest-level class in AWT event heirarchy.

Q13. Which event is generated by clicking once on an item in a list?
Ans. ItemEvent event is generated by clicking once on an item in a list.

Q15. Which event is generated by double clicking on an item in a list?
Ans. ActionEvent event is generated by double-clicking on an item in a list.


Program:-

/*===============================================
 * Program for Expt5:- Event Handling
 * Name: Paawan Chaudhary
 * Roll No: 12
 * Class: TYCO - Ist Shift
=================================================*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code="FactorialDemo.class" height=300 width=350>
</applet>*/
public class FactorialDemo extends Applet implements ActionListener
{
	TextField t1,t2;
	Button b1;
	public void init()
	{
	  t1=new TextField(5);
	  t2=new TextField(5);
	  b1=new Button("Factorial");
	  add(t1);
	  add(t2);
	  add(b1);
	  b1.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae)
	{
	    if(ae.getSource()==b1)
	    {
	        int no=Integer.parseInt(t1.getText());
	        int fact=1;
	        for(int i=1;i<=no;i++)
	        {
	             fact=fact*i;
            }
	        t2.setText(Integer.toString(fact));
	    }
    }
}
Output:-
Expt 4 Output
Download the .doc file for printing the Output.


EXPERIMENT No. 6

Aim:- To develop a program to handle events.

Questions:-
Q1. What do you understand by extending an AWT component?
Ans. It is creating a component by subclassing or extending a standard AWT component.

Q2. What is Event Listener?
Ans. An Event listener is an object that is notified when an event occurs. It has two major requirements. First, it must have been registered with one or more sources to receive notifications about specific types of events. Second, it must implement methods to receive and process these notifications.

Q3. Explain the Event Delegation Model?
Ans. The modern approach to handling events is based on the delegation event model, which defines standard and consistent mechanisms to generate and process events. Its concept is quite simple: a source generates an event and sends it to one or more listeners. In this scheme, the listener simply waits until it receives an event. Once an event is received, the listener processes the event and then returns.

Q5. Define Event.
Ans. An event is an object that describes a change in the state of a source. It can be generated as a consequence of a person interacting with the elements in a graphical user interface.

Q6. Why there is a need to extend an AWT component?
Ans. Doing so allows you to handle events in much the same way as they were handled under the original 1.0 version of Java.

Q7. Why is Event handling model in java known as 'Event Delegation Model'?
Ans.



EXPERIMENT No. 12

Aim:- To develop a program to design a form having two fields and a single button using swing components.

Questions:-
Q1. What is the base class of all Swing components?
Ans. All swing components are derived from abstract javax.swing.Jcomponet class

Q3. What is the difference between AWT and Swing?
Ans. AWT are heavy weight components while Swing are light weight components.
Light weight components have transparent pixels where as heavy weight is always opaque.
Light weight components are non-rectangular.Heavy weight components are rectangular.

Q4. List two disadvantages of swing.
Ans. It requires Java 2 or a separate JAR file If you're not very careful when programming, it can be slower than AWT (all components are drawn) Swing components that look like native components might not act exactly like native components.

Q15. What is preferred size of a component?
Ans. The preferred size of a component is the minimum component size that will allow the component to display normally.
OR
Maybe we have to write about the method in Java which is used to set the preffered size, i'll write more about that here soon

Q16. Which method of the Component class is used to set the position and size of a component?
Ans. setBounds method of the Component class is used to set the position and size of a component.


Program:-

/*===============================================
 * Program for Expt12:- Form in Frame
 * Name: Paawan Chaudhary
 * Roll No: 12
 * Class: TYCO - Ist Shift
=================================================*/
import java.awt.*;
import java.applet.*;
import javax.swing.*;

class Pass extends JFrame
{
	JTextField t1,t2;
	JButton b1,b2;
	JLabel l1,l2;
	Pass(String title)
       {
	super(title);
	
	Container cp=getContentPane();
	
	JFrame F1=new JFrame ("Pass");
	F1.setVisible(true);
	F1.setSize(200,100);
	F1.setTitle(title);

	

	

	l1=new JLabel("Name");
	l2=new JLabel("Pass");
	t1=new JTextField(30);
	t2=new JTextField(10);
	b1=new JButton("Login");
	b2=new JButton("Cancel");

	cp.add(l1);
	cp.add(t1);
	cp.add(l2);
	cp.add(t2);
	cp.add(b1);
	cp.add(b2);
	cp.setLayout(new GridLayout(3,2));
       }
	public static void main(String args[])
	{
		Pass P1=new Pass("Frame with JComponent");
		P1.setVisible(true);
		P1.setSize(200,100);
	}
}

EXPERIMENT No. 13

Aim:- To develop a program to design a form having two fields and a single button using swing components.

Questions:-
Q1. What is the base class of all Swing components?
Ans. All swing components are derived from abstract javax.swing.Jcomponet class

Q3. What is the difference between AWT and Swing?
Ans. AWT are heavy weight components while Swing are light weight components.
Light weight components have transparent pixels where as heavy weight is always opaque.
Light weight components are non-rectangular.Heavy weight components are rectangular.

Q4. List two disadvantages of swing.
Ans. It requires Java 2 or a separate JAR file.
If you're not very careful when programming, it can be slower than AWT (all components are drawn).
Swing components that look like native components might not act exactly like native components.

Q5. Explain the swing event dispatch mechanism.
Ans.

Q6. How to add and delete nodes in Jtree?
Ans.



EXPERIMENT No. 14

Aim:- To develop a program to design a form having two fields and a single button using swing components.

Questions:-
Q1. How to print a JTable?
Ans.

Q2. How to create your own JTable?
Ans.




View MyStats