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

Write a class encapsulating the concept of a domain name,assuming a domain name

ID: 3547368 • Letter: W

Question

Write a class encapsulating the concept of a domain name,assuming a domain name has a single attribute: the domain name itself (for instance, www.yahoo.com). Include a constructor, the accessors and mutators, and methods toString and equals.Also include the following methods: one returning whether or not the domain name starts with www; another returning the extension of the domain name (i.e., the letters after the last dot, for instance com, gov, or edu; if there is no dot in the domain name, then you should return

Explanation / Answer

import java.util.*;

class Domain
{
private String domain_name;
public Domain()
{
domain_name = "";
}
public Domain(String name)
{
domain_name = name;
}
public void SetDomainName(String name)
{
domain_name = name;
}
public String GetDomainName()
{
return domain_name;
}
public String toString()
{
return domain_name;
}
public boolean equals(Domain D)
{
return domain_name.equalsIgnoreCase(D.GetDomainName());
}
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.lastIndexOf('.');
if(first_index==last_index)
return "unknown";
return domain_name.substring(first_index+1, last_index);
}
}

public class DomainTest
{
public static void main(String[] args)
{
Domain yahoo_D = new Domain("www.yahoo.com");
Domain google_D = new Domain("www.google.com");
Domain wrong_D = new Domain("ww.com");
System.out.println("is Yahoo Domain is equal to Google Domain ?" + yahoo_D.equals(google_D));
System.out.println("is Yahoo Domain is is starts with www ?" + yahoo_D.is_domain_starts_with_www());
System.out.println("Yahoo Domain extension is " + 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("Wrog Domain name is " + wrong_D.name_of_domain());
}
}

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