A "Trig" Calculating Class
Posted
by
Clinton Scott
on Stack Overflow
See other posts from Stack Overflow
or by Clinton Scott
Published on 2013-10-28T21:05:09Z
Indexed on
2013/10/28
21:53 UTC
Read the original article
Hit count: 242
I have been trying to create a gui that calculates trigonometric functions based off of the user's input. I have had success in the GUI part, but my class that I wrote to hold information using inheritance seems to be messed up, because when I run it gives an error saying:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - constructor ArcTrigCalcCon in class TrigCalc.ArcTrigCalcCon cannot be applied to given types; required: double,double,double,double,double,double found: java.lang.Double,java.lang.Double,java.lang.Double reason: actual and formal argument lists differ in length at TrigCalc.TrigCalcGUI.(TrigCalcGUI.java:31) at TrigCalc.TrigCalcGUI.main(TrigCalcGUI.java:87) Java Result: 1
and says it is the object causing the problem. Below Will be my code. First I will put up my inheritance class with cosecant secant and cotangent and then my original class with the original 3 trig functions:
{
public ArcTrigCalcCon(double s, double cs, double t, double csc, double sc, double ct)
{
// Inherit from the Trig Calc class
super(s, cs, t);
cosecant = 1/s;
secant = 1/cs;
cotangent = 1/t;
}
public void setCsc(double csc)
{
cosecant = csc;
}
public void setSec(double sc)
{
secant = sc;
}
public void setCot(double ct)
{
cotangent = ct;
}
}
Here is the first Trigonometric class:
public class TrigCalcCon
{
public double sine;
public double cosine;
public double tangent;
public TrigCalcCon(double s, double cs, double t)
{
sine = s;
cosine = cs;
tangent = t;
}
public void setSin(double s)
{
sine = s;
}
public void setCos(double cs)
{
cosine = cs;
}
public void setTan(double t)
{
tangent = t;
}
public void set(double s, double cs, double t)
{
sine = s;
cosine = cs;
tangent = t;
}
public double getSin()
{
return Math.sin(sine);
}
public double getCos()
{
return Math.cos(cosine);
}
public double getTan()
{
return Math.tan(tangent);
}
}
and here is the demo class to run the gui:
public class TrigCalcGUI extends JFrame implements ActionListener
{
// Instance Variables
private String input;
private Double s, cs, t, csc, sc, ct;
private JPanel mainPanel, sinPanel, cosPanel, tanPanel, cscPanel, secPanel,
cotPanel, buttonPanel, inputPanel, displayPanel; // Panel Display
private JLabel sinLabel, cosLabel, tanLabel, secLabel, cscLabel,
cotLabel, inputLabel;
private JTextField sinTF, cosTF, tanTF, secTF,
cscTF, cotTF, inputTF; //Text Fields for sin, cos, and tan, and inverse
private JButton calcButton, clearButton; // Calculate and Exit Buttons
// Object
ArcTrigCalcCon trC = new ArcTrigCalcCon(s, cs, t);
public TrigCalcGUI()
{
// title bar text.
super("Trig Calculator");
// Corner exit button action.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create main panel to add each panel to
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(3,2));
displayPanel = new JPanel();
displayPanel.setLayout(new GridLayout(3,2));
// Assign Panel to each variable
inputPanel = new JPanel();
sinPanel = new JPanel();
cosPanel = new JPanel();
tanPanel = new JPanel();
cscPanel = new JPanel();
secPanel = new JPanel();
cotPanel = new JPanel();
buttonPanel = new JPanel();
// Call each constructor
buildInputPanel();
buildSinCosTanPanels();
buildCscSecCotPanels();
buildButtonPanel();
// Add each panel to content pane
displayPanel.add(sinPanel);
displayPanel.add(cscPanel);
displayPanel.add(cosPanel);
displayPanel.add(secPanel);
displayPanel.add(tanPanel);
displayPanel.add(cotPanel);
// Add three content panes to GUI
mainPanel.add(inputPanel, BorderLayout.NORTH);
mainPanel.add(displayPanel, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
//add mainPanel
this.add(mainPanel);
// size of window to content
this.pack();
// display window
setVisible(true);
}
public static void main(String[] args)
{
new TrigCalcGUI();
}
private void buildInputPanel()
{
inputLabel = new JLabel("Enter a Value: ");
inputTF = new JTextField(5);
inputPanel.add(inputLabel);
inputPanel.add(inputTF);
}
// Building Constructor for sinPanel cosPanel, and tanPanel
private void buildSinCosTanPanels()
{
// Set layout and border for sinPanel
sinPanel.setLayout(new GridLayout(2,2));
sinPanel.setBorder(BorderFactory.createTitledBorder("Sine"));
//
sinTF = new JTextField(5);
sinTF.setEditable(false);
sinPanel.add(sinTF);
// Set layout and border for cosPanel
cosPanel.setLayout(new GridLayout(2,2));
cosPanel.setBorder(BorderFactory.createTitledBorder("Cosine"));
cosTF = new JTextField(5);
cosTF.setEditable(false);
cosPanel.add(cosTF);
// Set layout and border for tanPanel
tanPanel.setLayout(new GridLayout(2,2));
tanPanel.setBorder(BorderFactory.createTitledBorder("Tangent"));
tanTF = new JTextField(5);
tanTF.setEditable(false);
tanPanel.add(tanTF);
}
// Building Constructor for cscPanel secPanel, and cotPanel
private void buildCscSecCotPanels()
{
// Set layout and border for cscPanel
cscPanel.setLayout(new GridLayout(2,2));
cscPanel.setBorder(BorderFactory.createTitledBorder("Cosecant"));
//
cscTF = new JTextField(5);
cscTF.setEditable(false);
cscPanel.add(cscTF);
// Set layout and border for secPanel
secPanel.setLayout(new GridLayout(2,2));
secPanel.setBorder(BorderFactory.createTitledBorder("Secant"));
secTF = new JTextField(5);
secTF.setEditable(false);
secPanel.add(secTF);
// Set layout and border for cotPanel
cotPanel.setLayout(new GridLayout(2,2));
cotPanel.setBorder(BorderFactory.createTitledBorder("Cotangent"));
cotTF = new JTextField(5);
cotTF.setEditable(false);
cotPanel.add(cotTF);
}
private void buildButtonPanel()
{
// Create buttons and add events
calcButton = new JButton("Calculate");
calcButton.addActionListener(new CalcButtonListener());
clearButton = new JButton("Clear");
clearButton.addActionListener(new ClearButtonListener());
buttonPanel.add(calcButton);
buttonPanel.add(clearButton);
}
@Override
public void actionPerformed(ActionEvent e)
{
}
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
// Declare boolean variable
boolean incorrect = true;
// Set input variable to input text field text
input = inputTF.getText();
ImageIcon newIcon;
ImageIcon frowny = new
ImageIcon(TrigCalcGUI.class.getResource("/Sad_Face.png"));
Image gm = frowny.getImage();
Image newFrowny = gm.getScaledInstance(100, 100,
java.awt.Image.SCALE_FAST);
newIcon = new ImageIcon(newFrowny);
// If boolean is true, throw exception
if(incorrect)
{
try{Double.parseDouble(input); incorrect = false;}
catch(NumberFormatException nfe)
{
String s = "Invalid Input "
+ "/n Input Must Be a Numerical value."
+ "/nPlease Press Ok and Try Again";
JOptionPane.showMessageDialog(null, s, "Invalid",
JOptionPane.ERROR_MESSAGE, newIcon);
inputTF.setText("");
inputTF.requestFocus();
}
}
// If boolean is not true, proceed with output
if (incorrect != true)
{
/* Set each text field's output to the String double value
* of inputTF
*/
sinTF.setText(input);
cosTF.setText(input);
tanTF.setText(input);
cscTF.setText(input);
secTF.setText(input);
cotTF.setText(input);
}
}
}
/**
* Private inner class that handles the event when
* the user clicks the Exit button.
*/
private class ClearButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
// Clear field
sinTF.setText("");
cosTF.setText("");
tanTF.setText("");
cscTF.setText("");
secTF.setText("");
cotTF.setText("");
// Clear textfield and set cursor focus to field
inputTF.setText("");
inputTF.requestFocus();
}
}
}
© Stack Overflow or respective owner