Question

In: Computer Science

for java, Behold, the power of interfaces! On this homework problem you'll implement a completely generic...

for java,

Behold, the power of interfaces! On this homework problem you'll implement a completely generic version of an algorithm to find the maximum of an array. Unlike in the past, when our algorithm only worked for int[] or double[], this version will work on any Java objects that are comparable, specifically any Java object that implements the Comparable interface. Create a public class named Max with a single class method named max. max should accept an array of Objects that implement Comparable and return the maximum. If the array is null or empty, you should return null. You will probably need to review the documentation for Comparable.

Solutions

Expert Solution

Hi. I have answered a very similar question before. Here is the completed code for this problem, including a class for testing. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks


//Max.java

public class Max {
        // required method
        public static <E extends Comparable<E>> E max(E[] objects) {
                // if array is null or is empty, returning null
                if (objects == null || objects.length == 0) {
                        return null;
                }
                // otherwise setting first element as maxVal
                E maxVal = objects[0];
                // looping through remaining elements
                for (int i = 1; i < objects.length; i++) {
                        // if current element is greater than maxVal, setting current
                        // element as new maxval
                        if (objects[i].compareTo(maxVal) > 0) {
                                maxVal = objects[i];
                        }
                }
                // returning maxVal
                return maxVal;
        }

}

//Test.java

import java.util.Arrays;

public class Test {
        public static void main(String[] args) {
                // creating arrays of different Comparable types
                String[] names = { "John", "Alice", "Damien", "Bran" };
                Integer[] integers = { 1, 0, 3, -2, 8, 5 };
                Double[] doubles = { 3.0, 5.9, 2.9, 1.5, 6.6, 4.3, 1.1 };
                Character[] letters = { 'A', 'Z', 'D', 'X' };

                // displaying arrays and max value of each of them
                System.out.println("Names: " + Arrays.toString(names));
                System.out.println("Max value: " + Max.max(names));

                System.out.println("Integers: " + Arrays.toString(integers));
                System.out.println("Max value: " + Max.max(integers));

                System.out.println("Doubles: " + Arrays.toString(doubles));
                System.out.println("Max value: " + Max.max(doubles));

                System.out.println("Letters: " + Arrays.toString(letters));
                System.out.println("Max value: " + Max.max(letters));

        }
}

/*OUTPUT*/

Names: [John, Alice, Damien, Bran]
Max value: John
Integers: [1, 0, 3, -2, 8, 5]
Max value: 8
Doubles: [3.0, 5.9, 2.9, 1.5, 6.6, 4.3, 1.1]
Max value: 6.6
Letters: [A, Z, D, X]
Max value: Z

Related Solutions

Behold, the power of interfaces! On this homework problem you'll implement a completely generic version of...
Behold, the power of interfaces! On this homework problem you'll implement a completely generic version of an algorithm to find the maximum of an array. Unlike in the past, when our algorithm only worked for int[] or double[], this version will work on any Java objects that are comparable, specifically any Java object that implements the Comparable interface. Create a public class named Max with a single class method named max. max should accept an array of Objects that implement...
You'll implement a completely generic version of an algorithm to find the maximum of an array....
You'll implement a completely generic version of an algorithm to find the maximum of an array. Unlike in the past, when our algorithm only worked for int[] or double[], this version will work on any Java objects that are comparable, specifically any Java object that implements the Comparable interface. Create a public class named Max with a single class method named max. max should accept an array of Objects that implement Comparable and return the maximum. If the array is...
Java-- For this homework you are required to implement a change calculator that will provide the...
Java-- For this homework you are required to implement a change calculator that will provide the change in the least amount of bills/coins. The application needs to start by asking the user about the purchase amount and the paid amount and then display the change as follows: Supposing the purchase amount is $4.34 and the paid amount is $20, the change should be: 1 x $10 bill 1 x $5 bill 2 x Quarter coin 1 x Dime coin 1...
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO)....
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO). Use the driver to then test each of the methods. Simply download it and place it with the other source files. Create a class GenLLQueue which has the following: Internal class ListNode which contains: Instance variable data of type T Instance variable link of type ListNode Default constructor that sets both instance variables to null Instance Variables head which is of type ListNode which...
In java: 4. Using non-generic techniques, implement a static method getMax() that takes an array of...
In java: 4. Using non-generic techniques, implement a static method getMax() that takes an array of type Comparable and returns the maximum element in the array. (i.e. "public static Comparable getMax(Comparable [] anArray)"). 5. Using the generic techniques to specify super-class relationships, implement a type safe version of the method in 4 named getMaxGen().
Please write a Java algorithm solving the following problem: Implement a Java method to check if...
Please write a Java algorithm solving the following problem: Implement a Java method to check if a binary tree is balanced. For this assignment, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one. 1. First, please create the following two classes supporting the Binary Tree Node and the Binary Tree: public class BinTreeNode<T> { private T key; private Object satelliteData; private BinTreeNode<T> parent;...
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
You'll notice that in the LearnAboutLanguages class there are a few todos. You need to implement...
You'll notice that in the LearnAboutLanguages class there are a few todos. You need to implement the three missing classes ( RussianSpeaker, JapaneseSpeaker and SwahiliSpeaker ). Then uncomment all the stuff marked "todo" and run the code! Submit 1. Your three new .java files 2. A screenshot of your correct code running on your computer. -- public class EnglishSpeaker implements LanguageSpeaker{    @Override    public String GetMyLanguage() {        return "English";    }    @Override    public String GetHi()...
Similar to in HW1, you'll implement a left shift of decimals in a string, with combining...
Similar to in HW1, you'll implement a left shift of decimals in a string, with combining of multiple like digits into a single digit of double the value. The main difference between HW1 and this is that now the string representing a row in 2048 can be of any length! You'll implement a Combine class in a comparable .java file. As before, the input string can only include the characters '_', '2', and '4'. Unlike before, it can be of...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT