Search This Blog(textbook name or author as the keywords)You can cantact me by the Contact Form

9/3/14

Starting Out with Java: From Control Structures through Objects , 5/EGaddis Lab Manual and Source Code solutions manual and test bank

Starting Out with Java: From Control Structures through Objects , 5/EGaddis Lab Manual and Source Code solutions manual and test bank

 

Chapter 1 Lab

Algorithms, Errors, and Testing

(Solution)

Lab Objectives

  • Be able to write an algorithm
  • Be able to compile a Java program
  • Be able to execute a Java program using the Sun JDK or a Java IDE
  • Be able to test a program
  • Be able to debug a program with syntax and logic errors.

Introduction

Your teacher will introduce your computer lab and the environment you will be using for programming in Java.

In chapter 1 of the textbook, we discuss writing your first program. The example calculates the user’s gross pay. It calculates the gross pay by multiplying the number of hours worked by hourly pay rate. However, it is not always calculated this way. What if you work 45 hours in a week? The hours that you worked over 40 hours are considered overtime. You will need to be paid time and a half for the overtime hours you worked.

In this lab, you are given a program which calculates user’s gross pay with or without overtime. You are to work backwards this time, and use pseudocode to write an algorithm from the Java code. This will give you practice with algorithms while allowing you to explore and understand a little Java code before we begin learning the Java programming language.

You will also need to test out this program to ensure the correctness of the algorithm and code. You will need to develop test data that will represent all possible kinds of data that the user may enter.

You will also be debugging a program. There are several types of errors. In this lab, you will encounter syntax and logic errors. We will explore runtime errors in lab 2.

1. Syntax Errors—errors in the “grammar” of the programming language. These are caught by the compiler and listed out with line number and error found. You will learn how to understand what they tell you with experience. All syntax errors must be corrected before the program will run. If the program runs, this does not mean that it is correct, only that there are no syntax errors. Examples of syntax errors are spelling mistakes in variable names, missing semicolon, unpaired curly braces, etc.

2. Logic Errors—errors in the logic of the algorithm. These errors emphasize the need for a correct algorithm. If the statements are out of order, if there are errors in a formula, or if there are missing steps, the program can still run and give you output, but it may be the wrong output. Since there is no list of errors for logic errors, you may not realize you have errors unless you check your output. It is very important to know what output you expect. You should test your programs with different inputs, and know what output to expect in each case. For example, if your program calculates your pay, you should check three different cases: less than 40 hours, 40 hours, and more than 40 hours. Calculate each case by hand before running your program so that you know what to expect. You may get a correct answer for one case, but not for another case. This will help you figure out where your logic errors are.

3. Run time errors—errors that do not occur until the program is run, and then may only occur with some data. These errors emphasize the need for completely testing your program.

Task #1 Writing an Algorithm

  1. Copy the file Pay.java (see code listing 1.1) from the Student CD or as directed by your instructor.
  2. Open the file in your Java Integrated Development Environment (IDE) or a text editor as directed by your instructor. Examine the file, and compare it with the detailed version of the pseudocode in step number 3, section 1.6 of the textbook. Notice that the pseudocode does not include every line of code. The program code includes identifier declarations and a statement that is needed to enable Java to read from the keyboard. These are not part of actually completing the task of calculating pay, so they are not included in the pseudocode. The only important difference between the example pseudocode and the Java code is in the calculation. Below is the detailed pseudocode from the example, but without the calculation part. You need to fill in lines that tell in English what the calculation part of Pay.java is doing.

Display “How many hours did you work?”.

Input hours.

Display “How much do you get paid per hour?”.

Input rate.

If hours are 40 or less, then store the value of hours times rate in the pay variable

Else store the value of 40 times rate (regular pay) added together with 1.5 times rate times the difference of hours and 40 (overtime pay) in the pay variable.

Display the value in the pay variable.

Task #2 Compile and Execute a Program

  1. Compile the Pay.java using the Sun JDK or a Java IDE as directed by your instructor.
  2. You should not receive any error messages.
  3. When this program is executed, it will ask the user for input. You should calculate several different cases by hand. Since there is a critical point at which the calculation changes, you should test three different cases: the critical point, a number above the critical point, and a number below the critical point. You want to calculate by hand so that you can check the logic of the program. Fill in the chart below with your test cases and the result you get when calculating by hand.
  4. Execute the program using your first set of data. Record your result. You will need to execute the program three times to test all your data. Note: you do not need to compile again. Once the program compiles correctly once, it can be executed many times. You only need to compile again if you make changes to the code.

Hours

Rate

Pay (hand calculated)

Pay (program result)

40

10

400

400.0

30

10

300

300.0

45

10

475

475.0

Task #3 Debugging a Java Program

  1. Copy the file SalesTax.java (see code listing 1.2) from the Student CD or as directed by your instructor.
  2. Open the file in your IDE or text editor as directed by your instructor. This file contains a simple Java program that contains errors. Compile the program. You should get a listing of syntax errors. Correct all the syntax errors, you may want to recompile after you fix some of the errors.
  3. When all syntax errors are corrected, the program should compile. As in the previous exercise, you need to develop some test data. Use the chart below to record your test data and results when calculated by hand.
  4. Execute the program using your test data and recording the results. If the output of the program is different from what you calculated, this usually indicates a logic error. Examine the program and correct logic error. Compile the program and execute using the test data again. Repeat until all output matches what is expected.

Item

Price

Tax

Total(calculated)

Total (output)

Shirt

10.00

0.55

10.55

10.55

Jeans

19.99

1.10

21.09

21.08945

Code Listing 1.1 (Pay.java)

//This program calculates the user's gross pay

import java.util.Scanner; //to be able to read from the keyboard

public class Pay

{

public static void main(String [] args)

{

//create a Scanner object to read from the keyboard

Scanner keyboard = new Scanner(System.in);

//identifier declarations

double hours; //number of hours worked

double rate; //hourly pay rate

double pay; //gross pay

//display prompts and get input

System.out.print("How many hours did you work? ");

hours = keyboard.nextDouble();

System.out.print("How much do you get paid per hour? ");

rate = keyboard.nextDouble();

//calculations

if(hours <= 40)

pay = hours * rate;

else

pay = (hours - 40) * (1.5 * rate) + 40 * rate;

//display results

System.out.println("You earned $" + pay);

}

}


Code Listing 1.2 (SalesTax.java)

//This program calculates the total price which includes sales tax

//solution

import java.util.Scanner;

public class SalesTax

{

public static void main(String[] args)

{

//identifier declarations

final double TAX_RATE = 0.055;

double price;

double tax;

double total;

String item;

//create a Scanner object to read from the keyboard

Scanner keyboard = new Scanner(System.in);

//display prompts and get input

System.out.print("Item description: ");

item = keyboard.nextLine();

System.out.print("Item price: $");

price = keyboard.nextDouble();

//calculations

tax = price * TAX_RATE;

total = price + tax;

//display results

System.out.print(item + " $");

System.out.println(price);

System.out.print("Tax $");

System.out.println(tax);

System.out.print("Total $");

System.out.println(total);

}

}

 

 

Chapter 2

MULTIPLE CHOICE

1. Which one of the following would contain the translated Java byte code for a program named Demo?

a.

Demo.java

b.

Demo.code

c.

Demo.class

d.

Demo.byte

ANS: C

2. To compile a program named First, use the following command:

a.

java First.java

b.

javac First

c.

javac First.java

d.

compile First.javac

ANS: C

3. A Java program must have at least one of these:

a.

Class definition

b.

Variable

c.

Comment

d.

System.out.println(); statement

ANS: A

4. In Java, the beginning of a comment is marked with:

a.

//

b.

""

c.

;

d.

#

ANS: A

5. The term ___________ typically refers to the device that displays console output.

a.

Standard output device

b.

Central processing unit

c.

Secondary storage device

d.

Liquid crystal display

ANS: A

6. In Java, ___________ must be declared before they can be used.

a.

Variables

b.

Literals

c.

Key words

d.

Comments

ANS: A

7. If the following Java statements are executed, what will be displayed?

System.out.println("The top three winners are\n");

System.out.print("Jody, the Giant\n");

System.out.print("Buffy, the Barbarian");

System.out.println("Adelle, the Alligator");

a.

The top three winners are

Jody, the Giant

Buffy, the Barbarian

Adelle, the Alligator

b.

The top three winners are

Jody, the Giant\nBuffy, the BarbarianAdelle, the Alligator

c.

The top three winners are Jody, the Giant\nBuffy, the BarbarianAdelle, and the Albino

d.

The top three winners are

Jody, the Giant

Buffy, the BarbarianAdelle, the Alligator

ANS: D

8. This is a value that is written into the code of a program.

a.

literal

b.

assignment statement

c.

variable

d.

operator

ANS: A

9. When the + operator is used with strings, it is known as the:

a.

Assignment operator

b.

String concatenation operator

c.

Addition operator

d.

Combined assignment operator

ANS: B

10. What would be printed out as a result of the following code?

System.out.println("The quick brown fox" +

"jumped over the \n"

"slow moving hen.");

a.

The quick brown fox jumped over the \nslow moving hen.

b.

The quick brown fox jumped over the

slow moving hen.

c.

The quick brown fox

jumped over the

slow moving hen.

d.

Nothing. This is an error.

ANS: D

11. Which of the following is not a rule that must be followed when naming identifiers?

a.

The first character must be one of the letters a-z, A-Z, and underscore or a dollar sign.

b.

Identifiers can contain spaces.

c.

Uppercase and lowercase characters are distinct.

d.

After the first character, you may use the letters a-z, A-Z, the underscore, a dollar sign, or digits 0-9.

ANS: B

12. Which of the following cannot be used as identifiers in Java?

a.

Variable names

b.

Class names

c.

Key words

d.

All of the above

e.

None of the above

ANS: C

13. In Java, it is standard practice to capitalize the first letter of:

a.

Class names

b.

Variable names

c.

Key words

d.

Literals

ANS: A

14. Which of the following is not a primitive data type?

a.

short

b.

long

c.

float

d.

String

ANS: D

15. Which of the following is valid?

a.

float y;

y = 54.9;

b.

float y;

double z;

z = 934.21;

y = z;

c.

float w;

w = 1.0f;

d.

float v;

v = 1.0;

ANS: C

16. The boolean data type may contain values in the following range of values

a.

true or false

b.

-128 to + 127

c.

- 2,147,483,648 to +2,147,483,647

d.

- 32,768 to +32,767

ANS: A

17. Character literals are enclosed in _____; string literals are enclosed in _____.

a.

single quotes; single quotes

b.

double quotes; double quotes

c.

single quotes; double quotes

d.

double quotes; single quotes

ANS: C

18. What is the result of the following expression?

10 + 5 * 3 - 20

a.

-5

b.

5

c.

25

d.

-50

ANS: B

19. What is the result of the following expression?

25 / 4 + 4 * 10 % 3

a.

19

b.

5.25

c.

3

d.

7

ANS: D

20. What will be displayed as a result of executing the following code?

int x = 5, y = 20;

x += 32;

y /= 4;

System.out.println("x = " + x + ", y = " + y);

a.

x = 32, y = 4

b.

x = 9, y = 52

c.

x = 37, y = 5

d.

x = 160, y = 80

ANS: C

21. What will be the value of z as a result of executing the following code?

int x = 5, y = 28;

float z;

z = (float) (y / x);

a.

5.60

b.

5.6

c.

3.0

d.

5.0

ANS: D

22. What will be the displayed when the following code is executed?

final int x = 22, y = 4;

y += x;

System.out.println("x = " + x +

", y = " + y);

a.

x = 22, y = 4

b.

x = 22, y = 26

c.

x = 22, y = 88

d.

Nothing, this is an error

ANS: D

23. In the following Java statement what value is stored in the variable name?

String name = "John Doe";

a.

John Doe

b.

The memory address where "John Doe" is located

c.

name

d.

The memory address where name is located

ANS: B

24. What will be displayed as a result of executing the following code?

int x = 6;

String msg = "I am enjoying this class.";

String msg1 = msg.toUpperCase();

String msg2 = msg.toLowerCase();

char ltr = msg.charAt(x);

int strSize = msg.length();

System.out.println(msg);

System.out.println(msg1);

System.out.println(msg2);

System.out.println("Character at index x = " +

ltr);

System.out.println("msg has " + strSize +

"characters.");

a.

I am enjoying this class.

I AM ENJOYING THIS CLASS.

i am enjoying this class.

Character at index x = e

msg has 24 characters.

b.

I am enjoying this class.

I AM ENJOYING THIS CLASS.

i am enjoying this class.

Character at index x = e

msg has 25 characters.

c.

I am enjoying this class.

I AM ENJOYING THIS CLASS.

i am enjoying this class.

Character at index x = n

msg has 24 characters.

d.

I am enjoying this class.

I AM ENJOYING THIS CLASS.

i am enjoying this class.

Character at index x = n

msg has 25characters.

ANS: D

25. What will be displayed as a result of executing the following code?

public class test

{

public static void main(String[] args)

{

int value1 = 9;

System.out.println(value1);

int value2 = 45;

System.out.println(value2);

System.out.println(value3);

value = 16;

}

}

a.

9

45

16

b.

94516

c.

9 45 16

d.

Nothing, this is an error

ANS: D

26. Which of the following is not a valid comment statement?

a.

// comment 1

b.

/* comment 2 */

c.

*/ comment 3 /*

d.

/** comment 4 */

ANS: C

27. When saving a Java source file, save it with an extension of

a.

.javac

b.

.class

c.

.src

d.

.java

ANS: D

28. Every Java application program must have

a.

a class named MAIN

b.

a method named main

c.

comments

d.

integer variables

ANS: B

29. To print "Hello, world" on the monitor, use the following Java statement

a.

SystemOutPrintln("Hello, world");

b.

System.out.println{"Hello, world"}

c.

System.out.println("Hello, world");

d.

Print "Hello, world";

ANS: C

30. To display the output on the next line, you can use the println method or use this escape sequence in the print method.

a.

\n

b.

\r

c.

\t

d.

\b

ANS: A

31. This is a named storage location in the computer's memory.

a.

Literal

b.

Constant

c.

Variable

d.

Operator

ANS: C

32. What would be displayed as a result of the following code?

int x = 578;

System.out.print("There are " +

x + 5 + "\n" +

"hens in the hen house.");

a.

There are 583 hens in the hen house.

b.

There are 5785 hens in the hen house.

c.

There are x5\nhens in the hen house.

d.

There are 5785

hens in the hen house.

ANS: D

33. Variables are classified according to their

a.

value

b.

data type

c.

names

d.

location in the program

ANS: B

34. The primitive data types only allow a(n) _____ to hold a single value.

a.

variable

b.

object

c.

class

d.

literal

ANS: A

35. If x has been declared an int, which of the following statements is invalid?

a.

x = 0;

b.

x = -58932;

c.

x = 1,000;

d.

x = 592;

ANS: C

36. Given the declaration double r;, which of the following statements is invalid?

a.

r = 326.75;

b.

r = 9.4632e15;

c.

r = 9.4632E15;

d.

r = 2.9X106;

ANS: D

37. Variables of the boolean data type are useful for

a.

working with small integers

b.

evaluating true/false conditions

c.

working with very large integers

d.

evaluating scientific notation

ANS: B

38. What is the result of the following expression?

25 - 7 * 3 + 12 / 3

a.

6

b.

8

c.

10

d.

12

ANS: B

39. What is the result of the following expression?

17 % 3 * 2 - 12 + 15

a.

7

b.

8

c.

12

d.

105

ANS: A

40. What will be displayed after the following statements have been executed?

int x = 15, y = 20, z = 32;

x += 12;

y /= 6;

z -= 14;

System.out.println("x = " + x +

", y = " + y +

", z = " +z);

a.

x = 27, y = 3.333, z = 18

b.

x = 27, y = 2, z = 18

c.

x = 27, y = 3, z = 18

d.

x = 37, y = 14, z = 4

ANS: C

41. What will be the value of z after the following statements have been executed?

int x = 4, y = 33;

double z;

z = (double) (y / x);

a.

8.25

b.

4

c.

8

d.

8.0

ANS: D

42. This is a variable whose content is read only and cannot be changed during the program's execution.

a.

operator

b.

literal

c.

named constant

d.

reserved word

ANS: C

43. What will be displayed after the following statements have been executed?

final double x;

x = 54.3;

System.out.println("x = " + x );

a.

x = 54.3

b.

x

c.

x = 108.6

d.

Nothing, this is an error.

ANS: D

44. Which of the following is a valid Java statement?

a.

String str = 'John Doe';

b.

string str = "John Doe";

c.

string str = 'John Doe';

d.

String str = "John Doe";

ANS: D

45. What will be displayed as a result of executing the following code?

int x = 8;

String msg = "I am enjoying java.";

String msg1 = msg.toUpperCase();

String msg2 = msg.toLowerCase();

char ltr = msg.charAt(x);

int strSize = msg.length();

System.out.println(msg);

System.out.println(msg1);

System.out.println(msg2);

System.out.println("Character at index x = " +

ltr);

System.out.println("msg has " + strSize +

" characters.");

a.

I am enjoying java.

I AM ENJOYING JAVA.

i am enjoying java.

Character at index x = j

msg has 20 characters.

b.

I am enjoying java.

I AM ENJOYING JAVA.

i am enjoying java.

Character at index x = o

msg has 20 characters.

c.

I am enjoying java.

I AM ENJOYING JAVA.

i am enjoying java.

Character at index x = o

msg has 19 characters.

d.

I am enjoying java.

I AM ENJOYING JAVA.

i am enjoying java.

Character at index x = y

msg has 19 characters.

ANS: C

46. Which of the following does not describe a valid comment in Java?

a.

Single line comments, two forward slashes - //

b.

Multi-line comments, start with /* and end with */

c.

Multi-line comments, start with */ and end with /*

d.

Documentation comments, any comments starting with /** and ending with */

ANS: C

47. Which of the following statements correctly creates a Scanner object for keyboard input?

a.

Scanner kbd = new Scanner(System.keyboard);

b.

Scanner keyboard(System.in);

c.

Scanner keyboard = new Scanner(System.in);

d.

Keyboard scanner = new Keyboard(System.in);

ANS: C

48. Which Scanner class method reads an int?

a.

readInt()

c.

getInt()

b.

nextInt()

d.

read_int()

ANS: B

49. Which Scanner class method reads a String?

a.

readString()

c.

getString()

b.

nextString()

d.

nextLine()

ANS: D

50. Which one of the following methods would you use to convert a string to a double?

a.

Byte.ParseByte

c.

Integer.ParseInt

b.

Long.ParseLong

d.

Double.ParseDouble

ANS: D

TRUE/FALSE

1. A Java program will not compile unless it contains the correct line numbers.

ANS: F

2. All Java statements end with semicolons.

ANS: F

3. Java is a case-insensitive language.

ANS: F

4. Although the dollar sign is a legal identifier character, you should not use it because it is normally used for special purposes.

ANS: T

5. Assuming that pay has been declared a double, the following statement is valid.

pay = 2,583.44;

ANS: F

6. Named constants are initialized with a value, that value cannot be changed during the execution of the program.

ANS: T

7. A variable's scope is the part of the program that has access to the variable.

ANS: T

8. In Java the variable named total is the same as the variable named Total.

ANS: F

9. Class names and key words are examples of variables.

ANS: F

10. Both character literals and string literals can be assigned to a char variable.

ANS: F

11. If the compiler encounters a statement that uses a variable before the variable is declared, an error will result.

ANS: T

12. Programming style includes techniques for consistently putting spaces and indentation in a program so visual cues are created.

ANS: T

No comments:

Post a Comment

Linkwithin

Related Posts Plugin for WordPress, Blogger...