Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

Having issues with Java return statements and I am not sure where I am going wrong. Some are working while others are not returning. Will post my code in reply. Thanks for the help if anyone is able!

OpenStudy (anonymous):

public class Payroll { private String salesPerson; private double salary; private double sales; private double salesCommission; private double percentBonus; private double totalCompensation; public void setSalesPerson (String name) { salesPerson = name; } public String getSalesPerson() { return salesPerson; } public void setSalary(double sal) { salary = sal; } public double getSalary() { return 75000; } public void setSales(double totalSales) { sales = totalSales; public double getSales() { return sales; } public void setSalesCommission(double salCom) { salesCommission = salCom; } public double getSalesCommission() { return .05; } public double getPercentBonus() { return salesCommission*sales; } public double getTotalCompensation() { return percentBonus+salary; } }

OpenStudy (anonymous):

I am using println statements to show me what is not outputting correctly. Also with the aid of an IDE debugger that is insistent that my returns are not returning. Pulling out what hair I have left.

OpenStudy (anonymous):

Its not because of this code...how are you using the class?

OpenStudy (anonymous):

It is second to my controlling class. Gather two inputs from the user and then it calls this class. I can post the controlling class' code as well.

OpenStudy (anonymous):

import java.util.Scanner; public class annualCompensation { // Program main method begins Java execution public static void main(String[] args) { // Creates scanner object to obtain user input Scanner userInput = new Scanner(System.in); Payroll myPayroll = new Payroll(); // Prompts the user for salesPerson name System.out.print ("Enter Salesperson's Name: "); // Uses new scanner to accept entry String name = userInput.next(); myPayroll.setSalesPerson(name); System.out.println(); //Creates blank line // Prompts the user for salary amount System.out.print ("Enter annual sales: "); // Uses new scanner to accept entry double totalAnnualSales = userInput.nextDouble(); myPayroll.setSales(totalAnnualSales); System.out.println(); //Creates blank line myPayroll.payrollMessage(); } }

OpenStudy (anonymous):

You need to type check the variables you input, since when you input something, its always in string format. Use parse integer, or double etc to get the exact format matching the type you want...

OpenStudy (anonymous):

I agree and have not gotten to that portion of the main method just yet. The program works with the arguments from the main, its only the return statements I have issues with atm. I am not sure why they are not returning their values to the top?

OpenStudy (anonymous):

What exactly is the error shown?

OpenStudy (anonymous):

Its not syntax, it is totally logical. There are no comp errors. When using the debugger when it goes through the methods in the 2nd class the instance variables at the top show a null value even though the math at the bottom works and the variables show population there. Its just not returning to the instance. My sales and salesperson is working just fine and I assume it is because it is user input.

OpenStudy (anonymous):

You ll laugh when you find out that there is a bracket error after public void setSales(double totalSales) { sales = totalSales;

OpenStudy (anonymous):

Done??

OpenStudy (anonymous):

I may not have gotten all of the code pasted in...let me double check. I would totally kick myself if it were just the attack of the curly brace lol

OpenStudy (anonymous):

hehe

OpenStudy (anonymous):

public class Payroll { private String salesPerson; private double salary; private double sales; private double salesCommission; private double percentBonus; private double totalCompensation; public void setSalesPerson (String name) { salesPerson = name; } public String getSalesPerson() { return salesPerson; } public void setSalary(double sal) { salary = sal; } public double getSalary() { return 75000; } public void setSales(double totalSales) { sales = totalSales; } public double getSales() { return sales; } public void setSalesCommission(double salCom) { salesCommission = salCom; } public double getSalesCommission() { return .05; } public double getPercentBonus() { return salesCommission*sales; } public double getTotalCompensation() { return percentBonus+salary; } public void payrollMessage() { System.out.println(getPercentBonus()); System.out.println(getTotalCompensation()); System.out.println(getSalesPerson()); System.out.println(salary); System.out.println(getSales()); System.out.println(salesCommission); // System.out.printf ("The annual salary for %s including" // + " the sales bonus is %.2f.",getSalesPerson(), // getTotalCompensation()); } }

OpenStudy (anonymous):

There it is. I must not have gotten it all copied. :/

OpenStudy (anonymous):

The printlns are just for testing, the commented out section was going to be my output when I got the returns to work.

OpenStudy (anonymous):

Well everything seems fine... You need to check that you are intializing the dependent variables...Thats all i can help.. :)

OpenStudy (anonymous):

LOL well thanks for taking a look at it. This is my second Java programming course and while the reading material is heaps better than the first, I've pulled out all but one hair! I'll check the initialization of the instanced variables. Thanks again.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!