Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE HELP IN JAVA: There are 2 input files, one contains days - one day (Strin

ID: 3868205 • Letter: P

Question

PLEASE HELP IN JAVA:

There are 2 input files, one contains days - one day (String) per line - and the other contains temperatures - again one (double) per line. You are to read in one day and one temp, and make a Temperature object. Do not try to separate month and date, just treat April 23 as a single String. You will need to create a Temperature class. You need only code the methods you need but make sure it has a compareTo (compares temps).

I have given you the main method. You will see that once you input all the data , create objects, and fill the array, you then print it out , and then find the hottest day in the year. Note that I just used a random number generator and it is as likely there were very hot days in December as very cold ones in July.

ask the user for a month, and then print the hottest day in that month. Note that you will need 2 different findLargest methods.

What I have so far:

public static void main(String[] args) {
  
  
Temperature [] myArray = new Temperature[365];
readData(myArray);
printData (myArray);
Temperature big=findLargest(myArray);
System.out.println("Hottest day was " + big);
System.out.println("Which month ?");
Scanner s = new Scanner(System.in);
String month=s.nextLine();
big= findLargest(month, myArray);
System.out.println("Hottest day in " + month + " was " + big);
}
  
private static void readData(Temperature[] t) {
Scanner scan1 = null, scan2 = null;
try {
scan1 = new Scanner(new BufferedInputStream(new FileInputStream("days.txt")));
scan2 = new Scanner(new BufferedInputStream(new FileInputStream("temps.txt")));

//daya.txt contains the days
//temps.txt contains the temperature for each corresponding day
//so the first temperature (double) in temps.txt is the temperature for
//January 1, the second is for January 2 etc.
//Input data, create Temperature objects, and fill array

for (int i = 0; i < 365; i++) {
t[i] = new Temperature();
t[i].day = scan1.nextLine();
t[i].temperature = Double.parseDouble(scan2.nextLine());
}

} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

private static void printData(Temperature[] t){
for (int i = 0; i < 365; i++) {
System.out.println("Day : " + t[i].day + " Temperature : " + t[i].temperature);
}
}

private static Temperature findLargest(Temperature[] t){
Temperature largest = new Temperature();
for (int i = 0; i < 365; i++) {
if (t[i].temperature > largest.temperature){
largest = t[i];
}
}
return largest;
}

private static Temperature findLargest(String month, Temperature[] t){
Temperature largest = new Temperature();
for (int i = 0; i < 365; i++) {
if (t[i].day.split(" ")[0].equals(month)){
if (t[i].temperature > largest.temperature){
largest = t[i];
}
}
}
return largest;
}
}

public class Temperature implements Comparable<Temperature>{
String month;
int day;
double temperature;
Temperature(String month,int day,double temperature){
this.month=month;
this.day=day;
this.temperature=temperature;
}

Temperature() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public String getmonth(){
return this.month;
}
public int getday(){
return this.day;
}
public double gettemperature(){
return this.temperature;
}
public String tostring(){
return this.month+" "+this.day+" "+this.temperature;
}
public int compareTo(Temperature o) {
return Double.compare(this.temperature, o.temperature);
}
}


  

Explanation / Answer

This should serve your purpose. Let me know if you like it...

CODE:

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
*
* @author Sam
*/
class TemperatureTest {
    public static void main(String[] args) {
      
    
       Temperature [] myArray = new Temperature[365];
       readData(myArray);
       printData (myArray);
       Temperature big=findLargest(myArray);
       System.out.println("Hottest day was " + big);
       System.out.println("Which month ?");
        Scanner s = new Scanner(System.in);
       String month=s.nextLine();
       big= findLargest(month, myArray);
       System.out.println("Hottest day in " + month + " was " + big);
    }
  
     private static void readData(Temperature[] t) {
        Scanner scan1 = null, scan2 = null;
        try {
            scan1 = new Scanner(new BufferedInputStream(new FileInputStream("days.txt")));
            scan2 = new Scanner(new BufferedInputStream(new FileInputStream("temps.txt")));

            //daya.txt contains the days
            //temps.txt contains the temperature for each corresponding day
            //so the first temperature (double) in temps.txt is the temperature for
            //January 1, the second is for January 2 etc.
            //Input data, create Temperature objects, and fill array

            for (int i = 0; i < 365; i++) {
                t[i] = new Temperature(scan1.nextLine(), Double.parseDouble(scan2.nextLine()));
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static void printData(Temperature[] t){
        for (int i = 0; i < 365; i++) {
System.out.println("Day : " + t[i].date + " Temperature : " + t[i].temperature);
        }
    }

    private static Temperature findLargest(Temperature[] t){
        Temperature largest = t[0];
        for (int i = 1; i < 365; i++) {
            if (t[i].compareTo(largest) > 0){
                largest = t[i];
            }
        }
        return largest;
    }

    private static Temperature findLargest(String month, Temperature[] t){
        Temperature largest = null;
        for (int i = 0; i < 365; i++) {
            if (t[i].date.split(" ")[0].equals(month)){
                if (largest == null)
                    largest = t[i];
                else if (t[i].compareTo(largest) > 0){
                    largest = t[i];
                }
            }
        }
        return largest;
    }
}

public class Temperature implements Comparable<Temperature>{
    String date;
    double temperature;
    Temperature(String date, double temperature){
        this.date=date;
        this.temperature=temperature;
    }

    public String getmonth(){
        return this.date;
    }
    public double gettemperature(){
        return this.temperature;
    }
    public String tostring(){
        return this.date+" "+this.temperature;
    }
    @Override
    public int compareTo(Temperature o) {
        return Double.compare(this.temperature, o.temperature);
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote