Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
A o a!
I'm trying to creat a calculator using java swing componants that have sin, cos and tan functions and also inverse of these functions. I'm using Math.sin(x); function and like this others. Here is a problem in my code these six functions not work correctly but other functions work correctly like sinh, cosh and tanh and some others. Below I past the code that's I write in my program. Kindly if anyone understand please tell me where I'm wrong.

What I have tried:

Java
private void cosActionPerformed(java.awt.event.ActionEvent evt) {                                    
        num = Double.parseDouble(jTextField1.getText()) ;
        jLabel1.setText("Cos "+num);
        ans = Math.cos(num);
        jTextField1.setText(Double.toString(ans));
    }
Posted
Updated 21-Mar-21 22:31pm
v2
Comments
CPallini 22-Mar-21 3:40am    
Hello, you should report also the:
- input
- output
- expected output
in order to get help.
Patrice T 22-Mar-21 3:42am    
'Here is a problem in my code these six functions not work correctly'
Be more specific.
What input, actual output and expected output, on what code ?
Mehboob Shaukat 22-Mar-21 3:49am    
Hello Dear
Here is whole code.

public class Testclass extends javax.swing.JFrame {

/**
* Creates new form Testclass
*/

double num,ans;

public Testclass() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
sin = new javax.swing.JButton();
cos = new javax.swing.JButton();
tan = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

sin.setText("Sin");
sin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sinActionPerformed(evt);
}
});

cos.setText("Cos");
cos.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cosActionPerformed(evt);
}
});

tan.setText("Tan");
tan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tanActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(sin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextField1)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cos, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addComponent(tan, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(sin, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(cos, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(tan, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
Mehboob Shaukat 22-Mar-21 3:51am    
Kindly read this and tell me where I'm wrong. I'm very thankful to you.
Patrice T 22-Mar-21 4:13am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

What input, actual output and expected output ?

It's probably because you are providing degrees, and math.cos expects radians.
cos(1) radian is 0.54030230586
cos(1) degree is 0.99984769515

To convert from degrees to radians, just multiply the degrees by π/180.
 
Share this answer
 
Comments
Mehboob Shaukat 22-Mar-21 4:33am    
I'm very very thankful to you sir.
Very very thank you sir
OriginalGriff 22-Mar-21 5:20am    
You're welcome!
Quote:
In my code I enter 1 using sin function it gives answer 0.8414709848078965 while original value is 0.017452 and when I enter 1 using cos function it returns me 0.540302 while original value is 0.999848 and same like this others

The problem is your expectation !
You expect sin to work in degree, but it works in radian.
Read documentation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900