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

**Question after given code** import java.io.*; import java.net.URL; import java

ID: 3825722 • Letter: #

Question

**Question after given code**

import java.io.*;
import java.net.URL;

import javax.swing.JOptionPane;

/**
* Class to find the temperature in a web page.
*
*/
public class TempFinder
{
  
/**
* Method to find the temperature in the passed
* file
* @param fileName the name of the file to look in
*/
public String getTemp(String fileName)
{
String seq = "<b>&deg";
String temp = null;
String line = null;
  
// try the following
try {
  
// read from the file
BufferedReader reader =
new BufferedReader(new FileReader(fileName));
  
// loop till end of file or find sequence
while ((line = reader.readLine()) != null &&
line.indexOf(seq) < 0)
{}
  
// if there is a current line
if (line != null)
{
// find the temperature
int degreeIndex = line.indexOf(seq);
int startIndex = line.lastIndexOf('>',degreeIndex);
temp = line.substring(startIndex + 1, degreeIndex);
}
  
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null,"Couldn't find file " + fileName);
} catch (Exception ex) {
   JOptionPane.showMessageDialog(null,"Error during read or write");
ex.printStackTrace();
}
return temp;
}
  
/**
* Method to get the temperature from a network
* @param urlStr the url as a string
* @return the temperature as a string
*/
public String getTempFromNetwork(String urlStr)
{
String temp = null;
String line = null;
String seq = "&deg";
  
try {
  
// create a url
URL url = new URL(urlStr);
  
// open a buffered reader on the url
InputStream inStr = url.openStream();
BufferedReader reader =
new BufferedReader(new InputStreamReader(inStr));
  
// loop till end of file or find sequence
while ((line = reader.readLine()) != null &&
line.indexOf(seq) < 0)
{}
  
// if there is a current line
if (line != null)
{
// find the temperature
int degreeIndex = line.indexOf(seq);
int startIndex = line.lastIndexOf('>',degreeIndex);
temp = line.substring(startIndex + 1, degreeIndex);
}
  
} catch (FileNotFoundException ex) {
   JOptionPane.showMessageDialog(null,"Couldn't connect to " + urlStr);
} catch (Exception ex) {
   JOptionPane.showMessageDialog(null,"Error during read or write");
ex.printStackTrace();
}
return temp;
}
  
public static void main(String[] args)
{
System.out.print("Reading current temp from stored Atlanta Journal file ");
TempFinder finder = new TempFinder();
String temp = finder.getTemp("ajc-weather.html");
if (temp == null)
System.out.println("Sorry, no temp was found in the file");
else
System.out.println("The current temperature is " + temp);
System.out.print(" Reading current temp from Bismarck Tribune ");
String urlString = "http://www.bismarcktribune.com/";
temp = finder.getTempFromNetwork(urlString);
if (temp == null)
System.out.println("Sorry, no temp was found at " +
urlString);
else
System.out.println("The current temp " +
"from the network is " + temp);
}
}

** Explain/describe how the getTempFromNetwork method works; basically describe each line of code
**Explain/describe how the 'getTemp' method logic is different from the 'getTempFromNetwork' method logic

Explanation / Answer


In getTempFromNetwork method:
1) we are passing urlStr as an argument.
2) Initialising variables temp and line to null and seq to "&deg".
3) Creating an instance of url.
4) Calling openStream() method of url to get the stream content.
5) Then opening a BufferReader on the inputstream and content is then read from bufferreader.
6) Running a while to read the lines matching the string "&deg" which is the content of seq variable.
7) If a line is found first we are fetching the index of "&deg" and storing in degreeIndex. The startIndex is set with the last index of ">" in backward direction starting from degreeIndex. Then extracting the temperature that is present in between the startIndex+1 and degreeIndex. The value storing in temp variable.
8) Handling exceptions such as file not found and error during reading or writing to file

In getTemp method:

1) we are passing fileName as an argument.
2) Initialising variables temp and line to null and seq to "<b>&deg".
3) Opening a BufferReader on the FileReader and content is then read from bufferreader.
4) Running a while to read the lines matching the string "&deg" which is the content of seq variable.
5) If a line is found first we are fetching the index of "&deg" and storing in degreeIndex. The startIndex is set with the last index of ">" in backward direction starting from degreeIndex. Then extracting the temperature that is present in between the startIndex+1 and degreeIndex. The value storing in temp variable.
6) Handling exceptions such as file not found and error during reading or writing to file

In getTempFromNetwork method we are trying to get temperature from the contents of the url. In getTemp method we are tying to get temperature from a file.