Question

Program Overview This brief exercise is designed for you to consider how reference variables behave when you are passing them1.2 Method name: readStudentNames Purpose: This method accepts an ArrayList of strings as a parameter. It loops through the AHow to Implement a for-each loop A for-each loop has the following structure. for (varType tmpVariable : dataStructure) e.g.Sample Output **** Passing Refereences - Reading 5 Names **** Main Method: Passing an Array, nameArray, by-value readStudentN

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// References.java

import java.util.Scanner;

public class References {

/**
   * method that accepts an array of strings as a parameter. It loops through the array
   * and asks the user to input names. It populates the array of strings with the names.
   * @param stringArray, an array of Strings
   */
   public static void readStudentNames(String[] stringArray)
   {
       Scanner keyboard = new Scanner(System.in);
       System.out.println("readStudentNames Method: Putting values inside stringArray parameter");
       // loop over the array, accepting strings for each array element
       for(int i=0;i<stringArray.length;i++)
       {
           System.out.print("Enter name "+(i+1)+": ");
           stringArray[i] = keyboard.nextLine();
       }
   }
  
   /**
   * method that accepts an ArrayList of strings as a parameter. It loops through the ArrayList
   * and asks the user to input names. It populates the ArrayList with the names.
   * @param stringArray, an ArrayList of Strings
   */
   public static void readStudentNames(ArrayList<String> stringList)
   {
       Scanner keyboard = new Scanner(System.in);
       System.out.println("readStudentNames Method: Putting values inside stringList parameter");
       // loop 5 times, to input 5 names into the array list
       for(int i=0;i<5;i++)
       {
           System.out.print("Enter name "+(i+1)+": ");
           stringList.add(keyboard.nextLine());
       }
   }
  
   public static void main(String[] args) {
  
       System.out.println("\t****** Passing References - Reading 5 Names *******");
       System.out.println("\nMain Method: Passing an Array, nameArray, by-value");
       String[] nameArray = new String[5];
       readStudentNames(nameArray);
       System.out.println("\nMain Method: The contents of nameArray are:");
       for(int i=0;i<nameArray.length;i++)
           System.out.println(nameArray[i]);
      
       System.out.println("\nMain Method: Passing an ArrayList, nameArrayList, by-value");
       ArrayList<String> nameArrayList = new ArrayList<String>();
       readStudentNames(nameArrayList);
       System.out.println("\nMain Method: The contents of nameArray are:");
       for(String name : nameArrayList)
           System.out.println(name);
   }  
}

// end of References.java

Output:

****** Passing References - Reading 5 Names ******* Main Method: Passing an Array, nameArray, by-value readStudentNames Metho



answered by: ANURANJAN SARSAM
Add a comment
Answer #2

// References.java

import java.util.Scanner;

public class References {

/**
   * method that accepts an array of strings as a parameter. It loops through the array
   * and asks the user to input names. It populates the array of strings with the names.
   * @param stringArray, an array of Strings
   */
   public static void readStudentNames(String[] stringArray)
   {
       Scanner keyboard = new Scanner(System.in);
       System.out.println("readStudentNames Method: Putting values inside stringArray parameter");
       // loop over the array, accepting strings for each array element
       for(int i=0;i<stringArray.length;i++)
       {
           System.out.print("Enter name "+(i+1)+": ");
           stringArray[i] = keyboard.nextLine();
       }
   }
  
   /**
   * method that accepts an ArrayList of strings as a parameter. It loops through the ArrayList
   * and asks the user to input names. It populates the ArrayList with the names.
   * @param stringArray, an ArrayList of Strings
   */
   public static void readStudentNames(ArrayList<String> stringList)
   {
       Scanner keyboard = new Scanner(System.in);
       System.out.println("readStudentNames Method: Putting values inside stringList parameter");
       // loop 5 times, to input 5 names into the array list
       for(int i=0;i<5;i++)
       {
           System.out.print("Enter name "+(i+1)+": ");
           stringList.add(keyboard.nextLine());
       }
   }
  
   public static void main(String[] args) {
  
       System.out.println("\t****** Passing References - Reading 5 Names *******");
       System.out.println("\nMain Method: Passing an Array, nameArray, by-value");
       String[] nameArray = new String[5];
       readStudentNames(nameArray);
       System.out.println("\nMain Method: The contents of nameArray are:");
       for(int i=0;i<nameArray.length;i++)
           System.out.println(nameArray[i]);
      
       System.out.println("\nMain Method: Passing an ArrayList, nameArrayList, by-value");
       ArrayList<String> nameArrayList = new ArrayList<String>();
       readStudentNames(nameArrayList);
       System.out.println("\nMain Method: The contents of nameArray are:");
       for(String name : nameArrayList)
           System.out.println(name);
   }  
}

// end of References.java

Output:

****** Passing References - Reading 5 Names ******* Main Method: Passing an Array, nameArray, by-value readStudentNames Metho

Add a comment
Know the answer?
Add Answer to:
Program Overview This brief exercise is designed for you to consider how reference variables behave when...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

    Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

  • Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring...

    Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem...

  • I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according...

    I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the...

  • Array lists are objects that, like arrays, provide you the ability to store items sequentially and...

    Array lists are objects that, like arrays, provide you the ability to store items sequentially and recall items by index. Working with array lists involves invoking ArrayList methods, so we will need to develop some basic skills. Let's start with the code below: The main method imports java.utii.ArrayList and creates an ArrayList that can hold strings by using the new command along with the ArrayList default constructor. It also prints out the ArrayList and, when it does, we see that...

  • Write a new program called TrickOr Treat that will do the following 1. In a while...

    Write a new program called TrickOr Treat that will do the following 1. In a while loop, say "Trick or Treat!" and allow the user to type in the name of a candy and store it in an ArrayList. Once the user types "Trick", then the loop should stop. Then, print out the total number of candies on the screen. (See example below) [1 points] 2. Write a method called countSnickers that will take an ArrayList of candy as an...

  • You will create a program with two methods, the main() method of course and another method...

    You will create a program with two methods, the main() method of course and another method called printArray(). The main method will define an array of 5 elements. A loop will be used to prompt the user to enter 5 numeric values which will go into the 5 array elements. The main() method will then call the printArray() method passing the array by reference, and the array length by value. The printArray() method will then print the contents of the...

  • Please create a program answering the following question in C PROGRAMMING. CorrectFormation Create a program and...

    Please create a program answering the following question in C PROGRAMMING. CorrectFormation Create a program and locally declare in main fname and Iname and completeName. Ask the user for their first and last name. Put the values into fname and Iname. You will create a function that is called as followed oinNames( fname, Iname, completeName ); You will pass the first name array, the last name array, and the array that will contain the complete name. The function named joinNames...

  • ArraysAndFiles.java and PartiallyFilledArray.java. They are shells that do not do anything. You will be adding code...

    ArraysAndFiles.java and PartiallyFilledArray.java. They are shells that do not do anything. You will be adding code after the comments in the main method and using the javadoc documentation to implement the other methods. Task 1: Send array data to the screen In ArraysAndFiles.java, create a new method printOnScreen and compile it. In the main method of ArraysAndFiles.java, add the code to declare and initialize the array of Strings and call printOnScreen to print the array on the screen. Task 2:...

  • In Java Main method Your main program will prompt the user for a test name and...

    In Java Main method Your main program will prompt the user for a test name and the number of scores the user will enter. After creating an arraylist to hold the scores, prompt the user for all the scores to hold in the arraylist. After all the scores are entered, then repeat back the score & letter grade. GradeBook Object Create an instance of a GradeBook object and pass the test name and arraylist to set the instance variables. At...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT