Question

In: Computer Science

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

Explanation:I have implemented the Max class with the max method,Customer class and Demo class,I have found the customer with the maximum id using the generic max() method that accepts a generic array that implements the Comparable object and it can return the maximum element of all the objects that are comparable.I have shown the output of the program, please find the images attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation.

//Max class

public class Max {

   public <E extends Comparable<E>> E max(E[] arrayOfObjects) {

       if (arrayOfObjects == null || arrayOfObjects.length == 0)
           return null;
       E maxElement = arrayOfObjects[0];
       for (E arrayElement : arrayOfObjects) {
           if (arrayElement.compareTo(maxElement) > 0) {
               maxElement = arrayElement;
           }
       }
       return maxElement;
   }

}

//Customer class


public class Customer implements Comparable<Customer> {
   Integer idCustomer;
   String name;
   String gender;
   String email;

   public Customer() {

   }

   public Customer(Integer idCustomer, String name, String gender,
           String email) {
       this.idCustomer = idCustomer;
       this.name = name;
       this.gender = gender;
       this.email = email;
   }

   @Override
   public String toString() {
       return "Customer [idCustomer=" + idCustomer + ", name=" + name
               + ", gender=" + gender + ", email=" + email + "]";
   }

   @Override
   public int compareTo(Customer o) {
       return idCustomer.compareTo(o.idCustomer);
   }

}

//Demo class


public class Demo {

   public static void main(String[] args) {
       Max maxObj = new Max();
       Customer customer1 = new Customer(1111, "Mr.Roy", "male", "abdc.com");
       Customer customer2 = new Customer(121, "Mr.Root", "male", "adfbc.com");
       Customer customer3 = new Customer(1451, "Mrs.Lily", "female",
               "sgh.com");
       Customer customer4 = new Customer(12511, "Mr.Henry", "male", "gdd.com");
       Customer customers[] = {customer1, customer2, customer3, customer4};
       Customer maxIdCustomer = maxObj.max(customers);
       System.out
               .println("The details of the customer having maximimum id is:");
       System.out.println(maxIdCustomer.toString());

   }

}

Output:


Related Solutions

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...
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...
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...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are 8 boxes, of which 1 box contains the prize and the other 7 boxes are empty. You select one box at first. Monty, who knows where the prize is, then opens 6 of the remaining 7 boxes, all of which are shown to be empty. If Monty has a choice of which boxes to open (i.e. if the prize is in the box you...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are 8 boxes, of which 1 box contains the prize and the other 7 boxes are empty. You select one box at first. Monty, who knows where the prize is, then opens 6 of the remaining 7 boxes, all of which are shown to be empty. If Monty has a choice of which boxes to open (i.e. if the prize is in the box you...
TrackMinMax i want the generic version based on java For this lab, you will create a...
TrackMinMax i want the generic version based on java For this lab, you will create a generic version of the IntTrackMinMax class you wrote in a previous lab, called TrackMinMax. The API is: Function Signature Description constructor TrackMinMax() constructor check void check(T i) compares i to the current minimum and maximum values and updates them accordingly getMin T getMin() returns the minimum value provided to check() so far getMax T getMax() returns the maximum value provided to check() so far...
(Implementing Interfaces) Implement the following interface and classes: a. Interface IsCompetition, which has no constants and...
(Implementing Interfaces) Implement the following interface and classes: a. Interface IsCompetition, which has no constants and two methods (see the classes below for details about what these methods should do): i. getSummary(), ii. getCompetitors() b. Class Game: A game object is associated with a home team, away team, home score, away score, and a date of competition. It should only be created using the following constructor: Game(String HTeam, String ATeam, int HScore, int AScore, LocalDate date). A game is also...
For this problem, you'll be writing a script that reads the content of a file, modifies...
For this problem, you'll be writing a script that reads the content of a file, modifies that content, and then writes the result to a new file. a. Define a function called modified() that takes a string and returns a "modified" version of the string. The type of modification is up to you (be creative!), but I have a couple requirements: You must make at least two different modifications, and at least one of those modifications must be some kind...
you will create a program with Java to implement a simplified version of RSA cryptosystems. To...
you will create a program with Java to implement a simplified version of RSA cryptosystems. To complete this project, you may follow the steps listed below (demonstrated in Java code) to guide yourself through the difficulties. Step I Key-gen: distinguish a prime number (20 pts) The generation of RSA's public/private keys depends on finding two large prime numbers, thus our program should be able to tell if a given number is a prime number or not. For simplicity, we define...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT