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 are encouraged to explore various constructors and methods of the awt package components.

Typology: Exercises

2011/2012

Uploaded on 07/07/2012

virat
virat 🇮🇳

5 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Program ming
Exercise-01
Da y-04
Java-Programming Day-04 Taking Turn
1. Activity-1 [Add ing Buttons ] [Do not use Design tools for this activity ]
a. Create an applet
b. Import AWT package
c. Take a look at the Compon ent clas ses you learnt today, did you find the m?
d. Declare a button b [ just declare it ]
e. In constructo r crea te button object using new keyw ord
f. Assign labe l it “Click It”
g. Add it u sing the add() metho d
h. Compile your apple t and view the result
i. Add four more buttons , compile and exe cute your applet
j. Resize the appletviewer or brow ser to just few columns (use mous e), see what happens to
your arrangemen t, are the chang ing the ir position?
2. Activity-2
a. Add a lab el “Anonymou s”
b. Add action method to your apple t
c. Us e follow ing action method for your “Click It” butto n;
public bo olean action (Event event,Object action){
String s;
s=(String)action; //getting labe l of the butt on clicked
labe l1.setText(s );
return true;
}
3. Activity-3
a. Create or modify your ap plet and ad d four buttons with labels “BLUE”,”PINK”,”GREEN”,”WHITE”
b. Add action method to your apple t
c. W rite code in your action method such tha t when a button is pre ssed the ba ckground color of
your app let 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 Che ckBox button
i. Find this class in the awt package
ii. Try find out its various cons tructors by us ing JBuilder7 fea tures [cons tructors are
disp layed when you try to create an instan ce of a class]
iii. C reat e few che ck boxe s and add to an applet
iv. Explo re its metho ds
v. Add a command butto n [the normal button]
vi. Add action metho d to your ap plet for command button
vii. Write cod e in action metho d that clear a checkbox or uncheck a che ckbox
5. Activity-5
a. Try crea ting a simple calculator by follow ing the following steps
i. C reat e an applet and open JBuilde r de sign tool
ii. Add 10 command buttons with labels [1 ,2,3,4,5,6,7,8,9,0]
iii. Add 1 more comman d button s with labels [+]
iv. Add 1 more command butt on with labe l “Result”
v. Add 1 more command butto n with label “Clea r”
vi. Add a te xtField “inputscre en”
vii. In JBuilder de sign too ls click on applet canvas and set layout property = null o r
XYLay out and arrange the
viii. Arrange the component to look like a traditional calculator
ix. Ope n source pane and ad d action method with code for follow ings
1. Create integer varia bles num1 num2 and resu lt
2. Get lab el of the butto n clicked and sa ve corresponding value in variab les
3. if result butt on is pressed display the sum of num1 and num2 in te xt 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 “Clea r” 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 s hould 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