SlideShare a Scribd company logo
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 UsingPredefined 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
}
}

More Related Content

DOCX
import java.util.Scanner;Henry Cutler ID 1234 7202.docx
PPT
intro to programming languge c++ for computer department
DOCX
Project 2 MARIE Start code at bottom of document1. IntroductionT.docx
PDF
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
DOC
Lewis jssap3 e_labman02
PPTX
Overview of C Programming (CSE115/CS1010)
PPTX
Ch2 Elementry Programmin as per gtu oop.pptx
PPTX
C programming language tutorial
import java.util.Scanner;Henry Cutler ID 1234 7202.docx
intro to programming languge c++ for computer department
Project 2 MARIE Start code at bottom of document1. IntroductionT.docx
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
Lewis jssap3 e_labman02
Overview of C Programming (CSE115/CS1010)
Ch2 Elementry Programmin as per gtu oop.pptx
C programming language tutorial

Similar to Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf (20)

DOC
Cosc 1436 java programming/tutorialoutlet
PDF
Integration Project Inspection 3
PPTX
9. DBMS Experiment Laboratory PresentationPPT
PPT
Introduction to Basic C programming 01
PPTX
Fundamental of programming Fundamental of programming
PPTX
CP 04.pptx
PDF
Maxbox starter
PPTX
C++ Functions
PPTX
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
PPTX
Introduction to C++ lecture ************
PPTX
Chap2programing.pptxdxnDSnfkezjnfqjdsckjqds
PPT
C chap02
PPT
C chap02
PPTX
C Programming - Basics of c -history of c
PPT
ch06-file-processing.ppt
PPT
Cinfo
PPTX
Unit2_3.pptx Chapter 2 Introduction to C#
PPT
Gift-VT Tools Development Overview
PDF
in C++ Design a class named Employee The class should keep .pdf
PPTX
Elementary_Variables_And_Data123456.pptx
Cosc 1436 java programming/tutorialoutlet
Integration Project Inspection 3
9. DBMS Experiment Laboratory PresentationPPT
Introduction to Basic C programming 01
Fundamental of programming Fundamental of programming
CP 04.pptx
Maxbox starter
C++ Functions
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
Introduction to C++ lecture ************
Chap2programing.pptxdxnDSnfkezjnfqjdsckjqds
C chap02
C chap02
C Programming - Basics of c -history of c
ch06-file-processing.ppt
Cinfo
Unit2_3.pptx Chapter 2 Introduction to C#
Gift-VT Tools Development Overview
in C++ Design a class named Employee The class should keep .pdf
Elementary_Variables_And_Data123456.pptx
Ad

More from info706022 (20)

PDF
Many biologists will talk about the group known as Ungulates, or hoo.pdf
PDF
Match the function with the appropriate organelle in the column at ri.pdf
PDF
It costs $16 to travel in a specific zone in paris. Now suppose thei.pdf
PDF
Let X and Y be two random variables whose joint probability density .pdf
PDF
Identify whether the Fed should continue its current pace of securit.pdf
PDF
If 2 and z are incompletely dominant, how many different phenotypes a.pdf
PDF
How are criminals maximizing their total utilitySolutionThe o.pdf
PDF
How do I change this javascript code so that the new page opens up b.pdf
PDF
Help with my biostats Homework. Please show all work!! Mendel develo.pdf
PDF
genetics q If the offspring of a dihydric testcross are roughly 50 .pdf
PDF
for fiscal year 2006, the national debt of a country was approximate.pdf
PDF
Explain why Linux makes system performance monitoring available to t.pdf
PDF
Discuss the relationships between competitive avantage, istinctive c.pdf
PDF
Determine the intervals of the domain over which each function is.pdf
PDF
A storage reservoir contains 200 kg of a liquid that has a specific .pdf
PDF
4. Define modal split model transportation demand . central vision .pdf
PDF
Comparison of dysplasia and hyperplasiaSolutionDysplasia Dys.pdf
PDF
“Web 2.0 is simply a new label for a range of web technologies and c.pdf
PDF
You are required, but not limited, to turn in the following source f.pdf
PDF
Write a function to merge two doubly linked lists. The input lists ha.pdf
Many biologists will talk about the group known as Ungulates, or hoo.pdf
Match the function with the appropriate organelle in the column at ri.pdf
It costs $16 to travel in a specific zone in paris. Now suppose thei.pdf
Let X and Y be two random variables whose joint probability density .pdf
Identify whether the Fed should continue its current pace of securit.pdf
If 2 and z are incompletely dominant, how many different phenotypes a.pdf
How are criminals maximizing their total utilitySolutionThe o.pdf
How do I change this javascript code so that the new page opens up b.pdf
Help with my biostats Homework. Please show all work!! Mendel develo.pdf
genetics q If the offspring of a dihydric testcross are roughly 50 .pdf
for fiscal year 2006, the national debt of a country was approximate.pdf
Explain why Linux makes system performance monitoring available to t.pdf
Discuss the relationships between competitive avantage, istinctive c.pdf
Determine the intervals of the domain over which each function is.pdf
A storage reservoir contains 200 kg of a liquid that has a specific .pdf
4. Define modal split model transportation demand . central vision .pdf
Comparison of dysplasia and hyperplasiaSolutionDysplasia Dys.pdf
“Web 2.0 is simply a new label for a range of web technologies and c.pdf
You are required, but not limited, to turn in the following source f.pdf
Write a function to merge two doubly linked lists. The input lists ha.pdf
Ad

Recently uploaded (20)

PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Basic Mud Logging Guide for educational purpose
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Pre independence Education in Inndia.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Classroom Observation Tools for Teachers
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Complications of Minimal Access Surgery at WLH
102 student loan defaulters named and shamed – Is someone you know on the list?
Basic Mud Logging Guide for educational purpose
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPH.pptx obstetrics and gynecology in nursing
Module 4: Burden of Disease Tutorial Slides S2 2025
O5-L3 Freight Transport Ops (International) V1.pdf
TR - Agricultural Crops Production NC III.pdf
RMMM.pdf make it easy to upload and study
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Pre independence Education in Inndia.pdf
Pharma ospi slides which help in ospi learning
GDM (1) (1).pptx small presentation for students
Renaissance Architecture: A Journey from Faith to Humanism
VCE English Exam - Section C Student Revision Booklet
Classroom Observation Tools for Teachers
Microbial disease of the cardiovascular and lymphatic systems
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
O7-L3 Supply Chain Operations - ICLT Program
Complications of Minimal Access Surgery at WLH

Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf

  • 1. 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).
  • 2. 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 UsingPredefined 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)
  • 3. // 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
  • 4. // 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;
  • 5. 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 } }