Simple Function, I'm just new to this. ("Operations")

edited May 2015 in How To...

I have an assignment and I just do not understand what it is that I have to do.

My assignment says: 1. Write a function in Processing, called “Operations” which will receive 3 values: int operType, int operand1, and int operand2. This function will perform a different operation based on operType:

operations(1,2,3); = Perform the addition of 2+3 operations(2,2,3); = Perform the multiplication of 2*3 operations(3,2,3); = Perform the subtraction of 2-3 operations(4,2,3); = Perform the division of 2/3 operations(5,2,3); = Perform the exponent of 23

The function should be of type float and return the result to the setup method for you to print it there HINT: Use if statements in your method to determine what operation you should use.

I understand how to do "calculator commands" as we call them in class but I do not understand what I have to do with the float.

Thank you.

Answers

  • edited May 2015 Answer ✓

    it's always best to start writing your code and then show it here

    you could call the function called “Operations” from draw() and use the return value (aka the result) in a println statement.

    • so you can call “Operations” from draw() 5 times each with another "calculator command"

    ;-)

  • edited May 2015
      import processing.pdf.*;
    
      void setup() {
        size (600, 600, PDF, "blank.pdf");
      }
    
      void draw() {
        operations(+, 2, 3);
        operation(*, 2, 3);
        operation(-, 2, 3);
        operation(/, 2, 3);
        operation(pow, 2, 3);
    
      void operations (int opertype, int operand1, int operand2) {
        println(opertype, operand1, operand2); 
      }
    

    I have this so far.

  • edited May 2015

    ok...

    the func "operations" should return something...

    you need to replace void by a type it should return...

    hint: see assignment

    also, you need to println the result the function is returning (that is in draw(), but in the assignment it says, do it in setup())

    when you use println, you don't need pdf

    println just prints to the text console

  • in the function, what would you do with "opertype" ?

    Hint: see assignment

    ;-)

  • So you get the idea…

    A switch, might be a good idea also...

    Untested:

    void setup() {  // this is run once.   
        size(400,400);
       println(multiFunctionFunction('a', 10, 20));
       println(multiFunctionFunction('b', 30, 40));
       println(multiFunctionFunction('c', 50, 60));
       println(multiFunctionFunction('d', 50, 60));
    
       } 
    
    float multiFunctionFunction(char c, int x, int y) { 
    
        if(c == 'a'){
                line(x, y, x+8, y+8);
                return 1.;
            }else if(c == 'b'){
                rect( x, y, 8, 8);
                return 2.;
                }else if(c == 'c'){
                    ellipse( x, y, 8, 8);
                return 3.;
                    }else{
                        return -1.;
                        }
    }
    
  • edited May 2015

    @vk I just read the plagiarism thread and I think you did too much for him in your answer.

    I think I told him what to do and he should do it himself to learn.

    So, no offence, I flaged your post, just to see happens next...........

    Best, Chrisir

    ;-)

  • @Chrisir, for forum rules we've gotta look at the link below, not that "plagiarism" thread:
    http://forum.processing.org/two/discussion/8042/processing-forum-rules-and-advices

    Here's the "Homework" excerpt: B-)

    Another annoyance: students ask for help for their homework, and in fear of being caught, delete their message.

    This is totally lacking of respect for those having took time to answer you. And the answers can be useful to somebody searching the forum.

    So the moderators always restore the deleted message. Just don't do it, it is useless and annoying. If you don't want your message to be seen, just don't ask it in a public forum!

    The worst way to ask for help for homework is to just paste in the text of the assignment and to ask for ready-to-use code... We are willing to help, and most teachers are OK with asking for help in a forum, but we all expect some work from you. Ie. you should show some code proving you worked on the topic, and you should ask specific questions about what is blocking you.

  • yeah, thanks for emphasizing my point here

    The worst way to ask for help for homework is to just paste in the text of the assignment

  • edited May 2015

    @Chrisir, I believe you've misunderstood the excerpt: :|
    The whole article is mostly about advises. Only a tiny portion of them are rules!
    An OP can simply paste the text assignment even though it's not the best procedure.
    And we aren't "law" cops for our fellow answerers either! =;

  • _vk_vk
    edited May 2015

    I think you did too much for him in your answer.

    Dear @Chrisir, I don't think so. But that's me. Where is the line? I've learned a lot from code in answers. I believe some times code is very eloquent. And this code can't be used as the answer to the assignment. To adapt it, one got to understand it. I would answer my son like this.

    But, again, that's me.

    I think it was clear that my intention was not to prevent or disturb OP's learning process.

    If care is not exercised to stay balanced in this matter, it can backfire, ending in preventing people to help others, afraid of forum's reaction. Good help can be lost... I do learn a lot from code answers.

    I think the point (about plagiarism) is made, and every one here agrees that no one should provide full code solutions, in a manner that the code could be used for cheating. But we don't need a patrol. If I was wrong, and crossed the line in this specific one here, well it was a little, with no intention. Not a big deal. Very few times (maybe 2 or 3) I've seen in this forum answers that could be used for cheating. And even those, were not clear about being assignments in first place... I believe we can relax a little in this particular matter. I think if we look back, before this discussion started, we will see a forum where this matter was not a big problem, and things were running smoothly. And again the point about plagiarism is clearly made by all the discussions on this issue. The only disagrees that still happening it is not if plagiarism is acceptable, its's not. They are about where to draw the line. My point is: the community common sense is good enough where it is... the line is well placed, we can relax and enjoy.

    This only is my humble opinion. Do what you got to do with my post. Flag, delete, ban... It's ok.

    :)

    edit: You see, before posting my judgment is the only one available... So I'll keep going by it. Once posted, then there is the community judgment, so judge... That's expected.

    peace, love and code !

  • no offence...

    you are doing great work here since years

    ;-)

  • _vk_vk
    edited May 2015

    I did not fill offended. :) cheers

    :)>-

  • Cheers mate!

  • @itgoesvnaaa :

    I think you can not pass a sign like + to a function

     operation(+, 2, 3);
    

    Did it work?

    I guess you need to say '+' instead, so it's a char.

    Anyway, in the assignment it says use 1,2,3,4 where you have +, * etc.

    So passing 1 as the first parameter tells the function, "hey, we want to use + later on".

    • Whereas the 2nd and 3rd parameter you are passing are the actual numbers you'll use in the calculation in your your function operations.

    read your assignment carefully. It's all in there.

    e.g. it says:

    Use if statements in your method [that's your function operations] to determine what operation you should use

    Chrisir ;-)

Sign In or Register to comment.