Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part One: Chapter 1 Lab Task #1 Compile and Execute a Program 1. Compile the Pay.java using the NetBeans or other Java IDE. 2. You

Part One:

Chapter 1 Lab

Task #1 Compile and Execute a Program

1. Compile the Pay.java using the NetBeans or other Java IDE.

2. You should not receive any error messages.

3. When this program is executed, it will ask the user for input. You should test three different cases: the working hour >, <, and = 40 hours.

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.

Task #2 Debugging a Java Program

1. Open the file SalesTax.java in NetBeans or other Java IDE. 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. If you use NetBeans, the recompiling may be automatic.

2. When all syntax errors are corrected, the program should compile. As in the previous exercise, you need to develop some test data.

3. Execute the program using your test data and save a screen shot about the output. There is a logic error on this program. Examine the program and correct logic error. Compile the program and execute using the test data again.

Use the following test data to check your logical error:

Comment Prompt:

C: Mywork\homework>java Sale Tax

Item description: A doll

Item price : $10.0

Tax $0.55

Total: $10.55

C:\MyWork\ homework>

Pay-notepad:

//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); } }

SalesTax-Notepad:

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

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; totl = 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); } }

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Part Two:

Chapter 2 Lab

Lab Objectives

Write arithmetic expressions to accomplish a task

Use casting to convert between primitive types

Use a value-returning library method and a library constant

Use string methods to manipulate string data

Communicate with the user by using the Scanner class

Task #1 Correcting Logic Errors in Formulas

Copy and compile the source file NumericTypes.java, run the program, and observe the output. Some of the output is incorrect. You need to correct logic errors in the average formula and the temperature conversion formula. The logic errors could be due to conversion between data types, order of operations, or formula problems. The necessary formulas are

average = (score1+score2) / numberOfScores

C = 5/9 (F-32)

Make sure that the output makes sense before you continue. The average of 95 and 100 should be 97.5 and the temperature that water boils is 100 degrees Celsius

Task #2 Using the Scanner Class for User Input

Add an import statement above the class declaration to make the Scanner class available to your program. In the main method, create a Scanner object and connect it to the System.in object.

Prompt the user to enter his/her first name.

Read the name from the keyboard using the nextLine method, and store it into a variable called firstName (you will need to declare any variables you use).

Prompt the user to enter his/her last name.

Read the name from the keyboard and store it in a variable called lastName.

Concatenate the firstName and lastName with a space between them and store the result in a variable called fullName.

Print out the fullName.

Compile, debug, and run, using your name as test data.

Task #3 Working with Strings

Use the charAt method to get the first character in firstName and store it in a variable called firstInitial (you will need to declare any variables that you use).

Print out the users first initial.

Use the toUpperCase method to change the fullName to all capitals and store it back into the fullName variable

Add a line that prints out the value of fullName and how many characters (including the space) are in the string stored in fullName (use the method length to obtain that information).

Compile, debug, and run. The new output added on after the output from the previous tasks should have your initials and your full name in all capital letters.

Numeric Tapes:

/** This program demonstrates how numeric types and operators behave in Java */

//TASK #2 Add import statement here to use the Scanner class //import java.util.Scanner; // TASK #2 to do - uncomment this line by deleting the leading // // Note: You need to import a class if you do not define it in the same directory with your source code.

public class NumericTypes { public static void main (String [] args) { //TASK #2 Create a Scanner object here - help : uncomment the line below by deleting the leading // //Scanner keyboard = new Scanner (System.in); //identifier declarations final int NUMBER = 2 ; // number of scores final int SCORE1 = 100; // first test score final int SCORE2 = 95; // second test score final int BOILING_IN_F = 212; // freezing temperature int fToC; // temperature in celsius double average; // arithmetic average String output; // line of output to print out //TASK #2 declare variables used here String firstName; // user's first name // TASK #2 to do - declare user's last name similar way for the first name // TASK #2 to do - declare user's full name similar way for the first name //TASK #3 declare variables used here char firstInitial; // user's first initial

// Find an arithmetic average average = SCORE1 + SCORE2 / NUMBER; // Task #1 help: add (score1 + score2) to get addition before division output = SCORE1 + " and " + SCORE2 + " have an average of " + average; System.out.println(output);

// Convert Fahrenheit temperatures to Celsius fToC = 5/9 * (BOILING_IN_F - 32); // Task #1 help: Use cast to change data type // fToC = (int) (5/(double)9 * (BOILING_IN_F - 32)); // Note: 5/9 is integer division and result is 0 // 5/(double)9 is double devision and result is 0.5555 output = BOILING_IN_F + " in Fahrenheit is " + fToC + " in Celsius."; System.out.println(output); System.out.println(); // to leave a blank line

// ADD LINES FOR TASK #2 HERE // prompt the user for first name // read the user's first name //System.out.print("Enter your first name: "); //help : uncomment the line below by deleting the leading // //firstName = keyboard.nextLine(); //help : uncomment the line below by deleting the leading // // To do - prompt the user for last name - follow the style of firstName // To do - read the user's last name follow the style of firstName // concatenate the user's first and last names //fullName = firstName + " " + lastName; //help : uncomment the line below by deleting the leading // // print out the user's full name //System.out.println(fullName + " has " + fullName.length() + " characters"); // help : uncomment the line below by deleting the leading //

System.out.println(); // to leave a blank line // ADD LINES FOR TASK #3 HERE // get the first character from the user's first name //firstInitial = firstName.charAt(0); //help : uncomment the line below by deleting the leading // // To do - print out the user's first initial // To do - convert the user's full name to all capital letters // help - use fullName = fullName.toUpperCase(); // To do - print out the user's full name in all capital letters

System.out.println(); // to leave a blank line } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems X Special Issue On Database And Expert Systems Applications Lncs 8220

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2013th Edition

3642412203, 978-3642412202

More Books

Students also viewed these Databases questions

Question

1. To take in the necessary information,

Answered: 1 week ago

Question

The nature and importance of the global marketplace.

Answered: 1 week ago