









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Lab; Class: Intro Obj Orient Programming; Subject: Computer Science; University: University of Alabama - Birmingham; Term: Unknown 1989;
Typology: Lab Reports
1 / 16
This page cannot be seen from the preview
Don't miss anything!










Other Identification _____________________
In this laboratory you will:
Recall from Chapter 1 of Computer Science: An Overview , that data items of type integer are normally stored in memory by using two's complement notation and that this imposes a limit on the size of the values that can be represented. For example, with a two's complement system using only four bits for storage, the largest value that can be represented is seven. Of course, if this was the largest integer your machine could represent, it would be of little use. Therefore, computer systems use more than four bits for integer storage and can store rather large values. But, limitations still exist. In the Java system, the maximum and minimum values of the numeric types are represented by the constants MAX_VALUE and MIN_VALUE. The smallest and largest int values are defined in the Integer class. These class constants can be accessed by using the name of the class in which the constant is defined, followed by the dot operator (.) and the name of the constant. Therefore, to access the largest and smallest int values, use Integer.MAX_VALUE and Integer.MIN_VALUE.
In this experiment you will investigate the storage of integer values in Java. Step 1. Compile and execute the program J03E01.java, and record the values of the smallest and largest int values. class J03E { public static void main(String[] args) { System.out.println( "In Java the smallest int value is " + Integer.MIN_VALUE); System.out.println( "In Java the largest int value is " + Integer.MAX_VALUE); } }
Step 2. Besides int, Java has three other integer types, long, short and byte. In this step, let's investigate the maximum and minimum values of the primitive data type byte. Information about and methods involving type byte are stored in a class called Byte. Both the Integer and Byte classes are in the API package java.lang, which is automatically imported into every Java program. Modify the program to print the smallest and largest byte values. Record the results.
Step 6. Using the values obtained in Step 1 and the analysis completed in Steps 2 through 5, determine the number of bits and bytes that are used to represent data of type int.
In this experiment you will determine how your computer system responds to programs that try to produce integer values that exceed the limits of INT_MIN and INT_MAX. Step 1. Execute the following program J03E02.java, and record the results. class J03E { public static void main(String[] args) { int number = Integer.MAX_VALUE - 2; int count = 0; while(count < 5) { System.out.println(number); number++; count++; } } }
Step 2. Briefly explain the results obtained in Step 1.
Step 3. Modify the program from Step 1 to record what happens on your system when you try to represent integers smaller than Integer.MIN_VALUE. Record the results.
Step 4. Explain the results obtained in Step 3.
As with the storage of integers, only a finite number of real values can be represented in floating-point notation in a computer's memory. Other values must be rounded to a value that can be represented. Thus, a value stored as type double is often merely an approximation of the desired value. The class Double contains information and methods pertaining to the primitive type double.
Step 4. Modify the code added in Step 3 by changing the value assigned to d. List the test values and the results. Choose values that help you find answers to these questions. Are the displayed values rounded or truncated? What is the maximum number of significant digits that can be displayed using data type double?
Step 1. Compile and execute the program J03E04.java. Record the results. class J03E { public static void main(String[] args) { double x = 1.0; int i = 0; while(i < 1000) { System.out.println(x); x += 0.0001; i++; } } }
Step 2. What would the program in Step 1 produce if all values were stored accurately?
Step 3. Explain the discrepancy between your answers in Steps 1 and 2.
In Java, data items of type character are stored as bit patterns according to the Unicode encoding scheme. In the text Computer Science: An Overview you learned that ASCII is an eight-bit code whereas Unicode is a 16-bit code. Moreover, Appendix C of Computer Science: An Overview contains a partial listing of ASCII. In a sense, this appendix is also a partial listing of Unicode because the Unicode encoding of each symbol listed can be obtained by merely adding eight 0s to the left of the ASCII encoding. Thus, since the character a is represented as 01100001 in ASCII, it is represented as 0000000001100001 in Unicode. The following program (J03E05) illustrates how we can identify the bit pattern used to represent a character. First note that if the variable ch (of type char) is assigned the value 'a', then the statement System.out.print(ch); prints the character a. We can, however, request that the bit pattern assigned to ch be interpreted as though the variable was of type int via the statement System.out.println((int)ch); Here the expression (int)ch
Step 3. Modify the program from Step 1 to find the characters immediately before and after the characters a, A, and Z in the Unicode order. Hint: Recall from Session 2 that the auto increment(++) and auto decrement(--) operators can be applied to variables of type char. Summarize your modifications, and record your findings below.
In general, c onversion refers to the interpretation of data as a type other than that originally intended. There are actually two types of conversion, and we have already seen both. In Experiment 3.5 a forced conversion, or cast , occurred when a char variable was cast as an int, resulting in the value stored in the variable to be interpreted, or converted, to an int. Casting is an example of an explicit conversion, that is, the programmer explicitly forces the conversion by using a cast. A second type of conversion is called coercion , which occurs automatically. Coercion occurs when performing an arithmetic operation between two numeric values of different types. In Java, if an arithmetic calculation has an integer operand and a floating-point operand, the integer value is coerced into floating-point representation before the operation can be performed. For example, before adding the int and double 3 + 2.5 both numbers must be of the same data type. Java automatically converts the 3 to 3.0, and the calculation 3.0 + 2. is performed to obtain an answer of 5.5. We saw examples of automatic, or implicit, coercion in Experiments 2.4 and 2.5. Note that an int can be converted to a double without a loss of information. Therefore, when required an int will be coerced to a double. However, a double cannot be converted to an int without losing the fractional portion of the number. Therefore, a double must be cast to an int.
Step 1. Compile the program J03E06.java. Record and explain the results. class J03E { public static void main(String[] args) { int x; double d; x = 5; d = x; System.out.println("int " + x + " double " + d); d = 2.95; x = d; System.out.println("int " + x + " double " + d); } }
Step 2. Correct the program in Step 1 by replacing the statement x = d; with x = (int)d; Record the results.
Step 2. Compile J03E07B.java. Record the error message. class J03E07B { public static void main(String[] args) { char c; c = 'A'; while(c < 'F') { System.out.println((int)c + " " + c); c = c + 1; } } }
Step 3. Correct the program in Step 2 by replacing the statement c = c + 1; with c = (char)(c + 1); Record the results, and compare these results to the results of the program J03E07A.java in Step 1.
Step 4. Identify all of the conversions in the revised code?
Step 5. Modify the condition of the loop in J03E07B. Change while (c < 'F') to while(c < 70). Record the results and explain why this works.
Step 6. Modify the program above so that it prints the Unicode characters and values for all the uppercase letters of the alphabet. Summarize your work below.
found the Unicode value of the space and question mark. Include a short interpretation of the results. Does this confirm that Unicode uses 2 bytes to store values of type char? 3.7. Modify the program written in 3.5 above, to print the printable characters that are defined on your system with Unicode values greater than 126. 3.8. Design an experiment to confirm that in Java items of type char are encoded in 16 bits (Unicode) rather than eight bits (ASCII).