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

Task 5: Write a program that takes an input file and produces the desired result

ID: 3773943 • Letter: T

Question

Task 5: Write a program that takes an input file and produces the desired results.

--------------------------------------------------------------------------------------------------------------------------

****GetMonth.java ****

--------------------------------------------------------------------------------------------------------------------------

**** weather1.txt****

--------------------------------------------------------------------------------------------------------------------------

**** weather2.txt ****

http://vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/assignments/assignment9/weather2.txt

--------------------------------------------------------------------------------------------------------------------------

**** weather3.txt ****

http://vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/assignments/assignment9/weather3.txt

--------------------------------------------------------------------------------------------------------------------------

**** crazy_weather.txt ****

http://vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/assignments/assignment9/crazy_weather.txt

--------------------------------------------------------------------------------------------------------------------------

**** nba.txt ****

http://vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/assignments/assignment9/nba.txt

--------------------------------------------------------------------------------------------------------------------------

****question****

File GetMonth.java contains an incomplete program, that processes CSV files containing weather data, like weather1.txt. For each line in the file (except the top one), the program prints out the month that that line corresponds to.

In this program, we make the assumption that the names of all columns are shown at the top line of the CSV file. We also make the assumption that the CSV file contains four columns, and the leftmost column specifies the date, in the format "day_name month/day/year".

Complete that program, by defining a getMonth function, that satisfies the following specs:

The function takes one argument, called date. Argument filename is a string specifying the date, in the format used in the weather1.txt file.

The function returns (as an int) the month from that date.

Note that the code provides a printMonths function, which is called from main. The printMonths function calls the getMonth function which you are asked to write.

IMPORTANT: You are NOT allowed to modify in any way the main function. You are free to define and use auxiliary functions. You are also free to use code written in class, posted on the course website, or available on the lecture slides.

Example CSV files you can test your program with are weather1.txt, weather2.txt, weather3.txt, crazy_weather.txt, and nba.txt.

An example run of the complete program is shown below.

--------------------------------------------------------------------------------------------------------------------------

****example output****

-------------------------------------------------------------------------------------------------------------------------

Explanation / Answer

GetMonth.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class GetMonth
{
public static void printMonths(String filename)
{
ArrayList<String> lines = readFile(filename);
if (lines == null)
{
return;
}
for (int i = 0; i < lines.size(); i++)
{
String line = lines.get(i);
String[] columns = line.split(",");

String date = columns[0];
int line_month = getMonth(columns[0]);
System.out.printf("row %d, month = %d ", i+1, line_month);
}
}
public static int getMonth(String date){
   String strs[] = date.split(" ");
   return Integer.parseInt(strs[1].split("/")[0]);
}
public static ArrayList<String> readFile(String filename){
   ArrayList<String> list = new ArrayList<String>();
   try{
   Scanner scan = new Scanner(new File(filename));
   while(scan.hasNextLine()){
       list.add(scan.nextLine());
   }
   }
   catch(FileNotFoundException e){
       System.out.println("Input file "+filename+" does nto exist");
   }
   return list;
}

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
while (true)
{
System.out.printf("Enter a filename (or q to quit): ");
String filename = in.next();
if (filename.equals("q"))
{
System.out.printf("Exiting... ");
System.exit(0);
}
printMonths(filename);
System.out.printf(" ");
}
}
}

Output:

Enter a filename (or q to quit): D:\weather1.txt
row 1, month = 3
row 2, month = 3
row 3, month = 3
row 4, month = 3
row 5, month = 3
row 6, month = 3
row 7, month = 4
row 8, month = 4
row 9, month = 4
row 10, month = 4
row 11, month = 4
row 12, month = 4
row 13, month = 7
row 14, month = 7
row 15, month = 7

Enter a filename (or q to quit): q
Exiting...

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