use java
Develop a design for the problem – that is determine a set of methods (specify the method header, the purpose of each parameter and the return type and a short explanation of what the method does) which together will solve the problem. Each method must have one clearly defined task. Break methods into several methods if they are too complicated. This requires you to think about how you want to approach the problem. (You will benefit from following the approach discussed in class for the balanced expressions.) You have to turn this in early.
Write a class HTMLTest which implements a method called checkHTML with the heading publicString checkHTML(String expression) which determines whether the given string is a well- formed HTML expression. The String parameter is the HTML expression to be checked. The method returns a String as described below. Your implementation must use a stack.
The string returned by checkHTML includes status information and
processing information.
Status information: The status information is one of the following
(exactly as given here; no leading or trailing blanks):
Success. Error: Closing tag does not match opening tag. Error: Closing tag encountered and stack empty. Error: Opening tag(s) left of stack.
Processing information: The processing information consist of the following 4 integers, separated by one space
: add a date (and time) when it is completed
: be realistic – don’t expect yourself to work wonders
: should specify something to be done not something to be avoided :
make the steps fit your work style, your personality, your thinking
: be specific – so you know where you are in your plan.
the assignment. The second step might be understanding the approach
(i) total number of items process,
(ii) the number of opening tags processed
(iii) the number of closing tags processed (including the current one if it causes and error)
(iv) the number of non-tags (items which are not white space and are neither an opening or
closing tag).
4. Write
a. How did you deal with the tags? Did you strip the delimiters
(<, >, /) immediately? Were they
a report about your solution. It must address the following questions. (rubrics and format posted)
part of what was pushed on the stack? How does your design address the statement “Tags are not
case sensitive”?
What are the similarities and differences between your algorithm and the balanced parentheses
algorithm?
How did you ensure that the correct processing information is available for display at the end?
Did you implement the original design you submitted? If not, what are the differences? Explain
why you needed to make the change and your current design. What did you learn from this
which might be useful in the future?
Hi, Please find the solution and rate the answer. Comments inline.
import java.time.LocalDateTime; import java.util.Stack; public class Main { public static void main(String[] args) { Main main = new Main(); System.out.println(); System.out.println(); System.out.println(main.checkHTML("<body>this is <h1>nice</head>")); } public String checkHTML(String expression){ String finalExp = expression.replace("\n",""); Stack<String> stack = new Stack<>(); String statusString=""; int len = finalExp.length(),i=0; while(i<len){ if(finalExp.charAt(i)=='<'){ if(finalExp.charAt(i+1)=='/'){ i++;//to cross '<' while(finalExp.charAt(i++)!='>'); // i++;//to cross '>' try { stack.pop();//to pop tag } catch (Exception e) { statusString+="No closing tag\n"; return statusString; } continue; } StringBuilder sb = new StringBuilder(); sb.append(finalExp.charAt(i)); while(finalExp.charAt(i++)!='>'){ sb.append(finalExp.charAt(i)); } i++;//to cross '>' stack.push(sb.toString()); }else{ while(finalExp.charAt(i++)!='<');//ignore text System.out.println();i--; } } if(stack.isEmpty()){ statusString+="Well formed HTML:"+ LocalDateTime.now();; }else{ statusString+="Not Well Formed:"+ LocalDateTime.now(); } return statusString; } }
Sample out:
Get Answers For Free
Most questions answered within 1 hours.