Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please explain every line of code import java.util.*; public class Hw3 { // Do not change this method public static void main(String[] args) { Scanner

Please explain every line of code

import java.util.*;

public class Hw3 {

// 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) { System.out.println("Entering the getPosInt method ..."); // Put code here to complete the method as described System.out.println("Leaving the getPosInt method ..."); // The return statement below is a stub -- replace it with the correct return return 0; } // Write this method public static String[] getNames(int m, Scanner theScan) { System.out.println("Entering the getNames method ..."); // Put code here to complete the method as described System.out.println("Leaving the getNames method ..."); // The return statement below is a stub -- replace it with the correct return 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 a total of five mistakes public static printSArray(String sArray) { System.out.println("Entering the printSArray method ..."); for (i = 0; i <= sArray.length; i++) System.out.println(sArray[i]) System.out.println("Leaving the printSArray method ..."); } }

To complete the assignment, complete the file called Hw3.java containing the Hw3 class. You should submit ONLY the Hw3.java file (and not in a zip file). I have provided a main method for you and that method should not be changed. You will complete the file by performing the following tasks:

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.

The following is some output from my completed solution:

How many people are there? Entering the getPosInt method ... Enter a positive (> 0) whole number: -3 Please enter a positive (> 0) number. Enter a positive (> 0) whole number: 0 Please enter a positive (> 0) number. Enter a positive (> 0) whole number: 4 Leaving the getPosInt method ... Entering the getNames method ... Enter a name: Gertrude Settle Enter a name: Prudence Berthiaume Enter a name: Misty Crystal Elam Enter a name: Shayna Wells Leaving the getNames method ... The names you entered are: Entering the printSArray method ... Gertrude Settle Prudence Berthiaume Misty Crystal Elam Shayna Wells Leaving the printSArray method ...

Note that there are print statements that tell you when you're entering and leaving each method so that you can distinguish what part of the output is occuring in each method. Please ask if something is not clear.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions