无法正确调用Java中的void函数以将输出正确输出到控制台。

huangapple go评论66阅读模式
英文:

Unable to correcty call a void function in Java to output to the console

问题

以下是您提供的代码的翻译部分:

Employee.java

public class Employee {
    private String _first_name;
    private String _last_name;
    private double _monthly_salary;

    public Employee(String first_name, String last_name, double monthlySalary) {
        setFirstName(first_name);
        setLastName(last_name);
        setMonthlySalary(monthlySalary);
    }

    public String getFirstName() {
        return _first_name;
    }

    public void setFirstName(String first_name) {
        _first_name = first_name;
    }

    public String getLastName() {
        return _last_name;
    }

    public void setLastName(String lastName) {
        _last_name = lastName;
    }

    public double getMonthlySalary() {
        return _monthly_salary;
    }

    public void setMonthlySalary(double monthlySalary) {
        if (monthlySalary < 0.0) {
            monthlySalary = 0.0;
        }
        _monthly_salary = monthlySalary;
    }

    public void developerInfo() {
        System.out.println("Name:    Erin");
        System.out.println("Course:  Object-Oriented Programming");
        System.out.println("Program: Three \n");
    }
}

EmployeeTest.java

public class EmployeeTest {
    public static void main(String[] args) {
        Employee employee1 = new Employee("John", "Doe", 2875.00);
        Employee employee2 = new Employee("Jane", "Doe", 3150.00);

        System.out.println("Before 20% raise:");
        System.out.printf("Employee 1: %s %s; Yearly Salary: $%.2f%n",
                employee1.getFirstName(), employee1.getLastName(),
                12 * employee1.getMonthlySalary());
        System.out.printf("Employee 2: %s %s; Yearly Salary: $%.2f%n",
                employee2.getFirstName(), employee2.getLastName(),
                12 * employee2.getMonthlySalary());

        employee1.setMonthlySalary(employee1.getMonthlySalary() * 1.2);
        employee2.setMonthlySalary(employee2.getMonthlySalary() * 1.2);

        System.out.println("After 20% raise:");
        System.out.printf("Employee 1: %s %s; Yearly Salary: $%.2f%n",
                employee1.getFirstName(), employee1.getLastName(),
                12 * employee1.getMonthlySalary());
        System.out.printf("Employee 2: %s %s; Yearly Salary: $%.2f%n",
                employee2.getFirstName(), employee2.getLastName(),
                12 * employee2.getMonthlySalary());
    }
}

请注意,开头的 "I have the following class written in the Employee.java script:" 和 "With the corresponding test class written in a separate script called EmployeeTest.java:" 这两句话没有被翻译,因为您要求只返回翻译好的部分。

英文:

I have the following class written in the Employee.java script:

public class Employee {
	//***************************************************************
	//
	//  Developer:         Erin
	//
	//  Program #:         3
	//
	//  File Name:         Program3.java
	//
	//  Course:            Object-Oriented Programming - Java 
	//
	//  Due Date:          
	//
	//  Instructor:        Prof. Fred
	//
	//  Chapter:           3
	//
	//  Description:
	//  Program creates an Employee class that includes three (3) private instances:
        //   1) first name (String)
        //   2) last name (String)
	//   3) monthly salary (double)
	//  Program provides:
	//   a) a constructor that initializes instance variables
	//   b) set and get methods for each instance variable
	//   c) test app named EmployeeTest that demonstrates Employee class capabilities
	//   d) two (2) Employee objs, from which obj&#39;s yearly salary is displayed
	//   e) display of 20% raise for each Employee;s yearly salary
	//
	//***************************************************************
		    private String _first_name;
		    private String _last_name;
		    private double _monthly_salary;

		    public Employee(String first_name, String last_name, double monthlySalary)
		    {
		       setFirstName(first_name);
		       setLastName(last_name);
		        setMonthlySalary(monthlySalary);
		    }
		   
		    public String getFirstName()
		    {
		        return _first_name;
		    }
		    public void setFirstName(String first_name)
		    {
		    	_first_name = first_name;
		    }
		   
		    public String getLastName()
		    {
		        return _last_name;
		    }
		   
		    public void setLastName(String lastName)
		    {
		    	_last_name =lastName;
		    }
		   
		    public double getMonthlySalary()
		    {
		        return _monthly_salary;
		    }
		   
		    public void setMonthlySalary(double monthlySalary)
		    {
		        if(monthlySalary &lt; 0.0)
		        {
		        	monthlySalary = 0.0;
		        }
		       
		        _monthly_salary = monthlySalary;
		    }
	//***************************************************************
	//
	//  Method:       developerInfo
	// 
	//  Description:  The developer information method of the program
	//
	//  Parameters:   None
	//
	//  Returns:      N/A 
	//
	//**************************************************************
    public void developerInfo()
    {
       System.out.println(&quot;Name:    Erin&quot;);
       System.out.println(&quot;Course:  Object-Oriented Programming&quot;);
       System.out.println(&quot;Program: Three \n&quot;);
	}
}

With the corresponding test class written in a separate script called EmployeeTest.java:

public class EmployeeTest {
	public static void main(String[] args) {
		
		**developerInfo();**
		
		Employee employee1 = new Employee(&quot;John&quot;, &quot;Doe&quot;, 2875.00);
		Employee employee2 = new Employee(&quot;Jane&quot;, &quot;Doe&quot;, 3150.00);

		System.out.printf(&quot;%s %n&quot;, &quot;Before 20% raise:&quot;);
		System.out.printf( &quot;Employee 1: %s %s; Yearly Salary: $%.2f\n&quot;,
                employee1.getFirstName(), employee1.getLastName(),
                12 * employee1.getMonthlySalary() );
        System.out.printf( &quot;Employee 2: %s %s; Yearly Salary: $%.2f\n&quot;,
                employee2.getFirstName(), employee2.getLastName(),
                12 * employee2.getMonthlySalary() );
		
		employee1.setMonthlySalary(employee1.getMonthlySalary() * 1.2);
		employee2.setMonthlySalary(employee2.getMonthlySalary() * 1.2);

		System.out.printf(&quot;%s %n&quot;, &quot;After 20% raise:&quot;);
		System.out.printf( &quot;Employee 1: %s %s; Yearly Salary: $%.2f\n&quot;,
                employee1.getFirstName(), employee1.getLastName(),
                12 * employee1.getMonthlySalary() );
        System.out.printf( &quot;Employee 2: %s %s; Yearly Salary: $%.2f\n&quot;,
                employee2.getFirstName(), employee2.getLastName(),
                12 * employee2.getMonthlySalary() );
		
	}
}

I cannot figure out how to call the developerInfo function in order get that to print to the console. Expected output is:

Name: Erin
Course: Object-Oriented Programming
Program: Three

Before 20% raise: 
Employee 1: John Doe; Yearly Salary: $34500.00
Employee 2: Jane Doe; Yearly Salary: $37800.00
After 20% raise: 
Employee 1: John Doe; Yearly Salary: $41400.00
Employee 2: Jane Doe; Yearly Salary: $45360.00

Any insight would be so appreciated! Thank you!

答案1

得分: 2

存在两种可能性。首先,你可以将方法设为静态的:

public static void developerInfo() {
    System.out.println("Name:    Erin");
    System.out.println("Course:  Object-Oriented Programming");
    System.out.println("Program: Three \n");
}

然后你可以使用 Employee.developerInfo() 调用该方法。第二种方法是你可以使用 Employee 类的实例来调用该方法:

employee1.developerInfo();

由于它与雇员本身不直接相关,我会选择第一种选项,或者将该方法外包出去。

英文:

There exists two possibilites. First you can make the method static:

public static void developerInfo() {
    System.out.println(&quot;Name:    Erin&quot;);
    System.out.println(&quot;Course:  Object-Oriented Programming&quot;);
    System.out.println(&quot;Program: Three \n&quot;);
}

Then you can call the method with Employee.developerInfo(). Second you can use an instance of the Employee class and you can call the method:

employee1.developerInfo();

Since it is not directly related to an employee, I would take the first option or outsource the method.

huangapple
  • 本文由 发表于 2020年9月20日 04:25:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63973122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定