











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 / 19
This page cannot be seen from the preview
Don't miss anything!












In this laboratory you will:
Intuitively, there are differences between the many kinds of data that we want to store in a computer. When entering an order at McDonald’s, a customer chooses to buy a quantity of different kinds of sandwiches at a stated price. The quantity of sandwiches is a whole number such as 1, 2, or
A variable is a name used in a program to refer to an item of data. A variable name, also called an identifier , is chosen by the programmer according to the following rules. An identifier o may consist of uppercase and lowercase letters, digits, $, and the underscore _ o may not begin with a digit o may not be a Java reserved word Examples of legal identifiers are price, choice1, r2d2, taxRate, totalCost. Examples of illegal identifiers are #calories, 2Bad, tax Rate, int.
A Java reserved word is a word that has a special meaning in Java and, therefore, may not be used in any other context. Examples of reserved words are: int, char, class. A good programming practice is to choose variable names that reflect their meaning in the program. Example: price rather than p. A Java guideline is to choose variable names that begin with lower case letters with additional words capitalized. Example: totalCost rather than total_cost. Before a variable can be used in a program, it must be declared; that is, the name and the data type must be described. This is done with a variable declaration statement of the form
"Special: Big Mac for $" + price creates the string "Special: Big Mac for $0.99" String concatenation can be used multiple times in the same expression. Thus, if the value of the string sandwich is "cheeseburger", then the statement System.out.println ("Special: " + sandwich + " for $" + price); displays Special: cheeseburger for $0.
Step 1. Compile and execute the program J02E01.java and record the results below class J02E { public static void main(String[] args) { int num = 2; double price = 1.89; String flavor = "chocolate"; System.out.println(num + " scoops of " + flavor + " ice cream"); } }
Step 2. Replace the single print statement in Step 1 with the two lines System.out.println(num + " scoops of " + flavor + " ice cream"); Compile and execute the modified program and record the results.
Step 3. Modify the code in Step 2 so that the following sentence is printed using the third variable price. There are several ways to do this. Compile and execute the program. Record your solution(s) and the results. 2 scoops of chocolate ice cream for $1.
Step 4. Modify the program by replacing the statement int num = 2; with int num; Compile the program and record the results of this modification.
Our programs are more interesting when the program's user can enter information from the keyboard. The keyboard produces an input stream of bytes called System.in, which is a predefined object available to all Java programs. This stream is normally processed by other objects which perform a particular task.
will cause the two's complement representation of 485 to be stored as the value of the variable num. (In Experiment 2.2 we will explore what occurs when a string, such as "5.7" or "two", is passed to the parseInt method.)
Step 1. Compile and execute the program J02E02.java. In response to the first request enter chocolate, and in response to the second request enter 2. Record the results. import java.util.Scanner; class J02E { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int num; String flavor; System.out.print("What flavor? "); flavor = scan.nextLine(); System.out.print("How many scoops? "); num = scan.nextInt(); System.out.println(num + " scoops of " + flavor + " ice cream"); } }
Step 2. Execute the program again, this time responding with chocolate and 2.5. Record what happens.
Step 3. Execute the program again, this time responding with 15 and 2. Record what happens.
Step 1. Compile and execute the program J02E03.java, which demonstrates how to read a double value from the keyboard. In response to the request enter 5.678. Record what happens. import java.util.Scanner; class J02E { public static void main(String[] args) { Scanner scan = new Scanner(System.in); double d; System.out.print("Enter a real number: "); d = scan.nextDouble(); System.out.println("You entered " + d); } }
Step 2. Execute the program again. Respond with 87 and record the results.
x--; //-- is called auto decrement Expressions such as x++ and x-- can be used in more complex expressions such as in the assignment statement y = 5 + x++; Here, y is assigned the result of adding 5 to x and then x is incremented by 1. In particular, the combination x = 2; y = 5 + x++; results in y being assigned the value 7 and x being assigned 3. Note that the value of x is not incremented until its original value has been used in the computation. If, instead, we want the incremented value to be used in the computation, we would use the expression ++x rather than x++. x = 2; y = 5 + ++x; In this case, after the execution, y is assigned 8 and x is assigned 3. In short, ++x means that the value of x is incremented first and this new x value is used in the remaining computation. Whereas, x++ means that the computation is completed with the original value of x and x is incremented after.
Step 1. Predict the output of the program class J02E { public static void main(String[] args) { System.out.println(7 + 3); ___________ System.out.println(7 - 3); ___________ System.out.println(7 * 3); ___________ System.out.println(7 / 3); ___________ } } Step 2. Compile and execute the program J02E04.java. Record the results and comment on any results that differ from your prediction.
Step 3. Modify the program in Step 1 by replacing each occurrence of 7 with 7.0 and each occurrence of 3 with 3.0. Compile and execute the program. Record the results, commenting on differences from the results in Step 2.
Step 4. Modify the program in Step 3 by replacing each occurrence of 7.0 with 7. Compile and execute the program. Record the results, commenting on differences from the results in Step 3.
Step 5. Modify the 4 print statements as shown. Predict the results. Then, compile and execute the program, recording the results below. Comment on any unexpected results. System.out.println(5 / 2); ___________ System.out.println(5.0 / 2); ___________ System.out.println(5 / 2.5); ___________ System.out.println(0.5 / 2.5); ___________
Step 4. Modify the program by adding a third integer, d, that is assigned a value of 2. Find the average of the three numbers. List your changes. Compile and execute the program. Record the results.
Step 1. The operator % is called the modulus operator. Compile and execute the program J02E06.java. Respond with 7 and 3. Record the results in the first row of the following table. import java.Scanner; class J02E { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter an integer: "); int x = scan.nextInt(); System.out.print("Enter an integer: "); int y = scan.nextInt(); System.out.println(x + " / " + y + " = " + (x / y)); System.out.println(x + " % " + y + " = " + (x % y)); } }
Step 2. Execute the program several times, each time changing the values you enter for x and y. Compile and execute the program. Record the results.
Step 3. What is the meaning of x % y?
Step 1. Predict the result of executing the program J02E07. class J02E { public static void main(String[] args) { int x, y, z; x = 4; y = x++ + 5; System.out.println("x = " + x + " y = " + y); x = 4; y = ++x + 5; System.out.println("x = " + x + " y = " + y); x = 4; y = x-- * 5; z = --x * 5; System.out.println("x = " + x + " y = " + y + " z = " + z); } }
Normally, instructions within a Java program are executed in the order in which they appear. While this may be sufficient for simple programs, it is not flexible enough for more complex tasks. Thus, Java provides a variety of control statements for redirecting the flow of execution. One of these is the while control statement. The while control statement is used to execute a certain block of code repetitively. The general form of the while statement is while ( condition ) { body } where body is a statement or a sequence of statements. If body consists of only one statement, then the opening and closing braces of the while statement can be omitted. Execution of the while statement begins with testing the condition. If true, the statement or statements within the body will be executed, and the condition will be tested once again. This cyclic process continues until the condition is found to be false, at which time control skips to the statement following the closing brace of the while statement. Due to its cyclic nature, we refer to the while statement as a while loop. Thus, the following statements int count = 5; while(count > 0) { System.out.println("Recycle!"); count--; } produce Recycle! Recycle! Recycle! Recycle! Recycle! Initially, count is 5 and the condition is true. Therefore, Recycle! is printed and count is decremented. The loop is executed five times, when count is 5, 4, 3, 2, and 1. When count is 0 , the condition is false and the loop is exited.
We will discuss the details of the condition part of a while statement in Session 5. For now, we merely note that the symbols <, >, == , and != are used to represent "is less than", "is greater than", "is equal to", and "is not equal to", respectively, and combined as in <= and >= to represent "is less than or equal to" and "is greater than or equal to".
Step 1. The following program J02E08.java is designed to print the integers 1 through 5. Compile and execute the program. Record the results. class J02E { public static void main(String[] args) { int count; count = 1; while(count < 5) { count++; System.out.println(count); } } }
Step 2. Explain the changes required to make the program perform as initially intended. Confirm your answer by making these changes and compiling and executing the corrected program.
2.1. Write a program with the declarations int x = 3, y = -2, w = 7, z = 20; that computes and prints the results of each of the following arithmetic problems. According to your results, summarize the precedence rules for the mathematical operators /, %, *, +, -. a. z / x / y b. z + x * y c. (z + x) * y d. z / - y * w e. 5 + z % x f. 5 + z / x g. z % w - z % w h. 9 - x * ( 2 + y ) 2.2. Write a program that calculates the perimeter and area of a rectangle whose integer dimensions are supplied by the user. 2.3. Write a program that calculates the perimeter and area of a rectangle whose real dimensions are supplied by the user. 2.4. Write a program that calculates the circumference and area of a circle whose radius is supplied by the user. Note: Java provides the value of π, use Math.PI 2.5. Write a program that reads in at least one user-entered int, double and String. Use these to print a "silly sentence". For example, if the user enters "elephant", 5, and 3.25, the silly sentence might be "The elephant ate 5 pizzas and gained 3.25 pounds." Be creative!! 2.6. Write a program that calculates a Celsius temperature from the Fahrenheit temperature supplied by the user. Use the formula C = 5/9(F - 32). 2.7. Write a program that calculates a Fahrenheit temperature from the Celsius temperature supplied by the user. Use the formula F = 9/5 C + 32. 2.8. Write a program that prints a user entered word 10 times in a column. 2.9. Write a program that prints a word N times in a column. Both the word and the integer N are supplied by the user. 2.10. Write a program that uses a while loop to find the sum of the numbers between 1 and 10. 2.11. Write a program that uses a while loop to find the sum of the numbers between 1 and N, where N is an integer value supplied by the user. 2.12. Write a program that uses a while loop to print the uppercase letters of the alphabet in a horizontal line. 2.13. In this session you learned a shorthand notation for incrementing/decrementing variables. A similar shorthand consists of an operation symbol followed by an assignment symbol such as *=, +=, -= or /=. Write a program that contains statements similar to those below. Based on the results, state what += appears to represent. Then, experiment with the other arithmetic operations to confirm your hypothesis. Present your findings in an organized manner. Extend your investigation by trying expressions such as x *= 2 + z. int x = 5; x += 3; System.out.println(x); x += 10; System.out.println(x);