Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Help comparing the first digit of an integer

  1. #1
    Manddd is offline Member
    Join Date
    Mar 2015
    Posts
    23
    Rep Power
    0

    Default Help comparing the first digit of an integer

    Hey guys, so I'm making a program that tracks packages and defines them based on their weight, type of casing, etc... This is all described in my tracking number. So first, I'm asked to create an abstract class for the Package type :

    Java Code:
    package assignment2;
    
    public abstract class Package {
    	protected long trackingNumber;
    	private double weight,cost;
    	
    	public Package(long number, double weight, double cost)
    	{
    		trackingNumber = number;
    		this.weight = weight;
    		this.cost = cost;
    	}
    	abstract void tracking();
    }
    Then I created 4 different child class. Letter ( Which will use the firstDigit method to see whether or not the letter is correctly weighted, etc... So let's say the number is 012345, then it would be accepted as a letter weighted in ounce, otherwise the tracking number will be change), then woodcrate which will do the same but check whether or not it is a wooden crate, etc... so on and so forth. Anyway, I was creating the Letter class and it seemed weird to me. I have this:

    Java Code:
    package assignment2;
    
    public class Letter extends Package {
    	public Letter(long trackingNumber, double weight, double cost)
    	{
    		super(trackingNumber,weight,cost);
    	}
    	@Override
    	public void tracking() {
    	 
    
    	}
    
    	
    }
    However, in the tracking method, I'd have to create it so it only checks the first digit of my number. So I thought of putting it to a string and checking whether or not index 0 is a 0 or 1. However, that seems very inefficient and I don't know if some of you would have an idea on how to create something more.... useful?

    Thank you!! -Marc

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help comparing the first digit of an integer

    putting it to a string and checking whether or not index 0 is a 0 or 1.
    Integer numbers in computers are stored in binary using the 8 bits of bytes. The Integer class has methods to convert the binary value of an int to a String with specified radix/base. The common base for most people is 10.
    Numbers when converted to Strings are not given leading 0s. If you want the value of 0123 to be different from 123 you will need to save the values as Strings, not as ints.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Manddd is offline Member
    Join Date
    Mar 2015
    Posts
    23
    Rep Power
    0

    Default Re: Help comparing the first digit of an integer

    Well, the thing is, wouldn't that create a danger where someone would screw up and create a tracking number that isn't only numbers but would create,let's say, a tracking number that is alphanumerical. So let's say I use the attribute
    Java Code:
     private String trackingNumber
    in order to have a tracking number with either a leading 0 or 1. However, someone could easily tamper with it and create a tracking number like this: 1a2334. no? I mean I could create conditions withing my mutator but it seems long for no reasons. Unless I'm missing something here :/

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Help comparing the first digit of an integer

    Validate the tracking number.

    If there is a rule behind what values are allowed in a model then the model should be validating them and throwing an error.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Help comparing the first digit of an integer

    Even though "trackingnumber" is called a number, it's not a number: it's an identifier. Just like a telephonenumber is not a number but an identifier. You don't caluculate with them. They are Strings. As Tolls said: make it a String, and make sure that your setTrackingNumber(String) method pukes when it's not valid according to your rules.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Help comparing the first digit of an integer

    Quote Originally Posted by SurfMan View Post
    As Tolls said: make it a String
    Poor Norm :(
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Help comparing the first digit of an integer

    I'm actually Norm in disguise...that's why I don't have an avatar, otherwise it would be too obvious.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Help comparing the first digit of an integer

    Quote Originally Posted by Tolls View Post
    I'm actually Norm in disguise...
    Poor Norm :(
    DarrylBurke likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Help comparing the first digit of an integer

    I swear the forum software swapped the names! I had it right! I blame PHP!
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Help comparing the first digit of an integer

    Quote Originally Posted by SurfMan View Post
    I swear the forum software swapped the names! I had it right! I blame PHP!
    Actually, I would believe that.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Help comparing the first digit of an integer

    Quote Originally Posted by gimbal2 View Post
    Poor Norm :(
    I would argue about that, but it's a fair cop, frankly.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Help comparing the first digit of an integer

    Quote Originally Posted by Manddd View Post
    However, someone could easily tamper with it and create a tracking number like this: 1a2334. no? I mean I could create conditions withing my mutator but it seems long for no reasons. Unless I'm missing something here :/
    Forget about conditions. Make an immutable tracking number class that validates the tracking number when it is constructed. Once it is validated, then break it apart and assign various fields or values to internal state variables. Then use getters to get the part of the tracking number you are interested in. In some cases, the getters could be boolean tests (like isFragile, isSpecialDelivery). It depends on how the tracking number is encoded. But it should only need to be validated once.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2013, 07:49 PM
  2. 4 digit decimal integer help appreciated very much
    By JMAsterson in forum New To Java
    Replies: 18
    Last Post: 03-31-2012, 12:41 AM
  3. comparing Graphs and Comparing Matrix
    By jetnor in forum New To Java
    Replies: 0
    Last Post: 03-27-2011, 01:40 AM
  4. Replies: 0
    Last Post: 01-23-2011, 12:37 AM
  5. Comparing types, integer with null
    By Felissa in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 06:32 AM

Posting Permissions

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