Need Help With Java

Stan Radner

DARKLY Regular
I'm having trouble with a program I am creating for my advanced java class. I want to call a value set as a double from two files and bring it to one file for a calculation.

Code:
Calculator2
 
/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - Final
*/
 
public class Calculator2
{
public static void main(String[] args)
{
new CalculatorGUI();
} // End of main
} // End of public class Calculator2.0
 
Title Panel
 
/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - Title Panel
*/
 
import javax.swing.*;
 
public class TitlePanel extends JPanel
{
private JLabel title; // To display the title
 
public TitlePanel()
{
title = new JLabel("Welcome to Calculator v2.0!"); // Create the title
add(title); // Add the label to this panel
} // End of TitlePanel
} // End of public class TitlePanel
 
CalculatorGUI (Not finished, Working on OperatorPanel)
 
/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0
*/
 
import javax.swing.util.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
 
public class CalculatorGUI extends JFrame
{
private NumberPanel1 numberOne;
private OperatorPanel operator;
private NumberPanel2 numberTwo;
private TitlePanel title;
private JPanel buttonPanel;
private JPanel calcButton
private JPanel exitButton
 
public CalculatorCalcGUI()
{
setTitle("Calculator v2.0"); // Display a title
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Specify an action for the close button
setLayout(new BorderLayout()); // Create a BorderLayout manager
 
// Custom Panels
numberOne = new NumberPanel1(); // First number panel
operator = new OperatorPanel(); // Operator panel
numberTwo = new NumberPanel2(); // Second number panel
title = new TitlePanel(); // Title panel
 
buildButtonPanel(); // Create the button panel
 
// Add the components to the content page
add(title, BorderLayout.NORTH); // Title panel in the northern section
add(numberOne, BorderLayout.WEST); // First number panel in the western section
add(operator, BorderLayout.CENTER); // Operator panel in the center
add(numberTwo, BorderLayout.EAST); // Second number panel in the eastern section
add(buttonPanel, BorderLayout.SOUTH); // Button panel in the southern section
 
// Pack the contents of the window and display it
pack();
setVisible(true);
} // End of CalculatorGUI
 
private void buildButtonPanel() // The buildButtonPanel method builds the button panel
{
buttonPanel = new JPanel(); // Create a panel for the buttons
 
// Create the buttons
calcButton = new JButton("Calculate"); // Calculate button
exitButton = new JButton("Exit"); // Exit button
 
// Register the action listeners
calcButton.addActionListener(new CalcButtonListener());
exitButton.addActionListener(new ExitButtonListener());
 
// Add the buttons to the button panel
buttonPanel.add(calcButton);
buttonPanel.add(exitButton);
} // End of buildButtonPanel
 
private class CalcButtonListener implements ActionListener // Handles event when user clicks the calculate button
{
public void actionPerformed(ActionEvent e)
{
double finalAnswer
 
// Calculate the final answer
finalAnswer =
//} // End of buildButtonPanel
//} // End of CalculatorCalcGUI
} // End of public class CalculatorGUI
 
Number1Panel
 
/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - First Number Panel
*/
 
import javax.swing.*;
import java.awt.*;
 
public class NumberPanel1 extends JPanel
{
// Set the values
public final double NUMBER_ZERO = 0;
public final double NUMBER_ONE = 1;
public final double NUMBER_TWO = 2;
public final double NUMBER_THREE = 3;
public final double NUMBER_FOUR = 4;
public final double NUMBER_FIVE = 5;
public final double NUMBER_SIX = 6;
public final double NUMBER_SEVEN = 7;
public final double NUMBER_EIGHT = 8;
public final double NUMBER_NINE = 9;
 
// Number radio buttons
private JRadioButton numberZero;
private JRadioButton numberOne;
private JRadioButton numberTwo;
private JRadioButton numberThree;
private JRadioButton numberFour;
private JRadioButton numberFive;
private JRadioButton numberSix;
private JRadioButton numberSeven;
private JRadioButton numberEight;
private JRadioButton numberNine;
 
// radio button group
private ButtonGroup bg;
 
public NumberPanel1()
{
setLayout(new GridLayout(10, 1)); // Create a GridLayout manager with 10 rows and one column
 
// Create the radio buttons
numberZero = new JRadioButton("0", true);
numberOne = new JRadioButton("1");
numberTwo = new JRadioButton("2");
numberThree = new JRadioButton("3");
numberFour = new JRadioButton("4");
numberFive = new JRadioButton("5");
numberSix = new JRadioButton("6");
numberSeven = new JRadioButton("7");
numberEight = new JRadioButton("8");
numberNine = new JRadioButton("9");
 
// Group the radio buttons
bg = new ButtonGroup();
bg.add(numberZero);
bg.add(numberOne);
bg.add(numberTwo);
bg.add(numberThree);
bg.add(numberFour);
bg.add(numberFive);
bg.add(numberSix);
bg.add(numberSeven);
bg.add(numberEight);
bg.add(numberNine);
 
// Add a border around the panel
setBorder(BorderFactory.createTitledBorder("First Number"));
 
// Add the radio buttons to the panel
add(numberZero);
add(numberOne);
add(numberTwo);
add(numberThree);
add(numberFour);
add(numberFive);
add(numberSix);
add(numberSeven);
add(numberEight);
add(numberNine);
} // End of NuberPanel1
 
public double getFirstNumber()
{
double fNumber = 0.0; The first double value I want to call in OperationPanel
 
if (numberZero.isSelected())
fNumber = NUMBER_ZERO;
else if (numberOne.isSelected())
fNumber = NUMBER_ONE;
else if (numberTwo.isSelected())
fNumber = NUMBER_TWO;
else if (numberThree.isSelected())
fNumber = NUMBER_THREE;
else if (numberFour.isSelected())
fNumber = NUMBER_FOUR;
else if (numberFive.isSelected())
fNumber = NUMBER_FIVE;
else if (numberSix.isSelected())
fNumber = NUMBER_SIX;
else if (numberSeven.isSelected())
fNumber = NUMBER_SEVEN;
else if (numberEight.isSelected())
fNumber = NUMBER_EIGHT;
else if (numberNine.isSelected())
fNumber = NUMBER_NINE;
 
return fNumber;
} // End of getFirstNuber
} // End of public class NumberPanel1
 
Number2Panel
 
/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - Second Number Panel
*/
 
import javax.swing.*;
import java.awt.*;
 
public class NumberPanel2 extends JPanel
{
// Set the values
public final double NUMBER_ZERO = 0;
public final double NUMBER_ONE = 1;
public final double NUMBER_TWO = 2;
public final double NUMBER_THREE = 3;
public final double NUMBER_FOUR = 4;
public final double NUMBER_FIVE = 5;
public final double NUMBER_SIX = 6;
public final double NUMBER_SEVEN = 7;
public final double NUMBER_EIGHT = 8;
public final double NUMBER_NINE = 9;
 
// Number radio buttons
private JRadioButton numberZero;
private JRadioButton numberOne;
private JRadioButton numberTwo;
private JRadioButton numberThree;
private JRadioButton numberFour;
private JRadioButton numberFive;
private JRadioButton numberSix;
private JRadioButton numberSeven;
private JRadioButton numberEight;
private JRadioButton numberNine;
 
// radio button group
private ButtonGroup bg;
 
public NumberPanel2()
{
setLayout(new GridLayout(10, 1)); // Create a GridLayout manager with 10 rows and one column
 
// Create the radio buttons
numberZero = new JRadioButton("0", true);
numberOne = new JRadioButton("1");
numberTwo = new JRadioButton("2");
numberThree = new JRadioButton("3");
numberFour = new JRadioButton("4");
numberFive = new JRadioButton("5");
numberSix = new JRadioButton("6");
numberSeven = new JRadioButton("7");
numberEight = new JRadioButton("8");
numberNine = new JRadioButton("9");
 
// Group the radio buttons
bg = new ButtonGroup();
bg.add(numberZero);
bg.add(numberOne);
bg.add(numberTwo);
bg.add(numberThree);
bg.add(numberFour);
bg.add(numberFive);
bg.add(numberSix);
bg.add(numberSeven);
bg.add(numberEight);
bg.add(numberNine);
 
// Add a border around the panel
setBorder(BorderFactory.createTitledBorder("Second Number"));
 
// Add the radio buttons to the panel
add(numberZero);
add(numberOne);
add(numberTwo);
add(numberThree);
add(numberFour);
add(numberFive);
add(numberSix);
add(numberSeven);
add(numberEight);
add(numberNine);
} // End of NuberPanel2
 
public double getSeondNumber()
{
double sNumber = 0.0; The second double value that I want to call in OperatorPanel
 
if (numberZero.isSelected())
sNumber = NUMBER_ZERO;
else if (numberOne.isSelected())
sNumber = NUMBER_ONE;
else if (numberTwo.isSelected())
sNumber = NUMBER_TWO;
else if (numberThree.isSelected())
sNumber = NUMBER_THREE;
else if (numberFour.isSelected())
sNumber = NUMBER_FOUR;
else if (numberFive.isSelected())
sNumber = NUMBER_FIVE;
else if (numberSix.isSelected())
sNumber = NUMBER_SIX;
else if (numberSeven.isSelected())
sNumber = NUMBER_SEVEN;
else if (numberEight.isSelected())
sNumber = NUMBER_EIGHT;
else if (numberNine.isSelected())
sNumber = NUMBER_NINE;
 
return sNumber;
} // End of getSecondNuber
} // End of public class NumberPanel2
 
OperatorPanel
 
/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - Operator Panel
*/
 
import javax.swing.*;
import java.awt.*;
 
public class OperatorPanel extends JPanel
{
public final char OPERATOR_ADD;
public final char OPERATOR_MINUS;
public final char OPERATOR_MULTIPLY;
public final char OPERATOR_DIVIDE;
public final char OPERATOR_POWER;
 
private JCheckBox add; // To select addition
private JCheckBox minus; // To select subraction
private JCheckBox multiply; // To select multiplication
private JCheckBox divide; // To select division
private JCheckBox power; // To select to the power of
 
public OperatorPanel()
{
setLayout(new GridLayout(5, 1)); // Create a GridLayout manager with 5 rows and one column
 
// Create the check boxes
add = new JCheckBox("Addition (+)");
minus = new JCheckBox("Subtraction (-)");
multiply = new JCheckBox("Multiplication (*)");
divide = new JCheckBox("Division (/)");
power = new JCheckBox("To the power of (^)");
 
// Add a border around the panel
setBorder(BorderFactory.createTitledBorder("Operator"));
 
// Add the check boxes to the panel
add(add);
add(minus);
add(multiply);
add(divide);
add(power);
} // End of OperatorPanel
 
public double getAnswer()
{
double output;
 
if (add.isSelected());
output = getFirstNumber.fNumber + getSecondNumber.sNumber; THIS IS WHERE I AM HAVING TROUBLE
} // End of getAnswer
} // End of public class Operator panel

I want to call the value from fNumber in NumberPanel1 and sNumber from NumberPanel2 to OperatorPanel and have them either added, subtracted, divided, etc based on what checkbox is selected.
 

14bux

Senior TF2 Admin
I have no advice other than to wrap the wall of code in a spoiler tag for space reasons
 

up-n-atom

DARKLY Regular
I want to call the value from fNumber in NumberPanel1 and sNumber from NumberPanel2 to OperatorPanel and have them either added, subtracted, divided, etc based on what checkbox is selected.

Wouldn't want to do your homework for you but I will give some initial clues. 1st look at the scope of the method getFirstNumber. 2nd getFirstNumber is a method. 3rd getFirstNumber() returns a type. 4th that type has no member fNumber. 5th reformat your post using a code tag and I can continue to nudge you along until I get fed up and give you the answer :P

testing 1...
Code:
class a {
    /* */
}
testing 2...
Code:
class b { }
 

Stan Radner

DARKLY Regular
Wouldn't want to do your homework for you but I will give some initial clues. 1st look at the scope of the method getFirstNumber. 2nd getFirstNumber is a method. 3rd getFirstNumber() returns a type. 4th that type has no member fNumber. 5th reformat your post using a code tag and I can continue to nudge you along until I get fed up and give you the answer :P

testing 1...
Code:
class a { }
testing 2...
Code:
class b {}

I don't need the fNumber defined in OperatorPanel as I can call it from NumberPanel1 and NumberPanel2 but I don't know the proper format to it.
 

up-n-atom

DARKLY Regular
I don't need the fNumber defined in OperatorPanel as I can call it from NumberPanel1 and NumberPanel2 but I don't know the proper format to it.

fNumber scope is to a method and therefore not accessible beyond it, unless it's static. NumberPanel1/2 are initialized as members to CalculatorGUI and their references to OperatorPanel are none existent.
 

Stan Radner

DARKLY Regular
fNumber scope is to a method and therefore not accessible beyond it, unless it's static. NumberPanel1/2 are initialized as members to CalculatorGUI and their references to OperatorPanel are none existent.

So how would I initialize them to Operator Panel?
 

shoottomaim

BANNED
Calculator2

/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - Final
*/

public class Calculator2
{
public static void main(String[] args)
{
new CalculatorGUI();
} // End of main
} // End of public class Calculator2.0

Title Panel

/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - Title Panel
*/

import javax.swing.*;

public class TitlePanel extends JPanel
{
private JLabel title; // To display the title

public TitlePanel()
{
title = new JLabel("Welcome to Calculator v2.0!"); // Create the title
add(title); // Add the label to this panel
} // End of TitlePanel
} // End of public class TitlePanel

CalculatorGUI (Not finished, Working on OperatorPanel)

/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0
*/

import javax.swing.util.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class CalculatorGUI extends JFrame
{
private NumberPanel1 numberOne;
private OperatorPanel operator;
private NumberPanel2 numberTwo;
private TitlePanel title;
private JPanel buttonPanel;
private JPanel calcButton
private JPanel exitButton

public CalculatorCalcGUI()
{
setTitle("Calculator v2.0"); // Display a title
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Specify an action for the close button
setLayout(new BorderLayout()); // Create a BorderLayout manager

// Custom Panels
numberOne = new NumberPanel1(); // First number panel
operator = new OperatorPanel(); // Operator panel
numberTwo = new NumberPanel2(); // Second number panel
title = new TitlePanel(); // Title panel

buildButtonPanel(); // Create the button panel

// Add the components to the content page
add(title, BorderLayout.NORTH); // Title panel in the northern section
add(numberOne, BorderLayout.WEST); // First number panel in the western section
add(operator, BorderLayout.CENTER); // Operator panel in the center
add(numberTwo, BorderLayout.EAST); // Second number panel in the eastern section
add(buttonPanel, BorderLayout.SOUTH); // Button panel in the southern section

// Pack the contents of the window and display it
pack();
setVisible(true);
} // End of CalculatorGUI

private void buildButtonPanel() // The buildButtonPanel method builds the button panel
{
buttonPanel = new JPanel(); // Create a panel for the buttons

// Create the buttons
calcButton = new JButton("Calculate"); // Calculate button
exitButton = new JButton("Exit"); // Exit button

// Register the action listeners
calcButton.addActionListener(new CalcButtonListener());
exitButton.addActionListener(new ExitButtonListener());

// Add the buttons to the button panel
buttonPanel.add(calcButton);
buttonPanel.add(exitButton);
} // End of buildButtonPanel

private class CalcButtonListener implements ActionListener // Handles event when user clicks the calculate button
{
public void actionPerformed(ActionEvent e)
{
double finalAnswer

// Calculate the final answer
finalAnswer =
//} // End of buildButtonPanel
//} // End of CalculatorCalcGUI
} // End of public class CalculatorGUI

Number1Panel

/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - First Number Panel
*/

import javax.swing.*;
import java.awt.*;

public class NumberPanel1 extends JPanel
{
// Set the values
public final double NUMBER_ZERO = 0;
public final double NUMBER_ONE = 1;
public final double NUMBER_TWO = 2;
public final double NUMBER_THREE = 3;
public final double NUMBER_FOUR = 4;
public final double NUMBER_FIVE = 5;
public final double NUMBER_SIX = 6;
public final double NUMBER_SEVEN = 7;
public final double NUMBER_EIGHT = 8;
public final double NUMBER_NINE = 9;

// Number radio buttons
private JRadioButton numberZero;
private JRadioButton numberOne;
private JRadioButton numberTwo;
private JRadioButton numberThree;
private JRadioButton numberFour;
private JRadioButton numberFive;
private JRadioButton numberSix;
private JRadioButton numberSeven;
private JRadioButton numberEight;
private JRadioButton numberNine;

// radio button group
private ButtonGroup bg;

public NumberPanel1()
{
setLayout(new GridLayout(10, 1)); // Create a GridLayout manager with 10 rows and one column

// Create the radio buttons
numberZero = new JRadioButton("0", true);
numberOne = new JRadioButton("1");
numberTwo = new JRadioButton("2");
numberThree = new JRadioButton("3");
numberFour = new JRadioButton("4");
numberFive = new JRadioButton("5");
numberSix = new JRadioButton("6");
numberSeven = new JRadioButton("7");
numberEight = new JRadioButton("8");
numberNine = new JRadioButton("9");

// Group the radio buttons
bg = new ButtonGroup();
bg.add(numberZero);
bg.add(numberOne);
bg.add(numberTwo);
bg.add(numberThree);
bg.add(numberFour);
bg.add(numberFive);
bg.add(numberSix);
bg.add(numberSeven);
bg.add(numberEight);
bg.add(numberNine);

// Add a border around the panel
setBorder(BorderFactory.createTitledBorder("First Number"));

// Add the radio buttons to the panel
add(numberZero);
add(numberOne);
add(numberTwo);
add(numberThree);
add(numberFour);
add(numberFive);
add(numberSix);
add(numberSeven);
add(numberEight);
add(numberNine);
} // End of NuberPanel1

public double getFirstNumber()
{
double fNumber = 0.0; The first double value I want to call in OperationPanel

if (numberZero.isSelected())
fNumber = NUMBER_ZERO;
else if (numberOne.isSelected())
fNumber = NUMBER_ONE;
else if (numberTwo.isSelected())
fNumber = NUMBER_TWO;
else if (numberThree.isSelected())
fNumber = NUMBER_THREE;
else if (numberFour.isSelected())
fNumber = NUMBER_FOUR;
else if (numberFive.isSelected())
fNumber = NUMBER_FIVE;
else if (numberSix.isSelected())
fNumber = NUMBER_SIX;
else if (numberSeven.isSelected())
fNumber = NUMBER_SEVEN;
else if (numberEight.isSelected())
fNumber = NUMBER_EIGHT;
else if (numberNine.isSelected())
fNumber = NUMBER_NINE;

return fNumber;
} // End of getFirstNuber
} // End of public class NumberPanel1

Number2Panel

/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - Second Number Panel
*/

import javax.swing.*;
import java.awt.*;

public class NumberPanel2 extends JPanel
{
// Set the values
public final double NUMBER_ZERO = 0;
public final double NUMBER_ONE = 1;
public final double NUMBER_TWO = 2;
public final double NUMBER_THREE = 3;
public final double NUMBER_FOUR = 4;
public final double NUMBER_FIVE = 5;
public final double NUMBER_SIX = 6;
public final double NUMBER_SEVEN = 7;
public final double NUMBER_EIGHT = 8;
public final double NUMBER_NINE = 9;

// Number radio buttons
private JRadioButton numberZero;
private JRadioButton numberOne;
private JRadioButton numberTwo;
private JRadioButton numberThree;
private JRadioButton numberFour;
private JRadioButton numberFive;
private JRadioButton numberSix;
private JRadioButton numberSeven;
private JRadioButton numberEight;
private JRadioButton numberNine;

// radio button group
private ButtonGroup bg;

public NumberPanel2()
{
setLayout(new GridLayout(10, 1)); // Create a GridLayout manager with 10 rows and one column

// Create the radio buttons
numberZero = new JRadioButton("0", true);
numberOne = new JRadioButton("1");
numberTwo = new JRadioButton("2");
numberThree = new JRadioButton("3");
numberFour = new JRadioButton("4");
numberFive = new JRadioButton("5");
numberSix = new JRadioButton("6");
numberSeven = new JRadioButton("7");
numberEight = new JRadioButton("8");
numberNine = new JRadioButton("9");

// Group the radio buttons
bg = new ButtonGroup();
bg.add(numberZero);
bg.add(numberOne);
bg.add(numberTwo);
bg.add(numberThree);
bg.add(numberFour);
bg.add(numberFive);
bg.add(numberSix);
bg.add(numberSeven);
bg.add(numberEight);
bg.add(numberNine);

// Add a border around the panel
setBorder(BorderFactory.createTitledBorder("Second Number"));

// Add the radio buttons to the panel
add(numberZero);
add(numberOne);
add(numberTwo);
add(numberThree);
add(numberFour);
add(numberFive);
add(numberSix);
add(numberSeven);
add(numberEight);
add(numberNine);
} // End of NuberPanel2

public double getSeondNumber()
{
double sNumber = 0.0; The second double value that I want to call in OperatorPanel

if (numberZero.isSelected())
sNumber = NUMBER_ZERO;
else if (numberOne.isSelected())
sNumber = NUMBER_ONE;
else if (numberTwo.isSelected())
sNumber = NUMBER_TWO;
else if (numberThree.isSelected())
sNumber = NUMBER_THREE;
else if (numberFour.isSelected())
sNumber = NUMBER_FOUR;
else if (numberFive.isSelected())
sNumber = NUMBER_FIVE;
else if (numberSix.isSelected())
sNumber = NUMBER_SIX;
else if (numberSeven.isSelected())
sNumber = NUMBER_SEVEN;
else if (numberEight.isSelected())
sNumber = NUMBER_EIGHT;
else if (numberNine.isSelected())
sNumber = NUMBER_NINE;

return sNumber;
} // End of getSecondNuber
} // End of public class NumberPanel2

OperatorPanel

/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - Operator Panel
*/

import javax.swing.*;
import java.awt.*;

public class OperatorPanel extends JPanel
{
public final char OPERATOR_ADD;
public final char OPERATOR_MINUS;
public final char OPERATOR_MULTIPLY;
public final char OPERATOR_DIVIDE;
public final char OPERATOR_POWER;

private JCheckBox add; // To select addition
private JCheckBox minus; // To select subraction
private JCheckBox multiply; // To select multiplication
private JCheckBox divide; // To select division
private JCheckBox power; // To select to the power of

public OperatorPanel()
{
setLayout(new GridLayout(5, 1)); // Create a GridLayout manager with 5 rows and one column

// Create the check boxes
add = new JCheckBox("Addition (+)");
minus = new JCheckBox("Subtraction (-)");
multiply = new JCheckBox("Multiplication (*)");
divide = new JCheckBox("Division (/)");
power = new JCheckBox("To the power of (^)");

// Add a border around the panel
setBorder(BorderFactory.createTitledBorder("Operator"));

// Add the check boxes to the panel
add(add);
add(minus);
add(multiply);
add(divide);
add(power);
} // End of OperatorPanel

public double getAnswer()
{
double output;

if (add.isSelected());
output = getFirstNumber.fNumber + getSecondNumber.sNumber; THIS IS WHERE I AM HAVING TROUBLE
} // End of getAnswer
} // End of public class Operator panel
:trollface:
 

Stan Radner

DARKLY Regular
Calculator2

/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - Final
*/

public class Calculator2
{
public static void main(String[] args)
{
new CalculatorGUI();
} // End of main
} // End of public class Calculator2.0

Title Panel

/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - Title Panel
*/

import javax.swing.*;

public class TitlePanel extends JPanel
{
private JLabel title; // To display the title

public TitlePanel()
{
title = new JLabel("Welcome to Calculator v2.0!"); // Create the title
add(title); // Add the label to this panel
} // End of TitlePanel
} // End of public class TitlePanel

CalculatorGUI (Not finished, Working on OperatorPanel)

/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0
*/

import javax.swing.util.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class CalculatorGUI extends JFrame
{
private NumberPanel1 numberOne;
private OperatorPanel operator;
private NumberPanel2 numberTwo;
private TitlePanel title;
private JPanel buttonPanel;
private JPanel calcButton
private JPanel exitButton

public CalculatorCalcGUI()
{
setTitle("Calculator v2.0"); // Display a title
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Specify an action for the close button
setLayout(new BorderLayout()); // Create a BorderLayout manager

// Custom Panels
numberOne = new NumberPanel1(); // First number panel
operator = new OperatorPanel(); // Operator panel
numberTwo = new NumberPanel2(); // Second number panel
title = new TitlePanel(); // Title panel

buildButtonPanel(); // Create the button panel

// Add the components to the content page
add(title, BorderLayout.NORTH); // Title panel in the northern section
add(numberOne, BorderLayout.WEST); // First number panel in the western section
add(operator, BorderLayout.CENTER); // Operator panel in the center
add(numberTwo, BorderLayout.EAST); // Second number panel in the eastern section
add(buttonPanel, BorderLayout.SOUTH); // Button panel in the southern section

// Pack the contents of the window and display it
pack();
setVisible(true);
} // End of CalculatorGUI

private void buildButtonPanel() // The buildButtonPanel method builds the button panel
{
buttonPanel = new JPanel(); // Create a panel for the buttons

// Create the buttons
calcButton = new JButton("Calculate"); // Calculate button
exitButton = new JButton("Exit"); // Exit button

// Register the action listeners
calcButton.addActionListener(new CalcButtonListener());
exitButton.addActionListener(new ExitButtonListener());

// Add the buttons to the button panel
buttonPanel.add(calcButton);
buttonPanel.add(exitButton);
} // End of buildButtonPanel

private class CalcButtonListener implements ActionListener // Handles event when user clicks the calculate button
{
public void actionPerformed(ActionEvent e)
{
double finalAnswer

// Calculate the final answer
finalAnswer =
//} // End of buildButtonPanel
//} // End of CalculatorCalcGUI
} // End of public class CalculatorGUI

Number1Panel

/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - First Number Panel
*/

import javax.swing.*;
import java.awt.*;

public class NumberPanel1 extends JPanel
{
// Set the values
public final double NUMBER_ZERO = 0;
public final double NUMBER_ONE = 1;
public final double NUMBER_TWO = 2;
public final double NUMBER_THREE = 3;
public final double NUMBER_FOUR = 4;
public final double NUMBER_FIVE = 5;
public final double NUMBER_SIX = 6;
public final double NUMBER_SEVEN = 7;
public final double NUMBER_EIGHT = 8;
public final double NUMBER_NINE = 9;

// Number radio buttons
private JRadioButton numberZero;
private JRadioButton numberOne;
private JRadioButton numberTwo;
private JRadioButton numberThree;
private JRadioButton numberFour;
private JRadioButton numberFive;
private JRadioButton numberSix;
private JRadioButton numberSeven;
private JRadioButton numberEight;
private JRadioButton numberNine;

// radio button group
private ButtonGroup bg;

public NumberPanel1()
{
setLayout(new GridLayout(10, 1)); // Create a GridLayout manager with 10 rows and one column

// Create the radio buttons
numberZero = new JRadioButton("0", true);
numberOne = new JRadioButton("1");
numberTwo = new JRadioButton("2");
numberThree = new JRadioButton("3");
numberFour = new JRadioButton("4");
numberFive = new JRadioButton("5");
numberSix = new JRadioButton("6");
numberSeven = new JRadioButton("7");
numberEight = new JRadioButton("8");
numberNine = new JRadioButton("9");

// Group the radio buttons
bg = new ButtonGroup();
bg.add(numberZero);
bg.add(numberOne);
bg.add(numberTwo);
bg.add(numberThree);
bg.add(numberFour);
bg.add(numberFive);
bg.add(numberSix);
bg.add(numberSeven);
bg.add(numberEight);
bg.add(numberNine);

// Add a border around the panel
setBorder(BorderFactory.createTitledBorder("First Number"));

// Add the radio buttons to the panel
add(numberZero);
add(numberOne);
add(numberTwo);
add(numberThree);
add(numberFour);
add(numberFive);
add(numberSix);
add(numberSeven);
add(numberEight);
add(numberNine);
} // End of NuberPanel1

public double getFirstNumber()
{
double fNumber = 0.0; The first double value I want to call in OperationPanel

if (numberZero.isSelected())
fNumber = NUMBER_ZERO;
else if (numberOne.isSelected())
fNumber = NUMBER_ONE;
else if (numberTwo.isSelected())
fNumber = NUMBER_TWO;
else if (numberThree.isSelected())
fNumber = NUMBER_THREE;
else if (numberFour.isSelected())
fNumber = NUMBER_FOUR;
else if (numberFive.isSelected())
fNumber = NUMBER_FIVE;
else if (numberSix.isSelected())
fNumber = NUMBER_SIX;
else if (numberSeven.isSelected())
fNumber = NUMBER_SEVEN;
else if (numberEight.isSelected())
fNumber = NUMBER_EIGHT;
else if (numberNine.isSelected())
fNumber = NUMBER_NINE;

return fNumber;
} // End of getFirstNuber
} // End of public class NumberPanel1

Number2Panel

/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - Second Number Panel
*/

import javax.swing.*;
import java.awt.*;

public class NumberPanel2 extends JPanel
{
// Set the values
public final double NUMBER_ZERO = 0;
public final double NUMBER_ONE = 1;
public final double NUMBER_TWO = 2;
public final double NUMBER_THREE = 3;
public final double NUMBER_FOUR = 4;
public final double NUMBER_FIVE = 5;
public final double NUMBER_SIX = 6;
public final double NUMBER_SEVEN = 7;
public final double NUMBER_EIGHT = 8;
public final double NUMBER_NINE = 9;

// Number radio buttons
private JRadioButton numberZero;
private JRadioButton numberOne;
private JRadioButton numberTwo;
private JRadioButton numberThree;
private JRadioButton numberFour;
private JRadioButton numberFive;
private JRadioButton numberSix;
private JRadioButton numberSeven;
private JRadioButton numberEight;
private JRadioButton numberNine;

// radio button group
private ButtonGroup bg;

public NumberPanel2()
{
setLayout(new GridLayout(10, 1)); // Create a GridLayout manager with 10 rows and one column

// Create the radio buttons
numberZero = new JRadioButton("0", true);
numberOne = new JRadioButton("1");
numberTwo = new JRadioButton("2");
numberThree = new JRadioButton("3");
numberFour = new JRadioButton("4");
numberFive = new JRadioButton("5");
numberSix = new JRadioButton("6");
numberSeven = new JRadioButton("7");
numberEight = new JRadioButton("8");
numberNine = new JRadioButton("9");

// Group the radio buttons
bg = new ButtonGroup();
bg.add(numberZero);
bg.add(numberOne);
bg.add(numberTwo);
bg.add(numberThree);
bg.add(numberFour);
bg.add(numberFive);
bg.add(numberSix);
bg.add(numberSeven);
bg.add(numberEight);
bg.add(numberNine);

// Add a border around the panel
setBorder(BorderFactory.createTitledBorder("Second Number"));

// Add the radio buttons to the panel
add(numberZero);
add(numberOne);
add(numberTwo);
add(numberThree);
add(numberFour);
add(numberFive);
add(numberSix);
add(numberSeven);
add(numberEight);
add(numberNine);
} // End of NuberPanel2

public double getSeondNumber()
{
double sNumber = 0.0; The second double value that I want to call in OperatorPanel

if (numberZero.isSelected())
sNumber = NUMBER_ZERO;
else if (numberOne.isSelected())
sNumber = NUMBER_ONE;
else if (numberTwo.isSelected())
sNumber = NUMBER_TWO;
else if (numberThree.isSelected())
sNumber = NUMBER_THREE;
else if (numberFour.isSelected())
sNumber = NUMBER_FOUR;
else if (numberFive.isSelected())
sNumber = NUMBER_FIVE;
else if (numberSix.isSelected())
sNumber = NUMBER_SIX;
else if (numberSeven.isSelected())
sNumber = NUMBER_SEVEN;
else if (numberEight.isSelected())
sNumber = NUMBER_EIGHT;
else if (numberNine.isSelected())
sNumber = NUMBER_NINE;

return sNumber;
} // End of getSecondNuber
} // End of public class NumberPanel2

OperatorPanel

/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - Operator Panel
*/

import javax.swing.*;
import java.awt.*;

public class OperatorPanel extends JPanel
{
public final char OPERATOR_ADD;
public final char OPERATOR_MINUS;
public final char OPERATOR_MULTIPLY;
public final char OPERATOR_DIVIDE;
public final char OPERATOR_POWER;

private JCheckBox add; // To select addition
private JCheckBox minus; // To select subraction
private JCheckBox multiply; // To select multiplication
private JCheckBox divide; // To select division
private JCheckBox power; // To select to the power of

public OperatorPanel()
{
setLayout(new GridLayout(5, 1)); // Create a GridLayout manager with 5 rows and one column

// Create the check boxes
add = new JCheckBox("Addition (+)");
minus = new JCheckBox("Subtraction (-)");
multiply = new JCheckBox("Multiplication (*)");
divide = new JCheckBox("Division (/)");
power = new JCheckBox("To the power of (^)");

// Add a border around the panel
setBorder(BorderFactory.createTitledBorder("Operator"));

// Add the check boxes to the panel
add(add);
add(minus);
add(multiply);
add(divide);
add(power);
} // End of OperatorPanel

public double getAnswer()
{
double output;

if (add.isSelected());
output = getFirstNumber.fNumber + getSecondNumber.sNumber; THIS IS WHERE I AM HAVING TROUBLE
} // End of getAnswer
} // End of public class Operator panel
:trollface:

You son of a bitch
 

up-n-atom

DARKLY Regular
You son of a bitch

Did you solve it? I'm not sure what the depth of your knowledge is suppose to be, meaning how far a long your course is. So it's dangerous to just splurt out a solution that may or may not be beyond what should be known. I can still point you towards the right direction. For example your two classes NumberPanel1 and NumberPanel2 are identical and therefore one is redundant, this is the purpose of OOP, reuse.

Code:
NumberPanel numberPanel1 = new NumberPanel();
NumberPanel numberPanel2 = new NumberPanel();

numberPanel1.getNumber();
numberPanel2.getNumber();

Another way to reduce the code and complexity is with loops and you can define your JRadioButtons as an array and you can use the Integer object to get a string from an int for the label. And this is where I might be overstepping the line as to what has been taught.

This forum really isn't the right place for this :P
 

Stan Radner

DARKLY Regular
Yeah I solved it. I cut down on some elements in my methods and just called the stored double value with a constructor. The assignment was only to create a calculator in one file and call everything using methods but I typically go all-out on my assignments. And this is my fourth Java course. I'm in my second year of college. I took a VB course for 2 semesters to see what it's like and it was really simple but I found it boring.

And my NumberPanel1 and NumberPanel2 classes are required as I'm creating a GUI and the main GUI file is going to need to reference those two other files for button input and then handle them via the ActionListener.

Calculator2 calls the file CalculatorGUI which calls the TitlePanel, NumberPanel1, NumberPanel2, and OperatorPanel files.

If I didn't have those separate files I could still compile Calculator2 but when I go to run it I would be presented with an error of something like "No main, applets, or file found".
 
Top