In Java, two doubles multiplied together are zero? -
so, i'm having odd issue here. when multiply value user.salary 1.1, reason becomes 0! original user.salary variable fine, confirmed it's supposed system.out.println.
import javax.swing.joptionpane; class employee{ string firstname; string lastname; double salary; employee (string firstname, string lastname, double salary){ this.firstname = firstname; this.lastname = lastname; this.salary = salary; } public string getfirstname() { homecoming firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public string getlastname() { homecoming lastname; } public void setlastname(string lastname) { this.lastname = lastname; } public double getsalary() { homecoming salary; } public void setsalary(double salary) { this.salary = salary; } } class namedialog{ public static void main( string[] args ) { string firstname = joptionpane.showinputdialog("what first name?"); string lastname = joptionpane.showinputdialog("what lastly name?"); string salarystring = joptionpane.showinputdialog("what salary?"); double salary = double.parsedouble(salarystring); employee user = new employee (firstname, lastname, salary); string message = string.format("hello, %s %s.", user.firstname, user.lastname); joptionpane.showmessagedialog(null, message); double raise = (user.salary)*1.1; joptionpane.showmessagedialog(null, "congratulations, have received raise! salary "+raise); } }
there seems problem in constructor:
employee (string firstname, string lastname, double salary){ this.firstname = firstname; this.lastname = lastname; this.salary = salary; //you assign salary itself, since //parameter salary (with caps) }
that way salary
not set , remains default value 0
. rest of issue maths. changing line this
this.salary = salary
will solve issue, tough suggest utilize java's naming convention , name parameters starting in lowercase, newsalary
example.
java double
No comments:
Post a Comment