Issuu on Google+

Full file at http://testbank360.eu/solution-manual-fundamentals-of-java-3rd-edition-lambert

LESSON 2 – FIRST JAVA PROGRAMS FILL IN THE BLANKS Complete the following sentences by writing the word or words in the blanks provided. 1. Java bears some resemblance to the C++ programming language, making it easier for programmers who know that language to pick up Java very quickly. 2. The Java compiler translates Java source code into byte code, not machine code. 3. Java program statements must be terminated by a(n) semicolon. 4. In the statement, System.out.println("Hello"), the period or dot between the word out and println is called the method selector operator. 5. The import statement can be used in a Java program to include an already written class in the new program.

TRUE/FALSE Circle T if the statement is true or F if the statement is false. . T

F

1. Java programs typically run slower than the same program developed using C++.

T

F

2. It is possible to limit the capabilities of a Java program because it runs within a virtual machine.

T

F

3. Object-oriented programs send messages to objects to accomplish specific tasks.

T

F

4. There are four separate and distinct steps involved in creating and running a Java program, and they are Edit (enter source code), Compile (convert to byte code), Interpret (convert to machine code), and Execute (run the machine code).

T

F

5. The process of creating an object before it can be used is called instantiating the object.

MULTIPLE CHOICE Select the best response for the following statements. 1. Which of the following is required in order to run a Java program on any given computer? b. a Java Virtual Machine a. a Windows operating system c. an Internet connection d. a Java object code translator 2. When a message is sent to object, the string of characters that appears within the parentheses

following the message is called a(n) ________________. a. argument c. action

b. note d. parameter

3. Java source code files are created and saved using what file extension? b. .java a. .jav c. .jsrc d. .jcod 4. Which of the following statements could be used to create a new object called MyBall, of a

class named Ball? a. Ball MyBall = new Ball(); c. MyBall(Ball) = new Ball;

b. Ball MyBall = new Ball; d. Ball.MyBall = new Ball;


Full file at http://testbank360.eu/solution-manual-fundamentals-of-java-3rd-edition-lambert

5. The code for application windows in Java is located in which class? b. JFrame a. JPanel c. JWindow d. JApplication

WRITTEN QUESTIONS Write your answers to the following questions. 1. What makes a Java program so easy to transport to another type of computer? How is that done? Java programs are usually very easy to transport to a different type of computer because of the JVM, the Java Virtual Machine. When a Java source code file is created, it is initially compiled into byte code. This is not true machine code, but a type of pseudo machine code. The byte code file is interpreted as the program is executed, on the machine executing the program, by the JVM. Any computer with a JVM installed on it can translate the byte code file and execute it on that machine. 2. What makes Java a more secure language than any other language? Is it possible to violate Java's security features? Java programs are considered more secure than programs written in other programming languages because of the Java Virtual Machine. Java programs all execute inside the JVM, which means they cannot perform any activities not supported by the JVM. This limits the ability of a Java program to cause harm to the computer it is running on. It is possible to find ways around Java's security measures, although it is quite rare that that occurs. 3. Explain the process of just-in-time (JIT) compilation. What is the end result of a program that is executed using JIT? Some, but not all, JVM's now include the ability to perform JIT compilation when a Java byte code file is processed. JIT translates each instruction into machine code the first time each instruction is executed. This translated instruction is retained in memory, and if the instruction is executed again, the already translated machine code instruction is executed instead of retranslating the byte code instruction. This makes the Java program run much faster than one that must translate every instruction every time it is executed. 4. Briefly describe the difference between a graphical user interface (GUI) and a terminal I/O interface. The GUI is an interface commonly used in most Windows and Macintosh software applications. The user is presented windows, dialog boxes, command and option buttons, and icons that can be manipulated to perform desired tasks. The terminal I/O interface is familiar to anyone who has worked on DOS or MS-DOS PC systems, or who works in a Unix environment. In a terminal I/O interface, the user is presented a prompt on the display screen. They must enter some system command, or a response to a question asked by the system at the prompt. When the Enter key is pressed, the program processes their input, and a new prompt, or possibly the result of some request, is then presented to the user. The terminal I/O interface is also sometimes called line-io, since every command must be entered on the current prompt line. 5. What is a "development environment", and what types of development environments exist for creating Java programs? The development environment is the method by which a Java program is created, compiled and executed. In the Unix environment, a Java program is usually written using a text editor, and then compiled and run by entering commands at the Unix command line. A DOS development environment is very similar to the Unix environment. The program may be written in a Windows


Full file at http://testbank360.eu/solution-manual-fundamentals-of-java-3rd-edition-lambert

text editor such as Notepad or WordPad, and then a DOS window can be opened to enter the commands to compile and execute the program. There are also programs called integrated development environments (IDE) available in Windows and Mac operating systems. In an IDE, the program is written, compiled and run from within the IDE. It has its own editor to create the program, and usually buttons or menus that can be accessed to compile and run the program without leaving the editor. 6. What is the difference between a variable and a variable value? How is a value assigned to a variable? A variable is a named location in memory that can be accessed and modified as the program executes. The variable value is the data contained within a variable. For example, a variable could be defined with the name of Alpha. The variable Alpha could then be set to contain a value of 25, which becomes the variables value. The assignment statement (=) is used to assign a value to a variable. For example, Alpha = 25; would assign the value 25 to the variable Alpha. 7.

What is a layout manager, and what functionality does it provide? Layout managers are created and attached to panels and frames in order to influence how components added to the panel or frame are positioned. Every container has a default layout manager, but other layout managers can be applied to them if needed. For frames, the default layout manager is an instance of the BorderLayout class. This layout manager divides a frame or panel into five areas: north, south, east, west, and center. Graphical components can be added to the frame or panel in any of these five quadrants. If fewer than five graphical components are added, the layout manager stretches the graphical components to fill the entire frame or panel. A GridLayout layout manager divides a frame or panel into a specified number of rows and columns (a grid). Each “cell� in the grid has the same size. When graphical components are added to a frame or panel with this layout manager, they are added to the next open cell in the grid (starting at the top-left of the grid, moving across rows, and then down to the next row).

CASE PROJECT In the case project for Chapter 1, you were asked to consider the design and analysis of a program that assists its user in creating a plan to pay off his/her debts. For this case project, you will be implementing a scaled-down version of this program. During the first case project, you may have come up with the following inputs and outputs: Inputs: Interest rate for each debt Amount of each debt Minimum monthly payment due for each debt Monthly income that can be used to pay down debt Outputs: Amount to pay for each debt for the given month The final balance of each debt after payments have been made The amount of interest paid on each debt for the month The amount of principal paid on each debt for the month Furthermore, you may have identified the following sub-problems: 1. 2. 3.

Gather inputs Perform calculations and store results Display final output


Full file at http://testbank360.eu/solution-manual-fundamentals-of-java-3rd-edition-lambert

You will now write a short program that collects the inputs for this debt (amount, minimum monthly payment, interest rate, and the user’s disposable income), and displays the outputs for this debt (amount to pay, final balance, interest paid, and principal paid). You should assume that there will be only one debt and that you are only performing one month’s worth of “debt counseling”. To write an accurate program, you will need the correct formulas. The following formula descriptions should get you started: 1. 2. 3. 4.

For this case project, since there is only one type of debt to consider, the amount to pay on that debt should be equal to the amount of disposable income. The amount of interest is the monthly interest rate multiplied by the initial debt balance. Remember that the interest rate given as an input will represent a yearly interest rate. The amount of principal paid will be the difference between the amount to pay on the debt and the interest paid. The final debt balance will be the initial debt balance plus the interest minus the payment amount.

The following is a “shell” for this program. The comments (lines that start with “// TO DO:”) indicate the tasks that you must complete. All variable declarations are provided. Copy this “shell” into your text editor, save it as a .java file, and fill in the appropriate program statements to make the program behave as expected.

import java.util.Scanner; public class DebtPaymentProgram

{

public static void main(String[] args) { // declare variables Scanner reader = new Scanner(System.in); double debtBalance; double interestRate; double minimumPayment; double disposableIncome; double paymentAmount; double finalBalance; double principalPaid; double interestPaid; // TO DO: gather inputs (debtBalance, interestRate, // minimumPayment, and disposableIncome). // TO DO: calculate amount of disposable income to // use on debt. // TO DO: calculate the amount of interest that will // be added to the debt during the month. // This will also be the amount of interest // that will be paid for the month. // TO DO: calculate the amount of principal that will // be paid for the month. // TO DO: calculate the final balance of the debt at // the end of the month. // TO DO: display the results (paymentAmount, // finalBalance, principalPaid, and // interestPaid).


Full file at http://testbank360.eu/solution-manual-fundamentals-of-java-3rd-edition-lambert

}


Full file at http://testbank360.eu/solution-manual-fundamentals-of-java-3rd-edition-lambert package chapter2; import java.util.Scanner; /** * Chapter 2's version of the debt payment program processes a single debt for a * single month. It prompts the user for the starting balance of the debt, the * interest rate, the minimum payment, and the user's disposable income. The * amount to pay on the debt, the interest paid, the principal paid, and * the debt's final balance after the payment has been applied are * calculated and are the outputs of the program. */ public class DebtPaymentProgram { /** * Executes the debt payment program. Gathers the inputs (debt balance, * interest rate, minimum payment, and disposable income), calculates the * amount that should be paid on the debt, and displays the output (amount * to pay on the debt, principal paid, interest paid, and final debt * balance). * * @param args * Any input arguments to the program */ public static void main(String[] args) { // declare variables Scanner reader = new Scanner(System.in); double interestRate; double debtBalance; double finalBalance; double disposableIncome; double principalPaid; double interestPaid; double minimumPayment; double paymentAmount; // TO DO: gather inputs (debtBalance, interestRate, // minimumPayment, and disposableIncome). System.out.print("Enter the amount of the debt: "); debtBalance = reader.nextDouble(); System.out.print("Enter the interest rate: "); interestRate = reader.nextDouble(); System.out.print("Enter the minimum payment: "); minimumPayment = reader.nextDouble(); System.out.print("Enter disposable income: "); disposableIncome = reader.nextDouble(); // TO DO: calculate amount of disposable income to // use on the debt. paymentAmount = disposableIncome; // TO DO: calculate the amount of interest that will // be added to the debt during the month. // This will also be the amount of interest // that will be paid for the month. interestPaid = debtBalance * (interestRate / 12); // TO DO: calculate the amount of principal that will // be paid for the month. principalPaid = paymentAmount - interestPaid; // TO DO: calculate the final balance of the debt at // the end of the month. finalBalance = debtBalance - principalPaid; // TO DO: display the results (paymentAmount, // finalBalance, principalPaid, and // interestPaid). System.out.println("Your income that can be applied to this debt is " + disposableIncome); System.out.println("You should pay $" + paymentAmount + " towards this debt"); System.out.println("You are paying $" + interestPaid + " in interest."); System.out.println("You are paying your principal down by $"


Full file at http://testbank360.eu/solution-manual-fundamentals-of-java-3rd-edition-lambert

}

}

+ principalPaid); System.out.println("Your debt at the end of the month will be $" + finalBalance);


Full file at http://testbank360.eu/solution-manual-fundamentals-of-java-3rd-edition-lambert

Now that you have the program written, compile and test it. When testing a program you should always create test plans for documentation. This includes coming up with realistic inputs to the program and calculating the expected output. After this is done, you can run the program with the test inputs and verify that the program output matches the expected output. In the following tables, you are given a series of test inputs and the expected program output. Run your program with each set of inputs, fill in the actual output of the program, and compare it to the expected output. Discrepancies between the expected output and actual output often indicate that you made a calculation error in the program.

Input: Test Case

Initial Debt

Interest Rate 0.20 0.30 0.25 0.00

Minimum Payment 200 20 0 40

Disposable Income 1000 1000 1000 500

1 2 3 4

10000 1000 0 2000

Interest Paid 166.67 25 0 0

Principal Paid 833.33 975 1000 500

Final Balance 9166.67 25 -1000 1500

Expected Output: Test Case 1 2 3 4

Payment Amount 1000 1000 1000 500

Actual Output: Test Case 1

Payment Amount 1000

2

1000

3 4

1000 500

Interest Paid

Principal Paid

Final Balance

166.6666666666666 6 24.99999999999999 6 0.0 0.0

833.3333333333334

9166.666666666666

975.0

25.0

1000.0 500.0

-1000.0 1500.0

When writing and running the program, consider the fact that this program is being developed for a client. Is the program easy to use? Are there appropriate prompts for input? Is the output data accompanied by appropriate labels? Is the data displayed correctly? These questions and more will be addressed in future case projects. From the actual output of the program, a few potential problems should be apparent. First, it is clear from all of the test cases that the output format is not appropriate for monetary values (Formatting of output is covered in chapter 7). Second, test cases 1 and 2 illustrate the effect that limited numerical precision for floating-point values can have. Finally, in test case 3, it is possible that the program should prevent the user from making a payment on the debt, because there is no balance for the debt. This is an issue that should be discussed with the client.


Solution manual fundamentals of java 3rd edition lambert