BLOOD DRIVE - AVERAGE Write an applet that will be used to gather data for a com
ID: 3671635 • Letter: B
Question
BLOOD DRIVE - AVERAGE
Write an applet that will be used to gather data for a company blood drive. The user will enter the number of pints of blood donated by employees in each of five departments and the program will find the average number of pints donated.
< Your applet will have an appropriate background and foreground color.
< At the top of the applet, use a title label with a larger font,
< Use an instruction label (to input blood amounts and click button),
< Use labels and textfields for each department's blood donations,
< Use 2 buttons, one to calculate the average and one to clear the text fields and result label.
< Use one results label for both the labeling of the average and the average.
< Use the flow layout and place everything at appropriate places on the applet (ex. make sure the title and instruction labels are in an appropriate spot on top, department labels are next to the appropriate department textfields, etc.).
< Put the cursor in the 1st department text field for the input and let the user take action to calculate the average by pressing enter in the last department textfield or clicking the calculate button.
When the user takes action (on the last department text field or average button):
< Get the blood amounts from the textfields
< Sum the pints donated and then calculate the average pints donated.
< Put the labeled results in your results label. The user should be able to use the components again.
When the user takes action (on the clear button):
< Clear the text fields and result label.
When the applet is created:
< Add a setSize() method to the init() to size out your applet. Adjust the size of the applet appropriately (so appropriate components are next to each other, everything fits, etc.).
< Save the java applet code with documentation - four lines for purpose at top and at least 5 lines throughout explaining what is going on.
< Run the program in your Applet Viewer in your size using the following data. If you made the size smaller, you are all set, if you made the sizse bigger, click the maximize button and then you are all set.
< Enter the numbers for the the 1st run, hit enter in the last textfield (or click button), take a picture with <alt><ptrscr> (active window) and paste into Paint and save in Paint as a .jpg.
< Click the clear button.
< Enter the numbers in the textfields for the 2nd run, and hit enter in the last textfield again. Take another picture and paste into your Paint picture so both can be seen. Save the picture again in Paint with both results (if both will not fit, you can make two output .jpg files).
< Submit both files (java code, output image of the two Applet Viewer images).
1st Run:
Department 1 40
Department 2 25
Department 3 60
Department 4 45
Department 5 35
2nd Run:
Department 1 60
Department 2 50
Department 3 70
Department 4 90
Department 5 80
Explanation / Answer
Blood Samples In Applet Format
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author HARE KRISHNA
*/
import java.awt.*;
import java.applet.*;
// import an extra class for the ActionListener
import java.awt.event.*;
// Tells the applet you will be using the ActionListener methods.
class ActionExample extends Applet implements ActionListener
{
Button avgCalculate;
Button clearTextfieldresult;
TextField dept1Field;
TextField dept2Field;
TextField dept3Field;
TextField dept4Field;
TextField dept5Field;
public void init()
{
// Now we will use the FlowLayout
setLayout(new FlowLayout());
avgCalculate = new Button("Average Calualte!");
clearTextfieldresult = new Button("Clear the textfield and Result!");
dept1Field = new TextField("Type here Something",35);
dept2Field = new TextField("Type here Something",35);
dept3Field = new TextField("Type here Something",35);
dept4Field = new TextField("Type here Something",35);
dept5Field = new TextField("Type here Something",35);
add(avgCalculate);
add(clearTextfieldresult);
add(dept1Field);
add(dept2Field);
add(dept3Field);
add(dept4Field);
add(dept5Field);
// Attach actions to the components
avgCalculate.addActionListener(this);
clearTextfieldresult.addActionListener(this);
}
// Here we will show the results of our actions
public void paint(Graphics g)
{
// Now that the color is set you can get the text out the TextField
// like this
g.drawString(dept1Field.getText(),20,100);
g.drawString(dept2Field.getText(),20,100);
g.drawString(dept3Field.getText(),20,100);
g.drawString(dept4Field.getText(),20,100);
g.drawString(dept5Field.getText(),20,100);
}
public void actionPerformed(ActionEvent evt)
{
// Here we will ask what component called this method
if (evt.getSource() == avgCalculate)
repaint();
// Actions of the clearTextfieldresult
else if (evt.getSource() == clearTextfieldresult)
{
// Change the text on the button for fun
clearTextfieldresult.setLabel("Blood Samples!");
// Changes the text in the TextField
dept1Field.setText("Blood sample of department 1!");
dept2Field.setText("Blood sample of department 2!");
dept3Field.setText("Blood sample of department 3!");
dept4Field.setText("Blood sample of department 4!");
dept5Field.setText("Blood sample of department 5!");
// Lets the applet show that message.
repaint();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.