Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Data Storage and Its Implications in Object Oriented Programming | CS 201, Lab Reports of Computer Science

Material Type: Lab; Class: Intro Obj Orient Programming; Subject: Computer Science; University: University of Alabama - Birmingham; Term: Unknown 1989;

Typology: Lab Reports

2009/2010

Uploaded on 04/12/2010

koofers-user-vf5-1
koofers-user-vf5-1 🇺🇸

2

(1)

10 documents

1 / 16

Toggle sidebar

Related documents


Partial preview of the text

Download Data Storage and Its Implications in Object Oriented Programming | CS 201 and more Lab Reports Computer Science in PDF only on Docsity! Session 3 Student Name ___________________________ Other Identification _____________________ Data Storage and Its Implications In this laboratory you will: 1. Investigate the storage systems used for three primitive data types, int, byte and double. 2. Learn how these storage systems are reflected in the execution of Java programs. 3.2 Data of Type Integer 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. Experiment 3.1 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 J03E01 { 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. 3.5 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. ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ Data of Type Real 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. 3.6 Experiment 3.3 Step 1. Execute the program J03E03.java and record the value of Double.MIN_VALUE. class J03E03 { public static void main(String[] args) { System.out.println( "In Java the smallest double value is " + Double.MIN_VALUE); } } ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ Step 2. Modify the program in Step 1 to print the value of the largest value that can be stored as a double. What changes did you make? ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ Step 3. Add the steps below (which assign a value with many significant digits to a double variable and then prints the variable) to the program in Step 1. Record the results. double d = 987.65432109876543210987654321; System.out.println(d); ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ 3.7 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? ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ Experiment 3.4 Step 1. Compile and execute the program J03E04.java. Record the results. class J03E04 { public static void main(String[] args) { double x = 1.0; int i = 0; while(i < 1000) { System.out.println(x); x += 0.0001; i++; } } } ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ 3.10 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. ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ Conversions In general, conversion 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.5 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. 3.11 Experiment 3.6 Step 1. Compile the program J03E06.java. Record and explain the results. class J03E06 { 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. ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ 3.12 Step 3. The revised program in Step 2 contains two conversions. Identify the statements with conversions and classify each conversion as either an automatic conversion (coercion) or a forced conversion (cast). ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ Experiment 3.7 Step 1. Compile and execute the program J03E07A.java. Explain the results. In particular, what conversion is taking place? class J03E07 { public static void main(String[] args) { int x; x = 65; while(x < 70) { System.out.println(x + " " + (char)x); x = x + 1; } } } ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ 3.15 Optional Post-Laboratory Problems 3.1. Suppose you want to write a program to manipulate integers lying outside the range of Integer.MIN_VALUE and Integer.MAX_VALUE? Java provides the primitive data type long for such an occasion. Although long has a greater range of representation compared to int, it too has its limitations. Find these values which are defined in the class Long. Do the same for the fourth integer type short which has a related class Short. Present the results in a neat format in which the min and max values are recorded for each of the four integer primitive types, listing them in order according to the number of bits used for storage. 3.2. There is a second floating-point primitive, float, and a related class Float. Write a program that prints out the MIN_VALUE and MAX_VALUE for a float. How does the range of float values compare to the range of double values? Which data type uses the larger number of bits for storage? 3.3. Write a program that receives a user-entered integer and prints the integer as a binary numeral. Use the method public static String toBinaryString(int n) from the Integer class that, when given an integer value returns a String containing the integer's binary representation. For example, Integer.toBinaryString(13) returns "1101" and, if x equals 4, then Integer.toBinaryString(x) returns "100". 3.4. Write a program that prints decimal numbers in the range 1 to 200 and their binary and hexadecimal equivalents. Hint: The Integer class also contains the method public static String toHexString(int n) Here is partial output: 1 1 1 2 10 2 3 11 3 4 100 4 . . . 200 11001000 c8 3.5. Write a program that prints a table of the printable ASCII subset of Unicode characters. The printable characters have values of 32 to 126. 3.6. A computer system does not necessarily support the entire Unicode character set. The Character class contains constants and methods pertaining to the primitive type char. Write a program that prints the max and min values for type char both as type char and as type int. Note: If no character appears to be printed, that could mean that the character is a space or is non-printable. If, a “?” is printed, that could mean that the character is a question mark or is undefined on this system. Refer back to Experiment 3.5, Step 2 in which you 3.16 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).
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved