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

  • Assignment is designed to develop your ability to create static methods and manipulate 1D and 2D...

    Assignment is designed to develop your ability to create static methods and manipulate 1D and 2D arrays in Java. Create a single Java class Matrix and inside the class create the specified static methods as described Task # Description 1 Create a matrix (known components) 2 Create a matrix (random components) 3 Create a matrix from vectors 4 Compare two matrices 5 Add two matrices 6 Subtract two matrices 7 Multiply a matrix by a scalar 8 Multiply two matrices...

  • [JAVA] I need help creating a method to calculate the Standard Deviation of a 2D Array....

    [JAVA] I need help creating a method to calculate the Standard Deviation of a 2D Array. This is what I have so far: public double calcStdDev() {    int total = 0;    for(int i = 0; i < arr.length; i++){        total += arr[i][i];    }    double mean = total / arr.length;    return mean; } The numbers I'm using at just 1,2,3,4,5,6 and the standard deviation is coming out wrong. Can I have help fixing my...

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

  • Create a LIFO class with following methods in java - public LifoList() The default constructor for...

    Create a LIFO class with following methods in java - public LifoList() The default constructor for LifoList class which will initialize your class variables maxSize to 0 and the int array to null. public LifoList(int maxSize) The constructor for LifoList class which takes the int input maxSize to initialize the size of the array. You should NOT reinitialize the array elsewhere. For example, creating an object as new LifoList(5) should create a LifoList whose maximum size is 5. If the...

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

  • I need help programming this question using C language. This program uses input data entered in...

    I need help programming this question using C language. This program uses input data entered in command line at the same time when the command or .exe file is entered. They are called command-line arguments. Because everything entered in command line is taken as a string, these arguments including the command itself or the .exe file name are stored in an array of char pointers, each pointer points to the first character of a string (i.e., argument). The inputs can...

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