Trying to print greater value that will show up 6.6, 6 and F This is the code I
ID: 3788840 • Letter: T
Question
Trying to print greater value that will show up 6.6, 6 and F This is the code I have so far. I don't know what wrong please help
public class GenericMethod
{
static Integer[] integerArray = {1,2,3,4,5,6};
static Double[] doubleArray = {1.1,2.2,3.3,4.4,5.5,6.6};
static Character[] charArray = {'A','B','C','D','E','F'};
public static void main(String[] args)
{
printArray(integerArray);
printArray(doubleArray);
printArray(charArray);
}
public static <T> void printArray (T[] in)
{
for (T i : in)
{
System.out.println(i);
}
System.out.println();
public class Entry <T> extends Comparable<T>
{
T value;
public Entry(T value)
{
this.value = value;
}
public int compareTo ()
{
return (value).compareTo (i);
}
public void display()
{
System.out.print(value.toString());
}
}
}
}
Explanation / Answer
Hi,
I have modified the code and highlighted the code changes below.
GenericMethod.java
public class GenericMethod
{
static Integer[] integerArray = {1,2,3,4,5,6};
static Double[] doubleArray = {1.1,2.2,3.3,4.4,5.5,6.6};
static Character[] charArray = {'A','B','C','D','E','F'};
public static void main(String[] args)
{
printArray(integerArray);
printArray(doubleArray);
printArray(charArray);
}
public static <T> void printArray (T[] in)
{
T max = in[0];
for (T i : in)
{
System.out.println(i);
if (max.toString().compareTo(i.toString())<0){
max = i;
}
}
System.out.println();
System.out.println("Max value is "+max);
System.out.println();
}
}
Entry.java
public class Entry <T> implements Comparable<T>
{
T value;
public Entry(T value)
{
this.value = value;
}
public void display()
{
System.out.print(value.toString());
}
public int compareTo(T o) {
return (value.toString()).compareTo (o.toString());
}
}
Output:
1
2
3
4
5
6
Max value is 6
1.1
2.2
3.3
4.4
5.5
6.6
Max value is 6.6
A
B
C
D
E
F
Max value is F
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.