Java Help:使用类

时间:2009-08-09 01:19:26

标签: java

现在我已经阅读了这篇文章,但我只是被困住了。任何帮助赞赏。我正在寻找提示。代码编译并运行正常,但我不认为变量存储在employee类中,我不确定我是否理解我对构造函数的使用权。

要求: 我已经完成了:

  

检查值以确保它们是   正数。

     

输入停止作为   员工姓名结束计划。

遇到麻烦

  

使用类存储

     
      
  • 名称
  •   
  • 小时费率
  •   
  • 工作时间
  •   
     

使用构造函数初始化员工信息   和该类中的方法   计算每周工资。

4 个答案:

答案 0 :(得分:2)

以下是一些提示:

  

使用构造函数初始化   员工信息

查看提供的代码,有一个构造函数不带参数,并将对象的字段初始化为零。

也许“初始化员工信息的构造函数”意味着构造函数应该能够接受Employee对象应该初始化其字段的值。

The Java TutorialsProviding Constructors for Your Classes上有一个页面,其中包含有助于创建此类构造函数的示例。

  

...和该类中的方法   计算每周工资

这似乎表明,应该有一种方法只对自身可见,而其他人无法获得,以便计算weeklyPay字段的值。

再次,从The Java Tutorials开始,Controlling Access to Members of a Class讨论了如何更改方法的可见性。

阅读作业的内容,好像看一下Lesson: Classes and ObjectsThe Java Tutorials可能是有用的,因为它提供了一些关于面向对象编程概念的好例子和解释

答案 1 :(得分:2)

除了你没有使用构造函数来设置你的变量这一事实,正如其他人所提到的那样。 setter方法正在执行no-ops。这就是为什么你没有得到你期望的结果。您将本地var设置为自身而不是成员var。

您需要更改本地var名称,更改成员var名称或更改setter以使用'this'关键字

 public void sethoursWorked(float hoursWorked)
 { 
    this.hoursWorked = hoursWorked;
 } 

答案 2 :(得分:1)

您会注意到,当您被要求制作计算每周工资的方法时,系统会要求您创建一个存储名称,小时费率和工作小时数的课程。换句话说,您不应该有weeklyPay实例变量。每当用户要求每周付费时(通过getWeeklyPay()方法),您直接计算并返回它,而不将其存储在实例变量中。

然后,为了实际使用该结果,您需要更改它:

Employee.getweeklyPay();// Calculate weeklyPay ( hoursWorked * hourlyRate )
weeklyPay = ( hoursWorked * hourlyRate );

这样的事情:

weeklyPay = employee.getWeeklyPay();

如果您实际上没有将方法的结果分配给某个变量,则无法使用结果。

答案 3 :(得分:0)

OP的最终答案:

/** Payroll3.java | Due 8/09/09 
** IT 2015 Java Programming | lazfsh | Axia College - University of Phoenix */

import java.util.Scanner;// Import and use scanner

public class Payroll3 {// main method begins
   public static void main( String args[] ) {

   // Initialize
   boolean stop = false;// Variable for exit procedure
   String endProgram = "";// Variable to explain exit procedures blank 1st time
   int version = 3;// Version number

   // Welcome message   
   System.out.printf( 
   "\nWelcome to the Payroll Program v.%d\n", version );//Welcome message      

   while ( stop == false) {// Run program until stop equals true 

      Scanner input = new Scanner( System.in );// new Scanner for CL input
      Employee Employee = new Employee(); // new Employee constructor

      // Prompt for and input name
      System.out.printf( "\nEnter name of the employee%s:", endProgram );
      Employee.setempName(input.nextLine());// Accept input & Store

      if ( Employee.getempName().equals( "stop" )) {// If = end program w/message
         System.out.printf( "\n%s\n%s\n\n", "....", "Thanks for using Payroll Program");
         stop = true;}
      else{// Continue retrieve hourlyRate, hoursWorked & Calculate

         do {// Prompt for and input hourlyRate
            System.out.printf( "\n%s", "Enter hourly rate: $" );
            Employee.sethourlyRate(input.nextFloat());// Accept input & Store
            if ( Employee.gethourlyRate() < 1 ) {// Show error for negative #
               System.out.printf( "%s", "Enter a positive number.");}
            else;{}// Continue
            } while ( Employee.gethourlyRate() < 1 );// End loop if positive number recieved

         do {// Prompt for and input hoursWorked
            System.out.printf( "\n%s", "Enter number of hours worked:" );
            Employee.sethoursWorked(input.nextFloat());// Accept input & Store
            if ( Employee.gethoursWorked() < 1 ) {// Show error for negative #
               System.out.printf( "%s", "Enter a positive number.");}
            else;{}// Continue
            } while ( Employee.gethoursWorked() < 1 );// End loop if positive number recieved

         // Display formatted results
         System.out.printf( "\n%s's weekly pay is $%,.2f\nHourly rate ($%,.2f) multiplied by hours worked (%.0f)\n\n...Ready for next employee.\n\n",
         Employee.getempName(), Employee.getweeklyPay(), Employee.gethourlyRate(), Employee.gethoursWorked() );

// Debug Line         Employee.showVariables();         

         }// end retrieve hourlyRate, hoursWorked & Calculate

         endProgram = ( ", \n(Or type \"stop\" to end the program)" );//explain exit procedure on second empName prompt

      }// ends program when stop equals true

   }// end method main

}//end class Payroll3

员工类

/** Employee Class | Initializes and storeds data for employee
      Also provides method to calculate weekly pay.
*/

// Import statements
import java.util.Scanner;// Import and use scanner


public class Employee {//Begin Employee class
   Scanner input = new Scanner( System.in );// new Scanner for CL input

   // Declare instance variables
   String empName; // Declare name as string
   float hourlyRate; // Declare hourlyRate as float
   float hoursWorked; // Declare hoursWorked as float

   // constructor initializes employee information
   public Employee() { // Initialize (clear) instance variables here
      empName = "";
      hourlyRate = 0; 
      hoursWorked = 0; 
      } // end constructor

   // Begin methods
   public void setempName(String empName) {// public method to set the employee name 
      this.empName = empName;}// end method setempName

   public String getempName() {// public method to get the employee name
      return empName;}// end method getempName

   public void sethourlyRate(float hourlyRate) {// public method to set the hourly Rate
      this.hourlyRate = hourlyRate;}// end method set hourly Rate

   public float gethourlyRate() {// public method to retrieve the hourly Rate
      return hourlyRate;} // end method get hourly Rate

   public void sethoursWorked(float hoursWorked) {// public method to set the hours Worked
      this.hoursWorked = hoursWorked;} // end method set hours Worked

   public float gethoursWorked() {// public method to retrieve the hours Worked
      return hoursWorked;} // end method get hours Worked

   public float getweeklyPay() {// public method to retrieve weekly Pay
      return ( hoursWorked * hourlyRate );}// end method get weeklyPay

   /* Test line
   public void showVariables(){
   System.out.printf( "empName=%s, hourlyRate=%s, hoursWorked=%s", this.empName, this.hourlyRate, this.hoursWorked );} */

   } // end class Employee