Question

part one : Review Exercises 1. Write method called raggedCount that takes an integer n as...

part one : Review Exercises

1. Write method called raggedCount that takes an integer n as argument and returns a ragged array of n rows of lengths 1, 2, 3, 4, ... , n and the whole array has the integers 1, 2, 3, 4, ... , n(n+1) 2 in row order. For example, raggedCount(4) should return the array: 1 2 3 4 5 6 7 8 9 10 1 2.

Write a method called arrayConcat that takes as argument two arrays a1 and a2 and returns an array that is the concatenation of the two arrays with the items of a2 following those of a1. 3. In the main method, write tests and print the results for all of the methods above. Feel free to implement helper printer methods to print your arrays (or simply implement those loops in main - you are welcome to use the Arrays.toString method).

part two: 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 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

-------

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

1

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

class Test{

public static void main(String[] args) {
int[][] r1 = raggedCount(4);
for(int i=0; i<r1.length; i++){
for(int j=0; j<r1[0].length;j++)
System.out.print(r1[i][j]+" ");
System.out.println("");
}

int[] a1 = {1,2,3,4};
int[] a2 = {5,6,7,8};
int[] r2 = arrayConcat(a1,a2);

for(int i=0; i<r2.length; i++) {
System.out.print(r2[i]+" ");
}


System.out.println("\n\n");
int myNum = 10;
argumentExampleOne(myNum);
System.out.println(myNum);

System.out.println("\n\n");

try {
Scanner s = new Scanner(new File("scannerExample.txt"));
argumentExampleTwo(s);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

}

public static int[][] raggedCount(int n){
int[][] arr = new int[2][n+1];
int c = 1;
for(int i = 0; i < 2; i++){
for(int j=0; j < n+1; j++){
arr[i][j] = c++;
}
}
return arr;
}

public static int[] arrayConcat(int[] a1, int[] a2){
int[] r = new int[a1.length+a2.length];
int i;
for(i=0; i < a1.length; i++)
r[i] = a1[i];
for(int j=0; j < a2.length; j++)
r[i++] = a2[j];
return r;
}

public static void argumentExampleOne(int n){
n = 2*n;
System.out.println(n);
}

public static void argumentExampleTwo(Scanner s){
System.out.println(s.next());
System.out.println(s.next());

try {
s = new Scanner(new File("file.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

O/P

Tagged array (n 4): 2 3 4 5 6 7 8 9 10 al[1, 2, 3, 4] a2[5, 6, 7, 8, 9] arrayConcat (al, a2)[1, 2, 3, 4, 5, 6, 7, 8, 9]

Add a comment
Know the answer?
Add Answer to:
part one : Review Exercises 1. Write method called raggedCount that takes an integer n as...
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 a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

  • Write a Java program that has a method called arrayAverage that accepts an arrary of numbers...

    Write a Java program that has a method called arrayAverage that accepts an arrary of numbers as an argument and returns the average of the numbers in that array. Create an array to test the code with and call the method from main to print the average to the screen. The array size will be from a user's input (use Scanner). - array size = int - avergage, temperature = double - only method is arrayAverage Output:

  • Part 3 (Lab2a) In this exercise you will: a. Write a method called secondTime that takes...

    Part 3 (Lab2a) In this exercise you will: a. Write a method called secondTime that takes as argument an integer corresponding to a number of seconds, computes the exact time in hours, minutes and seconds, then prints the following message to the screen: <inputseconds> seconds corresponds to: <hour> hours, <minute> minutes and <second> seconds Write another method called in Seconds that takes as arguments three integers: hours, minutes and seconds, computes the exact time in seconds, then returns the total...

  • package week_3; /** Write a method called countUppercase that takes a String array argument. You can...

    package week_3; /** Write a method called countUppercase that takes a String array argument. You can assume that every element in the array is a one-letter String, for example String[] test = { "a", "B", "c", "D", "e"}; This method will count the number of uppercase letters from the set A through Z in the array, and return that number. So for the example array above, your method will return 2. You will need to use some Java library methods....

  • JAVA Write a static method that takes an array of a generic type as its only...

    JAVA Write a static method that takes an array of a generic type as its only argument. The method should display the array and return the number of elements in the array. Test the method in main with at least three arrays of objects. SAMPLE OUTPUT Here is an Integer array 12 21 7 16 8 13 That array held 6 elements Here is a String array one two three four That array held 4 elements Here is a Double...

  • Language C Code Write a program that takes two integer arrays (A and B) and sums...

    Language C Code Write a program that takes two integer arrays (A and B) and sums them together (element wise). A third array to accept the result should be passed in as the output argument. Assume the arrays are all the same size. The argument N is the size of the arrays. Your code should provide a function with the following signature: void array Addition (int A[], int B[], int N, int output[]) { } Your code must also provide...

  • 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...

  • . In the method main, prompt the user for a non-negative integer and store it in...

    . In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...

  • Lab Objectives Be able to write methods Be able to call methods Be able to declare...

    Lab Objectives Be able to write methods Be able to call methods Be able to declare arrays Be able to fill an array using a loop Be able to access and process data in an array Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing...

  • In this lab, you will create one class named ArrayFun.java which will contain a main method...

    In this lab, you will create one class named ArrayFun.java which will contain a main method and 4 static methods that will be called from the main method. The process for your main method will be as follows: Declare and create a Scanner object to read from the keyboard Declare and create an array that will hold up to 100 integers Declare a variable to keep track of the number of integers currently in the array Call the fillArray method...

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