Question

Java programming: I need to create a method that is given a 2D char array and...

Java programming: I need to create a method that is given a 2D char array and a String that returns a part of the original 2D array. The input string will tell the method which rows from the input array need to be returned. For example, a 5x5 char array input along with a string "0 4" would return rows 1 and 5 of the input array. The String input will always be numbers divided by spaces and will not include numbers smaller than 0 nor numbers larger than inputArray.length-1 (because arrays count from 0 but .length counts from 1). The string input will also always be whole numbers. The output must be stored as a deep copy.  Return null when any of the parameters are empty or null

Example:

char[][] Input = {{'a', 'A', 'a', 'A', 'a'}, {'B', 'b', 'B', 'b', 'B'}, {'c', 'C', 'c', 'C', 'c'}, {'D', 'd', 'D', 'd', 'D'}}

String Input = "3 0 1"

char[][] Output = {{'D', 'd', 'D', 'd', 'D'}, {'a', 'A', 'a', 'A', 'a'}, {'B', 'b', 'B', 'b', 'B'}} (must be deep copy, not printed in the command window)

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

The answer to this question is as follows:

The code is as follows:

import java.util.*;
public class MyClass {
public static void main(String args[]) {
int rows,columns;
String str;
Scanner sc=new Scanner(System.in);
//reading the string
str=sc.nextLine();
char[][] input=new char[10][20];
char[][] output=new char[10][20];
//reading the rows and columns for the dimensions of the array
rows=sc.nextInt();
columns=sc.nextInt();
//reading the values into the input array is as follows
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
input[i][j]=sc.next().charAt(0);
}
}
//spliting the string and stroing the values into the ouput array
String[] array=str.split(" ");
int length=array.length;
for (String a : array)
{
int temp = Integer.parseInt(a);
for(int i=temp;i<length;i++)
{
for(int j=0;j<columns;j++)
{
output[i][j]=input[i][j];
}
}
}
//displaying the values that are stored in the output array
for(int i=0;i<length;i++)
{
for(int j=0;j<columns;j++)
{
System.out.print(output[i][j]+",");
}
System.out.println();
}

  
}
}

The input and ouput is as follows:

The example input is as follows:

3 0 1
4 5
a A a A a B b B b B c C c C c D d D d D

Here 3 0 1 represents the string

and the 4 5 represents the rows and columns for the dimensions for the array

and the last line is for the character array

Add a comment
Know the answer?
Add Answer to:
Java programming: I need to create a method that is given a 2D char array and...
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
  • 3. Write a method buildString which takes a 2D array of characters (char), and returns a...

    3. Write a method buildString which takes a 2D array of characters (char), and returns a string that is built from all the characters in that array. For example, the following method call should return "hello!" buildString( new char[] [ 'h', 'e',

  • I need little help with C language. I need to pass what I get from HexToBin(char*...

    I need little help with C language. I need to pass what I get from HexToBin(char* hexdec) into char* input so that what I got there it should pass it as string array parametr. Example: Enter IEEE-Hex: 40200000 Equivalent Binary value is : 01000000001000000000000000000000 Decimal Number: 2.5 #include <stdio.h> void HexToBin(char* hexdec) {    long int i = 0;    while (hexdec[i]) {        switch (hexdec[i]) {        case '0':            printf("0000");            break;...

  • Write a java program: Create a method fillRandom() that accepts an array of int as input...

    Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...

  • Programming project in Java: You are allowed to use the following methods from the Java API:...

    Programming project in Java: You are allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character any method Create a class called HW2 that contains the following methods: 1. isAlphabeticalOrder takes a String as input and returns a boolean: The method returns true if all the letters of the input string are in alphabetical order, regardless of case. The method returns false otherwise. Do not use arrays to...

  • Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {...

    Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {            if(arr == null || arr.length == 0) {                return -1;            }            for (int i = 0; i < arr.length; i++) {                if(arr[i] == ch) {                    return i;                }            }        return -1;       ...

  • IN C++ Create a method called xChar that will replace a specified char in a String...

    IN C++ Create a method called xChar that will replace a specified char in a String with the 'X' character. Your function must use a pointer to search and replace the char. You are not allowed to use array notation to solve this problem. Your function should take as arguments in the CString to search through and a char variable that holds the char to be searched for and replaced. The function should also return the number of replacements made....

  • Write a function named insertBeforeKey that takes the parameters as follows list as an char array,...

    Write a function named insertBeforeKey that takes the parameters as follows list as an char array, capacity for the capacity of the array, numItems has the number of items in the array, key is an element of the array before which newVal is to inserted. Assume key is always present in the array. and newVal has the new value to be inserted into the array. If the array is at capacity then the function should return -1, otherwise return 0...

  • Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include...

    Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. //...

  • This is for Java. Create ONE method/function that will return an array containing the row and...

    This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...

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

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