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

[JAVA]Your job is to complete the validPage method of the HTMLChecker class. Not

ID: 3852395 • Letter: #

Question

[JAVA]Your job is to complete the validPage method of the HTMLChecker class. Note that this method is passed one parameter, which is a String. The String contains HTML. The method returns true or false, depending on whether or not the String consists of valid HTML or not. Your program must do the following:

1. Identify tags. You may assume that whenever the “<” symbol appears, it begins a tag (of course, end tags begin with “</”). The tag will not contain any spaces, and will end with >. You may assume that the symbols < and > will not appear in the String unless they start or end a tag.

2. When your program encounters a “type a” or “type b” start tag, it should push the name of the tag onto a stack. For example, when it encounters the <html> tag in the text above, it pushes the string “html” onto the stack (since only start tags will be placed on the stack, I suggest that you strip the < > from the tag). However, for “type c” start tags such as <br> (which have no corresponding end tag), the tag name should not be pushed onto the stack.

3. When your program encounters an end tag, there are several possibilities:

a. If the stack is empty, then the HTML is invalid, and the validPage method should return false immediately. b. Otherwise, pop the stack. If the (popped) start tag matches the end tag, then the program should continue processing the rest of the String. c. It the start tag and end tag do not match, then it depends on whether or not the start tag is “type a” or “type b”. In the case of “type a” tags, which require a corresponding end tag, your method should return false. For “type b” tags, the the stack should continue to be popped until (i) it is empty (in which case your method should return false); or (b) until a matching start tag is found (in which case your method should continue processing the String)

4. If your program reaches the end of the HTML String, then it should return true of the stack is empty or if every remaining start tag on the stack if “type b”.
Here is an example of how your validPage should work on a small subset of the example HTML above. Assume the String passed as a parameter to validPage is all on a single line.


HTMLChecker.validPage(“<center><h3><b>CSC 300</b></h3></center><ol><li> This course is about <b>data structures</b> and their implementation in the Java language.<p><li>We will discuss Bags, Stacks, Queues, Lists, and Priority Queues</li></ol><p>In addition, we will discuss the running times of various operations on different data structures.”)
Note that when the </ol> tag is encountered (marked by *), several items are popped from the stack, since they are all “type b” start tags, which do not require end tags. If any of them were “type a” tags other than </b>, at this point the validPage method would return false.
If the stack were empty at the end of processing, then validPage should return true. In this case, the method also returns true, because the only tag(s) left on the stack are “type b” tags, which do not require an end tag.
For this html String, in the end your validPage method should return true. This is valid HTML, because the only tag left on the stack (<p>) is a “type b” tag, for which a </p> is optional.

Code I am providing

If you download the hw3.jar file, you will see a partially completed version of the validHTML method which you must complete. Here is a portion of the code I am providing. The intent of the starter code is to relieve you of the burden of writing the string-manipulation part of the validHTML method (mainly, identifying and extracting tags, and determining if they are start or end tags), so that you can concentrate on the correct manipulation of the stack.

In the JAR file, you will also find a class called HW3Tester. It calls validPage, passing it various examples of valid and invalid HTML. I have listed the type b and type c tags. If you encounter any other tag, you should assume that it is type a.

When your program is properly completed, the output of the main method should be

true false false true true                                     The following is the validHTML method

package hw3;
import java.util.Stack;

public class HTMLChecker {

// tags of different types. See homework write-up
// On tests that I run, I will only include these
// tags in my test HTML.
private static String[] typeA = {"b", "h1", "h2", "h3", "ol", "ul"};
private static String[] typeB = {"p", "li"};
private static String[] typeC = {"br", "hr"}; // hr = horizontal line

public static boolean validPage(String html) {

  // the stack that we'll use to save start tags for
  // which we are (possibly) expecting a corresponding
  // end tag. Whether the end tag is required or
  // optional depends on the type of the start tag ("type a"
  // or "type b")
  Stack<String> startTags = new Stack<String>();

  // find the index of the first start tag in html (the parameter
  // String)
  int tagStartPosition = html.indexOf('<');
  boolean isStartTag;
  while (tagStartPosition != -1) {
   
   // attempt to find the index of the end of the start tag
   int tagEndPosition = html.indexOf('>', tagStartPosition+1);
   if (tagEndPosition == -1) return false;

   //extract the tag
   String tag = html.substring(tagStartPosition+1,
     tagEndPosition);
   
   // determine if the tag is a start tag or and end tag
   if (tag.charAt(0) == '/') {
    tag = tag.substring(1);
    isStartTag = false;
   }
   else isStartTag = true;

   //fill in the rest of the code here

   tagStartPosition = html.indexOf('<', tagEndPosition+1);
  }
  // now we've reached the end of the string. Determine if
  // the String contained valid HTML or not
  return false; // replace
}

// "Type a" start tags require a matching end tag to follow
// at some point in the remaining html
private static boolean isTypeA(String tag) {
  for (String t : typeA)
   if (tag.equals(t)) return true;
  return false;
}

// "Type b" start tags may be followed by a matching end tag,
// but the end tag is optional
private static boolean isTypeB(String tag) {
  for (String t : typeB)
   if (tag.equals(t)) return true;
  return false;
}

// "Type c" tags are those start tags for which there
// is no corresponding end tag, such as <br>
private static boolean isTypeC(String tag) {
  for (String t : typeC)
   if (tag.equals(t)) return true;
  return false;
}

}

Explanation / Answer

package hw3;

import java.util.Stack;

public class HTMLChecker {

   // tags of different types. See homework write-up

   // On tests that I run, I will only include these

   // tags in my test HTML.

   private static String[] typeA = { "b", "h1", "h2", "h3", "ol", "ul" };

   private static String[] typeB = { "p", "li" };

   private static String[] typeC = { "br", "hr" }; // hr = horizontal line

   public static boolean validPage(String html) {

       // the stack that we'll use to save start tags for

       // which we are (possibly) expecting a corresponding

       // end tag. Whether the end tag is required or

       // optional depends on the type of the start tag ("type a"

       // or "type b")

       Stack<String> startTags = new Stack<String>();

       // find the index of the first start tag in html (the parameter

       // String)

       int tagStartPosition = html.indexOf('<');

       boolean isStartTag;

       while (tagStartPosition != -1) {

           // attempt to find the index of the end of the start tag

           int tagEndPosition = html.indexOf('>', tagStartPosition + 1);

           if (tagEndPosition == -1)

               return false;

           // extract the tag

           String tag = html.substring(tagStartPosition + 1, tagEndPosition);

           // determine if the tag is a start tag or and end tag

           if (tag.charAt(0) == '/') {

               tag = tag.substring(1);

               isStartTag = false;

           } else

               isStartTag = true;

          

           // fill in the rest of the code here

           if(isStartTag)

           {

               if(isTypeA(tag) || isTypeB(tag)) //push only type a and b tags

                   startTags.push(tag);

           }

           else

           {

               if(startTags.isEmpty()) //found a end tag but no matching start tag was pushed

                   return false;

               else

               {

                   String start = startTags.pop();

                   if(!start.equals(tag))

                   {

                      

                       if(isTypeA(start))

                           return false;

                       else if(isTypeB(start))

                       {

                           //keep popping till we find a matching start tag

                           //if stack becomes empty before finding matching tag, return false since its not valid

                           while(!start.equals(tag))

                           {

                               if(startTags.isEmpty())

                                   return false;

                               start = startTags.pop();

                           }

                       }

                   }

                     

               }

           }

          

           tagStartPosition = html.indexOf('<', tagEndPosition + 1);

       }

       // now we've reached the end of the string. Determine if

       // the String contained valid HTML or not

       while(!startTags.isEmpty())

       {

           String tag = startTags.pop();

           if(!isTypeB(tag))

               return false;

       }

       return true;

   }

   // "Type a" start tags require a matching end tag to follow

   // at some point in the remaining html

   private static boolean isTypeA(String tag) {

       for (String t : typeA)

           if (tag.equals(t))

               return true;

       return false;

   }

   // "Type b" start tags may be followed by a matching end tag,

   // but the end tag is optional

   private static boolean isTypeB(String tag) {

       for (String t : typeB)

           if (tag.equals(t))

               return true;

       return false;

   }

   // "Type c" tags are those start tags for which there

   // is no corresponding end tag, such as <br>

   private static boolean isTypeC(String tag) {

       for (String t : typeC)

           if (tag.equals(t))

               return true;

       return false;

   }

}

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