In Java Write a DomainName class that encapsulates the conce
In Java 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:
Constructor: accepts the domain name as an argument.//
getDomain: an accessor method for the domain name field.//
setDomain: a mutator method for the domain name field. //
prefix: a method returning whether or not the domain name starts with www. //
extension: a method 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 \"unknown\")
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- (Links to an external site.)--if there are fewer than two dots in the domain name, then your method should return \"unknown\")
. Write a program that demonstrates the DomainName class by prompting the user for the domain name and displays the output of the last three methods.
NOTE: The DomainName class is a new class within your project. This does not affect the name of your Java Project above
Solution
// DomainName.java
public class DomainName {
   private String domainName;
    private String myPrefix;
    private String myExtension;
    private String myName;
   
    public DomainName(String domainName)
    {
        this.domainName = domainName;
        setDomainValues(domainName);
    }
   
    public void setDomainValues(String domainName)
    {
        String[] parts = domainName.split(\"\\\\.\");
       
        /*
        * Question states that if there are fewer than two dots then
        * name is unknown. So with this there should always be three parts
        * prefix name and extension
        * This code was handling cases like
        * yahoo
        * www.yahoo
        * yahoo.com
        * www.yahoo.com
        * Please uncomment if that is expected behaviour
        *
        if (parts.length == 1)
        {
            this.myName = parts[0];
            return;
        }
        if(parts.length == 2)
        {
            if (domainName.startsWith(\"www\"))
            {
                this.myPrefix = parts[0];
                this.myName = parts[1];
            }
            else
            {
                this.myName = parts[0];
                this.myExtension = parts[1];
            }
            return;
        }
        */
        if(parts.length == 3)
        {
            this.myPrefix = parts[0];
            this.myName = parts[1];
            this.myExtension = parts[2];
        }
        else
        {
            this.myPrefix = \"\";
            this.myExtension = \"unknown\";
            this.myName = \"unknown\";
        }
    }
   
    public String getDomain()
    {
        return this.domainName;
    }
   
    public void setDomain(String domainName)
    {
        this.domainName = domainName;
        setDomainValues(domainName);
    }
   
    public boolean prefix()
    {
        return (myPrefix != null && myPrefix.equals(\"www\"));
    }
   
    public String extension()
    {
        if ((myExtension != null) && (!myExtension.isEmpty()))
        {
            return myExtension;
        }
        return \"unkown\";
    }
   
    public String name()
    {
        return myName;
    }
 }
// DomainNameTest.java
import java.util.Scanner;
 public class DomainNameTest {
   public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print(\"Please enter a domain name: \");
        String domainName = sc.next();
       
        DomainName dm = new DomainName(domainName);
        System.out.println(\"Does domain starts with \'www\': \" + dm.prefix());
        System.out.println(\"Name of the domain: \" + dm.name());
        System.out.println(\"Extension of domain: \" + dm.extension());
    }
 }
/*
Sample runs
Please enter a domain name: www.yahoo.com
 Does domain starts with \'www\': true
 Name of the domain: yahoo
 Extension of domain: com
Please enter a domain name: yahoo
 Does domain starts with \'www\': false
 Name of the domain: unknown
 Extension of domain: unknown
Please enter a domain name: www.yahoo
 Does domain starts with \'www\': false
 Name of the domain: unknown
 Extension of domain: unknown
Please enter a domain name: yahoo.com
 Does domain starts with \'www\': false
 Name of the domain: unknown
 Extension of domain: unknown
*/



