This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calculating commission

 
john corsic
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create a program that will calculate real estate commission. The user must enter a sale price and a property code. There are 3 property codes (R,M,C) and each one represents a certain commission rate. I've been trying to figure it out, but i must be missing something. Take a look and let me know:

// RealEstateCommission.java: Calculates real estate commissions
import javax.swing.JOptionPane;

public class Calculator{
/** Main method */
public static void main(String[] args) {
double salePrice;
char propertyType = ('R', 'M', 'C');
double commissionRate;


// Enter sale price
String salePriceString = JOptionPane.showInputDialog (null,
"Enter sale price",
"Calculator Input", JOptionPane.QUESTION_MESSAGE);

salePrice = Double.parseDouble(salePriceString);

// Enter property code (residential, multidwelling or commercial)
String propertyTypeString = JOptionPane.showInputDialog (null,
"Enter property code (residential (R), multidwelling (M) or commercial(C))",
"Calculator Input", JOptionPane.QUESTION_MESSAGE);




if (propertyType == 'R')
commissionRate = 0.070;
else if (propertyType == 'M')
commissionRate = 0.060;
else if (propertyType == 'C')
commissionRate = 0.035;
else{
JOptionPane.showMessageDialog (null,
"Error: Wrong property code, please try again",
"Calculator", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}


// Calculate commission
double commission = salePrice * commissionRate;

// Format to keep two digits after the decimal point
commission = (int)(commission * 100) / 100.0;


// Show results
JOptionPane.showMessageDialog(null,
"the commission is" + commission,
"Calculator Output",
JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A String and a char are different types; comparing a String and a char using "==" will give you a compilation error about "incompatible types."

Your choices are to either convert the input String to a char, and then use == for the comparison, or to use double-quoted Strings for the rate codes instead, and use the equals() method of String for the comparison. I'd recommend the latter, in case there's ever a multicharacter rate code.

 
john corsic
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ernest for the help, I did what you said, but it still doesn't work. Here is what it looks like now and i've also included the error message at the end.


// RealEstateCommission.java: Calculates real estate commissions
import javax.swing.JOptionPane;

public class Calculator{
/** Main method */
public static void main(String[] args) {
double salePrice;
char propertyType = ('R', 'M', 'C');
double commissionRate;


// Enter sale price
String salePriceString = JOptionPane.showInputDialog (null,
"Enter sale price",
"Calculator Input", JOptionPane.QUESTION_MESSAGE);

salePrice = Double.parseDouble(salePriceString);

// Enter property code (residential, multidwelling or commercial)
String propertyTypeString = JOptionPane.showInputDialog (null,
"Enter property code (residential (R), multidwelling (M) or commercial(C))",
"Calculator", JOptionPane.QUESTION_MESSAGE);




if (propertyType.equals ("R"))
commissionRate = 0.070;
else if (propertyType.equals ("M"))
commissionRate = 0.060;
else if (propertyType.equals ("C"))
commissionRate = 0.035;
else{
JOptionPane.showMessageDialog (null,
"Error: Wrong property code, please try again",
"Calculator Output", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}


// Calculate commission
double commission = salePrice * commissionRate;

// Format to keep two digits after the decimal point
commission = (int)(commission * 100) / 100.0;


// Show results
JOptionPane.showMessageDialog(null,
"the commission is" + commission,
"Calculator Output",
JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

}


C:\JAVA\Assignment2_1tests.java:8: ')' expected
char propertyType = ('R', 'M', 'C');
^
1 error

Tool completed with exit code 1
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by john corsic:
I'm trying to create a program that will calculate real estate commission. The user must enter a sale price and a property code. There are 3 property codes (R,M,C) and each one represents a certain commission rate. I've been trying to figure it out, but i must be missing something. Take a look and let me know:
...

So what problems did you encounter with this code? Did it compile? It looks like EFH took some time to find out that it doesn't. Once you've followed his suggestions above, please post any other compiler errors you get. If it compiles fine, then you should explain what happens when you run the program, especially if it does something unexpected. The more details like this that you can provide, the more quickly we will be able to answer your questions.

Keep Coding!

Layne
 
Henry Wong
author
Marshal
Pie
Posts: 21381
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C:\JAVA\Assignment2_1tests.java:8: ')' expected
char propertyType = ('R', 'M', 'C');
^
1 error


The propertyType variable is a character variable. To assign it, you need something that is a character -- which the expression on the right side of the equals sign clearly is not.

Henry
 
john corsic
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What wuuld you suggest?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest eliminating the propertyType variable altogether since I don't think you use it for anything; the tests you're running on "propertyType," you actually really intend to run on "propertyTypeString," right?
 
john corsic
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's what i intended.
 
john corsic
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how would i go about doing this?
Sorry for all the questinons, I've just gotten into java and am a little confused
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just delete the declaration of "propertyType", then change "propertyType" to propertyTypeString everywhere. It's likely there are more compiler errors to be found after this one is fixed: try to attack them one at a time, reading the messages carefully.

Here's a suggestion for the next program: write a few lines. Compile it. If it doesn't compile, fix it. Then test it, somehow or other, and make sure it does what you expect.

Then when it works, add a few more lines, and repeat the cycle. This kind of iterative approach is much easier than the "build it, push it off a cliff, see if it flies" approach.
 
john corsic
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C:\JAVA\Calculator.java:42: variable commissionRate might not have been initialized
double commission = salePrice * commissionRate;
^
1 error

Tool completed with exit code 1

I did that and this is the error i get.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so, as I said, read the error message. It says that "commissionRate" might not have been initialized. That means that at this line of the program, it's possible that commisionRate has never had a value assigned to it. In Java, it's illegal to use a variable that's never been assigned to. So look at the code, and make sure that, whatever happens, commissionRate gets a value before this line of code runs.

Note that the Java compiler doesn't know that System.exit() never returns.
 
john corsic
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Ernest for all the help. I finally figured it out thanks to your guideance.

Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic