In: Computer Science
Write a class encapsulating the concept of a corporate name (for example, IBM), assuming a corporate name has the following attribute: the corporate name. Include a constructor, the accessors and mutators, and methods toString() and equals(). Also include and method that returns a potential domain name by adding a www. at the beginning and .com at the end of the corporate name (for instance, if the corporate name is IBM, that method should return www.ibm.com). Write a client class to test all the methods in your class.
Source code of the program and its working are given below.Comments are also given along with the code for better understanding.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.
Working of the code
Source code
Corporate Class
public class Corporate { //declare instance variable corporateName private String corporateName; //constructor to initialize object public Corporate(String corporateName) { this.corporateName = corporateName; } //getter method to get the value of corporate name public String getCorporateName() { return corporateName; } //setter method to set the value of corporate name public void setCorporateName(String corporateName) { this.corporateName = corporateName; } //overriding toString() method @Override public String toString() { return "Corporate Name: "+corporateName; } //overriding equals() method @Override public boolean equals(Object obj) { //checking if an object is compared with itself or not if(obj==this) return true; //checking obj is not an instance of Corporate class or not if(!(obj instanceof Corporate)) return false; //typecasting obj to Corporate type Corporate c=(Corporate)obj; //checking their corporateName equal return this.corporateName.equals(c.corporateName); } //method to get domain name public String domainNmae() { return "wwww."+corporateName.toLowerCase()+".com"; } }
Client class
public class Client { public static void main(String[] args) { //creating two corporate objects Corporate c1 = new Corporate("Ibm"); Corporate c2 = new Corporate("Apple"); //modifying Ibm to IBM c1.setCorporateName("IBM"); //printing c1 object System.out.println(c1); //printing domain name of c1 System.out.println("Domain name: " + c1.domainNmae()); //printing c2 object System.out.println(c2); //printing domain name of c2 System.out.println("Domain name: " + c2.domainNmae()); //calling equals() method to check both c1 and c2 are equal or not System.out.println("Are " + c1.getCorporateName() + " and " + c2.getCorporateName() + " equal ? :" + c1.equals(c2)); } }
Screen shot of the code
Screen shot of the output