Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Programming Exercise-01 and Exercise-02 Day-04, Exercises of Java Programming

Instructions for two java programming exercises on day-04. The exercises cover creating and modifying applets, adding buttons and labels, handling button events, and creating a simple calculator using checkboxes and text fields. Students will learn how to import packages, declare and create components, and write action methods.

Typology: Exercises

2011/2012

Uploaded on 07/03/2012

netu
netu 🇮🇳

4.5

(4)

55 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Programming
Exercise-01
Day-04
Java-Programming Day-04 Taking Turn
1. Activity-1 [Adding Buttons] [Do not use Design tools for this activity ]
a. Create an applet
b. Import AWT package
c. Take a look at the Component classes you learnt today, did you find them?
d. Declare a button b [ just declare it ]
e. In constructor create button object using new keyword
f. Assign label it “Click It”
g. Add it using the add() method
h. Compile your applet and view the result
i. Add four more buttons, compile and execute your applet
j. Resize the appletviewer or browser to just few columns (use mouse), see what happens to
your arrangement, are the changing their position?
2. Activity-2
a. Add a label “Anonymous”
b. Add action method to your applet
c. Use following action method for your “Click It” button;
public boolean action(Event event,Object action){
String s;
s=(String)action; //getting label of the button clicked
label1.setText(s);
return true;
}
3. Activity-3
a. Create or modify your applet and add four buttons with labels “BLUE”,”PINK”,”GREEN”,”WHITE”
b. Add action method to your applet
c. Write code in your action method such that when a button is pressed the background color of
your applet changes according to the label of the button
4. Component Adventure [Try using a component which you didn’t learn in the class]
a. The CheckBox button
i. Find this class in the awt package
ii. Try find out its various constructors by using JBuilder7 features [constructors are
displayed when you try to create an instance of a class]
iii. Create few check boxes and add to an applet
iv. Explore its methods
v. Add a command button [the normal button]
vi. Add action method to your applet for command button
vii. Write code in action method that clear a checkbox or uncheck a checkbox
5. Activity-5
a. Try creating a simple calculator by following the following steps
i. Create an applet and open JBuilder design tool
ii. Add 10 command buttons with labels [1 ,2,3,4,5,6,7,8,9,0]
iii. Add 1 more command buttons with labels [+]
iv. Add 1 more command button with label “Result”
v. Add 1 more command button with label “Clear”
vi. Add a textField “inputscreen”
vii. In JBuilder design tools click on applet canvas and set layout property = null or
XYLayout and arrange the
viii. Arrange the component to look like a traditional calculator
ix. Open source pane and add action method with code for followings
1. Create integer variables num1 num2 and result
2. Get label of the button clicked and save corresponding value in variables
3. if result button is pressed display the sum of num1 and num2 in text field
x. Compile your applet and view results
Docsity.com
pf2

Partial preview of the text

Download Java Programming Exercise-01 and Exercise-02 Day-04 and more Exercises Java Programming in PDF only on Docsity!

Java Programming

Exercise-

Day-

Java-Programming Day-04 Taking Turn

  1. Activity-1 [Adding Buttons] [Do not use Design tools for this activity ] a. Create an applet b. Import AWT package c. Take a look at the Component classes you learnt today, did you find them? d. Declare a button b [ just declare it ] e. In constructor create button object using new keyword f. Assign label it “Click It” g. Add it using the add() method h. Compile your applet and view the result i. Add four more buttons, compile and execute your applet j. Resize the appletviewer or browser to just few columns (use mouse), see what happens to your arrangement, are the changing their position?
  2. Activity- a. Add a label “Anonymous” b. Add action method to your applet c. Use following action method for your “Click It” button;

public boolean action(Event event,Object action){ String s; s=(String)action; //getting label of the button clicked label1.setText(s); return true; }

  1. Activity- a. Create or modify your applet and add four buttons with labels “BLUE”,”PINK”,”GREEN”,”WHITE” b. Add action method to your applet c. Write code in your action method such that when a button is pressed the background color of your applet changes according to the label of the button 4. Component Adventure [Try using a component which you didn’t learn in the class] a. The CheckBox button i. Find this class in the awt package ii. Try find out its various constructors by using JBuilder7 features [constructors are displayed when you try to create an instance of a class] iii. Create few check boxes and add to an applet iv. Explore its methods v. Add a command button [the normal button] vi. Add action method to your applet for command button vii. Write code in action method that clear a checkbox or uncheck a checkbox
  2. Activity- a. Try creating a simple calculator by following the following steps i. Create an applet and open JBuilder design tool ii. Add 10 command buttons with labels [1 ,2,3,4,5,6,7,8,9,0] iii. Add 1 more command buttons with labels [+] iv. Add 1 more command button with label “Result” v. Add 1 more command button with label “Clear” vi. Add a textField “inputscreen” vii. In JBuilder design tools click on applet canvas and set layout property = null or XYLayout and arrange the viii. Arrange the component to look like a traditional calculator ix. Open source pane and add action method with code for followings
  3. Create integer variables num1 num2 and result
  4. Get label of the button clicked and save corresponding value in variables
  5. if result button is pressed display the sum of num1 and num2 in text field x. Compile your applet and view results

Docsity.com

Java Programming

Exercise-

Day-

Add following code in your action method to avoid exceptions

if(event.target instanceof Button){

s=(String)action; label1.setText(s); }

  1. Activity-

a. Write an applet for a super store and do the following i. Create a String array “Items” for 10 strings ii. Create a String variable “customerName”

iii. Set the layout to FlowLayout aligned to left iv. Add a text filed with label “Customer Name”

v. Add 10 Check boxes with labels, each label is the name of an item in the superstore vi. Add a text area with the label “Your Item List” vii. Add a button with label “Show List”

b. Write code in the action method for the “Show List” button for the following action when this button is pressed

i. Check the state of each checkbox ii. If a checkbox is checked get the label associated with the checkbox and save it to the “Item” array

iii. Get the text from the text field (the customer name) and save to the variable “customerName” (if text field is empty save “Anonymous” in the variable)

iv. Display customer name in the text area v. Display the contents of the array in the Text area

  1. Activity- a. Add a button “Clear” to your super store applet, the button when pressed should uncheck all the checkboxes

b. Add a button “Check All” to your super store applet, the button when pressed should check all the checkboxes

c. Add a button “New Customer” to your super store applet, the button when pressed should i. Uncheck all the checkboxes ii. Clear the text field

iii. Clear the text area

  1. Activity- 8

a. Try super store applet using some different layout

Docsity.com