Question: 1. Fill in the method below in order to complete a program called ReadAge. The program will keep asking an age value (using the message

1. Fill in the method below in order to complete a program called ReadAge. The program will keep asking an age value (using the message Enter age) as long as the user provides a negative value or a value larger than 120. Each time an invalid value is provided, the program will print the message Invalid value. Once a valid value has been provided, the program will print the provided value using the message Age: followed by the value. Use the Scanner class to read the value.

import java.util.Scanner; public class ReadAge {

public static void main(String[] args) {

2.

The method getMNumber takes a string as a parameter that represents a students M number. The string always starts with the letter M, and is followed by a number. The following are some examples of valid strings: M20912345, M20811111, M20933333, etc. The following is an example of an invalid string: MXYZ12345. The method returns the number (an integer) that follows M in the string. If the user provides an incorrect M number, that is, one with non-digits following the M, the method will print Invalid String to the console, using System.out.println(), and return the integer 0. For this problem:

Implement the getMNumber method.

Ensure that the message Invalid String is printed (using System.out.println) when an incorrect M number is entered, i.e., one with non-digits following the character M.

HINTS: Remember that the method charAt() returns the character at a particular position in a string, Character.isDigit(char thisChar) checks if thisChar is a digit, and Integer.parseInt(String intStr) parses a string into an integer.

Start with the following code:

import java.util.Scanner;

public class Utilities {

static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {

//Student enters a prompt for the user

String val = sc.nextLine();

System.out.println(getMNumber(val));

}

public static int getMNumber (String value) {

// Student completes so the integer following M is

// returned if value has only digits following the M,

// and otherwise prints Invalid String and returns 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!