java - Error when calling insertionsort method and how to print an arraylist -
i have created program stores temperature(double) , day(string) in arraylist, uses comparable interface , insertion sort sort arraylist. have left calling insertion sort method , print out original order of arraylist , sorted order. when call insertionsort method gives me error stating "invalid method declaration; return type required". why receiving error? print statement correct print out original arraylist? how update print out sorted arraylist? or can print out insertionsort method?
here code:
import java.io.*; import java.util.scanner; import java.util.arrays; import java.util.arraylist; public class dailytemperature implements comparable<dailytemperature> { //variables private double temperature; private string day; //gettemp & settemp methods public double gettemp() { return temperature; } public void settemp(double newtemp) { temperature = newtemp; } //getday & settemp methods public string getday() { return day; } public void setday(string newday) { day = newday; } public dailytemperature(string day, double temperature) { this.day = day; this.temperature = temperature; } public int compareto(dailytemperature other) { if (temperature < other.temperature) return -1; if (temperature == other.temperature) return 0; return 1; } public string tostring() { return("day of week" + this.getday() + "temperature" + this.gettemp()); } } import java.io.*; import java.util.scanner; import java.util.arraylist; public class dailytemperaturelist { public static void main (string [] args) { arraylist<dailytemperature> dailytemps = new arraylist<dailytemperature>(); dailytemps.add(new dailytemperature("mon", 87.1)); dailytemps.add(new dailytemperature("tue", 88.3)); dailytemps.add(new dailytemperature("wed", 81.2)); dailytemps.add(new dailytemperature("thu", 84.0)); dailytemps.add(new dailytemperature("fri", 76.3)); } public static <dailytemperature extends comparable<? super dailytemperature>> void insertionsort(arraylist<dailytemperature> dailytemps) { dailytemperature temp = null; int position = 0; //loop 2nd element on (int = 1; < dailytemps.size(); i++) { temp = dailytemps.get(i); position = i; while ( 0 < position && temp.compareto(dailytemps.get(position - 1 )) < 0) { dailytemps.set(position, dailytemps.get(position - 1)); position--; } dailytemps.set(position,temp); } } insertionsort(arraylist<dailytemperature> dailytemps); system.out.println( dailytemps.tostring() ); }
however when call insertionsort method gives me error stating "invalid method declaration; return type required". why receiving error?
you calling insertionsort() inside dailytemperaturelist class outside method not compile. moved call main() method, compiles, see bellow.
also print statement correct print out original arraylist? how update print out sorted arraylist? or can print out insertionsort method?
print out original list before calling insertionsort(), , after sorting print out sorted list:
system.out.println( "original list:" ); system.out.println( dailytemps.tostring() ); insertionsort( dailytemps); system.out.println( "sorted list:" );
output:
original list: [day of weekmontemperature87.1, day of weektuetemperature88.3, day of weekwedtemperature81.2, day of weekthutemperature84.0, day of weekfritemperature76.3] sorted list: [day of weekfritemperature76.3, day of weekwedtemperature81.2, day of weekthutemperature84.0, day of weekmontemperature87.1, day of weektuetemperature88.3]
corrected dailytemperaturelist class:
import java.io.*; import java.util.collections; import java.util.scanner; import java.util.arraylist; public class dailytemperaturelist { public static void main (string [] args) { arraylist<dailytemperature> dailytemps = new arraylist<dailytemperature>(); dailytemps.add(new dailytemperature("mon", 87.1)); dailytemps.add(new dailytemperature("tue", 88.3)); dailytemps.add(new dailytemperature("wed", 81.2)); dailytemps.add(new dailytemperature("thu", 84.0)); dailytemps.add(new dailytemperature("fri", 76.3)); system.out.println( "original list:" ); system.out.println( dailytemps.tostring() ); insertionsort( dailytemps); system.out.println( "sorted list:" ); system.out.println( dailytemps.tostring() ); } //public static <dailytemperature extends comparable<? super dailytemperature>> static void insertionsort(arraylist<dailytemperature> dailytemps) { dailytemperature temp = null; int position = 0; //loop 2nd element on (int = 1; < dailytemps.size(); i++) { temp = dailytemps.get(i); position = i; while ( 0 < position && temp.compareto(dailytemps.get(position - 1 )) < 0) { dailytemps.set(position, dailytemps.get(position - 1)); position--; } dailytemps.set(position,temp); } } }
Comments
Post a Comment