0

I've been given instructions to create a method that returns an Int "inputAge" with the condition that a previously input variable "age" from the method "run" is above 18.

However, since the Int "age" is declared and defined within the "run" method I'm not able to access it in the method returning the int from "inputAge", hence I'm unable to check if the Int "age" is greater than 18.

The instructions state I HAVE to use the "inputAge" method in this code, so no checking the input variable "age" is greater than 18 in the "run" method.

These instructions are lifted verbatim from my assigned worksheet (please forgive any broken English on the part of the instructor):

"Add the following feature in the Agency example. The age needs to be greater or equal to 18. If age is less than 18, the program will ask again, until the input is a number greater of equal to 18. You should implement a method called inputAge and call the method from run. The method will be as below (it is already added in the Java file available in the computer lab)"

public int inputAge()
{
        // add the code here to receive an integer number as input and 
        // check if the number is greater or equal to 18
        // if the number is less than 18, the program should ask the number again
        // until the input number is greater or equal to 18
        return(0);//note that 0 was added here just to be able to compile
}
import java.util.*;

public class AgencyInterface 
{ 
    private void run() 
    {
       Scanner console = new Scanner(System.in);
       Couple c = new Couple();
       int      age,end;
       String   name;
       
       do {
           System.out.print("first person: "); 
           System.out.print("name: "); 
           name = console.next();
           System.out.print("age: "); 
           age = console.nextInt();
           c.addData(1,name,age);

           System.out.print("second person: "); 
           System.out.print("name: "); 
           name = console.next();
           System.out.print("age: "); 
           age = console.nextInt();
           c.addData(2,name,age);

           System.out.println("********************");
           System.out.println(c.test());           
           System.out.println("********************");
           System.out.print("Quit? (0)yes (1)no: "); 
           end = console.nextInt();
           }
       while (end!=0);
    }
    public static void main(String[] args)
    {
           AgencyInterface agency = new AgencyInterface ();
           agency.run();
    }
    public int inputAge()
    {
            // add the code here to receive an integer number as input and 
            // check if the number is greater or equal to 18
            // if the number is less than 18, the program should ask the number again
            // until the input number is greater or equal to 18

                // The four comments above give instruction again what to do within here.
                // They were not written by me (Lachshmock).
            return(0);//note that 0 was added here just to be able to compile

    }

    }

1 Answer 1

1

Looks like you need to replace both occurrences of the lines:

System.out.print("age: "); 
age = console.nextInt();

with

age = inputAge();

Then inputAge should be something like:

public int inputAge()
{
    int age;
    do {
        // Get the age from stdin. Print error if < 18
    } while (age < 18);
    return age;
}

I left some things out since this looks like homework.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.