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 3 of 3

Thread: Re-using methods?

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re-using methods?

    So after a while I finally figured out the syntax to make a pretty simple password program. While I was at it, I thought I might try to run another method from a different class in the password program. Thing is, there were no errors, but the other methods would simply just not work. What is the proper syntax for getting a method from another class to work properly?

    Here's the short little bit of code that Im trying to get the method to work in:

    package OtherStuff;
     
    import acm.program.ConsoleProgram;
    import java.util.*;
     
    public class stringPassword extends ConsoleProgram {
     
    String password = "bob";
     
    public void run() {
    	ageCalc acalc = new ageCalc();
    	while (true) {
    		String input = readLine("Enter password here: ");
    		if (input.equals(password)) {
    			println("Access granted\n");
    			acalc.startAgeCalc();
    			break;
    		}
    		else
    			println("Access denied\n");
     
    		}
     
    	}
     
    }

    The other class is called obviously called ageCalc and the method im trying to run is startAgeCalc();. Everything in startAgeCalc() is public, and besides that I can't think of any other reason why this wouldnt work. But then again, I would have no idea in the first place as this will be my first time trying to call on a method from another class.

    Anyway, the solution is most likely simple, but I can't seem to figure it out. Could importing and extending ConsoleProgram be a playing factor? Thx in advance for the newbie help!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Re-using methods?

    Make sure your two classes know where each other are. If they are in the same package/folder, there's not really any extra work needed. If they are in different packages, you must import from the class that is using the other class. If they are in different project all-together, you'll need to figure out some way to either link projects together, or tell your program where to look for that class.

    Could you post either the compiler error or the runtime error you are getting?

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Re-using methods?

    Quote Originally Posted by helloworld922 View Post
    Make sure your two classes know where each other are. If they are in the same package/folder, there's not really any extra work needed. If they are in different packages, you must import from the class that is using the other class. If they are in different project all-together, you'll need to figure out some way to either link projects together, or tell your program where to look for that class.

    Could you post either the compiler error or the runtime error you are getting?
    Their both in the otherStuff package, so Im not sure if thats the problem. And the thing about the error is, Im getting no error whatsoever, just a non-functional piece of code. Heres the class I was trying to call a method from if it helps.

    package OtherStuff;
     
    import acm.program.ConsoleProgram;
     
     
    public class ageCalc extends ConsoleProgram {
     
    		String status;
    		int x;
    		int y;
     
    	public void run() {
    		int yearBorn = readInt("Enter year you were born: ");
    		int currentYear = readInt("Enter current year: ");
    		int currentAge = currentYear - yearBorn;
    		while (true) {
    			int inputYear = readInt("\nEnter a year: ");
    			int futureYear = inputYear - yearBorn;
    			int pastYear = inputYear - yearBorn;
     
    			if (inputYear < currentYear & inputYear != yearBorn) {
    				println("You were " + pastYear + " years old on this date.");
    			}
     
    			if (inputYear == yearBorn) {
    				println("This was the year you were born, and you are currently " + currentAge + " years old right now.");
    			}
     
    			if (inputYear > currentYear & inputYear != yearBorn) {
    				println("You will be " + futureYear + " years old in this year.");
    			}
     
    			if (inputYear == currentYear) {
    				println("This is the current year, and you are " + currentAge + " years old this year.");
    			}
    		}
    	}
     
     
     
    }

    Also, the method startAgeCalc in the original posted code was removed from the ageCalc class, and replaced with run(). They were the same exact copy and pasted piece of code, but I was just trying to experiment when I did that. So all in all, when you see startAgeCalc in the original post, that should be run().

Similar Threads

  1. Help with GUIs please & methods
    By killerknight141 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 2nd, 2010, 07:12 AM
  2. A question about inherited methods
    By joachim89 in forum Object Oriented Programming
    Replies: 1
    Last Post: January 12th, 2010, 09:06 PM
  3. Using variables from different methods?
    By Morevan in forum Object Oriented Programming
    Replies: 3
    Last Post: January 5th, 2010, 07:10 PM
  4. Working with Methods
    By duckman in forum Object Oriented Programming
    Replies: 3
    Last Post: November 9th, 2009, 08:27 PM
  5. numerical conversion methods..
    By chronoz13 in forum Java SE APIs
    Replies: 12
    Last Post: September 27th, 2009, 05:29 AM