Need help/stuck on overload generic method
up vote
0
down vote
favorite
I need to overload generic method printArray of GenericMethodtest so that
it takes two additional integer arguments,
lowsubscript
andhighsubscript
. A call to this method prints only the designated portion of the array. Validatelowsubscript
andhighsubscript
. if either is out of range, the overloadedprintarray
method should throw aninvalidsubscriptexception
; otherwise,printArray
should return the number of elements printed.
Then modify main to exercise both verisons of
printArray
on arraysintegerArray
,doubleArray
andcharacterArray
. Test all capabilities of both versions ofprintArray
.
This is what I have so far i'm stuck and don't know where to begin.
public class GenericMethodTest
{
public static void main(String args)
{
// create arrays of Integer, Double and Character
Integer integerArray = {1, 2, 3, 4, 5, 6};
Double doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
Character characterArray = {'H', 'E', 'L', 'L', 'O'}
System.out.printf("%nArray integerArray contains:%n");
printArray(integerArray); // pass an Integer array
System.out.printf("%nArray doubleArray contains:%n");
printArray(doubleArray); // pass a Double array
System.out.printf("%nArray characterArray contains:%n");
printArray(characterArray); // pass a Character array
}
// generic method printArray
public static <T> void printArray(T inputArray)
{
// display array elements
for (T element : inputArray)
System.out.printf("%s ", element);
System.out.println();
}
} // end class GenericMethodTest
java
add a comment |
up vote
0
down vote
favorite
I need to overload generic method printArray of GenericMethodtest so that
it takes two additional integer arguments,
lowsubscript
andhighsubscript
. A call to this method prints only the designated portion of the array. Validatelowsubscript
andhighsubscript
. if either is out of range, the overloadedprintarray
method should throw aninvalidsubscriptexception
; otherwise,printArray
should return the number of elements printed.
Then modify main to exercise both verisons of
printArray
on arraysintegerArray
,doubleArray
andcharacterArray
. Test all capabilities of both versions ofprintArray
.
This is what I have so far i'm stuck and don't know where to begin.
public class GenericMethodTest
{
public static void main(String args)
{
// create arrays of Integer, Double and Character
Integer integerArray = {1, 2, 3, 4, 5, 6};
Double doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
Character characterArray = {'H', 'E', 'L', 'L', 'O'}
System.out.printf("%nArray integerArray contains:%n");
printArray(integerArray); // pass an Integer array
System.out.printf("%nArray doubleArray contains:%n");
printArray(doubleArray); // pass a Double array
System.out.printf("%nArray characterArray contains:%n");
printArray(characterArray); // pass a Character array
}
// generic method printArray
public static <T> void printArray(T inputArray)
{
// display array elements
for (T element : inputArray)
System.out.printf("%s ", element);
System.out.println();
}
} // end class GenericMethodTest
java
Start by defining a newprintArray
method that takes additional parameters. At this point, all you have done is an assignment dump without any effort. Please see What can I ask about here, point 3.
– KevinO
Nov 10 at 20:32
what is your problem? how to return count?
– Deadpool
Nov 10 at 20:34
There is no need to use generics here, because arrays are covariant. Just declareObject inputArray
. Or, to put this another way, the fact the method is generic is entirely irrelevant to the question.
– Andy Turner
Nov 10 at 20:42
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to overload generic method printArray of GenericMethodtest so that
it takes two additional integer arguments,
lowsubscript
andhighsubscript
. A call to this method prints only the designated portion of the array. Validatelowsubscript
andhighsubscript
. if either is out of range, the overloadedprintarray
method should throw aninvalidsubscriptexception
; otherwise,printArray
should return the number of elements printed.
Then modify main to exercise both verisons of
printArray
on arraysintegerArray
,doubleArray
andcharacterArray
. Test all capabilities of both versions ofprintArray
.
This is what I have so far i'm stuck and don't know where to begin.
public class GenericMethodTest
{
public static void main(String args)
{
// create arrays of Integer, Double and Character
Integer integerArray = {1, 2, 3, 4, 5, 6};
Double doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
Character characterArray = {'H', 'E', 'L', 'L', 'O'}
System.out.printf("%nArray integerArray contains:%n");
printArray(integerArray); // pass an Integer array
System.out.printf("%nArray doubleArray contains:%n");
printArray(doubleArray); // pass a Double array
System.out.printf("%nArray characterArray contains:%n");
printArray(characterArray); // pass a Character array
}
// generic method printArray
public static <T> void printArray(T inputArray)
{
// display array elements
for (T element : inputArray)
System.out.printf("%s ", element);
System.out.println();
}
} // end class GenericMethodTest
java
I need to overload generic method printArray of GenericMethodtest so that
it takes two additional integer arguments,
lowsubscript
andhighsubscript
. A call to this method prints only the designated portion of the array. Validatelowsubscript
andhighsubscript
. if either is out of range, the overloadedprintarray
method should throw aninvalidsubscriptexception
; otherwise,printArray
should return the number of elements printed.
Then modify main to exercise both verisons of
printArray
on arraysintegerArray
,doubleArray
andcharacterArray
. Test all capabilities of both versions ofprintArray
.
This is what I have so far i'm stuck and don't know where to begin.
public class GenericMethodTest
{
public static void main(String args)
{
// create arrays of Integer, Double and Character
Integer integerArray = {1, 2, 3, 4, 5, 6};
Double doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
Character characterArray = {'H', 'E', 'L', 'L', 'O'}
System.out.printf("%nArray integerArray contains:%n");
printArray(integerArray); // pass an Integer array
System.out.printf("%nArray doubleArray contains:%n");
printArray(doubleArray); // pass a Double array
System.out.printf("%nArray characterArray contains:%n");
printArray(characterArray); // pass a Character array
}
// generic method printArray
public static <T> void printArray(T inputArray)
{
// display array elements
for (T element : inputArray)
System.out.printf("%s ", element);
System.out.println();
}
} // end class GenericMethodTest
java
java
edited Nov 10 at 20:44
Andy Turner
78.4k878129
78.4k878129
asked Nov 10 at 20:15
vic
41
41
Start by defining a newprintArray
method that takes additional parameters. At this point, all you have done is an assignment dump without any effort. Please see What can I ask about here, point 3.
– KevinO
Nov 10 at 20:32
what is your problem? how to return count?
– Deadpool
Nov 10 at 20:34
There is no need to use generics here, because arrays are covariant. Just declareObject inputArray
. Or, to put this another way, the fact the method is generic is entirely irrelevant to the question.
– Andy Turner
Nov 10 at 20:42
add a comment |
Start by defining a newprintArray
method that takes additional parameters. At this point, all you have done is an assignment dump without any effort. Please see What can I ask about here, point 3.
– KevinO
Nov 10 at 20:32
what is your problem? how to return count?
– Deadpool
Nov 10 at 20:34
There is no need to use generics here, because arrays are covariant. Just declareObject inputArray
. Or, to put this another way, the fact the method is generic is entirely irrelevant to the question.
– Andy Turner
Nov 10 at 20:42
Start by defining a new
printArray
method that takes additional parameters. At this point, all you have done is an assignment dump without any effort. Please see What can I ask about here, point 3.– KevinO
Nov 10 at 20:32
Start by defining a new
printArray
method that takes additional parameters. At this point, all you have done is an assignment dump without any effort. Please see What can I ask about here, point 3.– KevinO
Nov 10 at 20:32
what is your problem? how to return count?
– Deadpool
Nov 10 at 20:34
what is your problem? how to return count?
– Deadpool
Nov 10 at 20:34
There is no need to use generics here, because arrays are covariant. Just declare
Object inputArray
. Or, to put this another way, the fact the method is generic is entirely irrelevant to the question.– Andy Turner
Nov 10 at 20:42
There is no need to use generics here, because arrays are covariant. Just declare
Object inputArray
. Or, to put this another way, the fact the method is generic is entirely irrelevant to the question.– Andy Turner
Nov 10 at 20:42
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243026%2fneed-help-stuck-on-overload-generic-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Start by defining a new
printArray
method that takes additional parameters. At this point, all you have done is an assignment dump without any effort. Please see What can I ask about here, point 3.– KevinO
Nov 10 at 20:32
what is your problem? how to return count?
– Deadpool
Nov 10 at 20:34
There is no need to use generics here, because arrays are covariant. Just declare
Object inputArray
. Or, to put this another way, the fact the method is generic is entirely irrelevant to the question.– Andy Turner
Nov 10 at 20:42