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
-------
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]](http://img.homeworklib.com/questions/ce7bd9c0-90c1-11eb-8792-21287e2f10ba.png?x-oss-process=image/resize,w_560)
part one : Review Exercises 1. Write method called raggedCount that takes an integer n as...
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 sameArrayBackwards. The method takes an array of integers, and checks if the numbers in the array are the same going forward as going backwards and will return a boolean (true or false) depending on the result. You can assume that there is at least one element in the array. Method Header: public static boolean sameArrayBackwards (int [] arr) You will test your method in the main method of your program by...
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:
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...
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...
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...
1) Write a public static method named printArray, that takes two arguments. The first argument is an Array of int and the second argument is a String. The method should print out a list of the values in the array, each separated by the value of the second argument. For example, given the following Array declaration and instantiation: int[] myArray = {1, 22, 333, 400, 5005, 9}; printArray(myArray, ", ") will print out 1, 22, 333, 400, 5005, 9 printArray(myArray,...
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....
Tasks Write a program that prints in text mode Project 2 written by YOURNAME And shows in a graphical way, as shown below, the names stored in a file. The coordinates to print the names will be also read from another file. Here is an example of what your graphic output should look like with the two files that are provided for test purposes. Drawing Panel Adam Bernie Cameron Daniel Fanny Gerard Harold Issac Jackie Raitlyn Note that the colors...
This is code for c++. Any help would be great. #include <iostream> using namespace std; /* 1. Create a method that prints your name */ /* 2. Create an overloaded method that takes an integer and prints it out multiplied * by itself * Print out the argument inside the method and then call the method from main * and print it out * What was the typed of passing used inhere ? */ /* 3. Create an overloaded method...