3 Replies - 474 Views - Last Post: 02 July 2008 - 09:34 AM Rate Topic: -----

#1 freshoreo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-June 08

Problem calling program with parameters

Posted 01 July 2008 - 02:59 PM

I'm as tired of asking for help as you people are giving it. My professor gave us a brief overview of calling methods in the same file, but never went into depth on calling from another file. Then of course he gives us an assignment where we are to use 2 files and I can't get in contact with him. here is the code from my 1st (main) file:

import java.util.*;

public class NumberConvert
{
	public static void main(String args[])
	{
		System.out.println("Welcome to the Number Converter");
		System.out.println("");
		
		Scanner sc = new Scanner(System.in);
		int userNumber = 0;
		int choice = 0;
		
			System.out.println("Please enter a number between 1 and 100: ");
			userNumber = sc.nextInt();
			
			System.out.println("What would you like to convert your number to?");
			System.out.println("Type \"1\" for Binary \rType \"2\" for Octal \rType \"3\" for Hexadecimal");
			System.out.print("Your choice: ");
			choice = sc.nextInt();
			
			if (choice == 1)
			{
				Calculate.getBinary(userNumber);
			}
			else if (choice == 2)
			{
				Calculate.getOctal(userNumber);
			}
			else if (choice == 3)
			{
				Calculate.getHexadecimal(userNumber);
			}
			else
			{
				System.out.println("Invalid choice! Try again.");
			}
	}

}



And here is the file I am trying to call from:

public class Calculate
{
	public void getBinary(int userNumber)
	{
		System.out.print("The original number was " + userNumber + " and the binary converted number is " + Integer.toBinaryString(userNumber));
	}
	public void getOctal(int userNumber)
	{
		System.out.print("The original number was " + userNumber + " and the octal converted number is " + Integer.toOctalString(userNumber));
	}

	public void getHexadecimal(int userNumber)
	{
		System.out.print("The original number was " + userNumber + " and the hexadecimal converted number is " + Integer.toHexString(userNumber));
	}
} 



Any help is useful. Thank you!!

-Spencer

Is This A Good Question/Topic? 0
  • +

Replies To: Problem calling program with parameters

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4187
  • View blog
  • Posts: 11,847
  • Joined: 18-April 07

Re: Problem calling program with parameters

Posted 01 July 2008 - 03:29 PM

Ok first make sure both files are in the same directory. Then for each of your methods in the Calculate class add the word "static"

public class Calculate
{
    // Notice the word "static" used before "void"
    public static void getBinary(int userNumber)
    {
        System.out.print("The original number was " + userNumber + " and the binary converted number is " + Integer.toBinaryString(userNumber));
    }

    // Again we use "static"
    public static void getOctal(int userNumber)
    {
        System.out.print("The original number was " + userNumber + " and the octal converted number is " + Integer.toOctalString(userNumber));
    }

    // Last static again
    public static void getHexadecimal(int userNumber)
    {
        System.out.print("The original number was " + userNumber + " and the hexadecimal converted number is " + Integer.toHexString(userNumber));
    }
} 



So we added this word because you are attempting to call the methods of this class without first creating an instance of the "Calculate" class. Normally if you wanted to call this as an instance of the class, you would use the new keyword to create an instance and then call the methods of that instance like so...

// Create an instance
Calculate myCalculateClass = new Calculate();

// Call the method of that new instance variable
myConvertClass.getHexadecimal(5);



But since your code doesn't need to have an instance related with it, you can make your methods static and call them using the class name, like you are.

Just add the "static" to your methods and you are good to go. For more information look up static methods and it will tell you more about how they are used.

Good luck!

"At DIC we be static code ninjas... so static sometimes we don't even move. Sucks for dodging bullets." :snap:

This post has been edited by Martyr2: 01 July 2008 - 03:31 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Problem calling program with parameters

Posted 01 July 2008 - 04:26 PM

View PostMartyr2, on 1 Jul, 2008 - 03:29 PM, said:

"At DIC we be static code ninjas... so static sometimes we don't even move. Sucks for dodging bullets." :snap:

Lol :D :D :D
Was This Post Helpful? 0
  • +
  • -

#4 freshoreo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-June 08

Re: Problem calling program with parameters

Posted 02 July 2008 - 09:34 AM

Thank you!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1