www.webdeveloper.com
Results 1 to 8 of 8

Thread: First java Calculator

  1. #1
    Join Date
    Oct 2010
    Posts
    4

    First java Calculator

    Hello everyone I am new to Java and my teacher goes way too fast in class and am a day behind on this calculator. Our assignment is to make a Calculator using AppendDigit and Else if functions in the java code. Some of my other code in html may be messed up as well. I am just lost so here is the
    HTML:

    Code:
     <title>John's Calculator</title>
    	<style type="text/css">
    		.rt {text-align:right; font-family: Courier New;}
    		.numbttn {text-align: center; background-color: 
    		silver; font-family: Courier New; width 4eM;}
    		body {background-color: silver}
    		
    	</style>
    	
    	<script type="text/javascript" src="cal.js"></script>
    
    </head>
    
    <body>
    <form name="kpd">
    
    <table>
    <tr> <TABLE BORDER=4>
    <TR>
    <TD>
    <INPUT TYPE="text"   NAME="Input" Size="16">
    <br>
    </TD>
    </TR>
    <TR>
    <TD> <INPUT class="numbttn" TYPE="button" NAME="one"   VALUE="  1  " OnClick="AppendDigit('1')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="two"   VALUE="  2  " OnCLick="AppendDighit('2')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="three" VALUE="  3  " OnClick="AppedndDigit('3')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="plus"  VALUE="  +  " OnClick="AppendDigit('+')";/></td>
    </tr>
    
    <tr>
    <td> <INPUT class="numbttn" TYPE="button" NAME="four"  VALUE="  4  " OnClick="AppendDigit('4')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="five"  VALUE="  5  " OnCLick="AppendDigit('5')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="six"   VALUE="  6  " OnClick="AppendDigit('6')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="minus" VALUE="  -  " OnClick="AppendDigit(' - ')";/></td>
    </tr>
    
    
    <tr>
    <td> <INPUT class="numbttn" TYPE="button" NAME="seven" VALUE="  7  " OnClick="AppendDigit('7')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="eight" VALUE="  8  " OnCLick="AppendDigit('8')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="nine"  VALUE="  9  " OnClick="AppendDigit('9')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="times" VALUE="  x  " OnClick="AppendDigit(' * ')";/></td>
    </tr>
    
     <tr> 
    <td> <Input class="numbttn" TYPE="button" NAME="clear" VALUE="c" OnClick="AppendDigit('')";/></td>
    <td> <Input class="numbttn" TYPE="button" NAME="zero"  VALUE="0" OnClick="AppendDigit('0')";/></td>
    <td> <Input class="numbttn" TYPE="button" NAME="DoIt"  VALUE="=" OnClick="AppendDigit('=')";/></td>
    <td> <Input  class="numbttn"TYPE="button" NAME="div"   VALUE="/" OnClick="AppendDigit(' / ');"/></td>
    </r>
    </table>
    </form>
    </body>
    
    </html>
    and Here is the pathetic Javascript which I have barely started:

    Code:
    // cal Applet- John Falco- 20 October 2010
    
     function clear TB() {
    	document.form.kpd.display.value="0";
     }
     
     function AppendDigit (x) {
    	if (x==".") {
    		if ((!HasDecimal))
    	}
    	else {
    	document.forms.kpd.display.value = 
    			document.forms.kdp.display.value + X;
    };

  2. #2
    Join Date
    Apr 2009
    Posts
    96
    What exactly do you need?

  3. #3
    Join Date
    Oct 2010
    Posts
    4
    it's not completed obviously and it doesnt work at all. Only the numbers show up and they aren't aligned properly.

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415

    Exclamation Many errors ...

    You have several problems with the posted script:
    1. You cannot have spaces in function names.
    2. You miss spelled the calling of the AppendDigit function
    3. The AppendDigit function is incomplete and does not have matching '{}' characters
    4. 'kpd' and 'kdp' are not the same
    5. Two <table>s defined in HMTL, but only one completed
    6. All your math functions buttons will do nothing other than add the symbol into the display.
    7. There is no 'display' element and you should not name an element the same as a <input...> tag

    I'm suprised that you got anything to display at all.
    If you use FF, you might turn on the 'error console' and checkout some of the other errors.

    This is a javascript forum, not the java forum. All of your code is javascript and HTML.
    Maybe you are taking the wrong course if you are trying to use java.
    You better find a 'compass' or a 'tutor' pretty quick.
    Last edited by JMRKER; 10-20-2010 at 10:31 PM.

  5. #5
    Join Date
    Oct 2010
    Posts
    4
    OK cool thanks for the tips. I figured out all the javascript I needed now I need help with the HTML. My numbers are not showing up when i click on the button. It has to do with appenddigit right? Here is the finished Javascript:

    Code:
    // cal Applet- John Falco- 20 October 2010
    
    var HasDecimal;
    
    function ClearTB() {
    document.forms.kpd.display.value="0";
    HasDecimal = false;
    }
    
    // ------------------------------------------------------------------------------------------
    // function AppendDigit(x) -- concatenate the value in X to the texxt in
    // document.forms.kpd.display.value
    // ------------------------------------------------------------------------------------------
    function AppendDigit(x) {
    
    if (x==".") {
    if (!HasDecimal) {
    document.forms.kpd.display.value=
    document.forms.kpd.display.value + x;
    HasDecimal = true;
    };
    } else {
    if {document.forms.kpd.display.value=="0") {
    if (x!="0") {
    document.forms.kpd.display.value=
    document.forms.kpd.display.value + x;
    } else {
    document.forms.kpd.display.value = x;
    };
    };
    
    
    
    };
    }


    Here is the HTML which I am still having problems with:

    Code:
    <html>
    
    <head>
        <title>John's Calculator</title>
        <style type="text/css">
            .rt {text-align:right; font-family: Courier New;}
            .numbttn {text-align: center; background-color: 
            silver; font-family: Courier New; width: 4eM;}
            body {background-color: silver}
            
        </style>
        
        <script type="text/javascript" src="cal.js"></script>
    
    </head>
    
    <body>
    <form name="kpd">
    
    <tr> <TABLE BORDER=4>
    <table border="2" cellpadding="4" cellspacing="0" bordercolor="#000000">
    <tr>
    <td>
    <table border="0" cellpadding="2" cellspacing="0" width="100&#37;">
    <tr>
    <td colspan="4">
    <input type="text" name="calcResults" value="0" size="20" style="text-align: right"></td>
    </tr>
    
    <TR>
    <TD> <INPUT class="numbttn" TYPE="button" NAME="one"   VALUE="1" OnClick="AppendDigit('one')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="two"   VALUE="2" OnCLick="AppendDighit('two')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="three" VALUE="3" OnClick="AppedndDigit('three')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="plus"  VALUE="+" OnClick="AppendDigit('plus')";/></td>
    </tr>
    
    <tr>
    <td> <INPUT class="numbttn" TYPE="button" NAME="four"  VALUE="4" OnClick="AppendDigit('4')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="five"  VALUE="5" OnCLick="AppendDigit('5')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="six"   VALUE="6" OnClick="AppendDigit('6')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="minus" VALUE="-" OnClick="AppendDigit(' - ')";/></td>
    </tr>
    
    
    <tr>
    <td> <INPUT class="numbttn" TYPE="button" NAME="seven" VALUE="7" OnClick="AppendDigit('7')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="eight" VALUE="8" OnCLick="AppendDigit('8')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="nine"  VALUE="9" OnClick="AppendDigit('9')";/></td>
    <td> <INPUT class="numbttn" TYPE="button" NAME="times" VALUE="x" OnClick="AppendDigit(' * ')";/></td>
    </tr>
    
     <tr> 
    <td> <Input class="numbttn" TYPE="button" NAME="clear" VALUE="c" OnClick="AppendDigit('')";/></td>
    <td> <Input class="numbttn" TYPE="button" NAME="zero"  VALUE="0" OnClick="AppendDigit('0')";/></td>
    <td> <Input class="numbttn" TYPE="button" NAME="DoIt"  VALUE="=" OnClick="AppendDigit('=')";/></td>
    <td> <Input  class="numbttn"TYPE="button" NAME="div"   VALUE="/" OnClick="AppendDigit(' / ');"/></td>
    </tr>
    </table>
    </form>
    </body>
    
    </html>

  6. #6
    Join Date
    Apr 2009
    Posts
    96
    HTML Code:
    <html>
    <head>
    <title>john's calculator</title>
    <style type="text/css">
    .rt {text-align:right; font-family: courier new;}
    .numbttn {text-align: center; background-color: 
    silver; font-family: courier new; width: 4em;}
    body {background-color: silver}
    </style>
    <script language="javascript" type="text/javascript">
    // cal applet- john falco- 20 october 2010
    var hasdecimal;
    function cleartb() {
    document.getElementById("calcresults").value ="0";
    }
    // ------------------------------------------------------------------------------------------
    // function appenddigit(x) -- concatenate the value in x to the texxt in
    // document.forms.kpd.display.value
    // ------------------------------------------------------------------------------------------
    function appenddigit(x) {
    document.getElementById("calcresults").value = document.getElementById("calcresults").value + x;
    }
    </script>
    </head>
    <body>
    <form name="kpd">
    <table cellpadding="4" style="border-top:2px solid #000000;border-left:2px solid #000000;border-bottom:2px solid #000000;border-right:2px solid #000000;">
    <tr>
    <td colspan="4">
    <input type="text" name="calcresults" id="calcresults" value="0" size="36" style="text-align: right">
    </td>
    </tr>
    <tr>
    <td> <input class="numbttn" type="button" name="one"   value="1" onclick="appenddigit('1')"/></td>
    <td> <input class="numbttn" type="button" name="two"   value="2" onclick="appenddigit('2')"/></td>
    <td> <input class="numbttn" type="button" name="three" value="3" onclick="appenddigit('3')"/></td>
    <td> <input class="numbttn" type="button" name="plus"  value="+" onclick="appenddigit('+')"/></td>
    </tr>
    <tr>
    <td> <input class="numbttn" type="button" name="four"  value="4" onclick="appenddigit('4')"/></td>
    <td> <input class="numbttn" type="button" name="five"  value="5" onclick="appenddigit('5')"/></td>
    <td> <input class="numbttn" type="button" name="six"   value="6" onclick="appenddigit('6')"/></td>
    <td> <input class="numbttn" type="button" name="minus" value="-" onclick="appenddigit('-')"/></td>
    </tr>
    <tr>
    <td> <input class="numbttn" type="button" name="seven" value="7" onclick="appenddigit('7')"/></td>
    <td> <input class="numbttn" type="button" name="eight" value="8" onclick="appenddigit('8')"/></td>
    <td> <input class="numbttn" type="button" name="nine"  value="9" onclick="appenddigit('9')"/></td>
    <td> <input class="numbttn" type="button" name="times" value="x" onclick="appenddigit('*')"/></td>
    </tr>
    <tr> 
    <td> <input class="numbttn" type="button" name="clear" value="c" onclick="cleartb()"/></td>
    <td> <input class="numbttn" type="button" name="zero" value="0" onclick="appenddigit('0')"/></td>
    <td> <input class="numbttn" type="button" name="doit" value="=" onclick=""/></td>
    <td> <input class="numbttn" type="button" name="div" value="/" onclick="appenddigit('/');"/></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    Last edited by Rufe0; 10-21-2010 at 04:06 AM.

  7. #7
    Join Date
    Oct 2010
    Posts
    4
    Thanx for these tips but he wants us to use the if else functions.

  8. #8
    Join Date
    Apr 2009
    Posts
    96
    So use an if else in it somewhere

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles