H. G.
asked on
Java help
Ok, I need to write a program in a class named "whatever" and ignore the main method for a moment.
In this class, I need to create a static method with one parameter of type int[ ] and Inside the method
a. Keep all output on one line I guess using System.out.print( )
b. Display an opening square bracket character.
c. Loop through the array that was passed into the method, display the values in the array, add a comma
and a space after every value except the last one.
d. then display a closing square bracket character...I guess using System.out.println().
Then, in my main method I've got to create an array and pass the reference to this array to the "printArray" method.
int[ ] oneD = {5, 6, 7, 8};
Then in my main method, I need to add a blank println() statement after the method call just made.
and in the class, create an overloaded static method with one parameter of type int[ ] [] named "arr" and inside:
a. Use println() to display an opening square bracket character.
b. Loop through the two-dimensional array that was passed into the method.
i. First, use System.out.print() to display two space characters.
ii. Every element of this two-dimensional array that you are looping through is a one-dimensional
array of int. Call the other printArray method and pass to it each one-dimensional array in the
two-dimensional array.
c. Using println(), display a closing square bracket character.
Then in the main method, I need to create the following two-dimensional array and pass the reference to this array to the printArray method.
int[ ][ ] twoD = {{2, 4, 6, 8}, {8, 7, 9, 1},
{3, 5, 1, 2}};
In the main method, add a blank println() statement after the method call you just made.
In the main method, create the following ragged two-dimensional array. Pass the reference to this array to the
printArray method.
int[ ][ ] twoD2 = {{1, 2}, {3, 4, 5}, {6}, {7, 8, 9}};
I think the results (output) should look identical to this
/* Results:
[5, 6, 7, 8]
[
[2, 4, 6, 8]
[8, 7, 9, 1]
[3, 5, 1, 2]
]
[
[1, 2]
[3, 4, 5]
[6]
[7, 8, 9]
]
*/
Any help would be appreciated. Thanks!
In this class, I need to create a static method with one parameter of type int[ ] and Inside the method
a. Keep all output on one line I guess using System.out.print( )
b. Display an opening square bracket character.
c. Loop through the array that was passed into the method, display the values in the array, add a comma
and a space after every value except the last one.
d. then display a closing square bracket character...I guess using System.out.println().
Then, in my main method I've got to create an array and pass the reference to this array to the "printArray" method.
int[ ] oneD = {5, 6, 7, 8};
Then in my main method, I need to add a blank println() statement after the method call just made.
and in the class, create an overloaded static method with one parameter of type int[ ] [] named "arr" and inside:
a. Use println() to display an opening square bracket character.
b. Loop through the two-dimensional array that was passed into the method.
i. First, use System.out.print() to display two space characters.
ii. Every element of this two-dimensional array that you are looping through is a one-dimensional
array of int. Call the other printArray method and pass to it each one-dimensional array in the
two-dimensional array.
c. Using println(), display a closing square bracket character.
Then in the main method, I need to create the following two-dimensional array and pass the reference to this array to the printArray method.
int[ ][ ] twoD = {{2, 4, 6, 8}, {8, 7, 9, 1},
{3, 5, 1, 2}};
In the main method, add a blank println() statement after the method call you just made.
In the main method, create the following ragged two-dimensional array. Pass the reference to this array to the
printArray method.
int[ ][ ] twoD2 = {{1, 2}, {3, 4, 5}, {6}, {7, 8, 9}};
I think the results (output) should look identical to this
/* Results:
[5, 6, 7, 8]
[
[2, 4, 6, 8]
[8, 7, 9, 1]
[3, 5, 1, 2]
]
[
[1, 2]
[3, 4, 5]
[6]
[7, 8, 9]
]
*/
Any help would be appreciated. Thanks!
What is your question?
I agree with CEHJ.
The code you posted is almost there. I didn't have to change much at all to get the output you want to print.
The code you posted is almost there. I didn't have to change much at all to get the output you want to print.
ASKER
I'm not getting the desired output. What am I missing? I've been racking my brain with this
All I did was change line 25 from println to print and add a space character after the comma on line 34, and add a } to the end.
ASKER
so rrz, line 25
Is that what you suggest?
System.out.println("[");
should say
System.out.print("[");
and 34
System.out.print(arr[i] + ",");
should be
System.out.print(arr[i] + ", ");
and 54 should be
}}
Is that what you suggest?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome! Thanks.
Not DO school work, HELP.
Thanks again.
Not DO school work, HELP.
Thanks again.
ASKER
Open in new window