Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   GUI and User-Defined Method Problems (http://www.go4expert.com/forums/gui-user-defined-method-t27304/)

honeybee 7Dec2011 10:41

GUI and User-Defined Method Problems
 
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();
}
}

virxen 7Dec2011 17:39

Re: GUI and User-Defined Method Problems
 
try to change this

Code:

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



All times are GMT +5.5. The time now is 05:58.