• 09-16-2012, 02:39 PM
bleepblopblorg
Hello, I am really new at anything related to java and I would require some help, thank you in advance.

I have an assignment from school where I have to calculate the tax for a car based on it's type, be it 1,2 or 3, the fuel it uses and it's weight. I did that using a switch for the types then if statements based on the weight and then inside the fuel it uses.

However, my teacher told me there would be a better way to do this, what would that be? Arrays? If so, could you give me an example as to how I could proceed with that? I do not understand arrays well enough to use them for that.

here is the assignment :

Quote:

Make a class, Truck (LastBil), with the following fields: registration number (registreringsnr), year (årgang), brand (mærke), type (type), motor size (motorstørrelse), passenger seat number (antal passagersæder), size capacity (ladstørrelse), Kilometer counter (kmtæller), weight in kilos (vægt i kg), fuel consumption (brændstofforbrug).

For each field set – and get methods are to be created. The constructor must take
the following parameters: registration number, size capacity, kilometer counter. The rest of the fields are to be initialized to either 0 or be blank depending on the data type. Test that it works.

Make a method to be used every time the truck is fueled. It must summarize the fuel consumption, so that you have the total number of liters put on the truck. The method must not return anything, but it must take a number of liters as parameter.

Test that it works

A method is to be created, which can calculate the road tax for a truck. The information about road tax is given in on the next page. Use this table as a guide line. It contains the following information: Vecihle category (Køretøjs kategori) with 1=Varevogn (van), 2=Bil(car) and 3=Bus (bus).Weight in Kilos (Vægt i Kg), A1:Weight tax (Vægtafgift), B1: road tax with payment for mixed business and private usage (Vægtafgift og tillæg for blandet Erhverv/Privat), B2: road tax with payment for private usage (Vægtafgift og tillæg for rent Privat). Payment period (Opkrævningsperiode).

And this is the code I made for it(I didn't finish it because the teacher said I should look for other modalities) :

Code:

```public class Truck {     // instance variables - replace the example below with your own     private int iRegNumber;     private int iYear;     private String sBrand;     private int iType;     private int iMotorSize;     private int iPassNumber;     private int iSizeCap;     private int iKmCounter;     private int iWeight;     private int iFuelConsumpt;     private int iRoadTax;     private String sTypeBussines;         public Truck(int iSetRegNumber, int iSetSizeCap, int iSetKmCounter)     {         iRegNumber = iSetRegNumber;         iSizeCap = iSetSizeCap;         iKmCounter = iSetKmCounter;         iYear = 0;         sBrand = "";         iType = 0;         iMotorSize = 0;         iPassNumber = 0;         iWeight = 0;         iFuelConsumpt = 0;             }         public void vGetFuelCons(int iFuel)     {         iFuelConsumpt = iFuelConsumpt + iFuel;     }         public void vGetRoadTax(String sSetTypeBussines, int iSetYear, int iSetType, String sSetBrand, int iSetMotorSize, int iSetPassNumber, int iSetWeight)     {         sTypeBussines = sSetTypeBussines;         iYear = iSetYear;         sBrand = sSetBrand;         iType = iSetType;         iMotorSize = iSetMotorSize;         iPassNumber = iSetPassNumber;         iWeight = iSetWeight;         switch (iType) {             case 1:                         if(iWeight < 500)                         {                                                   if(sTypeBussines == "A1")                          {                             if ( sBrand == "Benzin")                             {                                 iRoadTax = 850;                             }                                                         if( sBrand == "Diesel" )                             {                                 iRoadTax = 1210;                             }                                                     }                                                     if(sTypeBussines == "B1")                          {                             if ( sBrand == "Benzin")                             {                                 iRoadTax = 1300;                             }                             if( sBrand == "Diesel" )                             {                                 iRoadTax = 1660;                             }                                                     }                                               }                                                               if(iWeight < 1000 && iWeight > 501)                         {                             if (sTypeBussines == "A1")                         {                             if ( sBrand == "Benzin")                             {                                 iRoadTax = 1090;                             }                                                         if ( sBrand == "Diesel" )                             {                                 iRoadTax = 1610;                             }                         }                             if (sTypeBussines == "B1")                         {                             if ( sBrand == "Benzin")                             {                                 iRoadTax = 1540;                             }                                                         if (sBrand == "Diesel")                             {                                 iRoadTax = 2060;                             }                                                 }                       }                                                               if(iWeight > 1001 && iWeight < 2001)                       {                             if (sTypeBussines == "A1")                             {                                 if ( sBrand == "Benzin")                                 {                                     iRoadTax = 1810;                                                                  }                                                                 if (sBrand == "Diesel")                                 {                                     iRoadTax = 2520;                                 }                             }                             if (sTypeBussines == "B1")                             {                                 if ( sBrand == "Benzin")                                 {                                     iRoadTax = 2260;                                 }                                                                 if (sBrand == "Diesel")                                 {                                     iRoadTax = 2970;                                 }                             }                       }                                                                   if (iWeight > 2000 && iWeight < 2500)                       {                                                       }                                                               break;                             case 2:                             break;                             case 3:                             break;             }     } }```
• 09-17-2012, 05:52 AM
Junky
A few issues...
Code:

`if (sTypeBussines == "A1")`
99% of the time comparing objects this way is incorrect. You should use the equals method instead.

Code:

```if(iWeight < 500) if(iWeight < 1000 && iWeight > 501)```
What if the vehicle is exactly 500 or 501?

See how you have a lot of duplicated code. I assume you then repeat the whole lot in case 2 and case 3 as well. There are several ways to simplify your code. One way would be to store the road tax values in a matrix (2D array) and store the fuel type and business type as values (indicies) instead of Strings.
• 09-17-2012, 08:56 AM
suyog53
Quote:

A few issues...
Java Code:
1

if (sTypeBussines == "A1")
99% of the time comparing objects this way is incorrect. You should use the equals method instead.
Replace with:
Code:

`if("A1".equals(sTypeBussines))`
• 09-25-2012, 10:11 AM
bleepblopblorg