Task 1 Correcting Logic Errors in Formulas Copy and compile

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
1. Add an import statement above the class declaration to make the Scanner
class available to your program.
2. In the main method, create a Scanner object and connect it to the System.in
object.
3. Prompt the user to enter his or her first name.
4. 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).
5. Prompt the user to enter his or her last name.
6. Read the name from the keyboard and store it in a variable called lastName.
7. Concatenate the firstName and lastName with a space between them and
store the result in a variable called fullName.
8. Print out the fullName.
9. Compile, debug, and run, using your name as test data.
10. Since we are adding on to the same program, each time we run the program we
will get the output from the previous tasks before the output of the current task.

Task #3 Working with Strings
1. 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).
2. Print out the user’s first initial.
3. Use the toUpperCase method to change the fullName to uppercase and store
it back into the fullName variable.
4. 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 length
method to obtain that information).
5. 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 uppercase
characters.

Task #4 Using Predefined Math Functions

Task #4 Using Predefined Math Functions

Add a line that prompts the user to enter the diameter of a sphere.

Read in and store the number into a variable called diameter (you will need to declare any variables that you use).

The diameter is twice as long as the radius, so calculate and store the radius in an appropriately named variable.

The formula for the volume of a sphere is

r3

Convert the formula to Java and add a line which calculates and stores the value of volume in an appropriately named variable. Use Math.PI for and Math.pow to cube the radius.

Print your results to the screen with an appropriate message.

Compile, debug, and run using the following test data and record the results.

Diameter

Volume (hand calculated)

Volume (resulting output)

2

25.4

875,000

Code Listing 2.1 (NumericTypes.java)

// TASK #2 Add an import statement for the Scanner class

// TASK #2(Alternate)

// Add an import statement for the JOptionPane class

/**

This program demonstrates how numeric types and

operators behave in Java.

*/

public class NumericTypes

{

public static void main (String [] args)

{

// TASK #2 Create a Scanner object here

// (not used for alternate)

// 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; // Boiling temperature

int fToC; // Temperature Celsius

double average; // Arithmetic average

String output; // Line of output

// TASK #2 declare variables used here

// TASK #3 declare variables used here

// TASK #4 declare variables used here

// Find an arithmetic average.

average = SCORE1 + SCORE2 / NUMBER;

output = SCORE1 + \" and \" + SCORE2 +

\" have an average of \" + average;

System.out.println(output);

// Convert Fahrenheit temperature to Celsius.

fToC = 5/9 * (BOILING_IN_F - 32);

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

// Prompt the user for last name

// Read the user\'s last name

// Concatenate the user\'s first and last names

// Print out the user\'s full name

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

// ADD LINES FOR TASK #3 HERE

// Get the first character from the user\'s first name

// Print out the user\'s first initial

// Convert the user\'s full name to uppercase

// Print out the user\'s full name in uppercase

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

// ADD LINES FOR TASK #4 HERE

// Prompt the user for a diameter of a sphere

// Read the diameter

// Calculate the radius

// Calculate the volume

// Print out the volume

}

}

Diameter

Volume (hand calculated)

Volume (resulting output)

2

25.4

875,000

Solution

public class NumericTypes

{

public static void main (String [] args)

{

// TASK #2 Create a Scanner object here

// (not used for alternate)

// 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; // Boiling temperature

int fToC; // Temperature Celsius

double average; // Arithmetic average

String output; // Line of output

// TASK #2 declare variables used here

// TASK #3 declare variables used here

// TASK #4 declare variables used here

// Find an arithmetic average.

average = SCORE1 + SCORE2 / NUMBER;

output = SCORE1 + \" and \" + SCORE2 +

\" have an average of \" + average;

System.out.println(output);

// Convert Fahrenheit temperature to Celsius.

fToC = 5/9 * (BOILING_IN_F - 32);

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

// Prompt the user for last name

// Read the user\'s last name

// Concatenate the user\'s first and last names

// Print out the user\'s full name

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

// ADD LINES FOR TASK #3 HERE

// Get the first character from the user\'s first name

// Print out the user\'s first initial

// Convert the user\'s full name to uppercase

// Print out the user\'s full name in uppercase

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

// ADD LINES FOR TASK #4 HERE

// Prompt the user for a diameter of a sphere

// Read the diameter

// Calculate the radius

// Calculate the volume

// Print out the volume

}

}

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 i
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 i
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 i
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 i
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 i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site