1. Member
Join Date
Sep 2012
Posts
2
Rep Power
0

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 :

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) :

Java 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 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")
{
}

if( sBrand == "Diesel" )
{
}

}

if(sTypeBussines == "B1")
{
if ( sBrand == "Benzin")
{
}
if( sBrand == "Diesel" )
{
}

}

}

if(iWeight < 1000 && iWeight > 501)
{
if (sTypeBussines == "A1")
{
if ( sBrand == "Benzin")
{
}

if ( sBrand == "Diesel" )
{
}
}
if (sTypeBussines == "B1")
{
if ( sBrand == "Benzin")
{
}

if (sBrand == "Diesel")
{
}

}
}

if(iWeight > 1001 && iWeight < 2001)
{
if (sTypeBussines == "A1")
{
if ( sBrand == "Benzin")
{
}

if (sBrand == "Diesel")
{
}
}
if (sTypeBussines == "B1")
{
if ( sBrand == "Benzin")
{
}

if (sBrand == "Diesel")
{
}
}
}

if (iWeight > 2000 && iWeight < 2500)
{

}

break;

case 2:

break;

case 3:

break;

}

}
}```

2. ## Re: Road Tax issue

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

Java 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.

3. Member
Join Date
Sep 2012
Location
Navi Mumbai
Posts
30
Rep Power
0

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:
Java Code:
`if("A1".equals(sTypeBussines))`

4. Member
Join Date
Sep 2012
Posts
2
Rep Power
0

Thank you so much, I did not know about the equals method, I will use it from now on. Also I realized the issue of using only smaller or larger signs. The code is now functional, thank you again.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•