This is the question?
The German mathematician Gottfried Leibniz developed the folling method to approximate the value of pi:

pi/4 = 1 - 1/3 + 1/5 - 1/7 + ......
Write a program that allows the user to specify the number of iterations used in this approximation and displays the resulting value.

This is the question?
The German mathematician Gottfried Leibniz developed the folling method to approximate the value of pi:

pi/4 = 1 - 1/3 + 1/5 - 1/7 + ......
Write a program that allows the user to specify the number of iterations used in this approximation and displays the resulting value.

O.K. I wrote it. Now what?

Maybe his professor will give you credit for it.

devonte15, what is the professor's email address, so Vernon can turn in the assignment?

i dont understand how to write a program for the question

That's why you're taking the class and doing homework. Post what you have so far and specific questions.

This is what i have so far.. the next thing i have to do is create a GUI to compute the value of pi!! HOW??

import TerminalIO.KeyboardReader;

public class PI{

   public static void main (String[] args){
      KeyboardReader reader = new KeyboardReader();
      int iterations, i, denominator;
      double numerator, approx;
      System.out.print ("Enter the number of iterations: ");
      iterations = reader.readInt();
      if (iterations <= 0)
         System.out.println ("Number of iterations must be positive");
      else{
         i = 1;
         numerator = 1.0;
         denominator = 1;
         approx = 0;
         while (i <= iterations) {
            approx = approx + numerator / denominator;
            numerator = - numerator;
            denominator = denominator + 2;
            i = i + 1;
         }
         System.out.println ("PI is approximately " + 4 * approx + ".");
      }
   }
}

Code tags please.

[code]

// paste code here

[/code]

Well, what happens when you run it? Does it approximate PI correctly? The GUI part and the PI calculation part are separate. You should take the PI calculation part out of main. Set up a function that takes the number of iterations as a parameter, and call it from another function.

public static double PIApproximate (int numIterations)
{
    // put code here
}

As far as the GUI goes, how familiar are you with Swing ?

I imagine you could have a JTextField where the user types in the number of iterations, and a JButton that, when pressed, reads the data from the JTextField, calls the function above, and displays the results somewhere, perhaps a JLabel.

i revised my program a little bit more but what is the mess up and where do i input the seperate GUI...Please help only 1 hour left to finish

import javax.swing.*;   
import BreezySwing.*;   
public class PIGuiALF extends GBFrame{   


  //Declaring variables 

  private JLabel       itLabel;   
  private JLabel       numLabel;   
  private IntegerField itField;   
  private IntegerField numField;   
  private JButton      enterButton;   

//Defining other variables 

  private int numiterations;
  private int it, num;
  private int denominator;   
  private double approx;   
  private double numerator;

//Instantiate window objects

  public PIGuiALF() {   
    itLabel = addLabel        ("Number of iterations",1,1,1,1);   
    itField = addIntegerField (0,1,2,1,1);   

    itLabel = addLabel        ("PI is approximately",2,1,1,1);   
    itField = addIntegerField (0,2,2,1,1);   

    enterButton = addButton       ("Enter",3,1,2,1);   
  }   

//Entering in loop and also a message box 

  public void buttonClicked(JButton buttonObj){   
    if (buttonObj == enterButton) {
    if (!(itField.isValid()) || !(numField.isValid()))   
        messageBox("Please enter a valid number");
      else{ 
        iterations = itField.getNumber();   
        num = numField.getNumber();   
        i = 1;
        numerator = 1.0;
        denominator = 1;
        approx = 0;
         while (i <= iterations) {
            approx = approx + numerator / denominator;
            numerator = - numerator;
            denominator = denominator + 2;
            i = i + 1;
         }
      }
     public static double PIApproximate (int numIterations)
{

}


      public static void main (String[] args){
        PIGuiALF theGUI = new ConvertWithGUI();
        theGUI.setLookAndFeel("MOTIF");
        theGUI.setSize (250, 100);
        theGUI.setVisible (true);
    }
  }
}

Use code tags.

I've never used BreezySwing, but regardless, here you calculate PI, but you don't do anything with the calculation. You need to stick the answer in the GUI somewhere. You don't do anything with approx .

public void buttonClicked(JButton buttonObj){
if (buttonObj == enterButton) {
if (!(itField.isValid()) || !(numField.isValid()))
messageBox("Please enter a valid number");
else{
iterations = itField.getNumber();
num = numField.getNumber();
i = 1;
numerator = 1.0;
denominator = 1;
approx = 0;
while (i <= iterations) {
approx = approx + numerator / denominator;
numerator = - numerator;
denominator = denominator + 2;
i = i + 1;
}
}

I revised it like yo said but now it saying that there is an illegal start of expression for

import javax.swing.*;   
import BreezySwing.*;   
public class PIGuiALF extends GBFrame{   


  //Declaring variables 

  private JLabel       itLabel;   
  private JLabel       numLabel;   
  private IntegerField itField;   
  private IntegerField numField;   
  private JButton      enterButton;   

//Defining other variables 

  private int numiterations;
  private int it, num;
  private int denominator;   
  private double numerator;

//Instantiate window objects

  public PIGuiALF() {   
    itLabel = addLabel        ("Number of iterations",1,1,1,1);   
    itField = addIntegerField (0,1,2,1,1);   

    itLabel = addLabel        ("PI is approximately",2,1,1,1);   
    itField = addIntegerField (0,2,2,1,1);   

    enterButton = addButton       ("Enter",3,1,2,1);   
  }   

//Entering in loop and also a message box 
  public static double PIGuiALF (int numIterations){  
  public void buttonClicked(JButton buttonObj){   
    if (buttonObj == enterButton) {
      if (!(itField.isValid()) || !(numField.isValid()))   
        messageBox("Please enter a valid number");
      else{ 
        iterations = itField.getNumber();   
        num = numField.getNumber();   
        i = 1;
        numerator = 1.0;
        denominator = 1;
            while (i <= iterations) {
            numerator = - numerator;
            denominator = denominator + 2;
            i = i + 1;
         }
      }

      public static void main (String[] args){
        PIGuiALF theGUI = new ConvertWithGUI();
        theGUI.setLookAndFeel("MOTIF");
        theGUI.setSize (250, 100);
        theGUI.setVisible (true);
    }
  }
}

So What would u do?

I imagine it's too late now and you've turned it in.

You have a function nested within another function here. You don't want that.

//Entering in loop and also a message box
public static double PIGuiALF (int numIterations){
public void buttonClicked(JButton buttonObj){

Nothing involving the GUI should be inside the function that calculates PI. It takes an integer and returns a double. It should be a calculation function, nothing else.

Use code tags.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.