In this part of the lab we will explore argument passing. In Java we can pass in
ID: 3576095 • Letter: I
Question
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 below), 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 file called "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 to method. Also, if your output file is blank, maybe you forgot to do something important at the end of your main...
------
in the file : quizGrades.txt
jane 10 9.6 9.8 8.9 10 8.4 9.5 9.7 10 9.7
bob 5.6 6.2 5.9 6.7 7.2 7.5 7 7.8 5.1 6
alice 9 9 8 8 7 8 7 8 9 9
steve 10 10 10 10 10 10 10 10 10 10
mary 0 0 0 0 7 7.5 7.8 8.2 8.2 8.3
jerry 1 2 3 4 5 6 7 8 9 10
stacie 3.4 4.2 3.9 4.6 4.5 4.8 5.6 5.7 5 4
john 10 9.5 9.7 8.5 10 8.1 9.8 10 10 9.7
ann 1 1 1 1 1 10 10 10 10 10
greg 2 3 3 3 4 3.5 3.5 4 4.2 4.5
debbie 8 8 8 8.9 8 8.4 8.5 8.7 8 8.7
tom 7.5 7.6 7.7 7.9 7 7.4 9.5 8.7 8.8 9.5
tina 6 7.6 7.8 6.9 7 7.4 7.5 7.7 8 8.3
----------
in the file : scannerExample.txt
zero one two three four five six seven
-------
Explanation / Answer
//Program1.java
public class Program1 {
public static void main(String[] args) {
//declare and set myNum =10
int myNum = 10;
//calling argumentExampleOne method
argumentExampleOne(myNum);
//print myNum after calling method
System.out.println("In main method, myNum = "+myNum);
}
//The method argumentExampleOne that takes myNum as integer
//and doubles the value and prints the myNum to console
public static void argumentExampleOne(int myNum) {
myNum=myNum*2;
System.out.println("In method argumentExampleOne, myNum = "+myNum);
}
}
sample output:
In method argumentExampleOne, myNum = 20
In main method, myNum = 10
--------------------------------------------------------------------------------------------------------------
2)
//Test program for argumentExampleTwo
//Program2.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Program2
{
private static String fileName="scannerExample.txt";
public static void main(String[] args) throws FileNotFoundException
{
//instance of Scanner class with File argument filename
Scanner scanner=new Scanner(new File(fileName));
//calling argumentExampleTwo
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 newscan=new Scanner(fileName);
//calling next method on newscanner will print the name of the file to console
System.out.println("Next word : "+newscan.next());
newscan.close();
}
}
Sample output:
First word : zero
Second word : one
Next word : scannerExample.txt
--------------------------------------------------------------------------------------------------------------
//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
{
//Scanner instance with file name quizGrades.tx
Scanner quizgradescanner=new Scanner(new File("quizGrades.txt"));
//PrintWriter class object with output.txt file
PrintWriter outputFileWriter=new PrintWriter(new File("output.txt"));
//Calling classGrades method with scanner and writer objects
classGrades(quizgradescanner, outputFileWriter);
//close file scanner objects
quizgradescanner.close();
outputFileWriter.close();
}
/**
* The method classGrades takes Scanner and PrintWriter objects
* as input arguments
*
* */
public static void classGrades(Scanner scanner, PrintWriter printWriter)
{
//Read until end of lines
while(scanner.hasNextLine())
{
//read name
String name=scanner.next();
//call quizAverage with scanner
double average=quizAverage(scanner);
//Write name and average score to file "output.txt"
printWriter.print(name+":");
printWriter.println(average);
}
}
/*
* The method quizAverage that takes Scanner class object
* and reads scores and converts to double and finds averatge 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 double value
total+=Double.parseDouble(scores[i]);
}
average=total/scores.length;
//return average
return average;
}//end of method quizAverage
}//end of the class
sample output:
quizGrades.txt
jane 10 9.6 9.8 8.9 10 8.4 9.5 9.7 10 9.7
bob 5.6 6.2 5.9 6.7 7.2 7.5 7 7.8 5.1 6
alice 9 9 8 8 7 8 7 8 9 9
steve 10 10 10 10 10 10 10 10 10 10
mary 0 0 0 0 7 7.5 7.8 8.2 8.2 8.3
jerry 1 2 3 4 5 6 7 8 9 10
stacie 3.4 4.2 3.9 4.6 4.5 4.8 5.6 5.7 5 4
john 10 9.5 9.7 8.5 10 8.1 9.8 10 10 9.7
ann 1 1 1 1 1 10 10 10 10 10
greg 2 3 3 3 4 3.5 3.5 4 4.2 4.5
debbie 8 8 8 8.9 8 8.4 8.5 8.7 8 8.7
tom 7.5 7.6 7.7 7.9 7 7.4 9.5 8.7 8.8 9.5
tina 6 7.6 7.8 6.9 7 7.4 7.5 7.7 8 8.3
output.txt
jane:9.56
bob:6.5
alice:8.2
steve:10.0
mary:4.7
jerry:5.5
stacie:4.57
john:9.530000000000001
ann:5.5
greg:3.47
debbie:8.32
tom:8.16
tina:7.42
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.