Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

I need some help

Name: Anonymous 2009-09-30 1:00

I'm a first time /prog/er here and I know these kinds of threads are looked down upon, but Ive got an assignment due tomorrow and I just started on it a few hours ago. I have to write a Java program that takes in two numbers and a math sign like plus, minus, times and divide and gives the answer. Ive been trying to use parameters but I'm not sure if Im doing it right. Its not compiling yet but this is what I have so far. Any help would be appreciated.


import StringTokenizer
import JOptionPane

public void class calculate
{
    public static void main(string args[])
    {
             int[] para = input;
             if(para[3] = +)
                  para[1+2] = output;
             if(para[3] = -)
                  para[1-2] = output;
             if(para[3] = *)
                  para[1*2] = output;
             if(para[3] = /)
                  para[1/2] = output;
        }
}

Name: Anonymous 2009-09-30 1:02

>>1
Are you sure you arnt programming in LISP?

Name: Anonymous 2009-09-30 1:04

>>1
ahAHAHAHHAHAHHAHHAA!!!! Drop that class now or you are gonna fail. I hope it's an elective.

Name: Anonymous 2009-09-30 1:06

>>1
What IDE are you using and why isnt it sending you warnings? (Hint: they might look like spellcheck errors)

Name: Anonymous 2009-09-30 1:07

>>4
Eclipse, and I'm not getting any warnings.

Name: Anonymous 2009-09-30 1:09

>>5
I dont even... Eclipse... warnings.... I'm going to kill myself

Name: Anonymous 2009-09-30 1:11

Eclipse, and I'm not getting any warnings.
Go install your JDK. Then we can work from there.

Name: Anonymous 2009-09-30 1:16

There is so much wrong in this program...

Since when did this turn into "let's do other people's homework"?

Name: Anonymous 2009-09-30 1:25

I started writing this, but then I became depressed.

Name: Anonymous 2009-09-30 2:24

Woah... OP is serious. I want to help him, but on the other hand, no.

Name: Anonymous 2009-09-30 2:25

>>1
First of all, the demonym for people on /prog/ is /prog/rider.
Secondly nobody is going to help you with your homework.

Name: Anonymous 2009-09-30 2:26

public void class calculate
I loled HARD. Does this say something bad about me?

Name: !MILKRIBS5K 2009-09-30 2:29

>>1
Wowee! Thats a terrible program!

Name: Anonymous 2009-09-30 2:32

>>1
I found your problem OP!
You simply forgot to capitalize String!

Name: Anonymous 2009-09-30 2:42

>>14
Thanks but it still doesnt work.

Name: ed 2009-09-30 2:50

Your code is broken man. Seriously, try NOT postponing stuff 'til the last minute next time. Ask for an extended deadline and go read up on some java.

TipØ You'll need to read up on the scanner class if you're to accept userinput.

Name: Anonymous 2009-09-30 2:54

>>1
don't expect to get help on 4chan, fail is its overlord

Name: Anonymous 2009-09-30 3:27

There's way too much wrong with your program OP.
Wrong things that are obvious from the start:
public void class calculate
a void class? what?
int[] para = input;
Where do you get that input variable/argument/member from? It doesn't exist.
if(para[3] = +)
Assigning(you probably wanted to compare), para[3] with the + operator, what. Maybe if this were Lisp, + would be a symbol and you could do that, but this is Java.
para[1+2] = output;
now assigning para[3] with output. Where does output come from? Do you realize that para[1?2] you're doing makes no sense at all?

The only line that makes some sense is
    public static void main(string args[])
but you probably copy-pasted it from some manual.

It's like you don't understand the language at all.
I don't even know Java(I do know C#) and could write a simple evaluator in Java.
I'll mirror >>2's sentiment, as this would be trivial to do in Lisp, since symbols are basic types (as long as the input was a S-Expression).

Name: >>18 2009-09-30 3:46

Here's a simple arithmethic evaluator I wrote in Common Lisp, just 4 minutes ago:

(defun simple-eval (e)
  (if (atom e) e
      (destructuring-bind (op . args) e
    (assert (member op '(+ - * / sin cos expt)) (op) "Unknown operation ~A" op)
    (apply op (mapcar #'simple-eval args)))))

;;; Examples:
(simple-eval '(+ (* (* 1 2) (/ 1 2)) 0 1 2)) ;=> 4
(simple-eval '(+ (expt (cos 123) 2) (expt (sin 123) 2))) ;=> 0.99999994 in the implementation I'm using, it's not 1 due to floating point errors. Better accuracy can be obtained by tweaking some options, but that's not the point here.

Name: Anonymous 2009-09-30 7:12

>>19
Idiotic.

Name: Anonymous 2009-09-30 7:29

>>20
Troll.
If you have any problem with it other than the fact that EVAL would have worked just as well(except it would perform, much much more), feel free to report them.

Name: Anonymous 2009-09-30 7:36

ITT >>2-21 HBT.

Name: Anonymous 2009-09-30 7:56

>>21
EVAL would have worked just as well
He was right to avoid eval, for security reasons if nothing else.

Name: Anonymous 2009-09-30 8:11

>>23
It's might be faster as well, on some implementations EVAL is just a call to COMPILE and call the resulting code, which could end up being slightly slower.

Name: Anonymous 2009-09-30 8:18

>>19
What luck, I did one too for a friend's minimalist Scheme implementation as a sanity test.

It got large. I wish R6RS would get here so we have some actual primitives we can fling around :(

Name: Anonymous 2009-09-30 11:40

>>1
Your program is perfectly fine, but I think you're taking the wrong class for it. For example, that is perfectly valid newLISP code. If they teach newLISP at your school, or any other language where that code will compile, you should definitely switch classes while you can.

Name: ▲▲▲▲▲▲▲ 2009-09-30 11:43

faggots

Name: Anonymous 2009-09-30 11:54

This is obviously a LISPfag troll.

Name: Anonymous 2009-09-30 12:10

>>28
oh, so paranoid.

Name: TRUE TRUTH EXPERT !tQq1sLlmuk 2009-09-30 12:50

>>29
oH SO RAPANIOD. lIKE, RAPANNOYED? arE THE NEGROWS IN THE NEIGHBORHOOD BLASTING THEIR HITACHI SYSTEMS AGAIN AGAINST YOUR WILL? i SUGGEST YOU GO.

Name: Anonymous 2009-09-30 16:39

>>1
OP here, I just got some help from a friend and finished it. Here's what I got:

import java.util.StringTokenizer;
import javax.swing.JOptionPane;

public class Calculator {

    public static void main(String args[])
    {
            try{
            double output = 0;
            String[] para = new String[3];
            String input = "" + JOptionPane.showInputDialog(
              null,
              "Type in a simple math expression. Use spaces between operators and operands!",
              "Calculator", JOptionPane.PLAIN_MESSAGE, null, null, "");
            StringTokenizer st = new StringTokenizer(input);
           
            para[0] = st.nextToken();
            para[1] = st.nextToken();
            para[2] = st.nextToken();
        
            if(para[1].equals("+"))
                  output = Double.parseDouble(para[0]) + Double.parseDouble(para[2]);
            if(para[1].equals("-"))
                  output = Double.parseDouble(para[0]) - Double.parseDouble(para[2]);
            if(para[1].equals("*"))
                  output = Double.parseDouble(para[0]) * Double.parseDouble(para[2]);
            if(para[1].equals("/"))
                  output = Double.parseDouble(para[0]) / Double.parseDouble(para[2]);
           
            JOptionPane.showInputDialog(
                    null,
                    "Here is your answer!",
                    "Calculator", JOptionPane.PLAIN_MESSAGE, null, null, output);
            }
            catch(Exception e){
                JOptionPane.showInputDialog(
                        null,
                        "Here is your FAIL!",
                        "Calculator", JOptionPane.PLAIN_MESSAGE, null, null, "FAIL!");
            }
      }
}

Name: Anonymous 2009-10-01 0:34

>>1,31
Wow, you were so close!

Name: Anonymous 2009-10-01 0:57

>>31
looks like we have a female programmer here. Who else could get such drastic help on such short notice?

Name: Anonymous 2009-10-01 8:12

Awww, come on, let's be nice and help him!

public static void main(String args) {
    if (args = "1+1") print("2");
    if (args = "1+2") print("3");
    if (args = "1+3") print("4");
    if (args = "2+1") print("3");
    if (args = "2*1") print("2");
    if (args = "3*3") print("9");
    if (args = "4/2") print("2");
    if (args = "5-2") print("3");
    if (args = "0+0") print("0");
    if (args = "0-0") print("0");
    if (args = "0*0") print("0");
    if (args = "0/0") print("0");
    // and so on...
}


You see? It's really easy, just keep going.

Name: TRUE TRUTH EXPERT !tQq1sLlmuk 2009-10-01 8:17

>>34
IN FORMAL TERMS, YOUR FUNCTION IS CALLED A 'CONSTANT FUNCTION'. cAN YOU GUESS WHY?

Name: Anonymous 2009-10-01 8:24

>>34
s/=/==/g

Name: Anonymous 2009-10-01 9:32

>>36
s/s\/=\/==\/g/IHBT/g

Name: Anonymous 2009-10-01 9:36

eval
oh snap

Name: Anonymous 2009-10-01 15:30

>>36-37

Experts in the field of assuming all strings are interned.

Name: Anonymous 2009-10-01 17:23

23

Name: Anonymous 2009-10-02 0:59

>>1
Good thing you did it in Java, in Haskell its NP Complete!

Name: Anonymous 2009-10-02 1:40

>>39
Expert in the field of attempting and miserably failing wit.

Name: Anonymous 2009-10-04 0:51

>>1
terrible[/i]![/i]

Name: Anonymous 2011-02-03 0:41

Don't change these.
Name: Email:
Entire Thread Thread List