Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need help with this java assignment program a classCompare3that provides a static method calledlargest. Methodlargestshould take threeComparableparameters and return the largest of the three
I need help with this java assignment
- program a classCompare3that provides a static method calledlargest. Methodlargestshould take threeComparableparameters and return the largest of the three (so its return type will also beComparable). Recall that methodcompareTois part of theComparableinterface, solargestcan use thecompareTomethod of its parameters to compare them.
- The method header for largest will look like this:public static Comparable largest(Comparable first, Comparable second, Comparable third)
You will get a compiler note as shown below. This is expected and acceptable at this time. The correct way to code this uses generics which are introduced in chapter 12.
Note: Compare3.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
- program a classComparisonswhose main method tests yourlargestmethod 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 tolargestdirectly in the call toprintln.) Note that sincelargestis 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 ofints toIntegers
- You will have 2 class files to submit
- You will get a compiler note as shown below. This is expected and acceptable at this time. The correct way to code this uses generics which are introduced in chapter 12
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started