[Solved] SOLVED:Chapter 1 Lab Algorithms, Errors, and Testing

30 $

File Name: SOLVED:Chapter_1_Lab_Algorithms,_Errors,_and_Testing.zip
File Size: 489.84 KB

SKU: [Solved] SOLVED:Chapter 1 Lab Algorithms, Errors, and Testing Category: Tag:

Or Upload Your Assignment Here:


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.IntroductionYour 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 examplecalculates 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 ©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.2formula, 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 Algorithm1. 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 texteditor as directed by your instructor. Examine the file, and compare it with thedetailed 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 programcode 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 importantdifference between the example pseudocode and the Java code is in the calculation. Below is the detailed pseudocode from the example, but without thecalculation part. You need to fill in lines that tell in English what the calculationpart 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.Display the value in the pay variable.©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

Task #2 Compile and Execute a Program1. Compile the Pay.java using the Sun JDK or a Java IDE as directed by yourinstructor.2. You should not receive any error messages.3. When this program is executed, it will ask the user for input. You shouldcalculate several different cases by hand. Since there is a critical point at whichthe calculation changes, you should test three different cases: the critical point, anumber above the critical point, and a number below the critical point. You wantto calculate by hand so that you can check the logic of the program. Fill in thechart 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 willneed to execute the program three times to test all your data. Note: you do notneed to compile again. Once the program compiles correctly once, it can beexecuted many times. You only need to compile again if you make changes to thecode.Task #3 Debugging a Java Program1. Copy the file SalesTax.java (see code listing 1.2) from the Student CD or asdirected by your instructor.2. Open the file in your IDE or text editor as directed by your instructor. This filecontains a simple Java program that contains errors. Compile the program. Youshould get a listing of syntax errors. Correct all the syntax errors, you may wantto recompile after you fix some of the errors.3. When all syntax errors are corrected, the program should compile. As in theprevious exercise, you need to develop some test data. Use the chart below torecord your test data and results when calculated by hand.4. Execute the program using your test data and recording the results. If the outputof the program is different from what you calculated, this usually indicates a logicerror. Examine the program and correct logic error. Compile the program andexecute using the test data again. Repeat until all output matches what isexpected.Item Price Tax Total(calculated) Total (output)Hours Rate Pay (handcalculated)Pay (programresult)©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.4Code Listing 1.1 (Pay.java)//This program calculates the user’s gross payimport java.util.Scanner; //to be able to read from the keyboardpublic class Pay{public static void main(String [] args){//create a Scanner object to read from the keyboardScanner keyboard = new Scanner(System.in);//identifier declarationsdouble hours; //number of hours workeddouble rate; //hourly pay ratedouble pay; //gross pay//display prompts and get inputSystem.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();//calculationsif(hours <= 40)pay = hours * rate;elsepay = (hours – 40) * (1.5 * rate) + 40 * rate;//display resultsSystem.out.println(“You earned $” + pay);}}©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.5Code Listing 1.2 (SalesTax.java)//This program calculates the total price which includes sales taximport java.util.Scanner;public class SalesTax{public static void main(String[] args){//identifier declarationsfinal double TAX_RATE = 0.055;double price;double taxdouble total;String item;//create a Scanner object to read from the keyboardScanner keyboard = new Scanner(System.in);//display prompts and get inputSystem.out.print(“Item description: “);item = keyboard.nextLine();System.out.print(“Item price: $”);price = keyboard.nextDouble();//calculationstax = price + TAX_RATE;totl = price * tax;//display resultsSystem.out.print(item + ” $”);System.out.println(price);System.out.print(“Tax $”);System.out.println(tax);System.out.print(“Total $”);System.out.println(total);}}

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] SOLVED:Chapter 1 Lab Algorithms, Errors, and Testing
30 $