1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

GUI and User-Defined Method Problems

Discussion in 'Java' started by honeybee, Dec 7, 2011.

  1. honeybee

    honeybee New Member

    Joined:
    Dec 7, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Hi, there! For my programming class, I had to create a program incorporating both GUIs and user-defined methods. We had to input two three-digit number and compute the sum of the cube of each digit of every number between those two. Then we need to print all numbers for which the sum of the three cubes is equivalent to the number. My program will compile and the GUI appears, but the data will not print. Here is the program.



    /* Sally Ruth Taylor, December 2, 2011
    This program allows the user to input two three-digit integers. It computes the sum of the
    cubes of each of the digits that comprise each integers and outputs the number if the sum is
    equivalent to the number. The numbers printed are comprised within a table. */

    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class SumOfCubes extends JFrame
    {
    private static JLabel lowNumL, highNumL;
    private static JTextField lowNumTF, highNumTF;
    private static JButton calculateB, exitB;
    private static CalculateButtonHandler cbHandler;
    private static ExitButtonHandler ebHandler;

    private static final int WIDTH = 500; // Setting the width of the GUI
    private static final int HEIGHT = 150; // Setting the height of the GUI


    public SumOfCubes()
    {
    // Creating the labels
    lowNumL = new JLabel("Enter the lower three-digit integer.", SwingConstants.RIGHT);
    highNumL = new JLabel("Enter the highter three-digit integer.", SwingConstants.RIGHT);

    // Creating the text fields
    lowNumTF = new JTextField(10);
    highNumTF = new JTextField(10);

    // Creating the calculate button
    calculateB = new JButton("Compute");
    cbHandler = new CalculateButtonHandler();
    calculateB.addActionListener(cbHandler);

    // Creating the exit button
    exitB = new JButton("Exit the program");
    ebHandler = new ExitButtonHandler();
    exitB.addActionListener(ebHandler);

    // Title of the Window
    setTitle("Sum of Cubes");

    // Getting the container
    Container pane = getContentPane();

    // Setting the layout
    pane.setLayout(new GridLayout(3,2));

    // Placing the components in the pane
    pane.add(lowNumL);
    pane.add(lowNumTF);
    pane.add(highNumL);
    pane.add(highNumTF);
    pane.add(calculateB);
    pane.add(exitB);

    // Formatting the Window
    setSize(WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    }


    private class CalculateButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    // Declare variables
    int lowNum, highNum;
    double cubeCounter;
    double sumOfCubes;
    boolean equivalent;

    // Get Input, Set Values
    lowNum = Integer.parseInt(lowNumTF.getText());
    highNum = Integer.parseInt(highNumTF.getText());
    cubeCounter = lowNum + 1.0;

    if (cubeCounter < highNum)
    {
    sumOfCubes = SumOfDigitsCubed(cubeCounter);
    equivalent = SumEquivalent(sumOfCubes, cubeCounter);
    while (equivalent == true)
    {
    System.out.println(sumOfCubes + " ");
    }
    }
    }
    }

    private class ExitButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    System.exit(0);
    }
    }

    public double SumOfDigitsCubed(double counter)
    {
    double firstDigit, removeFirst, secondDigit, thirdDigit, sumOfCubes;

    // Find each individual digit
    firstDigit = counter / 100.;
    removeFirst = counter % 100.;
    secondDigit = counter / 10.;
    thirdDigit = counter % 10.;

    sumOfCubes = Math.pow(firstDigit, 3.0) + Math.pow(secondDigit, 3.0)
    + Math.pow(thirdDigit, 3.0);

    return sumOfCubes;
    }

    public boolean SumEquivalent(double totalSum2, double counter2)
    {
    boolean equivalent;

    if (totalSum2 == counter2)
    {
    return true;
    }
    else
    {
    return false;
    }
    }


    public static void main(String[] args)
    {
    SumOfCubes sumofcubes = new SumOfCubes();
    }
    }
     
  2. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    try to change this

    Code:
    .............
    if (cubeCounter < highNum)
    {
    sumOfCubes = SumOfDigitsCubed(cubeCounter);
    equivalent=true;
    while (equivalent == true)
    {
        equivalent = SumEquivalent(sumOfCubes, cubeCounter);
    System.out.println(sumOfCubes + " ");
    }
    }
    .................
    
     

Share This Page