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

Write a program to satisfy the following programming problem: One can obtain a t

ID: 3687053 • Letter: W

Question

Write a program to satisfy the following programming problem: One can obtain a table of moon phases for a specified year at the URL http://aa.usno.navy.mil/cgi-bin/aa_moonphases.pl?year=NNNN where NNNN is the year for which the chart is requested. The web server returns a page containing an text table showing the moon phases in a given year. Write a program that asks the user for a year, asks the aa.usno.navy.mil server for the moon phase table, and then prints only the table showing the phases of the moon for that year.. Note that the year needs to be included in the URL string as the parameter to the request. A good strategy for solving this problem is to use a Boolean variable inTable to determine whether we are currently printing lines of data as we receive them from the server. Initially set the Boolean variable inTable to false. After you read a line of data from the server, do these three steps in order: 1. If inTable is true, check to see if the line contains the “” string, which indicates we’re finished receiving the moon phase table, so change inTable to false so we won’t print any more lines. 2. If inTable is true, print the line. 3. If inTable is false, check to see if the line contains the tag that starts the moon phase table, “

U.S. Naval Observatory
Astronomical Applications Department

Phases of the Moon

, , , , and tags using the String class’s replace() method to make the output even nicer, but that isn’t required.

import java.util.Scanner;

/**
   This program shows the moon phase table for the given year
*/
public class MoonPhaseTable
{
   public static void main(String[] args) throws IOException
   {
      Scanner in = new Scanner(System.in);
      System.out.print("Please enter the year (e.g. 1977): ");
      int year = in.nextInt();

      // Build the URL string and open a URLConnection.
      // Be sure to set the year entered by the user at the end of the URL string!!!
      . . .

      // Get the connection's input stream, and make a Scanner for it
      . . .

      bool inTable = false;
      while (. . .) {
         // Read input lines from the scanner into the String named line.
         . . .


if (inTable) {
      // Check if the line contains the </table> end tag -- if seen, we are not in the table any longer
      . . .
}
if (inTable) {
      // If inside the Table, print the line.
             // Optionally clean up any unwanted tags, such as
             // <pre>, <tr>, <td>, <b> before printing.
  
      . . .
}
if (!inTable) {
      // Check if the line contains the <table start tag -- if seen, turn on printing
      . . .
}
      }
   }
}

                               2011 Phases of the Moon                               Universal Time                New Moon   First Quarter       Full Moon    Last Quarter                     d  h  m         d  h  m         d  h  m         d  h  m            Jan  4  9 03    Jan 12 11 31    Jan 19 21 21    Jan 26 12 57       Feb  3  2 31    Feb 11  7 18    Feb 18  8 36    Feb 24 23 26       Mar  4 20 46    Mar 12 23 45    Mar 19 18 10    Mar 26 12 07       Apr  3 14 32    Apr 11 12 05    Apr 18  2 44    Apr 25  2 47       May  3  6 51    May 10 20 33    May 17 11 09    May 24 18 52       Jun  1 21 03    Jun  9  2 11    Jun 15 20 14    Jun 23 11 48       Jul  1  8 54    Jul  8  6 29    Jul 15  6 40    Jul 23  5 02       Jul 30 18 40    Aug  6 11 08    Aug 13 18 57    Aug 21 21 54       Aug 29  3 04    Sep  4 17 39    Sep 12  9 27    Sep 20 13 39       Sep 27 11 09    Oct  4  3 15    Oct 12  2 06    Oct 20  3 30       Oct 26 19 56    Nov  2 16 38    Nov 10 20 16    Nov 18 15 09       Nov 25  6 10    Dec  2  9 52    Dec 10 14 36    Dec 18  0 48       Dec 24 18 06                                                    

Explanation / Answer

Answer for Question:

This implementation code and the table given below may help you to solve the given moon problem ..

import java.util.Scanner;
import java.io.*;
import java.net.*;
/**
This program shows the moon phase table for the given year
*/
public class MoonPhaseTable
{
public static void main(String[] args) throws IOException
{
Scanner in = new Scanner(System.in); //read year number from user
System.out.print("Please enter the year (e.g. 1977): ");
int year = in.nextInt();

// Build the URL string and open a URLConnection
// Be sure to set the year on the URL string!!!
URL moonphase = new URL("http://aa.usno.navy.mil/cgi-bin/aa_moonphases.pl?year=2015/");
URLConnection mp = moonphase.openConnection();

// Get the connection's input stream, and make a Scanner for it
BufferedReader r = new BufferedReader(new InputStreamReader(mp.getInputStream()));

Scanner s = new Scanner(moonphase.openStream());

String inputLine;

boolean printingTable = inputLine.contains("table");
while ((inputLine = r.readLine()) !=null) {
// Read input lines from the scanner into the String named line.

if (printingTable) {   
// Check if the line contains the </table> end tag -- if seen, turn off printing
System.out.print("CONTAINS </table>");
s.close();
}
if (printingTable) {
// If inside the Table, print the line.
// Optionally clean up any unwanted tags, such as
// <pre>, <tr>, <td>, <b> before printing.
System.out.print(inputLine);

}
if (!printingTable) {
// Check if the line contains the <table ...> start tag -- if seen, turn on printing
System.out.print(inputLine);
}
}
}

}
  
  
<tr><td>
<pre>

<b>2011 Phases of the Moon</b>
Universal Time

New Moon First Quarter Full Moon Last Quarter   

d h m d h m d h m d h m

Jan 4 9 03 Jan 12 11 31 Jan 19 21 21 Jan 26 12 57
Feb 3 2 31 Feb 11 7 18 Feb 18 8 36 Feb 24 23 26
Mar 4 20 46 Mar 12 23 45 Mar 19 18 10 Mar 26 12 07
Apr 3 14 32 Apr 11 12 05 Apr 18 2 44 Apr 25 2 47
May 3 6 51 May 10 20 33 May 17 11 09 May 24 18 52
Jun 1 21 03 Jun 9 2 11 Jun 15 20 14 Jun 23 11 48
Jul 1 8 54 Jul 8 6 29 Jul 15 6 40 Jul 23 5 02
Jul 30 18 40 Aug 6 11 08 Aug 13 18 57 Aug 21 21 54
Aug 29 3 04 Sep 4 17 39 Sep 12 9 27 Sep 20 13 39
Sep 27 11 09 Oct 4 3 15 Oct 12 2 06 Oct 20 3 30
Oct 26 19 56 Nov 2 16 38 Nov 10 20 16 Nov 18 15 09
Nov 25 6 10 Dec 2 9 52 Dec 10 14 36 Dec 18 0 48
Dec 24 18 06   
</pre>
</td></tr>

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