I\'m required to write a DomainName class that encapsulates the concept of a dom
ID: 3786344 • Letter: I
Question
I'm required to write a DomainName class that encapsulates the concept of a domain name, assuming a domain name has a single attribute: the domain name itself (i.e. www.yahoo.com (Links to an external site.)). Include the following:
1. Constructor: accepts the domain name as an argument.
2. getDomain: an accessor method for the domain name field.
3. setDomain: a mutator method for the domain name field.
4. prefix: a method returning whether or not the domain name starts with www.
5. extension: a method returning the extension of the domain name (i.e. the letters after teh last dot, like com, gov, or edu; if there is no dot in the domain name, then you should return "unknown")
6. name: a method returning the name itself (which will be the characters between www and the extension; for instance, yahoo if the domain is www.yahoo.com --if there are fewer than two dots in the domain name, then your method should return "unknown").
This program should demonstrate teh DomainName class by prompting the user for the domain name and displays the output of the last three methods. Can you fix the following errors, and label parts of the program with comments so I can have a better understanding on how this program is constructed? Thank you.
public class LeavinesDomainName
{
private String domain_name;
public LeavinesDomainName()
{
domain_name = "";
}
public LeavinesDomainName(String name)
{
domain_name = name;
}
public void SetDomain(String name)
{
domain_name = name;
}
public String GetDomain()
{
return domain_name;
}
public String toString()
{
return domain_name;
}
public boolean equals(LeavinesDomainName D)
{
return domain_name.equalsIgnoreCase(D.GetDomain());
}
public boolean is_domain_starts_with_www()
{
return domain_name.startsWith("www");
}
public String get_extension_of_domain_name()
{
int index= domain_name.lastIndexOf('.');
if(index>0)
return domain_name.substring(index);
return "unknown";
}
public String name_of_domain()
{
int first_index= domain_name.indexOf('.');
int last_index= domain_name.indexOf('.');
if(first_index==last_index)
return "unknown";
return domain_name.substring(first_index=1, last_index);
}
}
public class DomainTest //ERROR on DomainTest
{
public static void main(String[] args)
{
LeavinesDomainName yahoo_D = new LeavinesDomainName("www.yahoo.com");
LeavinesDomainName bing_D = new LeavinesDomainName("www.bing.com");
LeavinesDomainName wrong_D = new LeavinesDomainName("ww.com");
System.out.println("Is Yahoo Domain equal to RectifyGaming Domain ?" + yahoo_D.equals(bing_D));;
System.out.println("Does Yahoo Domain start with www ?" + yahoo_D.get_extension_of_domain_name());
System.out.println("Yahoo Domain name is " + yahoo_D.name_of_domain());;
System.out.println("Wrong Domain extension is " + wrong_D.get_extension_of_domain_name());;
System.out.println("Wrong Domain name is " + wrong_D.name_of_domain());
}
}
Explanation / Answer
Hi, I have fixed all the issue.
Please let me knwo in case of any issue.
public class LeavinesDomainName
{
// instance variable
private String domain_name;
// default constructor that initializes domain name with empty name
public LeavinesDomainName()
{
domain_name = "";
}
// parameterized constructor
public LeavinesDomainName(String name)
{
domain_name = name;
}
// setter
public void SetDomain(String name)
{
domain_name = name;
}
// getter
public String GetDomain()
{
return domain_name;
}
// to string
public String toString()
{
return domain_name;
}
// equal method that compare two Doman Name Object
public boolean equals(LeavinesDomainName D)
{
return domain_name.equalsIgnoreCase(D.GetDomain());
}
// function to check whether domain name starts with "www"
public boolean is_domain_starts_with_www()
{
return domain_name.startsWith("www");
}
// function to return extension
public String get_extension_of_domain_name()
{
int index= domain_name.lastIndexOf('.');
if(index>0)
return domain_name.substring(index+1);
return "unknown";
}
public String name_of_domain()
{
int first_index= domain_name.indexOf('.');
int last_index= domain_name.lastIndexOf('.');
if(first_index==-1 || last_index == -1 || first_index==last_index)
return "unknown";
return domain_name.substring(first_index+1, last_index);
}
}
public class DomainTest //ERROR on DomainTest
{
public static void main(String[] args)
{
LeavinesDomainName yahoo_D = new LeavinesDomainName("www.yahoo.com");
LeavinesDomainName bing_D = new LeavinesDomainName("www.bing.com");
LeavinesDomainName wrong_D = new LeavinesDomainName("ww.com");
System.out.println("Is Yahoo Domain equal to RectifyGaming Domain ?" + yahoo_D.equals(bing_D));;
System.out.println("Does Yahoo Domain start with www ?" + yahoo_D.is_domain_starts_with_www());
System.out.println("Yahoo Domain name is " + yahoo_D.name_of_domain());;
System.out.println("Wrong Domain extension is " + wrong_D.get_extension_of_domain_name());;
System.out.println("Wrong Domain name is " + wrong_D.name_of_domain());
}
}
/*
Smaple run:
Is Yahoo Domain equal to RectifyGaming Domain ?false
Does Yahoo Domain start with www ?true
Yahoo Domain name is yahoo
Wrong Domain extension is com
Wrong Domain name is unknown
*/