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

Argument Passing In this part of the lab we will explore argument passing. In Ja

ID: 3771684 • Letter: A

Question

Argument Passing

In this part of the lab we will explore argument passing. In Java we can pass information that methods need in order to do their jobs via arguments. However, arguments of primitive type or immutable class type have a different behavior than that of mutable class type.

1. Write a void method (call it argumentExampleOne) that takes an integer argument, modifies its value to be twice as much as originally, and prints it. In your main, create an int variable int myNum = 10, then call argumentExampleOne(myNum), then print the variable myNum below that. What do you observe? Write a few words about what is happening as a block comment above this method in your code.

2. Write a void method (call it argumentExampleTwo) that takes a Scanner object as argument, and first prints the next two strings from it. Then in the same method (below the prints), reassigns the Scanner object (i.e. you should create a new Scanner associated with the same file as it was done in main and reassign the argument variable). Finally, below the reassignment, it should print the next word.

In main create a Scanner object associated with the file "scannerExample.txt" (file provided - see Canvas), call your method with this Scanner as argument, then below your call print the next thing from your Scanner object. What do you observe? Write a few sentences about what is happening as a block comment in your code.

3. A more involved Scanner exercise - we have a file where each line contains the name of a student (as a single string) followed by his/her quiz grades for the quarter (doubles). The data is all space separated and is in a file called quizGrades.txt (file provided - see Canvas). Our goal is to generate an output filecalled "output.txt" that contains the average quiz grade for each student on each line, for example:

alice: 8.4

bob: 3.7

...

To accomplish this goal, we will declare the Scanner and PrintWriter objects in main, but not use them there...instead (for the sake of modularity and a learning exercise) we will pass them to methods that will divide the labor:

• public static double quizAverage(Scanner s) - this method takes a Scanner object. You may assume this Scanner is ready to read data of type double. This method should read all the doubles, find and return their average.

• public static void classGrades(Scanner s, PrintWriter p) - this method should take a Scanner object and a PrintWriter object as arguments. The purpose of this method is to write the names of students (from the Scanner s) along with the average quiz grade for that student on each line of the file associated with the PrintWriter p. This method should call the quizAverage method in order to get the average grade for each student.

In a block comment, explain the behavior of the Scanner object as it was passed along from method tomethod. Also, if your output file is blank, maybe you forgot to do something important at the end of your main...

Explanation / Answer

//ArgumentPassing.java
public class Program1
{
   public static void main(String[] args)
   {
       //Declare an integer variable,myNum=10
       int myNum=10;
       System.out.println("myNum "+myNum);
       //Call argumentExampleOne
       argumentExampleOne(myNum);      
   }

   /**
   * The method argumentExampleOne that takes an integer
   * argument myNum and twice the number myNum.
   * */
   private static void argumentExampleOne(int myNum)
   {
       //Multiply myNum by 2 and assign to myNum
       myNum=myNum*2;
       System.out.println("In method, myNum ="+myNum);
      
   }//end of method argumentExampleOne
  
}//end of class

-------------------------------------------------------------------------

Sample output:

myNum 10
In method, myNum =20


------------------------------------------------------------------------

//Test program for argumentExampleTwo
//Program2.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Program2
{  
   //Set file name
   private static String fileName="scannerExample.txt";
   public static void main(String[] args) throws FileNotFoundException
   {
       //Create an instance of Scanner class with fileName
       Scanner scanner=new Scanner(new File(fileName));
       //call argumentExampleTwo with scanner object
       argumentExampleTwo(scanner);      
   }

   /**
   * The method argumentExampleTwo takes scanner class object
   * and read first two string and then create a new Scanner
   * and reassign file name and calls next method to print next
   * word. That prints the name of the file open by the newscanner object.
   * */
   private static void argumentExampleTwo(Scanner scanner)
   {      
       System.out.println("First word : "+scanner.next());
       System.out.println("Second word : "+scanner.next());
       //Close scanner object
       scanner.close();
       String fileName="scannerExample.txt";
       /*Create a new instance of Scanner with file name**/
       Scanner newscanner=new Scanner(fileName);
       //Note : Calling next method on the Scanner created prints the
       //name of the argument passed to the Scaner class.
       System.out.println("Next word : "+newscanner.next());
       //Close newscanner object
       newscanner.close();
   }
}

----------------------------------------------------------------------

Sample input file :scannerExample.txt

word1 word2 word3 word4

-----------------------------------------------------------------------

Sample Output:

First word : word1
Second word : word2
Next word : scannerExample.txt

---------------------------------------------------------------------


//Test program for method classGrades and quizAverage
//Program3.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Program3
{
   public static void main(String[] args) throws FileNotFoundException
   {
      
       //Create an instance of Scanner class with input file
       Scanner scanner=new Scanner(new File("quizGrades.txt"));
       //Create an instance of PrintWriter class with output.txt file
       PrintWriter writer=new PrintWriter(new File("output.txt"));
       //Calling classGrades method with scanner and writer objects
       classGrades(scanner, writer);
       //close file scanner objects
       scanner.close();
       writer.close();      
   }
  
   /**
   * The method classGrades takes Scanner and PrintWriter objects
   *
   * */
   public static void classGrades(Scanner scanner, PrintWriter printWriter)
   {      
       //Read until end of lines
       while(scanner.hasNextLine())
       {
           //Read name
           String name=scanner.next();
           //call quizAverae with scanner
           double average=quizAverage(scanner);
           //Write name and average score to file
           printWriter.print(name+":");
           printWriter.println(average);
       }
      
   }
  
   /*
   * The method quizAverage that takes Scanner class object
   * and returns the average of scores
   * */
   public static double quizAverage(Scanner scanner)
   {
       double average=0;
       double total=0;
       //call line and trim metods on string
       String arr=scanner.nextLine().trim();
       String[] scores=arr.split(" ");      
       for (int i = 0; i < scores.length; i++)
       {
           //convert string to integer values
           total+=Integer.parseInt(scores[i]);
       }
      
       average=total/scores.length;
       //return average
       return average;
      
   }//end of method quizAverage
  
}//end of the class


---------------------------------------------------------------------

Sample input file : quizGrades.txt

alice 8 6 9 8
bob 8 5 6 3

---------------------------------------------------------------------

sample output:

alice:7.75
bob:5.5

Note :sample file scores might be different from your sample quizGrades.txt

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote