Question

In: Computer Science

Using the Comparable Interface Write a class Compare3 that provides a static method largest. Method largest...

  1. Using the Comparable Interface
  1. Write a class Compare3 that provides a static method largest. Method largest should take three Comparable parameters and return the largest of the three (so its return type will also be Comparable). Recall that method compareTo is part of the Comparable interface, so largest can use the compareTo method of its parameters to compare them.

  1. Write a class Comparisons whose main method tests your largest method above.

  • First prompt the user for and read in three strings, use your largest method to find the largest of the three strings, and print it out. (It’s easiest to put the call to largest directly in the call to println.) Note that since largest is a static method, you will call it through its class name, e.g., Compare3.largest(val1, val2, val3).

  • Add code to also prompt the user for three integers and try to use your largest method to find the largest of the three integers. Does this work? If it does, it’s thanks to autoboxing, which is Java 1.5’s automatic conversion of ints to Integers. You may have to use the -source 1.5 compiler option for this to work.

Solutions

Expert Solution

import java.util.Scanner;

class Comparisons {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter first string: ");
        String s1 = in.nextLine();
        System.out.print("Enter second string: ");
        String s2 = in.nextLine();
        System.out.print("Enter third string: ");
        String s3 = in.nextLine();
        System.out.println("Largest string is " + Compare3.largest(s1, s2, s3));
        System.out.print("Enter first integer: ");
        Integer n1 = in.nextInt();
        System.out.print("Enter second integer: ");
        Integer n2 = in.nextInt();
        System.out.print("Enter third integer: ");
        Integer n3 = in.nextInt();
        System.out.println("Largest integer is " + Compare3.largest(n1, n2, n3));
    }
}

class Compare3 {

    public static <T extends Comparable<T>> T largest(T t1, T t2, T t3) {
        T largest = t1;
        if(t2.compareTo(largest) > 0) {
            largest = t2;
        }
        if(t3.compareTo(largest) > 0) {
            largest = t3;
        }
        return largest;
    }
}


Related Solutions

1. Write a class Compare3 that provides a static method largest. Method largest should take three...
1. Write a class Compare3 that provides a static method largest. Method largest should take three Comparable parametersand return the largest of the three (so its return type will also be Comparable). Recall that method compareTo is part ofthe Comparable interface, so largest can use the compareTo method of its parameters to compare them. 2. Write a class Comparisons whose main method tests your largest method above. •   First prompt the user for and read in three strings, use your...
Make the Measurable interface from Chapter 10 into a generic class. Provide a static method that...
Make the Measurable interface from Chapter 10 into a generic class. Provide a static method that returns the largest element of an ArrayList, provided that the elements are instances of Measurable. Be sure to return a value of type T. Measurable interface: public interface Measurable { double getMeasure(); } Using JAVA please
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
In this class add Comparable interface. In the driver program create a few objects and In...
In this class add Comparable interface. In the driver program create a few objects and In the driver program create a few objects and compare them . then create a list of those objects and sort them .A Quadratic is bigger than another Quadratic if it opens faster package pack2; /** * This is a program for defining a quadratic equation * @author sonik */ public class Quadratic { public int coeffX2 = 0; public int coeffX = 0; public...
For these exercises you are required to edit in the Comparable interface implementation, a compareTo() method,...
For these exercises you are required to edit in the Comparable interface implementation, a compareTo() method, starting with the class definition you obtained from the link, and submit that with a client that uses this ordering. If you are so inclined, you may submit a single client to drive your ordering enhancements for both classes, rather than one for each class. The exercises, for those without the text: 19. Modify the Point class from Chapter 8 so that it defines...
For these exercises you are required to edit in the Comparable interface implementation, a compareTo() method,...
For these exercises you are required to edit in the Comparable interface implementation, a compareTo() method, starting with the class definition you obtained from the link, and submit that with a client that uses this ordering. If you are so inclined, you may submit a single client to drive your ordering enhancements for both classes, rather than one for each class. The exercises, for those without the text: 19. Modify the Point class from Chapter 8 so that it defines...
Java Programming Using the class below, please ), write a static method called parse that parses...
Java Programming Using the class below, please ), write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1 parse(“{ { 4*X}”)...
in Java language, in most simple algorithm Using a stack class, write a static method called...
in Java language, in most simple algorithm Using a stack class, write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1...
Make a public class Creater that provides a single class (static) method named subtractor. subtractor takes...
Make a public class Creater that provides a single class (static) method named subtractor. subtractor takes a single int argument and returns a method that implements the following Create interface: public interface Create { int change(int something); --------------------------------------------------------------------- The returned “function” should implement Create so that it subtracts the value passed to subtractor. For example Create one = Creater.substractor(5); Create two = Creater.subtractor(-3); System.out.println(one.change(5)); // should print 0 System.out.println(second.change(3); // should print -6 The answer should be is a single...
data structure class: a. syntax for generics b. comparable interface c.how do you implement interface answer...
data structure class: a. syntax for generics b. comparable interface c.how do you implement interface answer for all of them please. answer for all of them please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT