JAVA Write a program that demonstrates the DomainName class
JAVA: 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.
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.
Solution
Hi,
Please see below the Java Class. Please comment for any queries\\feedbacks.
Thanks,
Anita
DomainName.java
import java.util.Scanner;
public class DomainName {
private String domain;
public String getDomain() {
return domain;
}
//Mutator method for domain
public void setDomain(String domain) {
this.domain = domain;
}
//Accessor method for domain
public DomainName(String domain) {
this.domain =domain;
}
//a method returning whether or not the domain name starts with www.
public boolean prefix(){
boolean retVal=false;
//checking if the domain name starts with www.
if(this.domain.startsWith(\"www.\", 0)){
retVal =true;
}
else{
retVal= false;
}
return retVal;
}
// a method returning the extension of the domain name
public String extension(){
String extension= \"\";
String [] domArr = this.getDomain().split(\"[.]\");
int length= domArr.length;
if(null== domArr || domArr.length ==0){
extension =\"unknown\";
}
else if(domArr.length>2){
extension = domArr[length-1];
}
return extension;
}
//a method returning the name itself
public String name(){
String name= \"\";
String extension =this.extension();
name =this.getDomain().replaceFirst(\"www.\", \"\");
name =name.replaceFirst(\"[.]\"+extension, \"\");
String [] domArr = this.getDomain().split(\"[.]\");
if(null== domArr || domArr.length ==0){
name =\"unknown\";
}
return name;
}
//main method
public static void main(String [] args){
Scanner scan = new Scanner(System.in);
System.out.println(\"Please Enter the domain name: \");
String domainName = scan.nextLine();
//creating domain object
DomainName domain1= new DomainName(domainName);
System.out.println(\"Does your Domain name contain \\\"www.\\\" :\"+domain1.prefix());
System.out.println(\"Domain Extension is : \"+ domain1.extension());
System.out.println(\"Domain Name is:\"+domain1.name());
}
}
Sample output:
Please Enter the domain name:
www.yahoo.com
Does your Domain name contain \"www.\" :true
Domain Extension is : com
Domain Name is:yahoo


