Activity 1. Printing Arrays. In this activity, you will have to implement three
ID: 3682648 • Letter: A
Question
Activity 1. Printing Arrays. In this activity, you will have to implement three printing methods.
Method 1: printArray
Parameter: a 1D array of Strings
Return type: void
This method prints the content of a 1D array of strings, one string per line.
Requirement: You are expected to use a for loop to implement this method.
Method2: printArray
Parameter: a 1D array of integers
Return type: void
This method prints the content of a 1D array of integers, with one space between each number and returning to the next line once done printing all numbers.
Requirement: You are expected to use a for loop to implement this method.
Method3: printArray
Parameter: a 2D array of integers
Return type: void
This method prints the content of a 2D array of integers, one line per row of the 2D array.
Requirement: You are expected to call the method printArray on 1D arrays of integers to implement this method.
Interesting (and important) point: You will notice that all methods have the same name. This is not a mistake. You are allowed to do this. It is actually very convenient because it allows you to describe the same functionality using the same name, even if it does not really apply to the same types of parameters or even the same number of parameters. This feature of Java (as well as of some – not all – other programming languages) is called “overloading”: we can “overload” the name of a method with different types of parameters. At execution time, the relevant body will be called based on the type(s) of parameters being passed.
Instructions:
Complete the code provided in Lab7Spring16.java where prompted (and only where prompted) to make sure that the code follows the above instructions.
Note: inability to follow instructions will be penalized by 10 points.
Now, here is what you have to turn in:
The modified java file called Lab7Spring16.java.
Method 1: printArray
Parameter: a 1D array of Strings
Return type: void
Explanation / Answer
Hi, Please find the below class implemented as per your requirement.
Lab7Spring16.java
public class Lab7Spring16 {
public static void main(String[] args) {
String s[] = {"Hai", "Hello", "Bye", "Good Morning", "Good Evening"};
int a[] = {1, 2, 3, 6, 7, 8, 5};
int b[][]={{50,60,55},{62,65,70},{72,66,77}};
System.out.println("String 1D array contents display....");
printArray(s);
System.out.println("Integer 1D array contents display....");
printArray(a);
System.out.println("Integer 2D array contents display....");
for(int i=0; i<b.length; i++){
printArray(b[i]);
}
}
public static void printArray(String str[]){
String temp = "";
for(int i=0; i<str.length; i++){
temp = temp + str[i] + " ";
}
System.out.println(temp);
}
public static void printArray(int a[]){
String temp = "";
for(int i=0; i<a.length; i++){
temp = temp + a[i] + " ";
}
temp = temp + " ";
System.out.println(temp);
}
}
Output:
String 1D array contents display....
Hai
Hello
Bye
Good Morning
Good Evening
Integer 1D array contents display....
1 2 3 6 7 8 5
Integer 2D array contents display....
50 60 55
62 65 70
72 66 77
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.