Question
Write the getPosInt method : This method takes a Scanner object as a parameter. It prompts the user to enter a positive whole number, and
Write the getPosInt method: This method takes a Scanner object as a parameter. It prompts the user to enter a positive whole number, and repeatedly prompts the user until he/she enters a positive (> 0) number. Once the user has entered a positive number, it returns that value. The method should not open or close a Scanner. That is done in the main method.
Write the getNames method: The method takes an integer m and a Scanner object as parameters. It first creates an array of m Strings. It then enters m names (which may include whitespace) from the user using the Scanner and places them into the array. Finally, it returns the filled array of names. The method should not open or close a Scanner. That is done in the main method.
Debug the printSArray method provided for you: This methods has both compilation and runtime errors, and for the assignment you need to correct the code so that it works. As you make a correction, you must put a comment above the line(s) changed that describes the correction you made. Your comments as well as your corrections will be graded. Also note that you should not change the basic functioning of the method. The number of each type of error is provided in the comments in the template.
package hw6;
import java.util.*;
public class Hw6 {
// Do not change this method public static void main(String[] args) { Scanner aScan = new Scanner(System.in); System.out.println("How many people are there? "); int max = getPosInt(aScan); String[] names = getNames(max, aScan); System.out.println("The names you entered are: "); printSArray(names); aScan.close(); } // Write this method public static int getPosInt(Scanner theScan) { // A stub -- delete when you have completed the method return 0; } // Write this method public static String[] getNames(int m, Scanner theScan) { // A stub -- delete when you have completed the method return new String[0]; } // The method below contains compilation and runtime errors which you // should: 1. correct and 2. mark so that I can see the changes you've made. // Do not change the basic functioning of the method -- just make it run. // There are four compilation errors and one run-time error public static printSArray(String sArray) { for (i = 0; i <= sArray.length; i++) System.out.println(sArray[i]) } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started