Solved

Creating a Separate Method to Calculate

Posted on 2004-08-20
5
180 Views
Last Modified: 2006-11-17
How would I go about writing a program that displays a salary of an employee or emplyoyee's and does the calculations in a separate method and returns the result to be displayed?

Very new to Javascript and Expert Exchange.

Thanks for the help
0
Comment
Question by:RgrWalker
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11853278
say you have an html form with several inputs: value1, value2, and answer:

<form name="calc">
 <input type="input" id="value1">
 <input type="input" id="value2">
 =
 <input type="input" id="answer">
 <input type="button" value="do calc" onClick="showCalc()">
</form>

then your script would be:

<script>

function showCalc()
{
  val1 = document.getElementById("value1").value;
  val2 = document.getElementById("value2").value;
  document.getElementById("answer").value = doAdd(val1,val2); // do this if you want to add them
  document.getElementById("answer").value = doMult(val1,val2); // do this if you want to multiply them
}

functino doAdd(val1, val2)
{
 return val1+val2;
}

functino doMult(val1, val2)
{
 return val1*val2;
}

</script>

-Trevor
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11853292
typo on my doAdd and doMult functions. should be:

function doAdd(val1, val2)
{
 return val1+val2;
}

function doMult(val1, val2)
{
 return val1*val2;
}
0
 

Author Comment

by:RgrWalker
ID: 11855998
So if it had to display the weekly salary who earned ## per hour and worked 40 hours and 13 overtime hours at a time and a half wage how would it look?

This is what I've come up with, I'm trying to figure out how I get it to display and calculate in a separate method.

public class Salary
{
public static void main(String[] args)
{
double Wage=25.00;
double numberOfHoursWorked=40;
double OverTimeHoursWorked=13;

double WeeklySalary=OvertimePay+(Wage*numberOfHoursWorked);
double OvertimePay=OverTimeHoursWorked*(Wage*1.5);

System.out.println("WeeklySalary: " +Weekly Salary);
}
}
0
 
LVL 8

Accepted Solution

by:
trevorhartman earned 50 total points
ID: 11856150
public class Salary
{
public static void main(String[] args)
{
double Wage=25.00;
double numberOfHoursWorked=40;
double OverTimeHoursWorked=13;

double OvertimePay= CalculateOverTime(Wage, OverTimeHoursWorked);
double WeeklySalary=OvertimePay + CalculateWeeklySalary(Wage, numberOfHoursWorked);

System.out.println("WeeklySalary: " +Weekly Salary);
}

public static double CalculateWeeklySalary (double Wage, double NumberOfHours)
{
  return Wage*NumberOfHours;
}
public static double CalculateOverTime (double Wage, double OverTimeHoursWorked)
{
  return OverTimeHoursWorked*Wage*1.5;
}

}
0
 

Author Comment

by:RgrWalker
ID: 11856222
Worked great, the only change I made were removing the Colan in this line....

System.out.println("WeeklySalary: " +Weekly Salary);

Thank you for your help
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maps stopped work unsure why 7 36
Angular - code works but need to make a change 14 32
Mobile page Scrolling down when refreshing 4 33
Index on a Table 6 15
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question