| RHS => Allan => Basic Programming => Exercises => Mail |
Updated:
02/02/2004 23:36 WebMaster: allanhn@rhs.dk |
The problem
You are to develop a program, which can handle calculation of the price for
a letter depending on it's weight.
The rules for the 6 different
types are as follows:
| weight | <= | 50 g | 4.25 DKK | |||
| 50 g | < | weight | <= | 100 g | 5.50 DKK | |
| 100 g | < | weight | <= | 250 g | 6.50 DKK | |
| 250 g | < | weight | <= | 500 g | 20.00 DKK | |
| 500 g | < | weight | <= | 1000 g | 28.00 DKK | |
| 1000 g | < | weight | Not a letter! |
For a start we define a simple version of the class Mail:
Datafields:
None!Constructors
Mail() { }Methods calculating the price:
public int findPrice(int weight)
Question 1: Worker class
Create a class Mail with a constructor and the
method:
double findPrice(int weight) {
.....
}
The method contains a lot of nested if-else sentences.
Compile it.
Tip: Very similar to Average & IfElse class !!
Question 2: Application class
Now, write an application class MailApp using an
object of the class Mail with the usual main method.
The MailApp class must construct a Mail object, read
in the weight in gram, use the method findPrice() and
print out the price.