Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with simple enum problem

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with simple enum problem

    It receives two double numbers and an operation to be performed on these numbers. This operation could be PLUS, MINUS, TIMES or DIVIDES. (Define an “enum” with these operations.) The method should return a double with the result.

    I dont know what to write for it to test properly in main, and im not sure if the code runs correctly. Does it?


     
    public class Perform {
     
    public enum Operation { 
    PLUS, 
    MINUS, 
    TIMES, 
    DIVIDES
    }	
     
     
    private static Operation _operation;
    double result;
    double num1;
    double num2;
     
     
     
     
     
     
     
     
    public Perform(Operation _operation) {
    this._operation = _operation; 
    }	
     
     
    public Operation getOperation() {
    return _operation;
    }
     
    public void setOperation(Operation anOperation) {
    this._operation = anOperation;
    }
     
     
    public static double theResult (double input1, double input2, Operation aOperation) {
    double temp1 = input1;
    double temp2 = input2;
    _operation = aOperation;
     
    switch(_operation) {
    case PLUS: return temp1 + temp2;
    case MINUS: return temp1 - temp2;
    case TIMES: return temp1 * temp2;
    case DIVIDES: return temp1/temp2;
    default: return -1;
     
    }
    }
     
    public static void main(String[] args) {
    Perform p = new Perform(2.0, 2.0, PLUS);
    System.out.println(" 2 + 2 = " + p.theResult( 2.0, 2.0 ) );
    }
     
    }


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Help with simple enum problem

    Does that output what you expect? Maybe you should use the java.util.Random class and use that to test, see if you get some obviously incorrect results, if not, your code probably runs fine.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with simple enum problem

    It doesnt run. I was just taking a shot in the dark at testing it with those values.. the program doesnt actually compile with those lines in the main class. Im unsure what you write to test it though..

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with simple enum problem

    the program doesnt actually compile
    You should post the full text of the error messages.

Similar Threads

  1. [SOLVED] Enum types, how does he know %s value?
    By beer-in-box in forum Java Theory & Questions
    Replies: 11
    Last Post: September 17th, 2011, 12:47 PM
  2. Using enum, returning null
    By 9erNumber16 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 13th, 2011, 05:40 PM
  3. Declaring enum problem
    By kumalh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 5th, 2011, 06:30 AM
  4. String to Enum problems...
    By alex13809 in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: August 2nd, 2011, 10:59 AM
  5. [SOLVED] Enum problem
    By pajfilms in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 21st, 2011, 07:26 AM