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

The following problem was already answered but it was incomplete. Is there a com

ID: 3620915 • Letter: T

Question

The following problem was already answered but it was incomplete. Is there a complete solution available? Can some else complete it? Please help

Overall Requirements

Every computer on the Internet has a unique identifying number, called an Internet protocol (IP) address. To contact a computer on the Internet, you send a message to the computer’s IP address. Here are some typical IP addresses:



216.27.6.136

224.0.118.62



There are different formats for displaying IP addresses, but the most common format is the dotted decimal format. The above two IP addresses use the dotted-decimal format. It’s called “dotted” because dots are used to split up the big IP address number into four smaller numbers. It’s called “decimal” because decimal numbers are used (as opposed to binary) for the four smaller numbers.



Each of the four smaller numbers is called an octet because each number represents eight bits (oct means eight). For example, the 216 octet represents 11011000 and the 27 octet represents 00011011.








YourNameProg1.java

Your driver class should contain the following main method:

public static void main(String[] args)

{

IpAddress ip = new IpAddress("216.27.6.136");

System.out.println(ip.getDottedDecimal());

System.out.println(ip.getOctet(4));

System.out.println(ip.getOctet(1));

System.out.println(ip.getOctet(3));

System.out.println(ip.getOctet(2));

} // end main


16

IpAddress.java

Implement an IpAddress class that stores an IP address as a dotted-decimal string and as four octet int’s. (8 points)



You must implement all of the following:



Instance variables: (8 points)

dottedDecimal – a dotted-decimal string. Example value: "216.27.6.136"

firstOctet, secondOctet, thirdOctet, fourthOctet – four int variables that store the octets for an IP address



Constructor: (16 points)

This constructor receives one parameter, a dotted-decimal string. You may assume that the parameter’s value is valid (i.e., no error checking required). The constructor initializes the instance variables with appropriate values. There are many ways to solve the problem of extracting octets from the given dotted-decimal string. We recommend that you use String methods to extract the individual octets as strings, and then use parseInt method calls to convert the octet strings to int’s.



getDottedDecimal method: (12 points)

This is a standard accessor method that simply returns the dottedDecimal instance variable’s value.



getOctet method: (12 points)

This method receives the position of one of the octets (1, 2, 3, or 4) and returns the octet that’s at that position.


56

Upload your page to the Dropbox.

NOTE: Complete your activity and submit it to the Dropbox in eCollege


4

Total Points


80

Sample Session:

When using the main method specified above, your output should be:

216.27.6.136

136

216

6

27

Explanation / Answer

please rate - thanks

import java.util.Scanner;
public class IpDriver
{
public static void main(String[] args)

{

IpAddress ip = new IpAddress("216.27.6.136");

System.out.println(ip.getDottedDecimal());

System.out.println(ip.getOctet(4));

System.out.println(ip.getOctet(1));

System.out.println(ip.getOctet(3));

System.out.println(ip.getOctet(2));

} // end main}
}

----------------------------------------------------------------

class IpAddress
{private int firstOctet, secondOctet, thirdOctet, fourthOctet;

public IpAddress(String a)
{int loc0,loc1;
loc1=a.indexOf('.');
firstOctet=Integer.parseInt(a.substring(0,loc1));
loc0=a.indexOf('.',loc1+1);
secondOctet=Integer.parseInt(a.substring(loc1+1,loc0));
loc1=a.indexOf('.',loc0+1);
thirdOctet=Integer.parseInt(a.substring(loc0+1,loc1));
fourthOctet=Integer.parseInt(a.substring(loc1+1));
}
public String getDottedDecimal()
{String a="",num;
num=Integer.toString(firstOctet);
a=a+num;
a=a+'.';
num=Integer.toString(secondOctet);
a=a+num;
a=a+'.';
num=Integer.toString(thirdOctet);
a=a+num;
a=a+'.';
num=Integer.toString(fourthOctet);
a=a+num;
return a;
}

public int getOctet(int pos)
{switch(pos)
    {case 1: return firstOctet;
    case 2: return secondOctet;
    case 3: return thirdOctet;
    }
    return fourthOctet;
}
}