Results 1 to 4 of 4
Thread: Road Tax issue
- 09-16-2012, 02:39 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 2
- Rep Power
- 0
Road Tax issue
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 :
And this is the code I made for it(I didn't finish it because the teacher said I should look for other modalities) :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).
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 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 #2
Re: Road Tax issue
A few issues...
99% of the time comparing objects this way is incorrect. You should use the equals method instead.Java Code:if (sTypeBussines == "A1")
What if the vehicle is exactly 500 or 501?Java Code:if(iWeight < 500) if(iWeight < 1000 && iWeight > 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 #3
Member
- Join Date
- Sep 2012
- Location
- Navi Mumbai
- Posts
- 30
- Rep Power
- 0
Re: Road Tax issue
Replace with: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.
Java Code:if("A1".equals(sTypeBussines))
- 09-25-2012, 10:11 AM #4
Member
- Join Date
- Sep 2012
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Access Road 0.7
By java software in forum Java SoftwareReplies: 0Last Post: 03-18-2012, 05:13 PM -
GUI Issue
By Nakinsige in forum New To JavaReplies: 1Last Post: 02-22-2012, 10:17 AM -
RMI issue
By yan1205 in forum NetworkingReplies: 0Last Post: 02-07-2012, 07:57 AM -
Regular Expression issue and setName() method issue
By geforce in forum New To JavaReplies: 2Last Post: 01-30-2012, 03:33 AM -
JSF issue
By premjo in forum New To JavaReplies: 0Last Post: 02-14-2010, 02:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks