Question

In: Computer Science

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 largest method to find the largest of the three strings, andprint it out. (It’s easiest to put the call to largest directly in the call to println.) Note that since largest is a staticmethod, 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 threeintegers. Does this work? If it does, it’s thanks to autoboxing, which is Java 1.5’s automatic conversion of ints toIntegers. You may have to use the -source 1.5 compiler option for this to work.

Solutions

Expert Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

import java.util.Scanner;

public class Comparisons {

  

   public static void main(String[] args) {

      

       Scanner sc = new Scanner(System.in);

      

       System.out.print("Enter three integes: ");

       int val1 = sc.nextInt();

       int val2 = sc.nextInt();

       int val3 = sc.nextInt();

      

       System.out.println("Largest: "+Compare3.largest(val1, val2, val3));

   }

}

class Compare3{

  

   public static Comparable largest( Comparable val1, Comparable val2, Comparable val3){

      

       // getting largest between val1 and val2

       Comparable largest = val1.compareTo(val2) > 0 ? val1 : val2;

      

       // getting largest between val1, val2, val3

       largest = largest.compareTo(val3) > 0 ? largest : val3;

      

       return largest;

   }

}

/*

Sample run:

Enter three integes: 89 23 90

Largest: 90

*/


Related Solutions

Name the project pa3 [Method 1] In the Main class, write a static void method to...
Name the project pa3 [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. [Method 2]...
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
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...
for java!! Make a public class Value that provides a static method named test. test takes...
for java!! Make a public class Value that provides a static method named test. test takes a single int argument and returns an anonymous object that implements the following Absolute interface: public interface Absolute { int same(); int opposite(); } -------------------------------------------------------------------------- The returned object should implement same so that it returns the passed int as it is, and opposite so that it returns the passed int as the int * -1. For example Absolute first = Value.test(-90) Absolute second =...
Write a class that has three overloaded static methods for calculating the areas of the following...
Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: - circles - rectangles - cylinders Here are the formulas for calculating the area of the shapes. Area of a circle: Area = π r2, where p is Math.PI and r is the circle's radius Area of a rectangle: Area = Width x Length Area of a cylinder: Area = π r2 h, where p is Math.PI, r is the radius of...
Write a class that has three overloaded static methods for calculating the areas of the following...
Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: - circles - rectangles - cylinders Here are the formulas for calculating the area of the shapes. Area of a circle: Area = π r2, where p is Math.PI and r is the circle's radius Area of a rectangle: Area = Width x Length Area of a cylinder: Area = π r2 h, where p is Math.PI, r is the radius of...
The abstract class SayHello and the static method process are defined as followed: class SayHello {...
The abstract class SayHello and the static method process are defined as followed: class SayHello {             private String greeting; SayHello(   )             {                         greeting = “Hello guys”;             } SayHello( String wts )             {                         greeting = wts;             }             void printGreeting( ); } static void process( SayHello obj ) {             obj.printGreeting(   ); } Write the statements to instantiate an object (with its instance variable initialized with the string “Bonjour mes amis” ) of the anonymous class that extends this abstract class by using the...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static)...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static) method named firstOne. firstOne accepts a String array and returns a map from Strings to Integer. The map must count the number of passed Strings based on the FIRST letter. For example, with the String array {“banana”, “apples”, “blueberry”, “orange”}, the map should return {“b”:2, “a”:1, “o”:1}. Disregard empty Strings and zero counts. Retrieve first character of String as a char using charAt, or,...
(In Java) Write three static functions that each take two double values which represent the two...
(In Java) Write three static functions that each take two double values which represent the two smaller sides of a right triangle. The first will calculate and return the area of the triangle, the second will calculate and return the length of the hypotenuse and the third will calculate and return the perimeter of the triangle. (Note: to calculate the perimeter, you need the value of the hypotenuse, so you must be calling that method from inside the other method.)...
USING JAVA: complete these one method in the BasicBioinformatics class /** * Class BasicBioinformatics contains static...
USING JAVA: complete these one method in the BasicBioinformatics class /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT