Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please explain every line of code import java.util.*; public class Hw3 { // Do n

ID: 3878796 • Letter: P

Question

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.

Explanation / Answer

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
// creating a variable and assigning it a negative values so
// that it can enter the while loop
int number = -1;
  
// while loop will keep asking user for a positive number
while(number<=0)
{
System.out.print("Enter a positive (> 0) whole number: ");
// getting an int value from Scanner
number = theScan.nextInt();
  
// if value is less than zero we need to show this message as per
// the output provided
if(number<=0)
System.out.println("Please enter a positive (> 0) number.");
}
  
System.out.println("Leaving the getPosInt method ...");
// The return statement below is a stub -- replace it with the correct return
return number;
}
  
// 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
  
// create an array of strings
String[] arrayNames = new String[m];
  
// for loop to get input values 'm' number of times
for(int i=0; i< m ; i++)
{
System.out.print("Enter a name:");
// assign values to array
arrayNames[i] = theScan.next();
}
  
System.out.println("Leaving the getNames method ...");
// The return statement below is a stub -- replace it with the correct return
return arrayNames;
}
  
// 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
  
// the input parameter should be an array type (1)
// the output parameter should be void as it is returning nothing (2)
public static void printSArray(String[] sArray) {
System.out.println("Entering the printSArray method ...");
// the i should have a datatype (3)
// the condition should be < not <= (4)
// ; missing from print statement (5)
for(int i = 0; i < sArray.length; i++)
System.out.println(sArray[i]);
System.out.println("Leaving the printSArray method ...");
}
}