Your driver class should contain the following main method: public static void m
ID: 3624207 • Letter: Y
Question
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
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
IpAddress.java
Implement an IpAddress class that stores an IP address as a dotted-decimal string and as four octet int’s. (8 points)
Implemet the following:
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
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
Explanation / Answer
please rate - thanks
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.