0

i want to use epsilon to check if the weight inputed by the user in tolerated the tolerated value can be for small from 15.50 - epsilon till 15.50 + epsilon and for big from 25.25 - epsilon till 25.25 + epsilon

    import java.util.Scanner;

public class ShippedItem {
    private int size; 
    private double weight; 
    public static final int SMALL = 1;
    private static final double SMALL_WEIGHT = 9.25; 
    public static final int BIG = 2;
    private static final double BIG_WEIGHT = 15.75; 
    private static final double EPSILON = 0.01;
    public ShippedItem() {
        this(SMALL, SMALL_WEIGHT);
    }

    
    public ShippedItem(int size, double weight) {
        this.size = size;
        this.weight = weight;
    }
    
    
    public int getSize() {
        return size;
    }
    
    
    public void setSize(int size) {
        this.size = size;
    }
    
    
    public double getWeight() {
        return weight;
    }
    
    
    public void setWeight(double weight) {
        this.weight = weight;
    }


    public boolean isShippedItemAcceptable() {
        boolean result = false;
        
        if(size == SMALL) return Math.abs(SMALL_WEIGHT - weight) <= EPSILON;
         if (size == SMALL) return Math.abs(SMALL_WEIGHT + weight) <= EPSILON;
         else
        if(size == BIG) return Math.abs(BIG_WEIGHT - weight) <= EPSILON;
        if(size == BIG) return Math.abs(BIG_WEIGHT + weight) <= EPSILON;
        return result;
    }
    
     




public static class User {
    
    private Scanner keyboard = new Scanner(System.in);
    

    public int inputInteger() {
        int value = keyboard.nextInt();
        keyboard.nextLine();
        return value;
    }
    

    public int inputInteger(String message) {
        System.out.println(message);
        int value = inputInteger();
        return value;
    }
    
    
    public double inputDouble() {
        double value = keyboard.nextDouble();
        keyboard.nextLine();
        return value;   
    }
    

    public double inputDouble(String message) {
        System.out.println(message);
        double value = inputDouble();
        return value;
    }





    public static void main(String[] args) {
        ShippedItem item = new ShippedItem();
        User user = new User();
    
        int itemSize;
        boolean result;
        double itemWeight;
        int acceptable = 0;
        int unacceptable = 0;
        int size = 0;
    
        
        
        
        System.out.println("enter bag size:");
        System.out.println("1 for regular");
        System.out.println("2 for large");
        Scanner keyboard = new Scanner(System.in);
        itemSize = keyboard.nextInt();

        
    System.out.println("enter weight:");
    itemWeight = keyboard.nextDouble(); 
    item.setWeight(itemWeight);
    result = item.isShippedItemAcceptable();
    System.out.println(result);
    
    System.out.println(item.isShippedItemAcceptable() ? "acceptable" : "unacceptable");
    }
}

}

its only working for the small item but for the large item its outputting unacceptable for all weigths

abuuu
  • 1
  • 1

1 Answers1

0

Check if the absolute difference between the weight and the accepted weight for the size is less than the epsilon.

if(size == SMALL) return Math.abs(SMALL_WEIGHT - weight) <= EPSILON;
if(size == BIG) return Math.abs(BIG_WEIGHT - weight) <= EPSILON;
return false; // maybe throw Exception for invalid size
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • i tried something like this but what i want is to have the user input compared to my values which are then small and big - and + the EPSILON and i dont want to print false i want to print something like acceptable or unacceptable – abuuu Jul 24 '21 at 22:48
  • @abuuu Then just use the result of calling the method. `System.out.println(item.isShippedItemAccepted() ? "acceptable" : "unacceptable");` – Unmitigated Jul 24 '21 at 22:50
  • @ unmitigated thanks that kinda worked but only for small so when i choose large and i input an acceptable weight its also printing unacceptable do you know how can i solve this – abuuu Jul 24 '21 at 23:02
  • @abuuu What did you input? It might just be a floating-point issue. See https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Unmitigated Jul 24 '21 at 23:04
  • i first inputed 2 for big and then in the enter the weight i entered 25.5 which should be accepted for big but it outputed unacceptable – abuuu Jul 24 '21 at 23:06
  • when I choose 1 for small and i enter a weight everything works perfectly but now the problem is in when i choose 2 for the big item – abuuu Jul 24 '21 at 23:08
  • @abuuu I can't reproduce the issue. It works for me: https://ideone.com/XQxODc – Unmitigated Jul 24 '21 at 23:08
  • what i want to do is to choose 1 or 2 for small or big then for small the acceptable weight should be from 15.50 - epsilon till 15.50 + epsilon and for big from 25.25 - epsilon till 25.25 + epsilon other than that will be unacceptable but my problem is only the small item part is wroking and when i choose the big im getting unacceptable fir every weight im entering – abuuu Jul 24 '21 at 23:15
  • @abuuu Did you check the example I provided? I can't reproduce the issue. It works fine here: https://ideone.com/XQxODc – Unmitigated Jul 24 '21 at 23:20
  • @abuuu It is not possible for me to help if you don't provide an example that shows the issue. – Unmitigated Jul 24 '21 at 23:20
  • i will edit the code above so you can take a look – abuuu Jul 24 '21 at 23:23
  • i editted my question now you can check my code – abuuu Jul 24 '21 at 23:34
  • @abuuu You forgot to set the size with `item.setSize(itemSize);` – Unmitigated Jul 25 '21 at 00:24