Results 1 to 12 of 12
  1. #1
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Another math game, attempting oop

    Hello, I am attempting to create a maths game. I want the user to enter their favourite number. The program then brings up a list of 4 options (mathematical operations) in which the user proceeds to their choice of sums. I want my other class to take the input and create 10 questions (one at a time) and ask the user for their input. Then as before, output correct/wrong depending on user input. Hope I have explained myself enough.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package home;
    import java.util.Scanner;
    /**
     *
     * @author [A!B]Labtec
     */
    public class Home {
    
       
        public static void main(String[] args) {
         
            int choiceNum;
            int favNum;
            char add = '+';
            char div = '/';
            char mul = '*';
            char sub = '-';
            
            MathSum mathObj = new MathSum('+','/','*','-');
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter your favourite number: ");
            favNum = scan.nextInt();
            System.out.println("Please enter a number between 1 - 4: ");
            System.out.println("1. Addition Sums");
            System.out.println("2. Division Sums");
            System.out.println("3. Multiplication Sums");
            System.out.println("4. Subtraction Sums");
            choiceNum = scan.nextInt();  
            
            switch (choiceNum) {
                case 1: MathSum.addition(favNum, add);
                        break;
                case 2: MathSum.division(favNum, div);
                        break;
                case 3: MathSum.multiplication(favNum, mul);
                        break;
                case 4: MathSum.subtraction(favNum, sub);
                        break;     
                default: System.out.println("You did not enter a correct number");
                        break;
            }
            
        }
        
        
    }
    Here is my other class, where I want the operations/questions to take place.:

    Java Code:
    package home;
    
    /**
     *
     * @author Deus Ex - JC Denton
     */
    public class MathSum {
        char a;
        char d;
        char m;
        char s;
        
        public MathSum(char a, char d, char m, char s) {
             a = '+';
             d = '/';
             m = '*';
             s = '-';
        }
        
        static int addition(int w, char a) {
            
            for (int n=1;n<=10;n++) {
            
                System.out.println();
            }
            
            return w;
        }
        
        static int division(int x, char d) {
            return x;
        }
        
        static int multiplication(int y, char m) {
            return y;
        }
        
        static int subtraction(int z, char s) {
            return z;
        }
        
    }
    I'm sure I have declared too many variables in the MathSum class. I have no compilation or run-time errors though at the moment. I would be very grateful for some feedback on how to achieve my problem above, or even just a push in the right direction.

    Kindest regards,

    NM.
    Last edited by Nanomech; 11-23-2011 at 09:50 PM.
    [A!B]Java

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: Another math game, attempting oop

    Why does the MathSum constructor ignore the values of its parameters?
    The constructor changes the values of the parameters and does NOT change the class variables.
    When the constructor exits, all will go away and no variable will have a value.

    I didn't see your question.

    What are the methods in the MathSum class supposed to do? They get values passed to them but don't seem to do any operations with them. ???

    Where the methods are called, nothing is done with the value that is returned.

  3. #3
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Another math game, attempting oop

    I thought I was using the values of the parameters by setting them equal to the relative character??

    The idea is that my constructor is meant to set the variables to the different mathematical operators. Once set, be able to pass the correct char to the relative method (e.g. '+' gets passed to the addition() method), along with the users favourite number. Then, use these to create 10 addition questions with each question using the users favourite number.

    Rather than passing char's to the methods, can i set them up ready to just receive the users input?

    My methods atm are empty, I just wanted to get some sort of functionality between the classes and also I wanted some advice on my code. It seems I have put set things where I shouldn't have and now find myself a little confused.

    Regards,

    NM.
    [A!B]Java

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: Another math game, attempting oop

    The idea is that my constructor
    You need to look at variables that are parameters to a constructor vs variables that are class variables.
    Your code in the constructor does NOT reference the class variables. It changes the values of the parameter variables.
    Change the names of the parameters so they are different from the names of the class variables.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Another math game, attempting oop

    You need to read and think about what people say. Your MathSum constructor has certain values passed to it via the parameters. Then inside the constructor you immediately throw away those values and assign other values to the parameter variables. NOTE: the instance variables and parameters have the same name. So you are working with the parameters not the instance variables due to scoping rules.

  6. #6
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Another math game, attempting oop

    Hi, I have had a look and modified my MathSum.class. What I am trying to achieve is for the constructor to set the char's to their relative operation. Then pass these to the individual methods. First I want to sort the constructor out before starting on the methods. I have been looking through examples of constructors, trying to find one which explains it most simply. This is what I have. Showing no compilation or run-time errors.

    Java Code:
    package home;
    
    /**
     *
     * @author Deus Ex - JC Denton
     */
    public class MathSum {
        public char add;
        public char div;
        public char mul;
        public char sub;
        
        public MathSum(char a, char d, char m, char s) {
             add = a;
             div = d;
             mul = m;
             sub = s;
        }
        
        static void addition(int userInput) {
            
            
        }
        
        static void division(int userInput) {
            
        }
        
        static void multiplication(int userInput) {
            
        }
        
        static void subtraction(int userInput) {
            
        }
        
    }
    My question is am I on the right track with the constructor? Would you just create the individual char's inside the relative method?

    Regards,

    NM.
    Last edited by Nanomech; 12-11-2011 at 09:59 PM.
    [A!B]Java

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: Another math game, attempting oop

    Yes that looks like the values passed to the constructor would be saved in the class's variables.

  8. #8
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Another math game, attempting oop

    Ok so I have changed a couple of things here and there in both classes. I am getting the desired result at this moment. I just get the feeling that I am overloading the method. Ideally I wanted just one method to be able to accept the correct char depending on which sums the user wants to answer, along with their favorite number and print the questions. I just feel like I am overloading the methods because seeing what I've done in the addition() method, I'll have to create all that in the other methods.

    Java Code:
    package home;
    import java.util.Scanner;
    /**
     *
     * @author [A!B]Labtec
     */
    public class Home {
       
        public static void main(String[] args) { 
             int choiceNum;
            int favNum;
            
            MathSum mathObj = new MathSum('+','/','*','-');
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter your favourite number: ");
            favNum = scan.nextInt();
            System.out.println("Please enter a number between 1 - 4: ");
            System.out.println("1. Addition Sums");
            System.out.println("2. Division Sums");
            System.out.println("3. Multiplication Sums");
            System.out.println("4. Subtraction Sums");
            choiceNum = scan.nextInt();  
            
            switch (choiceNum) {
                case 1: MathSum.addition(favNum);
                        break;
                case 2: MathSum.division(favNum);
                        break;
                case 3: MathSum.multiplication(favNum);
                        break;
                case 4: MathSum.subtraction(favNum);
                        break;     
                default: System.out.println("You did not enter a correct number");
                        break;
            }
        } 
    }
    Java Code:
    package home;
    import java.util.Random;
    
    /**
     *
     * @author Deus Ex - JC Denton
     */
    public class MathSum {
        public static char add;
        public static char div;
        public static char mul;
        public static char sub;
        
        public MathSum(char a, char d, char m, char s) {
             add = a;
             div = d;
             mul = m;
             sub = s;
        }
        
        public static void addition(int userInput) {
            
            //userInput variable holds favNum value from Home.java class
            int i;
            int randNum;
            Random randomObject = new Random();
            
            System.out.println("Ok now you will be asked 10 mathematical addition questions all using your favourite number.");
            System.out.println("Your job is to provide the answer, good luck!");
            
            for(i=1;i<=10;i++) {
                
                randNum = randomObject.nextInt(99)+1;
                System.out.println(i+". "+userInput+add+randNum+"= ");
                
            }
        }
        
        static void division(int userInput) {
            
        }
        
        static void multiplication(int userInput) {
           
        }
        
        static void subtraction(int userInput) {
            
           
        }
        
    }
    Just looking for some advise really. I am not asking for any answers because then I will never learn. I just want to know if I'm going in the wrong direction completely.

    Regards,

    NM.
    [A!B]Java

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: Another math game, attempting oop

    I wanted just one method to be able to accept the correct char depending on which sums the user wants to answer
    Sorry, I do not understand what you are trying to do.
    I don't see any use for the 4 characters you are passing to the constructor. Would they ever be different from the 4 standard math operators? Would a every not be '+'?

  10. #10
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Another math game, attempting oop

    No, they will always remain the same. What I want to do is get the users favourite number. Then ask them which sums they want to do (addition/division/etc/etc). depending on what they input, I want to be able to pass that specific char to the method along with their favourite number. Once they enter the method, use a for loop to get 10 questions, one at a time and test the result). This is a long way off. I don't want the methods to return anything now. I need to be able to pass the correct char and their number into the method. I have changed the code a little. Here are both classes again. I have one method now which I want to do all the calculations.

    Java Code:
    ackage home;
    import java.util.Scanner;
    /**
     *
     * @author [A!B]Labtec
     */
    public class Home {
       
        public static void main(String[] args) { 
            int choiceNum;
            int favNum;
            
            
            MathSum mathObj = new MathSum('+','/','*','-');
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter your favourite number: ");
            favNum = scan.nextInt();
            System.out.println("Please enter a number between 1 - 4: ");
            System.out.println("1. Addition Sums");
            System.out.println("2. Division Sums");
            System.out.println("3. Multiplication Sums");
            System.out.println("4. Subtraction Sums");
            choiceNum = scan.nextInt();  
            
            switch (choiceNum) {
                case 1: MathSum.calculations(favNum);
                        break;
                case 2: MathSum.calculations(favNum);
                        break;
                case 3: MathSum.calculations(favNum);
                        break;
                case 4: MathSum.calculations(favNum);
                        break;     
                default: System.out.println("You did not enter a correct number");
                        break;
            }
        } 
    }
    Java Code:
    package home;
    import java.util.Random;
    
    /**
     *
     * @author Deus Ex - JC Denton
     */
    public class MathSum {
        public static char add;
        public static char div;
        public static char mul;
        public static char sub;
        
        public MathSum(char a, char d, char m, char s) {
             //set values when object was created within parenthesis in Home.java
             add = a; //add = '+'
             div = d; //div = '/'
             mul = m; //mul = '*'
             sub = s; //sub = '-'
        }
        
        public static void calculations(int userInput) {
            
            //userInput variable holds favNum value from Home.java class
            int i;
            int randNum;
            Random randomObject = new Random();
            
            System.out.println("Ok now you will be asked 10 mathematical addition questions all using your favourite number.");
            System.out.println("Your job is to provide the answer, good luck!");
            
            for(i=1;i<=10;i++) {
                
                randNum = randomObject.nextInt(99)+1;
              
                
            }
        }
    }
    Regards,

    NM.
    [A!B]Java

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: Another math game, attempting oop

    Was there a question?

  12. #12
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Another math game, attempting oop

    No. Close this thread please.

    Thank you.
    [A!B]Java

Similar Threads

  1. Math game
    By Nanomech in forum New To Java
    Replies: 61
    Last Post: 09-06-2011, 09:36 AM
  2. Replies: 8
    Last Post: 07-17-2011, 07:24 AM
  3. Replies: 0
    Last Post: 12-13-2010, 06:52 PM
  4. Replies: 2
    Last Post: 11-21-2008, 08:36 PM
  5. Attempting to make a Law of Sines program
    By taco89 in forum New To Java
    Replies: 1
    Last Post: 02-14-2008, 04:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •