In: Computer Science
LANGUAGE: JAVA
Create a New Project called YourLastNameDomainName.
Write a DomainName class that encapsulates the concept of a domain name, assuming a domain name has a single attribute: the domain name itself. 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.
ALSO, DO NOT FORGET TO INCLUDE THE FOLLOWING AS A COMMENT SECTION AT THE TOP OF YOUR PROGRAM:
1. Student Name
2. Program Name
3. Short Program Description
4. Due Date
Domain Name Rubric | ||
Criteria | Ratings | Points |
Correct Project Name | 2.0 | |
Constructor Defined Correctly | 2.0 | |
Accessor and Mutator Method Defined Correctly | 2.0 | |
Prefix Method Defined Correctly | 2.0 | |
Extension Method Defined Correctly | 2.0 | |
Name Method Defined Correctly | 2.0 | |
DomainName Class Created Correctly | 2.0 | |
Output Correct | 4.0 | |
Total Possible Points | 20.0 |
// DomainName.java
public class DomainName
{
private String domainName;
public DomainName()
{
domainName = "";
}
public DomainName(String name)
{
domainName = name;
}
public void SetDomain(String name)
{
domainName = name;
}
public String GetDomain()
{
return domainName;
}
public boolean prefix()
{
return
domainName.startsWith("www");
}
public String extension()
{
int index=
domainName.lastIndexOf('.');
if(index>0)
return
domainName.substring(index+1);
return "unknown";
}
public String name()
{
int first=
domainName.indexOf('.');
int last=
domainName.lastIndexOf('.');
if(first==-1 || last == -1 ||
first==last)
return
"unknown";
return
domainName.substring(first+1, last);
}
public static void main(String[] args)
{
DomainName y = new
DomainName("www.bing.com");
System.out.println("Domain
start with www ? " + y.prefix());
System.out.println("Domain
name: " + y.name());;
System.out.println("Domain
extension: " + y.extension());;
}
}
/*
output:
Domain start with www ? true
Domain name: bing
Domain extension: com
*/