Question: Looking for some help with this: Constructors Constructor methods inside a class are methods that determine how an object is going to be created when
Looking for some help with this:
Constructors
Constructor methods inside a class are methods that determine how an object is going to be created when some program instantiate one of them using the new clause. Their main use is to initialize the objects instance variables. For example, last week I presented the Chair class that represented chairs. This class had the following instance variables:
private String color; // instance variable for color of the Chair private int numberOfLegs; // instance variable to hold # of legs The following are two appropriate constructors that could be included in that class:
public Chair (String newColor, int newNumberOfLegs ) { this.setColor(newColor); this.setNumberOfLegs(newNumberOfLegs);
}
public Chair ( ) { this (brown, 4); } The first constructor is a constructor with parameters. The second constructor, without parameters, is called the default constructor. All constructors other than the default are known as the overloaded constructors.
In both constructors all that we are doing is given initial values to the instance variables color and legs. In the default constructor we use default values (that is the reason for the constructors name). In the overloaded constructor we take the values provided by whoever is creating the object to initialize its instance variables. This method uses the mutator methods of the class to avoid code repetition.
Notice that the default constructor is actually calling the constructor with parameters, by passing the initial values to this constructor with the keyword this. The advantage of this approach is that it centralizes all the initializations into one method, the constructor. This makes the code easier to maintain in the future, because we potentially will need to modify only one constructor if something changes.
Notice also that the call to a constructor from another constructor does not need the dot (".") after the keyword "this". This is the only place where "this" is not followed by a dot.
A driver program may use these constructors as follows:
firstChair = new Chair(); // Using default constructor secondChair = new Chair(red, 3); // Using overloaded constructor
The equals method
When we want to compare objects we need to define what do we mean by equal. For example if I have two chairs, how can I say they are equal? Are they equal if they have the same color alone? Should they also have the same number of legs? What about shape? Should they have the same shape? If I am trying to match chairs in a living room, certainly all these factors should be taken into consideration, but what if all I want to do is to sit? I really do not care whats the chair color or the number of legs, as long as I can sit on it. To me, all chairs where I can sit will be equal. So the idea of equality depends of the context. That is why when describing an object we have the opportunity to indicate exactly what equal means. We do this by writing an appropriate equals method. In this method we compare the only thing we can compare in objects, their attributes, also known as their instance variables. This method will check if the attributes of the objects we are comparing are the same. We are free to select which attributes we are going to compare. It is our definition of equality after all! For example, last week I presented the Chair class that represented chairs. This class had only two instance variables, the chairs color (color) and its number of legs (numberOfLegs). The following is an equals method that will check if both, the color and the number of legs, in two chairs are the same. If that is the case the equals method will return true, otherwise it will return false:
public boolean equals (Chair secondChair) { return ((this.getColor().equals(secondChair.getColor()) && (this.getNumberOfLegs() == secondChar.getNumberOfLegs()) )
}
The method receives as a parameter a second chair (secondChair) of type Chair. This secondChair object is going to be compared against the current object. That current object is represented by the keyword this. Therefore this represents the first object. First we are comparing the color of both chairs. We obtain the color using the accessor method from the Chair class, getColor. According to my definition of the getColor method, each time it is called it produces a String with the color of their respective chair, and because we are comparing Strings, we use the equals method from the String class to compare them (We always should use the equals method when comparing reference objects). Similarly, we use the getNumberOfLegs accessor method to retrieve the number of legs of each chair. This method will return integer numbers, therefore they can be compared using the == symbol (We always should use the == symbol when comparing primitives). If both expressions are true, the whole expression is true, because they are connected by the AND connector (&&). So if both conditions are true, we can say that the Chair objects are equal, this chair and the second chair are equal. That is why we return the result of this logical expression. If any of the conditions is false, the whole logical expression is false, meaning that one of the attributes do not match, and that is why the chairs cannot be classified as equal. This false outcome is also returned in that sentence. Using two Chair objects (chair1 and chair2) we can compare them using the equals method as follows:
if (chair1.equals(chair2)) { // actions when both Chair objects are equal }
We could also compare them the other way around, it will work exactly the same:
if (chair2.equals(chair1)) { // actions when both Chair objects are equal }
In the first case chair1 will be the current object (this) and chair2 will be the second chair (secondChair) inside equals. In the second case, the roles are reversed.
Questions
In last weeks discussion thread you wrote a class description for some objects. This week you must refine your description by writing two constructors (a default constructor and a constructor with parameters) and an equals method to implement your definition of equality among your objects. Write the constructors and the equals method inside the class you created last week. Write testing code in the Class Driver to test your new constructor and equals method. Show also a screen capture of the driver running your programs.
Requirements
Due by Thursday at 11:59pm (CST). You must post your initial response(s) to the topic(s). Due by Sunday at 11:59pm (CST). You must respond to other student posting. You must respond, to the best of your knowledge, the questions or requests made by Instructor and/or students, especially Instructors requests for corrections. Each of the postings should be about 1-2 paragraphs long. Review the "Discussion Grading Policy" under "Assignments and Grading".
Grading Criteria for Discussions:
Components of the Discussion Grade
Points
1 point for having an original posting to the discussion, regardless of quality. This posting must try to answer the original question in the discussion, and must create the students own thread.
1
1 point for having a response to other students response threads, regardless of quality. Responses to questions in your own personal response thread do not count. This point is granted for responses to other students threads.
1
1 point for having a posting to the discussion before Thursday, regardless of quality.
1
5 points for providing an adequate example of a Java Class with the requested modifications by the end of the week.
5
5 points for providing an adequate example of a Java Driver Class with the requested modifications by the end of the week.
3
2 points for responses to other students threads. Points granted for this item may vary depending of their usefulness to the recipient student (more than complimentary), and their contribution to the learning experience of the whole class.
2
1 point for addressing responses to other students (or the instructor) by their names.
1
1 points for correct use of Nettiquette.
1
TOTAL
15
And here is my discussion for last week i have to refine:
So for the first program:
public class AnnualVacation {
private String place; private String year; public String getPlace() { return this.place; } public String getYear() { return this.year; } public void setPlace(String newPlace) { this.place = newPlace; } public void setYear(String newYear) { this.year = newYear; } }
And for the Driver Program:
public class AnnualVacationDriver {
public static void main (String[] args) {
AnnualVacation firstAnnualVacation;
AnnualVacation secondAnnualVacation;
AnnualVacation thirdAnnualVacation;
firstAnnualVacation = new AnnualVacation();
secondAnnualVacation = new AnnualVacation();
thirdAnnualVacation = new AnnualVacation();
firstAnnualVacation.setPlace("Cancoon");
secondAnnualVacation.setPlace("Cozumel");
thirdAnnualVacation.setPlace("Miami");
firstAnnualVacation.setYear("2019");
secondAnnualVacation.setYear("2020");
thirdAnnualVacation.setYear("2021");
System.out.println("In"+" "+ firstAnnualVacation.getYear() +" "+"you will be taking your vacation to the beautiful beaches of"+" "+ firstAnnualVacation.getPlace());
System.out.println("And for"+" "+ secondAnnualVacation.getYear() +" "+"you will be cruising to the beautiful island of"+" "+ secondAnnualVacation.getPlace());
System.out.println("Then in"+" "+ thirdAnnualVacation.getYear() +" "+"you will be headed to the warm beaches of"+" "+ thirdAnnualVacation.getPlace());
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
