import java.lang.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class CalcConstruct {
public static void main(String[] args){
FrameConstruct y = new FrameConstruct("Calculator 3.0");
}
}
enum Operator {
MULTIPLY, DIVIDE, ADD, SUBTRACT, EXPONENT
}
class FrameConstruct extends JFrame {
static String left = "";
static String right = "";
static Operator oper = null;
static boolean isLeft = true;
static JTextField textfield = new JTextField();
static JButton a = new JButton("7");
JButton b = new JButton("8");
JButton c = new JButton("9");
JButton d = new JButton("/");
JButton e = new JButton("4");
JButton f = new JButton("5");
JButton g = new JButton("6");
JButton h = new JButton("*");
JButton i = new JButton("1");
JButton j = new JButton("2");
JButton k = new JButton("3");
JButton l = new JButton("+");
JButton m = new JButton("0");
JButton n = new JButton("=");
JButton o = new JButton("-");
JButton p = new JButton(".");//BUTTON INITILIZATION
JButton q = new JButton("+/-");
JButton pi= new JButton("\u03C0");
JButton to= new JButton("^");
JButton fa= new JButton("!")
FrameConstruct(String title) {
super(title);
setSize(300,300);
setResizable(false);
a.setFocusable(true);
a.requestFocusInWindow();
textfield.setEditable(false);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event){
System.exit(0);
}
});
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbcbutt1= new GridBagConstraints();
GridBagConstraints gbcbutt2= new GridBagConstraints();
GridBagConstraints gbctext= new GridBagConstraints();
gbctext.gridwidth = GridBagConstraints.REMAINDER;
gbctext.fill = GridBagConstraints.BOTH;
gbctext.weighty = 1.0;
gbcbutt1.weightx = 1.0;
gbcbutt1.fill = GridBagConstraints.BOTH;
gbcbutt2.weightx = 1.0;
gbcbutt1.weighty = 1.0;
gbcbutt2.fill = GridBagConstraints.BOTH;
gbcbutt2.gridwidth = GridBagConstraints.REMAINDER;
gbcbutt2.weighty = 1.0;
gbl.setConstraints(a, gbcbutt1);
gbl.setConstraints(b, gbcbutt1);
gbl.setConstraints(c, gbcbutt1);
gbl.setConstraints(e, gbcbutt1);
gbl.setConstraints(f, gbcbutt1);
gbl.setConstraints(g, gbcbutt1);
gbl.setConstraints(i, gbcbutt1);
gbl.setConstraints(j, gbcbutt1);
gbl.setConstraints(k, gbcbutt1);
gbl.setConstraints(m, gbcbutt1);
gbl.setConstraints(q, gbcbutt1);
gbl.setConstraints(p, gbcbutt1);
gbl.setConstraints(pi,gbcbutt1);
gbl.setConstraints(to,gbcbutt1);
gbl.setConstrainte(fa,gbcbutt1);
gbl.setConstraints(d, gbcbutt2);
gbl.setConstraints(h, gbcbutt2);
gbl.setConstraints(l, gbcbutt2);
gbl.setConstraints(o, gbcbutt2);
gbl.setConstraints(textfield, gbctext);
gbl.setConstraints(n, gbcbutt2);
setLayout(gbl); //FRAME SETUP
a.addKeyListener(new KeyListener(){
public void keyReleased(KeyEvent event){
}
public void keyPressed(KeyEvent event){
}
public void keyTyped(KeyEvent ev){
char key1 = ev.getKeyChar();
switch (key1) {
case '0':m.doClick(); break;
case '1':i.doClick(); break;
case '2':j.doClick(); break;
case '3':k.doClick(); break;
case '4':e.doClick(); break;
case '5':f.doClick(); break;
case '6':g.doClick(); break;
case '7':a.doClick(); break;
case '8':b.doClick(); break;
case '9':c.doClick(); break;
case '+':l.doClick(); break;
case '-':o.doClick(); break;
case '*':h.doClick(); break;
case '/':d.doClick(); break;
case '.':p.doClick(); break;
case '=':
case '\n':n.doClick(); break;
case '!':fa.doClick(); break;
case '^':to.doClick(); break;
}
}
}
);
a.addActionListener(new NumListener());
b.addActionListener(new NumListener());
c.addActionListener(new NumListener());
e.addActionListener(new NumListener());
f.addActionListener(new NumListener());
g.addActionListener(new NumListener());
i.addActionListener(new NumListener());
j.addActionListener(new NumListener());
k.addActionListener(new NumListener());
m.addActionListener(new NumListener());
p.addActionListener(new NumListener());
d.addActionListener(new OperListener());
h.addActionListener(new OperListener());
l.addActionListener(new OperListener());
q.addActionListener(new NumListener());
o.addActionListener(new OperListener());
n.addActionListener(new EqualListener());
pi.addActionListener(new NumListener());
fa.addActionListener(new FactorialListener());
to.addActionListener(new OperListener());//ACTION LISTENERS
getContentPane().add(textfield);
getContentPane().add(a);
getContentPane().add(b);
getContentPane().add(c);
getContentPane().add(d);
getContentPane().add(e);
getContentPane().add(f);
getContentPane().add(g);
getContentPane().add(h);
getContentPane().add(i);
getContentPane().add(j);
getContentPane().add(k);
getContentPane().add(l);
getContentPane().add(m);
getContentPane().add(q);
getContentPane().add(p);
getContentPane().add(o);
getContentPane().add(pi);
getContentPane().add(to); //ADD COMPONENTS
getContentPane().add(fa);
getContentPane().add(n);
setVisible(true); //SHOW THE FRAME
FrameConstruct.a.requestFocusInWindow();
}
}
class FactorialListener implements ActionListener {
public void actionPerformed(ActionEvent event){
// WHAT SHOULD I PUT HERE?
}
}
class NumListener implements ActionListener { //STORE NUMBERS IN FrameConstruct.left AND FrameConstruct.right
public void actionPerformed(ActionEvent event){
String number = ((JButton)(event.getSource())).getText();
if(FrameConstruct.left==""){
FrameConstruct.textfield.setText("");
}
String initialtext = FrameConstruct.textfield.getText();
if(number=="+/-"){
number="-";
}
if(number=="\u03C0"){
number = "3.1415926535897932384626";
}
if(FrameConstruct.isLeft){
FrameConstruct.left += number;
}
else{
FrameConstruct.right += number;
}
FrameConstruct.textfield.setText(initialtext+number);
FrameConstruct.a.requestFocusInWindow();
}
}
class OperListener implements ActionListener { //STORE OPERATORS IN FrameConstruct.oper
public void actionPerformed(ActionEvent event){
String initialtext = FrameConstruct.textfield.getText();
String operator = ((JButton)(event.getSource())).getText();
FrameConstruct.isLeft = false;
if(operator.equals("/")){
FrameConstruct.oper = Operator.DIVIDE;
FrameConstruct.textfield.setText(initialtext+" / ");
} else if(operator.equals("*")){
FrameConstruct.oper = Operator.MULTIPLY;
FrameConstruct.textfield.setText(initialtext+" * ");
} else if(operator.equals("-")){
FrameConstruct.oper = Operator.SUBTRACT;
FrameConstruct.textfield.setText(initialtext+" - ");
} else if(operator.equals("+")){
FrameConstruct.oper = Operator.ADD;
FrameConstruct.textfield.setText(initialtext+" + ");
} else if(operator.equals("^")){
FrameConstruct.oper = Operator.EXPONENT;
FrameConstruct.textfield.setText(initialtext+" ^ ");
}
FrameConstruct.a.requestFocusInWindow();
}
}
strictfp class EqualListener implements ActionListener {//PERFORM OPERATIONS WHEN = IS PRESSED
public void actionPerformed(ActionEvent event){
try {
if(FrameConstruct.isLeft){
FrameConstruct.textfield.setText("No operator");
FrameConstruct.isLeft = true;
FrameConstruct.left="";
FrameConstruct.right="";
return;
}
java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
nf.setMaximumFractionDigits(9);
nf.setGroupingUsed(false);
double myLeft = (new Double(FrameConstruct.left)).doubleValue();
double myRight = (new Double(FrameConstruct.right)).doubleValue();
if(FrameConstruct.oper.equals(Operator.DIVIDE)){
FrameConstruct.textfield.setText(nf.format(myLeft)+" / "+nf.format(myRight)+" = "+nf.format(myLeft/myRight));
} else if(FrameConstruct.oper.equals(Operator.MULTIPLY)){
FrameConstruct.textfield.setText(nf.format(myLeft)+" * "+nf.format(myRight)+" = "+nf.format(myLeft*myRight));
} else if(FrameConstruct.oper.equals(Operator.SUBTRACT)){
FrameConstruct.textfield.setText(nf.format(myLeft)+" - "+nf.format(myRight)+" = "+nf.format(myLeft-myRight));
} else if(FrameConstruct.oper.equals(Operator.ADD)){
FrameConstruct.textfield.setText(nf.format(myLeft)+" + "+nf.format(myRight)+" = "+nf.format(myLeft+myRight));
} else if(FrameConstruct.oper.equals(Operator.EXPONENT)){
FrameConstruct.textfield.setText(nf.format(myLeft)+" ^ "+nf.format(myRight)+" = "+nf.format(Math.pow(myLeft,myRight)));
}
FrameConstruct.isLeft = true;
FrameConstruct.left="";
FrameConstruct.right="";
} catch (Exception e){
FrameConstruct.textfield.setText("Syntax error");
FrameConstruct.left="";
FrameConstruct.right="";
FrameConstruct.isLeft = true;
}
FrameConstruct.a.requestFocusInWindow();
}
}
9 Replies - 1610 Views - Last Post: 03 May 2008 - 11:46 AM
#1
Factorials of ANY number, not just positive integers
Posted 01 May 2008 - 04:56 PM
I have been working on a Java calculator for quite a bit now, and I would like to be able to find the factorial of a number using only the Java API. However, I want the program to be able to calculate the factorial of any real number, not just natural numbers. (The definition of a factorial of a non-natural number can be found Here) This is my calculator so far. What code should I use for the FactorialListener class?
Replies To: Factorials of ANY number, not just positive integers
#2
Re: Factorials of ANY number, not just positive integers
Posted 01 May 2008 - 05:02 PM
Quote
not just natural number
Are you saying that you have all positive integers worked out?
0! = 0
-1! = -(1!)
-2! = +(2!)
-3! = -(3!)
-4! = +(4!)
See where I'm going? Take the factorial regularly, and then if the number is negative and odd; inverse the answer.
#3
Re: Factorials of ANY number, not just positive integers
Posted 01 May 2008 - 05:19 PM
spearfish, on 1 May, 2008 - 05:02 PM, said:
Quote
not just natural number
Are you saying that you have all positive integers worked out?
0! = 0
-1! = -(1!)
-2! = +(2!)
-3! = -(3!)
-4! = +(4!)
See where I'm going? Take the factorial regularly, and then if the number is negative and odd; inverse the answer.
Actually, that is not how you take the factorial of a negative integer. The factorial of a negative integer is undefined. However, every other real number has a real factorial, including negative non-integral real numbers. I need a way to calculate the factorials of non-negative integers (easy), positive non-integral real numbers, and negative non-integral real numbers.
Come on, is anyone going to help me anytime soon?
P.S. 0! = 1.
This post has been edited by herefishyfishy: 01 May 2008 - 05:55 PM
#4
Re: Factorials of ANY number, not just positive integers
Posted 01 May 2008 - 08:47 PM
Interesting your link... learned a lot about probability and espected size of solution
See PI a few places
but nothing really effective about calculating the factorial of a real number....
See PI a few places
but nothing really effective about calculating the factorial of a real number....
#5
Re: Factorials of ANY number, not just positive integers
Posted 01 May 2008 - 10:57 PM
Are you kidding?
Can we factorial a negative number?
My calculator cannot work it....
Can we factorial a negative number?
My calculator cannot work it....
#6
Re: Factorials of ANY number, not just positive integers
Posted 02 May 2008 - 03:32 PM
#7
Re: Factorials of ANY number, not just positive integers
Posted 02 May 2008 - 04:37 PM
I don't think there is an easy way to calculate these values. You are going to have to implement the
gamma function and perform the limit.
There is an approximation function that is provided on wikipedia for non-integer (positive value) factorials
http://en.wikipedia.org/wiki/Factorial
The runtime is exponential buts that's expected. The function in gamma1 should
be easy to implement in java. This is just an approximation mind you. I don't know if this approximation
works for negative values as I haven't tried it. It does seem to work for positive values though
gamma function and perform the limit.
There is an approximation function that is provided on wikipedia for non-integer (positive value) factorials
http://en.wikipedia.org/wiki/Factorial
The runtime is exponential buts that's expected. The function in gamma1 should
be easy to implement in java. This is just an approximation mind you. I don't know if this approximation
works for negative values as I haven't tried it. It does seem to work for positive values though
Attached image(s)
#8
Re: Factorials of ANY number, not just positive integers
Posted 03 May 2008 - 05:18 AM
skaoth, on 2 May, 2008 - 04:37 PM, said:
I don't think there is an easy way to calculate these values. You are going to have to implement the
gamma function and perform the limit.
There is an approximation function that is provided on wikipedia for non-integer (positive value) factorials
http://en.wikipedia.org/wiki/Factorial
The runtime is exponential buts that's expected. The function in gamma1 should
be easy to implement in java. This is just an approximation mind you. I don't know if this approximation
works for negative values as I haven't tried it. It does seem to work for positive values though
gamma function and perform the limit.
There is an approximation function that is provided on wikipedia for non-integer (positive value) factorials
http://en.wikipedia.org/wiki/Factorial
The runtime is exponential buts that's expected. The function in gamma1 should
be easy to implement in java. This is just an approximation mind you. I don't know if this approximation
works for negative values as I haven't tried it. It does seem to work for positive values though
I tried using the gamma function approximation, and my answers were VERY inaccurate. So then I tried using
class FactorialListener implements ActionListener {
public void actionPerformed(ActionEvent event){
try{
double myLeft = (new Double(FrameConstruct.left)).doubleValue();
double result = 1;
int i;
result=Math.exp(myLeft*Math.log(myLeft)-myLeft+Math.log(myLeft*(1+4*myLeft*(1+2*myLeft)))/6+Math.log(Math.PI)/2);
java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
nf.setMaximumFractionDigits(9);
nf.setGroupingUsed(false);
FrameConstruct.textfield.setText(nf.format(myLeft)+"! ="+nf.format(result));
FrameConstruct.isLeft = true;
FrameConstruct.left="";
FrameConstruct.right="";
} catch (Exception e){
FrameConstruct.textfield.setText("Syntax error");
FrameConstruct.left="";
FrameConstruct.right="";
FrameConstruct.isLeft = true;
}
FrameConstruct.a.requestFocusInWindow();
}
}
and it works, but it calculates 10! as 3628797.914471705, while its real value is 3628800.
This post has been edited by herefishyfishy: 03 May 2008 - 05:19 AM
#9
Re: Factorials of ANY number, not just positive integers
Posted 03 May 2008 - 06:14 AM
herefishyfishy, on 3 May, 2008 - 08:18 AM, said:
and it works, but it calculates 10! as 3628797.914471705, while its real value is 3628800.
Floating point operations in computers are always a little quirky. This is know and there's not a whole lot you can do about it, except allow for it.
Implement a function that does a standard factorial calculation for positive integers. Only use the approximation formula on those that can not be solved otherwise.
#10
Re: Factorials of ANY number, not just positive integers
Posted 03 May 2008 - 11:46 AM
Thank you!
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote






|