Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a network function which takes in IP address like 192.168.1.1 and returns

ID: 3826128 • Letter: C

Question

Create a network function which takes in IP address like 192.168.1.1 and returns 192.168.001.001
And another function when user enter aa:bb:cc:dd:ee:ff it should return AA:BB:CC:DD:EE:FF and if the user enters dd:ee:ff it should return 00:00:00:DD:EE:FF Create a network function which takes in IP address like 192.168.1.1 and returns 192.168.001.001
And another function when user enter aa:bb:cc:dd:ee:ff it should return AA:BB:CC:DD:EE:FF and if the user enters dd:ee:ff it should return 00:00:00:DD:EE:FF Create a network function which takes in IP address like 192.168.1.1 and returns 192.168.001.001
And another function when user enter aa:bb:cc:dd:ee:ff it should return AA:BB:CC:DD:EE:FF and if the user enters dd:ee:ff it should return 00:00:00:DD:EE:FF

Explanation / Answer

here is your code in Java below. I have added neccessary comments here.

import java.util.Scanner;

public class NetworkFunction {
   public static void main(String[] args) {
       Scanner s = new Scanner(System.in);

       System.out.print("Enter IP address:");
       String ipAddress = s.next();

       String convertedIPAddress = convertIP(ipAddress);
       System.out.println("Converted IP address:" + convertedIPAddress);//printing converted ip address
       System.out.print("Enter Mac Address1:");
       String macAddress = s.next();
       String convertedMac = convertMac(macAddress);
       System.out.println("Converted MAC address:" + convertedMac);//printing converted mac1
       System.out.print("Enter Mac Address2:");
       String macAddress1 = s.next();
       convertedMac = convertMac(macAddress1);
       System.out.println("Converted MAC address:" + convertedMac);//printing converted mac2
       s.close();
   }

   public static String convertIP(String s) {
       String[] ipArray = s.split("\.");//dot cannot be used directly here .It needs to be escaped with escape character
       String ip;
       for (int i = 0; i < ipArray.length; i++) {
           ip = ipArray[i];
           if (ip.length() < 3) {//appending zeroes if length is less then 3
               int len = ip.length();
               for (int k = 0; k < 3 - len; k++) {
                   ip = "0" + ip;
               }
               ipArray[i] = ip;
           }
       }
       String res="";
       for (int i = 0; i < ipArray.length; i++) {// building the result
           if(i != ipArray.length-1){
               res = res+ipArray[i]+".";
           } else {
               res = res+ipArray[i];
           }
       }
       return res;
   }

   public static String convertMac(String s) {
       String[] macArray = s.split(":");
       if (macArray.length < 6) {// if number of entered terms with colon is less then 6, then appending zeroes to match the format
           for (int i = 0; i < 6 - macArray.length; i++) {
               s = "00:" + s;
           }
       }
       macArray = s.split(":");
       String res="";
       for (int i = 0; i < macArray.length; i++) {//building result and converting to upper case
           if(i != macArray.length-1){
               res = res+macArray[i].toUpperCase()+":";
           } else {
               res = res+macArray[i].toUpperCase();
           }
       }
       return res;
      
   }
}

Sample Output:

Enter IP address:192.168.1.1
Converted IP address:192.168.001.001
Enter Mac Address1:aa:bb:cc:dd:ee:ff
Converted MAC address:AA:BB:CC:DD:EE:FF
Enter Mac Address2:dd:ee:ff
Converted MAC address:00:00:00:DD:EE:FF

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote