Welcome to Dream.In.Code
Become a Java Expert!

Join 149,477 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,758 people online right now. Registration is fast and FREE... Join Now!




Need help w/creating a separate method to do calc

 
Reply to this topicStart new topic

Need help w/creating a separate method to do calc

DiggBlu
17 May, 2007 - 06:21 AM
Post #1

New D.I.C Head
*

Joined: 24 Apr, 2007
Posts: 10


My Contributions
I'm trying to write a Java application that contains a method that calculates and displays the weekly salary for an employee who earns $25 an hour, works 40 regular hours and 13 overtime hours, and earns time and one-half(wage * 1.5) for overtime hours worked. I need to create a separate method to do the calculation and return the result to the main() method to be displayed. Here's what I have so far (my compiler returns an error of ';' expected on line 6??? Please help:

import javax.swing.*;
public class Salary

{
public static void main(String[] args)

double hourlyRate = 25.0;
double regularHours = 40.0;
double overtimeHours = 13.0;

public void displayWeeklySalary()
{
weeklySalary = JOptionPane.showMessageDialog(null,
"The employee's weekly salary for " + regularHours +
"regular hours and " + overtimeHours +
"\novertime hours at " + hourlyRate + "per hour is: $"
+ calcSalary(hourlyRate, regularHours, overtimeHours));

}

public static double calcSalary(double regularHours, double overtimeHours, double hourlyRate)
{
return (regularHours * hourlyRate) + (overtimeHours * (1.5 * hourlyRate));

}

}


User is offlineProfile CardPM
+Quote Post

Ellie
RE: Need Help W/creating A Separate Method To Do Calc
17 May, 2007 - 07:45 AM
Post #2

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions
Your code needs a little re-ordering. For example your class variables should be outside any methods. Also, displayWeeklySalary is an instance method, so acts on an instance of your class, so you can't just call it from the main method without instantiating some kind of salary object.

So either like this:
CODE
import javax.swing.*;
public class Salary

{
    double hourlyRate = 25.0;
double regularHours = 40.0;
double overtimeHours = 13.0;
public static void main(String[] args)
{
Salary s = new Salary();

s.displayWeeklySalary();
System.exit(0);

}

public void displayWeeklySalary()
{
//weeklySalary =
JOptionPane.showMessageDialog(null,
"The employee's weekly salary for " + regularHours +
"regular hours and " + overtimeHours +
"\novertime hours at " + hourlyRate + "per hour is: $"
+ calcSalary(hourlyRate, regularHours, overtimeHours));

}

public static double calcSalary(double regularHours, double overtimeHours, double hourlyRate)
{
return (regularHours * hourlyRate) + (overtimeHours * (1.5 * hourlyRate));

}

}


Or like this:
CODE
import javax.swing.*;
class SalaryMethods

{
    double hourlyRate = 25.0;
double regularHours = 40.0;
double overtimeHours = 13.0;


public void displayWeeklySalary()
{
//weeklySalary =
JOptionPane.showMessageDialog(null,
"The employee's weekly salary for " + regularHours +
"regular hours and " + overtimeHours +
"\novertime hours at " + hourlyRate + "per hour is: $"
+ calcSalary(hourlyRate, regularHours, overtimeHours));

}

public static double calcSalary(double regularHours, double overtimeHours, double hourlyRate)
{
return (regularHours * hourlyRate) + (overtimeHours * (1.5 * hourlyRate));

}

}
public class Salary {
    
    public static void main(String[] args)
{
SalaryMethods s = new SalaryMethods();

s.displayWeeklySalary();
System.exit(0);

}
}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:12PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month