supply code for each one to produce the indicated results. 1. Method hypotenuse(
ID: 3878811 • Letter: S
Question
supply code for each one to produce the indicated results. 1. Method hypotenuse(double a, double b) Recall from your studies in mathematics that a right triangle has two perpendicular sides connected to a third side, typically referred to as the hypotenuse. The Pythagorean Theorem states that the relationship between the first two sides, a and b, and the hypotenuse, c, is a2 + b2= c2. This hypotenuse method accepts values for the right sides of a triangle, a and b, as arguments. Square these two values, add them, and then return the square root of the sum using theMath.pow() and Math.sqrt() methods. Do not use multiplication, and do not use the Math.hypot() method. 2. Methods max(double a, double b) and min(double a, double b) These methods accept two double values and return the largest or smallest of the two. No special Math or String methods are needed. 3. Methods max(String a, String b) and min(String a, String b) These methods accept two String values and return the lexicographically bigger or smaller of the two. You will need a String method here. 4. Method isSameText(String a, String b) This method compares strings a and b to see if they contain the same text. A String method may be needed here. 5. Method getLongestString(String a, String b, String c) This method compares the number of characters in each string rather than their lexicographic qualities. How do you determine how many characters there are in a string? Since there are three strings to compare, this method is slightly more challenging. 6. Method getShortestString(List list) The list may contain zero or more strings. Your task is to find the string in the list with the fewest characters. If the incoming list is null or contains zero strings, return null from this method. Otherwise, you will need to loop through the list to find the smallest string. This task will require the use of methods from both the List interface and the String class. 7. Methods getStringBeforeComma(String s) and getStringAfterComma(String s) These methods expect a string that has a comma somewhere inside of it. Your task will be to correctly provide the text leading up to the comma, as well as the text after the comma, but not the comma itself. This will require at least two methods from the String class.
Please help. If it can be detailed to each question that would be amazing. I'm trying to learn how to understand this.
Explanation / Answer
Hey, Please follow below.. I have added comments where ever required...
1) hypotenuse(double i, double j) method
public class App {
public static void main(String[] args) {
double hypotenuse = hypotenuse(3, 4);
System.out.println(hypotenuse);
}
private static double hypotenuse(double i, double j) {
double hypotenuse = Math.sqrt(Math.pow(i, 2) + Math.pow(j, 2)); //first calculates i and j to the power of 2, adds them, and then calculates the square root of sum.
return hypotenuse;
}
}
2) max(double a, double b) method...
public class App {
public static void main(String[] args) {
double max = max(3, 4);
System.out.println(max);
}
private static double max(double i, double j) {
if(i >= j) { // return i, if i is greater, or also if it is equal to j, since if both i and j are equal, both i or j is the maximum.
return i;
} else {
return j;
}
}
}
3) min(double a, double b) method...
public class App {
public static void main(String[] args) {
double min = min(4, 6.2);
System.out.println(min);
}
private static double min(double i, double j) {
if(i >= j) { // return j, if i is greater, or also if it is equal to j, since if both i and j are equal, both i or j is the maximum.
return j;
} else {
return i;
}
}
}
4) max(String a, String b) method..
public class App {
public static void main(String[] args) {
String max = max("abcde", "abcdf");
System.out.println(max);
}
private static String max(String i, String j) {
int x = i.compareTo(j); // The first 4 characters of both the strings
// are identical but the last character differ
// as a result, ascii of value of e - ascii
// value of f is returned.
if (x > 0) {
return i;
} else {
return j;
}
}
}
5) min(String a, String b) method..
public class App {
public static void main(String[] args) {
String min = min("abcdex", "abnd");
System.out.println(min);
}
private static String min(String i, String j) {
int x = i.compareTo(j);
if (x > 0) {
return j;
} else {
return i;
}
}
}
6) isSameText(String a, String b) method...
public class App {
public static void main(String[] args) {
boolean isSame = isSameText("abcdex", "abnd");
System.out.println(isSame);
}
private static boolean isSameText(String i, String j) {
if(i.equals(j)) { // equals method checks the content of two strings and returns true if are equal and false otherwise
return true;
} else {
return false;
}
}
}
7) getLongestString(String a, String b, String c) method...
public class App {
public static void main(String[] args) {
String longest = getLongestString("abcdex", "dsnd", "abnssss");
System.out.println(longest);
}
private static String getLongestString(String i, String j, String k) {
String longest = "";
// length() method returns the length of String
if(i.length() > j.length()) { // check if i is greater than j in length
if(i.length() > k.length()) { // now check if i is also greater than k in length
longest = i;
} else { // if i is less than k in length, it means that k is the longest String, since i is already greater than j, and if k is greater than i's length, it automatically is greater than j's length
longest = k;
}
} else { // program flow will reach else part if length of j is greater than i, now just check j is greater than k also, to be the longest
if(j.length() > k.length()) {
longest = j;
} else {
longest = k;
}
}
return longest;
}
}
8) getShortestString(List list) method...
import java.util.ArrayList;
import java.util.List;
public class App {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Maria");
list.add("Danaerys");
list.add("sam");
list.add("John");
String shortest = getShortestString(list);
System.out.println(shortest);
}
private static String getShortestString(List<String> list) {
if (list == null || list.size() == 0)
return null;
String shortest = list.get(0);
for (int i = 1; i < list.size(); i++) {
String str = list.get(i);
if (str.length() < shortest.length()) {
shortest = str;
}
}
return shortest;
}
}
9) getStringBeforeComma(String s) method....
public class App {
public static void main(String[] args) {
String str = getStringBeforeComma("Sam, the slayer");
System.out.println(str);
}
private static String getStringBeforeComma(String item) {
if (item == null || !item.contains(",")) {
return null;
}
String[] split = item.split(",");
return split[0];
}
}
10) getStringAfterComma(String s) method....
public class App {
public static void main(String[] args) {
String str = getStringAfterComma("Sam, the slayer");
System.out.println(str);
}
private static String getStringAfterComma(String item) {
if (item == null || !item.contains(",")) {
return null;
}
String[] split = item.split(",");
return split[1];
}
}
Thanks and Happy Learning !!!!!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.