Broatlas

MACAddress

Apr 19th, 2016
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. import java.util.Scanner;
  2. /*    Class MACAddress                            
  3.  *    Purpose:  this class models a networking hardware address - also known as MAC Address
  4.  *    Author:  Robert Taracha
  5.  *    Data:   address:  String (to hold the 12 char address)
  6.  *    Methods:  default constructor - initializes to blank string
  7.  *              initial constructor (String) - intiializes to String parameter as long as it is 12 chars long
  8.  *              initial constructor (MACAddress) - intializes to String in parameter MACAddress object
  9.  *              readAddress (Scanner): boolean - reads a MACAddress from the Scanner object buffer and returns
  10.  *                              boolean if successful
  11.  *              isEqual(MACAddress):boolean - compares the current object string to string in parameter object
  12.  *                              and return boolean true if equal, else returns boolean false
  13.  *              isGreaterThan(MACAddress): boolean - compares for greater than.
  14.  *              toString():String - returns String address
  15.  */
  16.  
  17. public class MACAddress {
  18.     public String getAddress() {
  19.         return address;
  20.     }
  21.  
  22.     private String address;
  23.    
  24.     public MACAddress() {
  25.         address = new String();
  26.     }
  27.    
  28.     public MACAddress(String address) {
  29.         if (address.length() == 12)
  30.             this.address = new String(address);
  31.         else this.address = new String();   // if error, set to empty address
  32.     }
  33.     public MACAddress(MACAddress mac) {
  34.         this.address = new String(mac.address);
  35.     }
  36.    
  37.     public boolean readAddress(Scanner inFile) {
  38.         address = inFile.next();
  39.         if (address.length() == 12)
  40.             return true;
  41.         else {
  42.             address = new String();  // if error, set to empty address
  43.             return false;
  44.         }
  45.     }
  46.    
  47.     public boolean isEqual (MACAddress rhs) {
  48.         return rhs.address.equalsIgnoreCase(this.address);
  49.     }
  50.    
  51.     public boolean isGreaterThan (MACAddress rhs) {
  52.         return (this.address.compareToIgnoreCase(rhs.address) > 0);
  53.     }
  54.    
  55.     public String toString() {
  56.         String out = address.substring(0,2);
  57.         for (int i=2; i<12; i+=2)
  58.             out +=  ":" + address.substring(i, i+2);
  59.         return out;
  60.     }
  61. }
Add Comment
Please, Sign In to add comment