1. Engineering
  2. Computer Science
  3. task 1 correcting logic errors in formulas enter numerictypesjava see...

Question: task 1 correcting logic errors in formulas enter numerictypesjava see...

Question details

Task #1 Correcting Logic Errors in Formulas  

  1. Enter NumericTypes.java (see code listing 2.1)
  2. Compile the source file, 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 = sum of score 1 and score 2 divided by numberOfScores

                                C = jEv64yB9afJRxBgPexvnAusG+4iZIJthGmbXm3EV (F – 32)

  1. Each time you make changes to the program code, you must compile again for the changes to take effect before running the program again.
  2. 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/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/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 all capitals 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 method length 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 all capital letters.

Task #4 Using Predefined Math Functions

  1. Add a line that prompts the user to enter the diameter of a sphere.
  2. Read in and store the number into a variable called diameter (you will need to declare any variables that you use).
  3. The diameter is twice as long as the radius, so calculate and store the radius in an appropriately named variable.  
  4. The formula for the volume of a sphere is

                             V = 4 / 3 π r 3

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.

  1. Print your results to the screen with an appropriate message.
  2. 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

Task #5 – Create a program from scratch

In this task the student will create a new program that calculates gas mileage in miles per gallon. The student will use string expressions, assignment statements, input and output statements to communicate with the user.

  1. Under an existing Java project (or create a new java project if you like), create a new Class named Mileage in Eclipse.
  2. public class Mileage{

                                           public static void main(String[] args) {

                                                        // add your declaration and code here

                                            }

}

  1. Translate the algorithm below into Java. Don’t forget to declare variables before they are used. Each variable must be one word only (no spaces).

Print a line indicating this program will calculate mileage

Print prompt to user asking for miles driven Read in miles driven

Print prompt to user asking for gallons used

Read in gallons used

Calculate miles per gallon by dividing miles driven by gallons used

Print miles per gallon along with appropriate labels

  1. Compile the program and debug, repeating until it compiles successfully.
  2. Run the program and test it using the following sets of data and record the results:

Miles driven

Gallons used

Miles per gallon (hand calculated)

Miles per gallon (resulting output)

2000

100

500

25.5

241.5

10

100

0

  1. The last set of data caused the computer to divide 100 by 0, which resulted in what is called a runtime error. Notice that runtime can occur on programs which compile and run on many other sets of data. This emphasizes the need to thoroughly test you program with all possible kinds of data.
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution