R
Rune Runnestoe
Hi,
I am trying to make a simple calculator for the web by using sessions.
When accessing the .jsp-file for the first time, the user will see:
----
Welcome to the calculator
<an input field to to write numbers in >
<a button for adding this number> <a button for deleting this number>
---
By writing a number in the text field and clicking on the button "Add
the number", the welcome message will be replaced by a status message
listing all the numbers that have been added so far, and what is the
sum of all numbers.
For example, it can be like this:
---
Your numbers so far is:
* 4
* 5
The sum of the numbers so far is: 9
<an input field to to write numbers in >
<a button for adding this number> <a button for deleting this number>
---
The button "Delete a number" is used to delete the first occurence of
this number in the number list.
The code must consider error situation like trying to delete a number
that does not exist in the list, or trying to add or delete an
alphanumeric character that cannot be converted to a number. In those
situations, the user shall be presentet for another .jsp-file, showing
this message:
---
The number is not valid ! Try again
<link back to main page>
---
This is my propose for the class Calculator:
---
package myPackage;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Vector;
public class Calculator{
private int number;
private ArrayList<Calculator> numberList = new
ArrayList<Calculator>();
private Vector list;
public Calculator(int startNumber) {
number = startNumber;
}
public int finnTall() {
return number;
}
public void settTall(int newNumber) {
number = newNumber;
}
public boolean isTheNumberValid(String thisNumber){
boolean valid;
try{
int number = Integer.parseInt(thisNumber);
valid = true;
}catch (NumberFormatException e) {
valid = false;
}
return valid;
}
public Enumeration numberList() {
return list.elements();
}
}
---
And here is my attempt to make the code for the .jsp-file:
---
<html><head><title>Calculator</title></head>
<body>
<%@ page import="myPackage.Calculator" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.servlet.*" %>
<%!
public void makeANumberList(HttpSession session) {
//making a Calculator-object containing the number 0
Calculator calculator = new Calculator(0);
//making a session attribute of the Calculator-object
session.setAttribute("numberList", calculator);
}
%>
<%
int sum = 0;
int i = 0;
//if the session-attribute "numberList" doesn't exist,
//we generate it by using the method defined above.
if(session.getAttribute("numberList") == null){
makeANumberList(session);
}
//making the session-attribute "numberList" available locally:
ArrayList<Calculator> allTheNumbers =
(ArrayList)session.getAttribute("numberList");
if(request.getParameter("add") != null){
try{
String[] numberTab =
request.getParameterValues("numberfield");
out.println("Your numbers so far is: ");
for(i = 0; i < numberTab.length; i++){
int theNumber = Integer.parseInt(numberTab);
sum += theNumber;
out.println(theNumber);
}
out.println("The sum of the numbers so far is: " + sum);
}catch (NumberFormatException e) {
out.println("The number is not valid ! Try again");
}
}else if(request.getParameter("delete") != null){
try{
String thisNumber = request.getParameter("numberfield");
String[] numberTab =
request.getParameterValues("numberfield");
int numHit = 1;
for (i = 0; i < numberTab.length; i++){
if(numberTab.equals(thisNumber) && numHit == 1){
allTheNumbers.remove(numberTab);
numHit++; //a counter to assure we don't remove
more than one occurence of similar numbers
}
}
out.println("Your numbers so far is: ");
for(i = 0; i < numberTab.length; i++){
int theNumber = Integer.parseInt(numberTab);
sum += theNumber;
out.println(theNumber);
}
out.println("The sum of the numbers so far is: " + sum);
}catch(NumberFormatException e){
out.println("The number is not valid");
}
}else{ //both parameters are null, as when you first time access
the file
out.println("<strong>Welcome to the calculator</strong>");
}
%>
<br>
<form action= "" method="post">
<ul>
<%
for (i = 0; i < allTheNumbers.size(); i++){
out.println("<li>" + allTheNumbers.get(i).finnTall() +
"</li>");
sum += allTheNumbers.get(i).finnTall();
}
%>
</ul>
The sum of the numbers so far is: <%= sum %>
<br>
<input type="text" name="numberfield" size="30" >
<br>
<input type="submit" name="add" value="Add a number" />
<input type="submit" name="delete" value="Remove a number" />
</form>
</body>
</html>
---
When I run Tomcat, I get a lot of error messages. It seems like I get
a ClassCastException when I try to get an ArrayList from the
session-object.
I am a bit confused whether I should use ArrayList, Vector or
Enumeration. Are one as good as the others ? I haven't made use of the
method "isTheNumberValid" from the Calculator class, may be I don't
need it.
I would also appreciate views or examples of how I can (if I can) make
the code more efficient. Can I for instance reduce the number of loops
?
I haven't made the .jsp-file for the error- message yet, but regard
that as a minor issue.
Thanks in advance for guidance and code help.
Regards
Rune Runnestø
I am trying to make a simple calculator for the web by using sessions.
When accessing the .jsp-file for the first time, the user will see:
----
Welcome to the calculator
<an input field to to write numbers in >
<a button for adding this number> <a button for deleting this number>
---
By writing a number in the text field and clicking on the button "Add
the number", the welcome message will be replaced by a status message
listing all the numbers that have been added so far, and what is the
sum of all numbers.
For example, it can be like this:
---
Your numbers so far is:
* 4
* 5
The sum of the numbers so far is: 9
<an input field to to write numbers in >
<a button for adding this number> <a button for deleting this number>
---
The button "Delete a number" is used to delete the first occurence of
this number in the number list.
The code must consider error situation like trying to delete a number
that does not exist in the list, or trying to add or delete an
alphanumeric character that cannot be converted to a number. In those
situations, the user shall be presentet for another .jsp-file, showing
this message:
---
The number is not valid ! Try again
<link back to main page>
---
This is my propose for the class Calculator:
---
package myPackage;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Vector;
public class Calculator{
private int number;
private ArrayList<Calculator> numberList = new
ArrayList<Calculator>();
private Vector list;
public Calculator(int startNumber) {
number = startNumber;
}
public int finnTall() {
return number;
}
public void settTall(int newNumber) {
number = newNumber;
}
public boolean isTheNumberValid(String thisNumber){
boolean valid;
try{
int number = Integer.parseInt(thisNumber);
valid = true;
}catch (NumberFormatException e) {
valid = false;
}
return valid;
}
public Enumeration numberList() {
return list.elements();
}
}
---
And here is my attempt to make the code for the .jsp-file:
---
<html><head><title>Calculator</title></head>
<body>
<%@ page import="myPackage.Calculator" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.servlet.*" %>
<%!
public void makeANumberList(HttpSession session) {
//making a Calculator-object containing the number 0
Calculator calculator = new Calculator(0);
//making a session attribute of the Calculator-object
session.setAttribute("numberList", calculator);
}
%>
<%
int sum = 0;
int i = 0;
//if the session-attribute "numberList" doesn't exist,
//we generate it by using the method defined above.
if(session.getAttribute("numberList") == null){
makeANumberList(session);
}
//making the session-attribute "numberList" available locally:
ArrayList<Calculator> allTheNumbers =
(ArrayList)session.getAttribute("numberList");
if(request.getParameter("add") != null){
try{
String[] numberTab =
request.getParameterValues("numberfield");
out.println("Your numbers so far is: ");
for(i = 0; i < numberTab.length; i++){
int theNumber = Integer.parseInt(numberTab);
sum += theNumber;
out.println(theNumber);
}
out.println("The sum of the numbers so far is: " + sum);
}catch (NumberFormatException e) {
out.println("The number is not valid ! Try again");
}
}else if(request.getParameter("delete") != null){
try{
String thisNumber = request.getParameter("numberfield");
String[] numberTab =
request.getParameterValues("numberfield");
int numHit = 1;
for (i = 0; i < numberTab.length; i++){
if(numberTab.equals(thisNumber) && numHit == 1){
allTheNumbers.remove(numberTab);
numHit++; //a counter to assure we don't remove
more than one occurence of similar numbers
}
}
out.println("Your numbers so far is: ");
for(i = 0; i < numberTab.length; i++){
int theNumber = Integer.parseInt(numberTab);
sum += theNumber;
out.println(theNumber);
}
out.println("The sum of the numbers so far is: " + sum);
}catch(NumberFormatException e){
out.println("The number is not valid");
}
}else{ //both parameters are null, as when you first time access
the file
out.println("<strong>Welcome to the calculator</strong>");
}
%>
<br>
<form action= "" method="post">
<ul>
<%
for (i = 0; i < allTheNumbers.size(); i++){
out.println("<li>" + allTheNumbers.get(i).finnTall() +
"</li>");
sum += allTheNumbers.get(i).finnTall();
}
%>
</ul>
The sum of the numbers so far is: <%= sum %>
<br>
<input type="text" name="numberfield" size="30" >
<br>
<input type="submit" name="add" value="Add a number" />
<input type="submit" name="delete" value="Remove a number" />
</form>
</body>
</html>
---
When I run Tomcat, I get a lot of error messages. It seems like I get
a ClassCastException when I try to get an ArrayList from the
session-object.
I am a bit confused whether I should use ArrayList, Vector or
Enumeration. Are one as good as the others ? I haven't made use of the
method "isTheNumberValid" from the Calculator class, may be I don't
need it.
I would also appreciate views or examples of how I can (if I can) make
the code more efficient. Can I for instance reduce the number of loops
?
I haven't made the .jsp-file for the error- message yet, but regard
that as a minor issue.
Thanks in advance for guidance and code help.
Regards
Rune Runnestø