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 and highsubscript. A call to this method prints only the designated portion of the array. Validate lowsubscript and highsubscript. if either is out of range, the overloaded printarray method should throw an invalidsubscriptexception; otherwise, printArray should return the number of elements printed.



Then modify main to exercise both verisons of printArray on arrays integerArray, doubleArray and characterArray. Test all capabilities of both versions of printArray.




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









share|improve this question
























  • 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

















up vote
0
down vote

favorite












I need to overload generic method printArray of GenericMethodtest so that




it takes two additional integer arguments, lowsubscript and highsubscript. A call to this method prints only the designated portion of the array. Validate lowsubscript and highsubscript. if either is out of range, the overloaded printarray method should throw an invalidsubscriptexception; otherwise, printArray should return the number of elements printed.



Then modify main to exercise both verisons of printArray on arrays integerArray, doubleArray and characterArray. Test all capabilities of both versions of printArray.




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









share|improve this question
























  • 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















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 and highsubscript. A call to this method prints only the designated portion of the array. Validate lowsubscript and highsubscript. if either is out of range, the overloaded printarray method should throw an invalidsubscriptexception; otherwise, printArray should return the number of elements printed.



Then modify main to exercise both verisons of printArray on arrays integerArray, doubleArray and characterArray. Test all capabilities of both versions of printArray.




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









share|improve this question















I need to overload generic method printArray of GenericMethodtest so that




it takes two additional integer arguments, lowsubscript and highsubscript. A call to this method prints only the designated portion of the array. Validate lowsubscript and highsubscript. if either is out of range, the overloaded printarray method should throw an invalidsubscriptexception; otherwise, printArray should return the number of elements printed.



Then modify main to exercise both verisons of printArray on arrays integerArray, doubleArray and characterArray. Test all capabilities of both versions of printArray.




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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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


















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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues