Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Learning Activity: Rounding Objectives See the effects of the IEEE 754 standard on floating-point calculations and round-off errors. Instructions For this quiz, you need to create some simple code...

1 answer below »
Learning Activity: Rounding
Objectives
See the effects of the IEEE 754 standard on floating-point calculations and round-off e
ors.
Instructions
For this quiz, you need to create some simple code that performs some multiplication operations and prints out the results. You do not have to submit your code.
Float calculations
Initially, declare a float variable and give it the initial value 3.0.
float number = 3.0f;
Now, multiply the number by 10 and print out the value, using default precision as shown.
number *= 10.0f;
System.out.println(number);
Repeat this 20 times. (Yes, this would be a natural place to use a loop. You will find it helpful if you print out both the new value of number and the iteration. So, iteration 1 prints out 30.0; iteration 2 prints out 300.0, etc.)
The expected final value is 3 · 1020. However, the computed value is 2.9999997E20. This value is well below the upper bound for float values, which is around 3.4 · 1038. The numerical output is not the mathematically expected result due to the round-off e
or inherent in floating-point numbers (float and double) as described in IEEE 754. In this case, obviously, the round-off e
or is in the float data type.
Use this simple program to answer the first four questions in the quiz.
What is the value where the round-off e
or is first apparent?
Which iteration is the round-off e
or first apparent?
What is the significance of the letter 'f' in the assignment of the initial value of the variable number?
What happens if the 'f' is omitted in the initial value?
Double calculations
Now, repeat the same exercise. However, this time use the data type double, rather than float. It's up to you to determine the appropriate number of iterations to perform to see the round-off e
or. (Hint: It will be less than 60 iterations.)
Using this double implementation, answer the remaining questions in the quiz:
What is the value where the round-off e
or is first apparent?
Which iteration is the round-off e
or first apparent?
Does this behavior change if the initial value is specified as 3.0f or 3.0?
Does this behavior change if the initial value is specified as 3 (int)?
Briefly the reason for the answers to questions 7 and 8.
Something "odd" happens in the printed values for iterations 30-33, inclusive. What is it? Try to explain the phenomenon.
Note: Questions 7 and 8 refer to the initial value given to the double variable:
double number = 3.0f;
double number = 3.0;
double number = 3;
Just for Fun
Try the same processing using the int data type.
int number = 3;
How far can you go before you see an e
or in the values? What is that e
or?
Submission
Write-up the answers to the 10 questions in a plain text file and submit that file.
Please number the questions. There is no need to repeat the question text.
Leave a blank line between the answer lines.
Do not forget to include a header, with your name and this quiz name and number.
Leave a blank line between the header information and the answers.

Learning Activity: Properties
In this lab, you will work with the documentation in the standard li
ary to find properties in the classes there.
Educational Objectives
Know how to navigate the Java API documentation
Recognize the property pattern
Instructions
Go to the standard API documentation
In the
owser, go to the Java API (application programming interface) documentation:
http:
docs.oracle.com/javase/8/docs/api/ (Links to an external site.).
There is a link for it at the top of our course Moodle page as well. This uses frames.
The "package list" frame is the small frame in the top-left corner. It contains the names of the packages in the standard Java li
ary. We will use the packages java.awt and javax.swing in this lab. These two packages support graphic output (creating windows and drawing stuff in them).
The "package" frame is the tall, skinny frame that takes up the rest of the left side of the screen. It lists the classes, interfaces, exceptions, and e
ors. Clicking one of the package names in the "package list" frame displays only the contents of the selected package in the "package" frame. Initially, the "package" frame lists all of the contents of the whole standard li
ary. When the "package" frame displays the contents of a particular package, the contents are separated into categories: interfaces, classes, enums, exceptions, and e
ors. In this lab, we will work only with documentation for classes.
The "class" frame is the large frame that takes up the rest of the
owser window. This is where the documentation for individual classes and interfaces appears. The name of the package in the package frame is a link that will load an overview of the package into the class frame. Yes, I know it seems like we should call the frame something else, but these names are derived from the actual frame names in the HTML documentation.
Explore the API documentation for properties
Navigate to the java.util package. We will use this package to explore some properties. Note: The names for the different types of properties described here is not universal terminology. It's just for this lab so that we can discuss different patterns for properties.
Get/Set properties
The most typical pattern for properties uses two methods, one to query the value of the property (a get method) and one to update the value of the property (a set method). For example, navigate to the java.util.TimeZone class. In the method summary, you will find:
public String getID()
public void setID(String ID)
Notice there is very strong symmetry in these two method headers. The property name follows get- and set- in the method names, in this case, ID. The type for this property is the return type of get and the parameter type of set, in this case, String. The return type for set is void and that there are no parameters for get. All of this is typical for the bulk of the properties in the standard li
ary. We will call this the get/set pattern.
[fodder: We have seen this pattern and, in fact, even coded it. Where was that?]
Another example from the same class:
public int getRawOffset()
public void setRawOffset(int offsetMillis)
Notice it has exactly the same pattern. The name is rawOffset. The type is int.
There are other kinds of patterns that the methods for properties use. These other patterns do not have the same symmetry that we see in these two examples. For this lab, we will examine three asymmetric patterns.
Read-only properties
Another common pattern for properties is the read-only property. These properties are characterized by having only a get method with no co
esponding set method. For an example, navigate to the java.applet.Applet class.
public AppletContext getAppletContext()
You will notice that there is no co
esponding setAppletContext. If you think about applet technology, this makes sense. What is the applet context? It's the web page in which the applet appears. The applet cannot programmatically change its context, that is, it cannot "jump" to a new web page.
Multi-valued properties
There are some instances where the properties have both get and set methods, but they are not set up as a symmetric pair of get/set methods that we described above. This happens when the property is composed of multiple separate values. Needless to say, the set method can take multiple parameters, but the get method cannot return multiple values. For these multi-valued properties, there will be multiple get methods co
esponding to a single multiple-parameter set method. For an example, navigate to the java.net.URL class in version 1.7. The primary data within this class is the URL, which is composed a number of separate pieces of information. Notice that there are a number of get methods for a single set method:
public String getProtocol()
public String getHost()
public int getPort()
public String getFile()
public String getRef()
public void set(String protocol, String host, int port, String file, String ref)
This property doesn't have a name since it is the primary data for the class. So, the set method in this case is simply set. In many of the properties you will find for this lab, the property will have a name; so, the set method will be setName (where name is the name of the property), rather than simply set.
Here is another example, taken from java.util.Calendar.
public set(int year, int month, int date)
public set(int year, int month, int date, int hourOfDay, int minute)
public set(int year, int month, int date, int hourOfDay, int minute, int second)
public get(int field)
where field is one of the following values:
Calendar.YEAR
Calendar.MONTH
Calendar.DATE
Calendar.HOUR_OF_DAY
Calendar.MINUTE
Calendar.SECOND
Note that this class also includes the following method, which is set up more like a traditional get/set pair with the get method shown above:
public set(int field, int value)
Boolean properties
The final type of pattern for properties that is used by the standard li
ary is a difference in the naming of boolean (true/false) properties. These properties often have two methods, one to modify the property value and one to query it. So, there is a set method, as you would expect. The query method, though, is not named get. For an example, navigate to the java.io.File class. There you will find this pair of methods for the executable property.
public void setExecutable(boolean executable)
public boolean canExecute()
Notice that the getter is not named getExecutable. This odd naming is actually to help reabability of the code. For example:
File myFile = new File(...);
...
if(file.canExecute()) {

stuff to do
In addition to the ve
"can", Boolean query method also start with "is" and "has". (This list is not exhaustive.)
Here is another example. This time from java.net.Socket:
public void close()
public boolean isClosed()
In this case, there really isn't a setter for the closed property. To change the state of the Socket object, we simple close the socket. As it turns out, this is an i
evocable change to the closed property. Because of that, the close method is not really a setter for the property. A method named setClosed would suggest that a closed Socket object could be reopened. So, closed is a read-only property as well.
Write-only properties
These properties have a set method with no co
esponding get method. They are very rare within the API.
Important Note:
Please be very careful to distinguish between read-only, multi-valued, boolean, and write-only properties. They can potentially be confused, since the symmetric get/set pattern is not
Answered Same Day Oct 02, 2021

Solution

Ramachandran answered on Oct 03 2021
137 Votes
Order-92663/properties.txt
Get/Set Properties (10)
package    class         property         type
java.awt     FlowLayout        newAlign        int
java.awt     FlowLayout        hgap            int
java.awt    FlowLayout        vgap            int
java.awt    TextComponent        text            String
javax.swing    ImageIcon        image            Image
javax.swing    ImageIcon         description        String
javax.swing    JLabel            horizontalAlignment    int
java.awt    MenuItem        label            String
java.awt    TextComponent        font            Font
java.awt    Checkbox        state            boolean
    
Read-Only Properties (2)
package     class         property         type
java.awt     PointerInfo        location        Point
java.awt     Menu            isHelpMenu        boolean
Multi-Valued Properties (2)
package      class         headers
java.awt     Rectangle        public void setRect(double x, double y, double width, double height)
java.awt     Rectangle        public double getX()    
java.awt     Rectangle        public double getY()
java.awt     Rectangle        public double...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here