Read the lecture for Chapter 7, the following text and then answer the question at the end. Notice the grading criteria this week is different than other weeks
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 object’s 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 constructor’s 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 what’s 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 chair’s 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 week’s 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.
In: Computer Science
Match each term to the correct definition.
A. Chemical separation
B. Gravity filtration
C. Physical separation
D. Vacuum filtration
E. Evaporation
F. Heterogeneous mixture
G. Homogenous mixture
H. Distillation
I. Extraction
J. Decantation
-varies in composition, distinct clumps of each component appear in the mixture
-uniform in composition, the components cannot be visually distinguished
-uses heat to remove a volatile solvent from a dissolved solution, leaving behind the non-volatile components as a solid residue
-exploit some difference in the physical properties of two species in order to separate them
-exploit reactivity to convert one component to a new species which can be easily separated from the other components that are present
-uses a solvent to selectively dissolve one component while leaving other components in the solid state
-uses gravity to pull the liquid through a filter paper to separate a liquid from a solid
-uses a vacuum to pull the liquid through a filter paper to separate a liquid from a solid.
-The process of pouring a solution off a heavy solid that settles to the bottom of the container
-Takes advantage of the difference in the boiling point between the two components
In: Chemistry
The National Sleep Foundation used a survey to determine whether
hours of sleeping per night are independent of age
(Newsweek, January 19, 2004). The following show the hours
of sleep on weeknights for a sample of individuals age 49 and
younger and for a sample of individuals age 50 and older.
| Hours of Sleep | |||||||
| Age | Fewer than 6 | 6 to 6.9 | 7 to 7.9 | 8 or more | Total | ||
| 49 or younger | 38 | 56 | 70 | 76 | 240 | ||
| 50 or older | 30 | 56 | 71 | 103 | 260 | ||
Conduct a test of independence to determine whether the hours of
sleep on weeknights are independent of age. Use = .05.
Use Table 12.4.
A.) Compute the value of the 2 test statistic (to 2
decimals).
B.) Using the total sample of 500, estimate the percentage of
people who sleep less than 6, 6 to 6.9, 7 to 7.9, and 8 or more
hours on weeknights (to 1 decimal).
| Less than 6 hours | % |
| 6 to 6.9 hours | % |
| 7 to 7.9 hours | % |
| 8 or more hours | % |
In: Statistics and Probability
The National Sleep Foundation used a survey to determine whether
hours of sleeping per night are independent of age
(Newsweek, January 19, 2004). The following show the hours
of sleep on weeknights for a sample of individuals age 49 and
younger and for a sample of individuals age 50 and older.
| Hours of Sleep | |||||||
| Age | Fewer than 6 | 6 to 6.9 | 7 to 7.9 | 8 or more | Total | ||
| 49 or younger | 34 | 58 | 80 | 68 | 240 | ||
| 50 or older | 35 | 64 | 70 | 91 | 260 | ||
| Less than 6 hours | % |
| 6 to 6.9 hours | % |
| 7 to 7.9 hours | % |
| 8 or more hours | % |
In: Statistics and Probability
The National Sleep Foundation used a survey to determine whether hours of sleeping per night are independent of age (Newsweek, January 19, 2004). The following show the hours of sleep on weeknights for a sample of individuals age 49 and younger and for a sample of individuals age 50 and older.
| Hours of Sleep | |||||||
| Age | Fewer than 6 | 6 to 6.9 | 7 to 7.9 | 8 or more | Total | ||
| 49 or younger | 30 | 64 | 76 | 70 | 240 | ||
| 50 or older | 30 | 58 | 80 | 92 | 260 | ||
Conduct a test of independence to determine whether the hours of
sleep on weeknights are independent of age. Use = .05.
Use Table 12.4.
Compute the value of the X2 (Chi2) test statistic (to 2
decimals).????
b) Using the total sample of 500, estimate the percentage of people who sleep less than 6, 6 to 6.9, 7 to 7.9, and 8 or more hours on weeknights (to 1 decimal).
| Less than 6 hours | % |
| 6 to 6.9 hours | % |
| 7 to 7.9 hours | % |
| 8 or more hours | % |
In: Math
The National Sleep Foundation used a survey to determine whether hours of sleeping per night are independent of age (Newsweek, January 19, 2004). The following show the hours of sleep on weeknights for a sample of individuals age 49 and younger and for a sample of individuals age 50 and older. Hours of Sleep Age Fewer than 6 6 to 6.9 7 to 7.9 8 or more Total 49 or younger 37 58 71 74 240 50 or older 34 60 79 87 260
a.Conduct a test of independence to determine whether the hours of sleep on weeknights are independent of age. Use = .05.
Compute the value of the 2 test statistic (to 2 decimals).
b.Using the total sample of 500, estimate the percentage of people who sleep less than 6, 6 to 6.9, 7 to 7.9, and 8 or more hours on weeknights (to 1 decimal).
Less than 6 hours %
6 to 6.9 hours %
7 to 7.9 hours %
8 or more hours %
In: Math
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods that operate on an array of int values. You will code all the methods and use your main method to test your methods. Your class should be named Array Your class will have the following methods (click on the method signatures for the Javadoc description of the methods): [ https://bit.ly/2GZXGWK ] public static int sum(int[] arr) public static int sum(int[] arr, int firstIndex, int lastIndex) public static double average(int[] arr) public static double average(int[] arr, int firstIndex, int lastIndex) public static int maxValue(int[] arr) public static int maxValue(int[] arr, int firstIndex, int lastIndex) public static int indexOfFirstMaxValue(int[] arr) public static int indexOfFirstMaxValue(int[] arr, int firstIndex, int lastIndex) public static int numberOfBelowAverageElements(int[] arr) public static int numberOfBelowAverageElements(int[] arr, int firstIndex, int lastIndex) public static void rotateElements(int[] arr) public static void rotateElements(int[] arr, int rotationCount) public static void reverseArray(int[] arr) For example, given the following array: myArray = {45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21, 14, 7, 16, 49, 58, 72} Your methods will return the following values: 1. Sum of whole array = 1253 2. Sum of elements 12-18 = 343 3. Average of whole array = 50.12 4. Average of elements 12-18 = 49.0 5. Max of whole array = 100 6. Max of elements 12-18 = 98 7. Index of first Max of whole array = 8 8. Index of first Max of elements 12-18 = 13 9. Count of elements below average of whole array = 13 10. Count of elements below average of elements 12-18 = 4 11. Rotating once myArray = {72, 45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21, 14, 7, 16, 49, 58} 12. Rotating 5 more times myArray = {14, 7, 16, 49, 58, 72, 45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21} 13. Reversing the array myArray = {21, 32, 44, 75, 57, 98, 16, 72, 48, 55, 100, 69, 15, 79, 82, 89, 18, 22, 45, 72, 58, 49, 16, 7, 14}
In: Computer Science
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods that operate on an array of int values. You will code all the methods and use your main method to test your methods. Your class should be named Array Your class will have the following methods (click on the method signatures for the Javadoc description of the methods): [ https://bit.ly/2GZXGWK ]
public static int sum(int[] arr) public static int sum(int[] arr, int firstIndex, int lastIndex)
public static double average(int[] arr) public static double average(int[] arr, int firstIndex, int lastIndex)
public static int maxValue(int[] arr) public static int maxValue(int[] arr, int firstIndex, int lastIndex)
public static int indexOfFirstMaxValue(int[] arr)
public static int indexOfFirstMaxValue(int[] arr, int firstIndex, int lastIndex)
public static int numberOfBelowAverageElements(int[] arr)
public static int numberOfBelowAverageElements(int[] arr, int firstIndex, int lastIndex)
public static void rotateElements(int[] arr) public static void rotateElements(int[] arr, int rotationCount)
public static void reverseArray(int[] arr)
For example, given the following array: myArray = {45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21, 14, 7, 16, 49, 58, 72}
Your methods will return the following values:
1. Sum of whole array = 1253
2. Sum of elements 12-18 = 343
3. Average of whole array = 50.12
4. Average of elements 12-18 = 49.0
5. Max of whole array = 100
6. Max of elements 12-18 = 98
7. Index of first Max of whole array = 8
8. Index of first Max of elements 12-18 = 13
9. Count of elements below average of whole array = 13
10. Count of elements below average of elements 12-18 = 4 1
1. Rotating once myArray = {72, 45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21, 14, 7, 16, 49, 58}
12. Rotating 5 more times myArray = {14, 7, 16, 49, 58, 72, 45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21}
13. Reversing the array myArray = {21, 32, 44, 75, 57, 98, 16, 72, 48, 55, 100, 69, 15, 79, 82, 89, 18, 22, 45, 72, 58, 49, 16, 7, 14}
In: Computer Science
The earnings, dividends, and stock price of Shelby Inc. are expected to grow at 8% per year in the future. Shelby's common stock sells for $23.75 per share, its last dividend was $2.50, and the company will pay a dividend of $2.70 at the end of the current year.
Can you guys show me how to work them out please not just the answer I want to comprehend it. Mostly confused on C. rs= 8% +3% =11 but it's not right please explain why and what numbers are supposed to go there.
In: Finance
Can someone please help me with these questions with steps for
Statistics for the business course??
In: Statistics and Probability