I have an assignment with 1 piece of code. The assignment is in 3 parts. I have answered all 3 but I am not sure if I understand the questions correctly. I want to know if I can post the assignment question with the code. Can someone then read the question - look at the code and just tell me if I have understood correctly or if I need to start over.
Question:
(See Creating HTML Forms in Java ref below as assign4)Modify the program in Assign4 to synchronize access to the instance variable, balance. Save the program as SyncBank.java. Because balance is a double and not an object, it cannot be used as the monitor. Use synchronized methods or synchronized blocks of code as appropreate. Simultaneously test two threads as was done in Assign4. Because the threads can complete too quickly to determine if they are interfering with each other, delay the adding of a deposit by inserting the follwing doce within the synchronized block or method:
//Delay a deposit for a visible amount of time try { Thread.currentThread().sleep(10000); } catch(InterruptedException e) {}
Question continue....:
Write a short paragraph that explains how the displayed balance verifies that access to the variable, balance, is synchronized.
My Code:
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.net.*; import java.text.DecimalFormat; public class SyncBank extends HttpServlet { private Account act; public DecimalFormat pattern = new DecimalFormat("$#0,000.00"); // To set a patter for display public Double actBalance = 0.00; // initial display value public String displayBalance = pattern.format(actBalance); double entered = 0.0; public void init(ServletConfig conf)throws ServletException { super.init(conf); act = new Account(); } public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); res.setHeader("Expires","Tues, 01 Jan 1980 00:00:00 GMT"); //HttpSession session = req.getSession(true); // get current session out.println("<html>"); out.println("<head>"); out.println("<title>Online Bank ATM Simulator</title>"); out.println("</head>"); out.println("<body>"); out.println("<form method=POST action=/servlet/HTMLBank>"); out.println("<center>"); out.println("<hr>"); out.println("<h1>Bank ATM Simulation</h1><br>"); out.println("Amount: <input type=text name=ENTRY size=20 maxlength=15><br><br>"); out.println("Balance:"+displayBalance+"<br><br>"); out.println("<input type=submit name=DEPOSIT value=\"Deposit\"> "); out.println("<input type=submit name=WITHDRAW value=\"Withdraw\"> "); out.println("<input type=submit name=BALANCE value=\"Balance\"> <br><br>"); out.println("<hr>"); out.println("</form>"); out.println("</table>"); out.println("</body>"); out.println("</html>"); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { try { entered = Double.parseDouble(req.getParameter("ENTRY")); } catch(NumberFormatException e) { entered = 0.0; } synchronized(act) { try { Thread.currentThread().sleep(10000); //sleap for milliseconds } catch(InterruptedException e) {} if((req.getParameter("WITHDRAW")!=null) && (entered < actBalance) && (entered > 0)) { actBalance = actBalance - entered; doGet(req, res); } else if((req.getParameter("DEPOSIT")!=null) && (entered > 0)) { actBalance = actBalance + entered; doGet(req, res); } else if(req.getParameter("BALANCE")!=null) { displayBalance = pattern.format(actBalance); doGet(req, res); } else { res.setContentType("text/html"); PrintWriter out = res.getWriter(); res.setHeader("Expires","Tues, 01 Jan 1980 00:00:00 GMT"); //HttpSession session = req.getSession(true); // get current session out.println("<HTML>"); out.println("<HEAD>"); out.println("<TITLE>Incorrect Input Warning..</TITLE>"); out.println("</HEAD>"); out.println("<BODY>"); out.println("<H1>Invalid Entry</H1>"); out.println("<H2>You have entered an incorrect amount!!!</H2>"); out.println("<H3>You must enter a number only and it must be greater than 0.0</H3>"); out.println("</from>"); out.println("</BODY>"); out.println("</HTML>"); } } } class Account { public int balance; } }