Create a WeatherList class that has an instance variable of an array of Weather
ID: 3562879 • Letter: C
Question
Create a WeatherList class that has an instance variable of an array of Weather objects, using the Weather class. The WeatherList class will be similar to the IntList class. It should include methods to search for a numeric target with a binary search method and search for a String target with a sequential search, sort on a conditions and a sort on the temperature. It should also have an add method, increaseSize() method, and a toString() method. Add a method to get the average temperature of all the temperatures call it averageTemp(). To input the data, the Driver program could ask the user to enter a condition, temperature, and a date (in the form of mm/dd/yy) for 11 different weather objects (data given below). Or you may read this data from a file or hard code it in main. The Driver program should:
a. Print the array as entered
Explanation / Answer
import java.util.*;
public class Weather implements Comparable<Weather>
{
public String condition;
public int temperature;
public Calendar date;
public Weather()
{
condition=null;
temperature=0;
date=new GregorianCalendar();
}
public Weather(String cond, int temp, Calendar d)
{
condition=cond;
temperature=temp;
date=d;
}
public String getCondition()
{
return condition;
}
public int getTemperature()
{
return temperature;
}
public Calendar getDate()
{
return date;
}
public int compareTo(Weather compareTemp)
{
int compareQuantity = ((Weather) compareTemp).getTemperature();
//ascending order
return this.temperature - compareQuantity;
}
public String toString()
{
return getCondition()+" "+ getTemperature()+" "+getDate();
}
public int compare(Weather w1, Weather w2) {
if(w1.getTemperature() == w2.getTemperature()){
return 0;
} else {
return -1;
}
}
}
import java.util.*;
public class WeatherList
{
public String cond;
public int temp;
public Calendar d;
//public int month, year,day;
public ArrayList<Weather> weather;;
public int size;
public WeatherList()
{}
public WeatherList(String cond, int temp, Calendar date)
{
this.cond=cond;
this.temp=temp;
this.d=date;
weather=new ArrayList<Weather>();
}
//add method
public void addMethod(String cond, int temp, Calendar d)
{
Weather w=new Weather(cond, temp,d);
weather.add(w);
}
// size increase method
public void setsize()
{
if(weather.size()==0)
{
size=weather.size()*2;
}
}
//averageTemp method that returns average of temperatures
public int averageTemp()
{
int avg;
int total=0;
List<Weather> list= weather;
for(Weather ls: list)
total=+ls.getTemperature();
avg=total/weather.size();
return avg;
}
public String toString()
{
return "CONDITION TEMPERATURE DATE";
}
public void display()
{
for(int i=0; i<weather.size(); i++)
System.out.println(weather.get(i));
}
public void searchCondition(String s)
{
boolean f=false;
for (int i = 0; i<weather.size(); i++)
{
if (weather.get(i).equals(s))
{
System.out.println("The date when it is Foggy is: " +weather.get(i).getDate());
System.out.println("The difference in the temperature is: " + (averageTemp()- weather.get(i).getTemperature()));
f = true;
}
}
if (!f)
System.out.println("Key not found");
}
public void sortTemperature()
{
List<Weather> l= weather;
Collections.sort(l);
for(Weather str: weather){
System.out.println(toString());
System.out.println(str.getCondition()+" "+ str.getTemperature()+" "+str.getDate());
}
}
/*public void findTemp()
{
//int searchkey=75;
int index = Collections.binarySearch(weather, "75");
System.out.println("Element found at : " + index);
}
*/
}
import java.util.*;
public class WeatherTest
{
public static void main(String args[])
{
Calendar gc=new GregorianCalendar();
String con;
int tem;
int day, month, year;
Calendar d;
ArrayList<WeatherList> w1=new ArrayList<WeatherList>();
Scanner scan=new Scanner(System.in);
gc.set(45, 9+1, 12);
WeatherList w2=new WeatherList("Foggy", 67, gc);
gc.set(20, 10+1, 2);
WeatherList w3=new WeatherList("Rainy", 30, gc);
gc.set(29, 0+1, 22);
WeatherList w4=new WeatherList("Cold", 45, gc);
w1.add(w2);
w1.add(w3);
w1.add(w4);
System.out.println(w1.toString());
for(int i=0; i<w1.size();i++)
{
w1.get(i).display();
}
System.out.println();
for(int i=0;i<w1.size();i++)
System.out.println("The average of Temperatures is :"+ w1.get(i).averageTemp());
System.out.println("Searching for the condition Foggy");
for(int i=0; i<w1.size();i++)
{
w1.get(i).display();
}
System.out.println("Sorting the data by temperatures: ");
w1.sortTemperature();
}
}