Pass by value problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • geebanga88
    New Member
    • Aug 2007
    • 62

    #1

    Pass by value problem

    Hi i am new to java, and am learning how to pass values through methods.

    i have this program which does not seem to pass the values correctly?
    Plz note im not allowed to use return values for this program.
    Code:
     
    class prac7assignment 
    {
       
       
        public static void main(String[] args) { 
           
            int modulusValue;
           
            System.out.println("Welcome to the Modulus calculator");
            
            getdata();
       
          return;
        }
     
        public static void getdata() {
            int number1, number2;
            
            System.out.println("Please enter your first number:");
            number1 = Keybd.in.readInt(); 
            
            System.out.println("Please enter your second number:"); 
            number2 = Keybd.in.readInt();
            
            calculate_modulus(number1, number2); 
            
            return;
        } 
        
        public static void calculate_modulus(int number1, int number2) { 
            int modulusValue; 
            
           modulusValue = number1 % number2; 
           displayModulus (modulusValue); 
           return;
            
      }  
        
        public static void displayModulus (int modulusValue) { 
           
            System.out.println("The modulus value is: " + modulusValue);
            
            return;
        }  
    }
    the modulus doesnt always give the correct answer. E.g. 1 % 2 = 5 but the program gives 1? E.g. 11 % 5 = 2, but program gives 1?
    i think because the values are not being passed properly. But again im sorta new to doing thing in java so not sure.
    Anyone noe what im doing wrong and how to fix it?
    Last edited by geebanga88; Oct 4 '07, 07:05 AM. Reason: code change
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    I just run this program and it "works"

    [CODE=java]import java.util.*;
    class prac7assignment
    {


    public static void main(String[] args) {

    int modulusValue;

    System.out.prin tln("Welcome to the Modulus calculator");

    getdata();

    return;
    }

    public static void getdata() {
    Scanner s = new Scanner(System. in);
    int number1, number2;

    System.out.prin tln("Please enter your first number:");
    number1 = s.nextInt();

    System.out.prin tln("Please enter your second number:");
    number2 = s.nextInt();

    calculate_modul us(number1, number2);

    return;
    }

    public static void calculate_modul us(int number1, int number2) {
    int modulusValue;

    modulusValue = number1 % number2;
    displayModulus (modulusValue);
    return;

    }

    public static void displayModulus (int modulusValue) {

    System.out.prin tln("The modulus value is: " + modulusValue);

    return;
    }
    }

    [/CODE]

    All those returns are hopelessly useless in there as well.

    Comment

    • geebanga88
      New Member
      • Aug 2007
      • 62

      #3
      Just wondering what is the scanner function and why do you use it over keybd.in?

      Comment

      • geebanga88
        New Member
        • Aug 2007
        • 62

        #4
        Ps thanks for the help the programm is working:)

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by geebanga88
          Just wondering what is the scanner function and why do you use it over keybd.in?
          Read all about it here.

          Comment

          • geebanga88
            New Member
            • Aug 2007
            • 62

            #6
            thanks for the reference

            Comment

            Working...