hi i have a class called sunset that is suposed to take various sunsets and sunr
ID: 3548352 • Letter: H
Question
hi i have a class called sunset that is suposed to take various sunsets and sunrise times with different dates and print them out on a graph. This is for java language. I have this so far and was hoping some one could fix it.
public static void main(String[] args)throws IllegalArgumentException,
FileNotFoundException, IOException
{
BufferedReader in = getReader("sunlast.txt");
Sun sun = readSun(in);
while (sun != null)
{
System.out.println(sun.date+" = date"+sun.sunRiseHour);
sun = readSun (in);
}
}
public static class Sun
{
public int date;
public int sunRiseHour;
public int sunSetHour;
public Sun( int date, int sunRiseHour,int sunSetHour)
{
this.date = date;
this.sunRiseHour = sunRiseHour;
this.sunSetHour = sunSetHour;
}
public Sun()
{
date = 0;
sunRiseHour=0;
sunSetHour=0;
}
public int getdate()
{
return date;
}
public int getsunRiseHour()
{
return sunRiseHour;
}
public int getsunSetHour()
{
return sunSetHour;
}
}
public static BufferedReader getReader(String name)throws IllegalArgumentException,
FileNotFoundException, IOException
{
if(name==null)
{
throw new IllegalArgumentException("Filename is null");
}
BufferedReader answer = new BufferedReader(new FileReader(name));
return answer;
}
public static Sun readSun(BufferedReader in) throws IOException
{
int date;
int sunRiseHour;
int sunSetHour;
String line ="";
String [] data;
if (line == null)
return null;
line = in.readLine();
data = line.split("");
date = Integer.parseInt(data[0]);
sunRiseHour = Integer.parseInt(data[1]+data[2]);
sunSetHour=Integer.parseInt(data[3]+data[4]);
System.out.println("data = "+data);
return new Sun(date,sunRiseHour,sunSetHour);
}
}
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Sun
{
public int date;
public int sunRiseHour;
public int sunSetHour;
public Sun( int date, int sunRiseHour,int sunSetHour)
{
this.date = date;
this.sunRiseHour = sunRiseHour;
this.sunSetHour = sunSetHour;
}
public Sun()
{
date = 0;
sunRiseHour=0;
sunSetHour=0;
}
public static void main(String[] args)throws IllegalArgumentException,
FileNotFoundException, IOException
{
BufferedReader in = getReader("sunlast.txt");
Sun sun = readSun(in);
while (sun != null)
{
System.out.println(sun.date+" = date"+sun.sunRiseHour);
sun = readSun (in);
}
}
public int getdate()
{
return date;
}
public int getsunRiseHour()
{
return sunRiseHour;
}
public int getsunSetHour()
{
return sunSetHour;
}
public static BufferedReader getReader(String name)throws IllegalArgumentException,
FileNotFoundException, IOException
{
if(name==null)
{
throw new IllegalArgumentException("Filename is null");
}
BufferedReader answer = new BufferedReader(new FileReader(name));
return answer;
}
public static Sun readSun(BufferedReader in) throws IOException
{
int date;
int sunRiseHour;
int sunSetHour;
String line ="";
String [] data;
line = in.readLine();
data = line.split("");
date = Integer.parseInt(data[0]);
sunRiseHour = Integer.parseInt(data[1]+data[2]);
sunSetHour=Integer.parseInt(data[3]+data[4]);
System.out.println("data = "+data);
return new Sun(date,sunRiseHour,sunSetHour);
}
}
//Please note...the program works fine....I can verify it for you but for that pls send me the sunlast.txt file
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.