Question
i need this done in java


plz, add comments for every single method. also, make as simple as possible.


3. Write a GUI for computing the statistics of numbers input from the user. The GUI should have a text field and a button for
0 0
Add a comment Improve this question Transcribed image text
Answer #1

3. CODE:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

class StatsFrame extends JFrame implements ActionListener {

private JTextField tfNumber = new JTextField(10);

private JButton btnAdd = new JButton("ADD"); // creates the add button

private JLabel lblMax = new JLabel(); // creates label for maximum

private JLabel lblMin = new JLabel(); // creates label for minimum

private JLabel lblAvg = new JLabel(); // creates label for average

ArrayList<Double> numbers = new ArrayList<Double>();

public StatsFrame() {

setLayout(new FlowLayout()); // sets the new FlowLayout

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set the default operation upon close

setSize(200,500); // sets the size

setVisible(true); // sets the JFrame visible

add(tfNumber);

add(btnAdd);

btnAdd.addActionListener(this);

add(lblMax);

add(lblMin);

add(lblAvg);

}

//the button click event

public void actionPerformed(ActionEvent e) {

try{

double inputNum = Double.parseDouble(tfNumber.getText());// get the input number

numbers.add(inputNum);//add the number to the arraylist

}

catch(NumberFormatException ex) {

// an error message to be displayed upon numberformat exception occurs

JOptionPane.showMessageDialog(this, "Invalid Input", "Error", JOptionPane.ERROR_MESSAGE);

}

double max = Collections.max(numbers);// get the maximum of numbers from the arraylist

double min = Collections.min(numbers);// get the minimum of numbers from the arraylist

double sum = 0;// a variable that holds the sum of the arraylist

for (int i = 0; i < numbers.size() ;i++ ) {

sum += numbers.get(i);

}

double avg = sum/numbers.size();

//setting the text for appropriate labels

lblMax.setText("Current Max = " + String.valueOf(max));

lblMin.setText("Current Min = " + String.valueOf(min));

lblAvg.setText("Current Avg. = " + String.valueOf(avg));

}

}

//driver code

public class NumberStats{

public static void main(String[] args) {

StatsFrame sf = new StatsFrame();

}

}

OUTPUT:

4. CODE:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

// the jframe with all the fields necessary for calculating the balance every year

class BalanceFrame extends JFrame implements ActionListener {

private JLabel lblInAmt = new JLabel("Enter initial balance:");// a label for a prompt to input initial balance

private JLabel lblInterestRate = new JLabel("Enter Interest rate:");// a label for a prompt to input interest rate

private JLabel lblYears = new JLabel("Enter no of Years:");// a label for a prompt to input no. of years

private JTextField tfInAmt = new JTextField(10);// a textfield to get initial amount

private JTextField tfInterestRate = new JTextField(10);// a textfield to get interest rate

private JTextField tfYears = new JTextField(10);// a textfield to get no. of years

private JButton btnCalculate = new JButton("Calculate"); // creates the calculate button

private JTextArea taAmount = new JTextArea(); // creates textarea for showing initial balance

private JTextArea taBalance = new JTextArea(); // creates textarea for showing balance report

public BalanceFrame() {

setLayout(new FlowLayout()); // sets the new FlowLayout

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set the default operation upon close

setSize(270,500); // sets the size

setVisible(true); // sets the JFrame visible

//adding all the components to the frame

add(lblInAmt);

add(tfInAmt);

add(lblInterestRate);

add(tfInterestRate);

add(lblYears);

add(tfYears);

add(btnCalculate);

btnCalculate.addActionListener(this);

add(taAmount);

add(taBalance);

taAmount.setEditable(false);

taBalance.setEditable(false);

}

//the button click event

public void actionPerformed(ActionEvent e) {

double initialBalance = 0; double interestRate = 0 ;double noOfYears = 0;

try{

initialBalance = Double.parseDouble(tfInAmt.getText());// get the initial balance

interestRate = Double.parseDouble(tfInterestRate.getText());// get the interest rate

noOfYears = Double.parseDouble(tfYears.getText());// get the no of years

}

catch(NumberFormatException ex) {

// an error message to be displayed upon numberformat exception occurs

JOptionPane.showMessageDialog(this, "Invalid Input", "Error", JOptionPane.ERROR_MESSAGE);

System.exit(0);

}

double thisYearBalance = 0;

String balanceEachYear = "";// the string to show the balance report each year

taAmount.setText("Initial amount: $" + initialBalance);// set initial amount

for (int i = 0; i < noOfYears ;i++ ) {

thisYearBalance += initialBalance + (interestRate /100);// calculating the balance this year

balanceEachYear += "After Year: " + (i + 1) + "$" + thisYearBalance + "\n";

}

taBalance.setText(balanceEachYear); // set balance report each year in the textarea

}

}

//driver class

public class ShowBalance{

public static void main(String[] args) {

BalanceFrame bf = new BalanceFrame();

}

}

OUTPUT:

ERROR MESSAGE:

Add a comment
Know the answer?
Add Answer to:
I need this done in java plz, add comments for every single method. also, make as simple as po...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • Write a calculator program using JavaScript in HTML in the same HTML file. (There will only...

    Write a calculator program using JavaScript in HTML in the same HTML file. (There will only be 1 HTML file containing everything that you use for this program.) *************JavaScript Functions should be written in the HTML <head> .............. </head> tag, **************** (Please be mindful of the formatting of the text of your program. Meaning that do not copy paste everything in a single line. Add clear comments for a better understanding of your program) as follows Assignment: create a form...

  • need this in #c . You will now add server side validation code to the frmPersonnel...

    need this in #c . You will now add server side validation code to the frmPersonnel page. Currently, when the Submit button is pressed, the frmPersonnelVerified page is displayed. This is because the frmPersonnelVerified page is set as the Submit button's PostBackUrl property. Instead of having the page go directly to the frmPersonnelVerified page when the Submit button is pressed, we want to do some server side validation. If any of the validation rules fail, we will redisplay the frmPersonnel...

  • Please write it in Java language 2. (Myinterface.java) The program description below was found waaaaay back...

    Please write it in Java language 2. (Myinterface.java) The program description below was found waaaaay back in the archives of the Equinox history database. It is the specification for a simply "computer store storefront". Even though computer stores are now extinct, you find the idea charming and decide to use the specification as inspiration to write an interface for one of the Equinox systems. Read carefully: write your own GUI for one of the Equinox systems, drawing inspiration from the...

  • JAVA Hello I am trying to add a menu to my Java code if someone can...

    JAVA Hello I am trying to add a menu to my Java code if someone can help me I would really appreacite it thank you. I found a java menu code but I dont know how to incorporate it to my code this is the java menu code that i found. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class MenuExp extends JFrame { public MenuExp() { setTitle("Menu Example");...

  • ***Please use java code for the question below*** Write a program that calculates the future value...

    ***Please use java code for the question below*** Write a program that calculates the future value of a given investment at a given interest rate for a specified number of years. The formula for this calculation is: value = investmentAmount * (1 + monthly interest rate)years*12 Use text fields for the user to enter the numbers (Investment Amount, Number of Years, and Annual Interest). To get/load data from the textbox, (in this case doubles) use the following structure: (bold are...

  • I tried to complete a Java application that must include at a minimum: Three classes minimum...

    I tried to complete a Java application that must include at a minimum: Three classes minimum At least one class must use inheritance At least one class must be abstract JavaFX front end – as you will see, JavaFX will allow you to create a GUI user interface. The User Interface must respond to events. If your application requires a data backend, you can choose to use a database or to use text files. Error handling - The application should...

  • This is for java programming Please Show plenty of comments so I can follow the steps...

    This is for java programming Please Show plenty of comments so I can follow the steps Design a class named Account that contains: • A private int data field named id for the account (default 0) • A private double data field named balance for the account (default 0) • A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. • A private Date data field named...

  • NEED HELP with HTML with Javascript embedding for form validation project below. I have my code...

    NEED HELP with HTML with Javascript embedding for form validation project below. I have my code below but I'm stuck with validation. If anyone can fix it, I'd really appreciate. ****************************************************************************** CODE: <!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> <html> <head> <title>Nice</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script> var textFromTextArea; function getWords(){ var text =...

  • given below are the project description and their Html and css files i need javascript according to the project and other files!

    HTML------------------------------------------------------CSS---------------------------------------------------WEB230 - JavaScript 1 Assignment 7 - FormsSome of these tasks would be better done in HTML or CSS but do them in JavaScript to practice whatwe have learned.1. Select the form element and save it in a variable. From here we can access all of the form fields.2. When the page loads do the following:add the password value "monkey"select the favourite city "New York"clear the textarea3. Add an event handler to the "name" field to change the background color...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT