Question

In: Computer Science

Write a class encapsulating the concept of a corporate name (for example, IBM), assuming a corporate...

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.

Solutions

Expert Solution

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

  • In Corporate class an instance variable corporateName is created
  • A constructor is defined to initialize the variable
  • getter and setter method are defined to get and set corporateName
  • toString() of object class is overridden to print object variable values directly
  • equals() method is overridden to check two object of Corporate class are equal or not
  • A client class is designed, inside it two Corporate objects c1 and c2 are created
  • setCorporateName() method is invoked to modify c1's name from Ibm to IBM
  • objects values are directly printed
  • domainNmae() method is invoked to find and print domain name of the objects
  • equals() method is invoked to check c1 and c2 are equal or not

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


Related Solutions

Write a class encapsulating the concept of the weather forecast, assuming that it has the following...
Write a class encapsulating the concept of the weather forecast, assuming that it has the following attributes: the temperature and the sky conditions (e.g. sunny, snowy, cloudy, rainy, etc.).  Include a default constructor, an overloaded constructor, accessors and mutators, and the methods, toString() and equals(). Temperature, in Fahrenheit, should be between -50 and +150; the default value is 70, if needed. The default sky condition is sunny. In Addition, include a method that converts Fahrenheit to Celsius. Celsius temperature = (Fahrenheit...
Write a class encapsulating the concept of a Student, assuming that the Student has the following...
Write a class encapsulating the concept of a Student, assuming that the Student has the following attributes: the name of the student, the average of the student, and the student’s GPA. Include a default constructor, an overloaded constructor, the accessors and mutators, and methods toString() and equals(). Also include a method returning the letter grade base on the following range for the average: Average Range Letter Grade 90-100 A 85-89   B+ 80-84 B 75-79 C+ 70-74 C 65-69 D+ 60-64...
Write a class encapsulating the concept of a Student, assuming that a student has the following...
Write a class encapsulating the concept of a Student, assuming that a student has the following attributes: last name, first name, id, array of grades. Include a constructor, the accessors and mutators, and method toString. Also code the following methods: one returning the GPA using the array of grades (assuming each grade represents a course grade and all courses have the same number of credit hours) and a method to add a course grade to the array of grades (this...
Write a class encapsulating the concept of a Student Employee, assuming that a student has the...
Write a class encapsulating the concept of a Student Employee, assuming that a student has the following attributes: last name, first name, id, array of hours worked weekly. Include a constructor, the accessors and mutators, and method toString. Also code the following methods: one returning the average weekly hours and a method to add the hours to the array of hoursWorked (this means creating a large array). Let this array store the weekly hours (not daily hours) worked for each...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming a telephone number has only a single attribute: aString representing the telephone number. Include a constructor, the accessor and mutator, and methods 'toString' and 'equals'. Also include methods returning the AREA CODE (the first three digits/characters of the phone number; if there are fewer than three characters in the phone number of if the first three characters are not digits, then this method should...
Write a class encapsulating the concept of a student, assuming a student has the following attributes:...
Write a class encapsulating the concept of a student, assuming a student has the following attributes: a name, a Social Security number, and a GPA (for instance, 3.5). Include a constructor, the accessors and mutators, and methods toString and equals. Write a client class (test driver with main method) to test all the methods in your class (create 2 student objects, print the name, social security number and GPA of the 2 students, check if the two student’s GPA is...
Write a class encapsulating a board game. A board game has the following additional attributes: the...
Write a class encapsulating a board game. A board game has the following additional attributes: the number of players and whether the game can end in a tie. Code the constructor and the toString method of the new class. You also need to include a client class(with the main method) to test your code. code in Python
Homework Assignment 4 Instructions: Class name must be: HW4_yourName For example: Michael will name the class...
Homework Assignment 4 Instructions: Class name must be: HW4_yourName For example: Michael will name the class of homework assignment 4 as HW4_Michael Grading Rubric: Code running and as per the required conditions and giving expected output = 10 points File named as per instructions = 1 point Comments in code = 4 points Problem: Average calculation for a list Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for...
Write a Java class. The class name must be ShapeMetrics, which means the file name must...
Write a Java class. The class name must be ShapeMetrics, which means the file name must be ShapeMetrics.java. Provide the following functions: 1. getAreaOfRectangle(), with two float arguments, width and height, in that order, returning a float value which is the area of the rectangle with that width and height. 2. getSpaceDiagonalOfRectangularCuboid (), with three float arguments, width, height, and depth, in that order, returning a float value which is the length of the diagonal line which bisects the cuboid....
IN JAVA!   Write a class Store which includes the attributes: store name, city. Write another class...
IN JAVA!   Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT