What is going on with this exception and how do I fix it?
import java.util.*; //needed to use methods in Arrays class public class HW11 { /* * create a method called longest() that takes in three String arguments * return the longest String * for example, if the arguments are "cat", "bird", "HORSE" * the method return the String "HORSE" */ //TODO: write the method here //this is the constructor method for the longest class public String longest(String word1, String word2, String word3) { int a = word1.length(); int b = word2.length(); int c = word3.length(); String F=word1.toUpperCase(); String G=word1.toUpperCase(); String H=word1.toUpperCase(); String Q=("exception ought to be thrown, a magician should decide Tie."); if (a>b&&a>c) { return F; }; if (b>a&&b>c) { return G; } if (c>a&&c>b) { return H; } else return Q; } /* * create a method reverseOrder() that takes in three String arguments * first, capitalize all the words * then put the Strings in REVERSE alphabetical order * return an array of the capitalized Strings in reverse order * for example, if the arguments are "cat", "bird", "HORSE" * the method should return the array: ["HORSE", "CAT", "BIRD"] * * hint: the .toUpperCase() method in the String class and the .sort() method in the Arrays class might be useful * the Arrays class is located in the java.util package */ //TODO: write the method here public static String reverseOrder(String word1, String word2, String word3) { int a = word1.length(); int b = word2.length(); int c = word3.length(); String gammaNablaF; String gammaNablaG; String gammaNablaH; gammaNablaF= word1.toUpperCase(); gammaNablaG= word2.toUpperCase(); gammaNablaH= word3.toUpperCase(); String Possibility1; String Possibility2; String Possibility3; String Possibility4; String Possibility5; String Possibility6; Possibility1 = gammaNablaH + ", " + gammaNablaG + ", " + gammaNablaF; Possibility2 = gammaNablaG + ", " + gammaNablaH + ", " + gammaNablaF; Possibility3 = gammaNablaG + ", " + gammaNablaF + ", " + gammaNablaH; Possibility4 = gammaNablaF + ", " + gammaNablaG + ", " + gammaNablaH; Possibility5 = gammaNablaF + ", " + gammaNablaH + ", " + gammaNablaG; Possibility6 = gammaNablaH + ", " + gammaNablaF + ", " + gammaNablaG; // 3 Permute 3... there are 6 possible String length orders, ignoring ties. if (a>b&&a>c) { if (b>c) { return Possibility1;} else if (c>b) { return Possibility2;} else { return Possibility1;}} if (b>a&&b>c) { if (a>c) { return Possibility6;} else if (c>a) { return Possibility5;} else { return Possibility5;}} if (c>a&&c>b) { if (b>c) { return Possibility4;} else if (c>b) { return Possibility3;} else { return Possibility3;}} else { return Possibility1;} } /* * create a method lastLetters() that takes in three String arguments * return a new String made up of the last letter of each argument, in capital letters * for example, if the arguments are "cat", "bird", "HORSE" * the method should return the String "TDE" */ //TODO: write the method here //the main method...don't modify this public static void main(String args[]) { System.out.print("Please enter a word: "); String word1 = StdIn.readString(); System.out.print("Please enter another word: "); String word2 = StdIn.readString(); System.out.print("Enter one last word, please: "); String word3 = StdIn.readString(); System.out.println("word1: " + word1); System.out.println("word2: " + word2); System.out.println("word3: " + word3); //this code tests the new methods System.out.println("The longest word is: " + longest(word1,word2,word3)); System.out.println("Here are the words in reverse alphabetical order:"); String[] r = reverseOrder(word1,word2,word3); System.out.println(Arrays.toString(r)); System.out.println("The last letters of the words forms the word: " + lastLetters(word1,word2,word3)); } } //end HW11
Cannot make a static reference to the non-static method longest(java.lang.String, java.lang.String, java.lang.String) from the type HW11 Wat does that mean?
main is static, your other class isn't.
Also you never call your homework class, you called it's constructor "Longest..." I would also do all the calculations within the HW11 class, or create a hw11 object and work it in your main....
Join our real-time social learning platform and learn together with your friends!