Instructions A Write compile and run a Java program that 1 u
Instructions:
A. Write, compile, and run a Java program that:
(1) uses a single line comment to document your source code with your name and section number. This should be the very first line of your program,
(2) uses a println statement to print your name and section number as the first line of your output,
(3) 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.
(4) Write a class Comparisons whose main method tests your largest method above.
(a) 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) and
(b) 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\'s automatic conversion of ints to Integers.
Solution
Hi, Please find my code.
Please let me know in case of any issue:
########### Compare3.java ##########
/*
* @author: Pravesh Kumar
* #section: 1
*/
public class Compare3 {
// this method compares three values and return largest value
public static Comparable largest(Comparable x, Comparable y, Comparable z){
if(x.compareTo(y) > 0){
// x > y && x > z
if(x.compareTo(z) > 0)
return x;
else // z >=x > y
return z;
}else{ // y >=x
//y >=x && y > Z
if(y.compareTo(z) > 0)
return y;
else // z >= y >==x
return z;
}
}
}
############ Comparisons.java ###############
import java.util.Scanner;
/*
* @author: Pravesh Kumar
* #section: 1
*/
public class Comparisons {
public static void main(String[] args) {
// scanner object to take user input
Scanner sc = new Scanner(System.in);
System.out.println(\"Name: Pravesh Kumar, Section: 1\");
System.out.println(\"Enter three string value: \");
String first = sc.next();
String second = sc.next();
String third = sc.next();
System.out.println(\"largest: \"+Compare3.largest(first, second, third));
System.out.println();
System.out.println(\"Enter three integer value: \");
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
System.out.println(\"largest: \"+Compare3.largest(x, y, z));
}
}
/*
Sample run:
Name: Pravesh Kumar, Section: 1
Enter three string value:
pravesh
mukesh
apple
largest: pravesh
Enter three integer value:
5
3
7
largest: 7
*/


