Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In-class Practice Guide – 8 “Area” application: 1. Create a new Windows Forms pr

ID: 3679873 • Letter: I

Question

In-class Practice Guide – 8 “Area” application:

1. Create a new Windows Forms project called “Area.”

2. Create a user interface that looks similar to this:

3. Create an event-handler for the “Calculate Area” button.

4. In the event-handler add validation code to detect if the user forgot to enter a value in the radius textbox.

5. Also add a try/catch block that will handle the situation in which the user enters a non-numeric value in the textbox.

6. Still in the event-handler add a statement where you parse and store the value the user entered in a variable (integer).

7. Now go to Project in the top menu of Visual Studio. Click on Add Class and create a new class called Circle (in the field where you enter Circle, do not delete the extension “.cs”). The new class code will be displayed.

8. Enter the code we went over during the lecture. So, this class will have 2 constructors (one default, and one that accepts one integer value). It will also have a class method called Area(). Plus, it will have one class member called “radius.”

9. In the “Calculate Area” event-handler create a new instance of the class Circle.

10. Now add a statement that will call the Area method of the class Circle and will assign the result to a double variable.

11. Write a statement that will display the result back in the form.

12. Add functionality to the Clear button.

13. Add functionality to the Exit button

Area Enter Radius Value: Calculate Area Area: Clear Exit

Explanation / Answer

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Circle extends JApplet implements ActionListener {
JLabel area;
JTextField radius;
JTextField a1;
JButton calculate;
JButton clear;
JButton ext;
public void init() {
JPanel panel = new JPanel();     
panel.add(new JLabel("Radius:"));          
radius = new JTextField();    
a1= new JTextField();                 
panel.add(radius);
calculate = new JButton("Calculate");              
panel.add(calculate);    
panel.add(new JLabel("Area:"));                     
area = new JLabel();
     panel.add(a1)                      
panel.add(area);                                                            
panel.add(new JLabel(""));
clear= new JButton("Clear");              
panel.add(clear);
ext= new JButton("ext");              
panel.add(ext);                                                         
calculate.addActionListener(this);     
ext.addActionListener(this);           
getContentPane().add(panel);                       
public void actionPerformed(ActionEvent arg0) {
String str1=ae.getActionCommand();
if(str1.equals("calculate"))
{
String str = radius.getText();                     
double r = Double.parseDouble(str);             
double surface = (r * r) * Math.PI;            
a1.setText("" + surface);
}
if(str1.equals("clear"))
{
a1.setText("");
}
if(str1.equals("ext"))
{
System.exit(0);
}
                   
}    
}