Results 1 to 5 of 5
  1. #1
    CyberReaper93's Avatar
    Join Date
    Jan 2017
    Gender
    male
    Posts
    2
    Reputation
    10
    Thanks
    0

    Question Trying to figure out how to code this program correctly

    My program is based on Shipping Charges and what I have to do is tell the user how much tax is due and this will be based on the amount of miles the package travels as well as the weight. Now, I have the code written out, however, the problem I face is that for every 1000 miles traveled the tax goes up. The charges are as seen below:

    *************** Table of Charges ******************
    Rate per 1000
    Weight of Package (Kg) Miles Shipped
    ************************************************** ***
    1 Kg or less $1.70
    Over 1 Kg and <= 5 Kg $2.20
    Over 5 Kg and <= 10 Kg $6.70
    Over 10 Kg $9.80

    Cost will be doubled for distance > 1000 miles

    So, for instance, if I had a package that weighed 1kg and traveled 2000 miles, its should be: 3.40.

    So, my question is how do I have the program determine the tax of the object for every 1000 miles traveled? I thought about using if or switch statements, but that just seemed excessive and there must be a way to tell java for every thousand miles traveled multiply the tax by that number.

    What I have so far:

    Code:
    import java.util.Scanner;
    import java.math.*;
    public class MyShippingCharges3{
    private static double WeightOfPackage;
    private static int MilesShipped;
    private static double tax = 1.7;
    private static double tax1 = 2.2;
    private static double tax2 = 6.7;
    private static double tax3 = 9.8;
    private static double result;
       public static void main(String [] args){
          readData();
          calculations();
          printOutput();
          displayTable();
       }
          
       public static void readData(){
          Scanner sc = new Scanner(System.in);
          System.out.print("Enter weight of package in KG: ");
          WeightOfPackage = sc.nextDouble();
          System.out.print("Enter how many miles the package will travel: ");
          MilesShipped = sc.nextInt();
       }
       
       public static void calculations(){
            if (WeightOfPackage <= 1 && MilesShipped <= 1000){
             result = tax;
                if (MilesShipped > 1000 && MilesShipped < 2000){
                   result = tax * 2;
                   }
             }
            
            else if (WeightOfPackage > 1 && WeightOfPackage <= 5) {
                result = 1000 / MilesShipped * tax1;
            } else if (WeightOfPackage > 5 && WeightOfPackage <= 10) {
                result = 1000 / MilesShipped * tax2;
            } else if (WeightOfPackage > 10) {
                result = 1000 / MilesShipped * tax3;
            }    }
       
       public static void printOutput(){
           System.out.print("Your cost is: " + result);
       
       }
       
       public static void displayTable(){
       
       
       }
       
    }
    The calculations method varies slightly because I was trying different things and for some reason, my first calculation outputs 0.0?

  2. #2
    P4$$1V3's Avatar
    Join Date
    Feb 2017
    Gender
    male
    Posts
    59
    Reputation
    10
    Thanks
    13
    hmm it does look like you are taking a slightly more complicated rout than i would have taken and your syntax is something out of the ordinary... but to be honest there is no correct rout there is only a correct answer. now are you using an ide? if so i would recommend looking into the debugger it can help you out a ton. but for the sake of this answer i will tell you whats wrong. Here's what i did :

    Code:
    import java.util.Scanner;
    
    public class MyShippingCharges {
    	
    	public static void main(String[] args) {
    		
    		//initialize variables (this is done within main because its unnessessary to have so many methods)
    		int milesShipped;
    		double weightOfPackage, tax = 1.7, tax1 = 2.2, tax2 = 6.7, tax3 = 9.8, result;
    		
    		//ask user input
    		Scanner input = new Scanner(System.in);
    		System.out.println("Enter the weight of package in KG :");
    		weightOfPackage = input.nextDouble();
    		System.out.println("Enter distance shipped in miles :");
    		milesShipped = input.nextInt();
    		
    		//first lets check how far the item traveled so we can get our multiplier
    		float multiplier = 0;
    		
    		if (milesShipped <= 1000){
    			multiplier = 1;
    		}else {
    			multiplier = milesShipped / 1000;
    		}
    		
    		//second ill check the weight of the package
    		if (weightOfPackage >= 10){
    			//now multiply the tax by the (multiplier)
    			
    			result = multiplier*tax3;
    			
    		}else if (weightOfPackage >= 5){
    			
    			result = multiplier*tax2;
    		
    		}else if (weightOfPackage >= 1){
    			
    			result = multiplier*tax1;
    		
    		}else {
    			
    			result = multiplier*tax;
    		}
    		System.out.println("Your extimated cost will be : "+result);
    		System.out.println("Thanks for using P4$$1V3's tax calculator!");
    	}	
    }
    Please leave a thanks if this helped you
    Last edited by P4$$1V3; 02-08-2017 at 07:00 PM.

  3. #3
    Mayion's Avatar
    Join Date
    Oct 2012
    Gender
    male
    Location
    Bed
    Posts
    13,504
    Reputation
    4018
    Thanks
    8,372
    My Mood
    Twisted
    Quote Originally Posted by P4$$1V3 View Post
    but for the sake of this answer i will tell you whats wrong
    You haven't told OP what was wrong, nor have you provided information on what differs your version from his'. (An explanation I suppose)

    Not to mention the style you have taken in your code is rather worse than the original's; You have jammed all the code in main, which is not an efficient way to work (unpleasant to look at, harder to modify & is bad on a larger scale)
    I do not use any type of messenger outside of MPGH.
    Inactive but you can reach me through VM/PM.










     

    Donator - 30 August 2013
    Battlefield Minion - 26 October 2013

    Blackshot Minion - 14 January 2014/16 September 2014
    Minecraft Minion - 7 February 2014/16 September 2014
    WarRock Minion - 23 February 2014
    League of Legends Minion - 21 March 2014

    Minion+ - 15 May 2014
    Other Semi-Popular First Person Shooter Minion - 8 August 2014
    CrossFire Minion - 23 October 2014
    Programming Section Minion - 13 November 2014
    Marketplace Minion - 7 December 2014

    Official Middleman - 7 December 2014 - 27 June 2015
    Moderator - 29 December 2014
    Project Blackout Minion - 10 January 2015
    News Force Interviewer - January 2015
    Steam Games Minion - 21 March 2015
    Dragon Nest Minion - 31 March 2015
    Publicist - April 2015 - 21 September 2015
    Global Moderator - 25 August 2015
    Super User - 13 August 2016



  4. #4
    P4$$1V3's Avatar
    Join Date
    Feb 2017
    Gender
    male
    Posts
    59
    Reputation
    10
    Thanks
    13
    Quote Originally Posted by Mayion View Post

    You haven't told OP what was wrong, nor have you provided information on what differs your version from his'. (An explanation I suppose)

    Not to mention the style you have taken in your code is rather worse than the original's; You have jammed all the code in main, which is not an efficient way to work (unpleasant to look at, harder to modify & is bad on a larger scale)
    Oh sorry about that bro, it seemed like op was a beginner and possibly looking for help on homework or something so i just quickly coded up a very simple version of what he wanted. also please explain to me in detail how my rout is inefficient. For a a program this size its impossible to tell any difference in speed / time. also i did leave a few bread crumbs in my code on how it was done but you are right i didnt fix his code instead i just made my own and gave it to him which is pretty stupid.

    also how is this unpleasant to look at? like wtf are you saying op's original code was easier to look at??? lol like who's the judge on how your code looks. as my philosophy / programming teacher in high-school said there is no correct rout there is only a correct answer... so what its all packed in main it works and pretty damn well.
    Last edited by P4$$1V3; 02-10-2017 at 02:35 PM.

  5. #5
    Kappatos's Avatar
    Join Date
    Jun 2015
    Gender
    male
    Posts
    152
    Reputation
    44
    Thanks
    11
    My Mood
    Cool
    "programming teacher in high-school said there is no correct rout there is only a correct answer"

    Thats for starters.
    Why?Example : God object

Similar Threads

  1. [Solved] I need other codes and need to find out how to use this website:Black ops 2
    By TheonechosenCX xbox in forum Call of Duty Black Ops 2 Help
    Replies: 2
    Last Post: 03-23-2015, 02:51 AM
  2. Replies: 4
    Last Post: 08-19-2014, 01:02 AM
  3. [Solved] Trying to figure out how to aim bot
    By mr903tv in forum Call of Duty Ghosts Discussions & Help
    Replies: 10
    Last Post: 08-06-2014, 10:39 PM
  4. [WTS] Just trying to figure out how much an account like this would be worth.
    By Acerix in forum Adventure Quest Worlds (AQW) Selling / Trading / Buying
    Replies: 2
    Last Post: 05-02-2014, 02:34 AM
  5. Trying to figure out some JavaScript code.
    By Corndog in forum General
    Replies: 13
    Last Post: 08-21-2011, 03:39 PM