Results 1 to 4 of 4

Thread: Road Tax issue

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

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

    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 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;
    
                }
        
    
    
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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. #3
    suyog53 is offline Member
    Join Date
    Sep 2012
    Location
    Navi Mumbai
    Posts
    30
    Rep Power
    0

    Default Re: Road Tax issue

    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. #4
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default Re: Road Tax issue

    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.

Similar Threads

  1. Access Road 0.7
    By java software in forum Java Software
    Replies: 0
    Last Post: 03-18-2012, 06:13 PM
  2. GUI Issue
    By Nakinsige in forum New To Java
    Replies: 1
    Last Post: 02-22-2012, 11:17 AM
  3. RMI issue
    By yan1205 in forum Networking
    Replies: 0
    Last Post: 02-07-2012, 08:57 AM
  4. Regular Expression issue and setName() method issue
    By geforce in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 04:33 AM
  5. JSF issue
    By premjo in forum New To Java
    Replies: 0
    Last Post: 02-14-2010, 03:19 PM

Posting Permissions

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