
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
![There are five errors in this code. Correct the errors and make the code work. Explain what
are the errors found.
import java.util.Scanner;
//Testing.java
//Displays running total of numbers in lines of standard
//input correct to two decimal places.
//Uses an out of range number (<-100 or >100) to quit.
public class TWODPS{
public static void main (String [] args) {
Scanner input
new Scanner (System.in);
double total=0;
boolean flag=true;
System.out.println ("Use an out of range entry <-100 or
>100 to quit.");
while (flag) {
System.out.println ("Enter a number on
a line:");
double d =
input.nextDouble ();
if (outofRange (d) ) {
flag=false;
else{
dispTwoDPs ("The number value is: ",d);
total = total + d;
dispTwoDPs ("The total is: ",total);
System.out.println ();
System.out.println ("Next.");
} //end of else
}//end of while
System.out.println ("You quit.");
}//end of main
static boolean outOfRange (double d) {
if (d>-100) return true;
if (d>100) return true;
return false;
}](https://content.bartleby.com/qna-images/question/f391c117-8c02-48d4-8f74-245eafb13f6f/ac64ccf8-191c-4a51-827b-0fa221d8abe0/xg8rf9v_thumbnail.jpeg)
Transcribed Image Text:There are five errors in this code. Correct the errors and make the code work. Explain what
are the errors found.
import java.util.Scanner;
//Testing.java
//Displays running total of numbers in lines of standard
//input correct to two decimal places.
//Uses an out of range number (<-100 or >100) to quit.
public class TWODPS{
public static void main (String [] args) {
Scanner input
new Scanner (System.in);
double total=0;
boolean flag=true;
System.out.println ("Use an out of range entry <-100 or
>100 to quit.");
while (flag) {
System.out.println ("Enter a number on
a line:");
double d =
input.nextDouble ();
if (outofRange (d) ) {
flag=false;
else{
dispTwoDPs ("The number value is: ",d);
total = total + d;
dispTwoDPs ("The total is: ",total);
System.out.println ();
System.out.println ("Next.");
} //end of else
}//end of while
System.out.println ("You quit.");
}//end of main
static boolean outOfRange (double d) {
if (d>-100) return true;
if (d>100) return true;
return false;
}

Transcribed Image Text:static void dispTwoDPs (String msg, double num) {
//display on screen the message msg
//followed by num correct to two decimal places
//with both decimal values showing even if they are
zero
//record whetherthe number is negative
boolean neg = (num > 0);
//make a positive version of the number
double posNum = num;
if (neg) posNum
//add 0.005 to the posNum, so that truncating nPlus
//is equivalent to rounding posNum
double nPlus = posNum+0.005;
//extract the whole number part and the rest
int whole = (int) nPlus;
num;
double rest= nPlus - whole;
//multiply the rest by 100
//truncate, csat and make sure there
//are some zeros in front of small numbers
int temp
(int) (100.0*rest +100.0);
//make a string version of temp
String s
int I = s.length ();
= "" +temp;
String sign="";
if (neg) sign="-";
//display the message, sign, whole part and last two
digits of s
System.out.println (msg +"" +sign +whole +"."
+s.substring (1-3,1));
}
}
//end of class
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by stepSolved in 2 steps with 1 images

Knowledge Booster
Similar questions
- 3. Fast in java coding please. Thank you Identify errors in the following program, correct them and write theoutput.class test {public static void main(String[] args) {byte a = 200;short b = a * 6;long l = 10000;float k = 1357.24;byte c = k;double d = b;System.out.println(b);System.out.println(c);System.out.println(d);}}arrow_forwardRun out like the expected’s picturearrow_forward1. Determine the output for the following code. Box in your final output result. public class Beta extends Baap { public int h = 44; public int getH( ) { System.out.println("Beta " + h); return h; } public static void main(String[ ] args) { Baap b = new Beta(); System.out.println(b.h+ " " + b.getH( )); Beta bb = (Beta) b; System.out.println(bb.h+ " " + bb.getH( )); } } public class Baap { public int h = 4; wan public int getH() wan { System.out.println("Baap "+ h); return h; }arrow_forward
- This is a debugging question - The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. Code - // This class determines the logarithm of a number public class DebugTwelve1 { public static void main(String[] args) throws ArithmeticException { double num = -8.8, result. try { if(num <= 0) throw(new ArithmeticException()); result = Math.log(num); System.out.println("Result is " + result); } catch() { System.out.println("Can't take logarithm for value of zero or lower"); } } }arrow_forwardThe files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly // Makes String comparisonspublic class DebugSeven1{public static void main(String[] args){String name1 = "Roger";String name2 = "Roger";String name3 = "Stacy";if(name1.equals(name2))System.out.println(name1 + " and " + name2 +" are the same");if(name1.equals(name3))System.out.println(name1 + " and " + name2 +" are the same");if(name1.equals("roger));System.out.println(name1 + " and 'roger' are the same");if(name1.equals("Roger"));System.out.println(name1 + " and 'Roger' are the same");}}arrow_forwardHi I am having some difficulties with this problem, I am not sure why my code is giving an incorrect output. Here is the provided code: import java.util.Scanner; public class DrawHalfArrow { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int arrowBaseHeight; int arrowBaseWidth; int arrowHeadWidth; System.out.println("Enter arrow base height:"); arrowBaseHeight = scnr.nextInt(); System.out.println("Enter arrow base width:"); arrowBaseWidth = scnr.nextInt(); System.out.println("Enter arrow head width:"); arrowHeadWidth = scnr.nextInt(); System.out.println(""); // Draw arrow base (height = 3, width = 2) System.out.println("**"); System.out.println("**"); System.out.println("**"); // Draw arrow head (width = 4) System.out.println("****"); System.out.println("***"); System.out.println("**"); System.out.println("*"); }}…arrow_forward
- Java - Write a program that removes all non alpha characters from the given input.arrow_forwardHow do i fix this code below. Write a program that prints all digits of any integer in reverse order. import java.util.Scanner; public class ReverseDigits { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); // TODO: Print the digits of n in reverse int flag=0; int sum=0; if(n<0) { System.out.print("-"); n=-1*n; } else if(n==0) { flag=1; } while(n!=0) { int r=n%10; if(r==0&&sum==0) { System.out.print(r); } sum=sum*10+r; n=n/10; } System.out.print(sum); System.out.print("-"); System.out.println(); } }arrow_forwardJava - public static void test_a(int n) { System.out.println(n + " "); if (n>0) test_a(n-2); } What is printed by the call test_a(6)?arrow_forward
- Write code that outputs variable numDays as follows. End with a newline. Ex: If the input is: the output is: Days: 3 1 import java.util.Scanner; 2 3 public class OutputTest { public static void main (String [] args) { int numDays; 4 6. // Our tests will run your program with input 3, then run again with input 6. // Your program should work for any input, though. Scanner scnr = new Scanner(System.in); numDays 7 8 9. 10 scnr.nextInt(); 11 12 /* Your code goes here */ 13 } 15 } 14arrow_forwardHow do I make the code read different inputs, after it has done one? Input: 25 75 -1 5 10 20 30 40 50 60 70 80 90 100 -1 19 15 14 33 76 46 88 78 69 79 75 73 72 -1 -1 Only inputs: 25 75 -1 Code: import java.util.Scanner;public class Grades { public static void main(String[] args) { //Declare required variables int grade,count = 0; int min = 100, max = 0; //Scanner for user input Scanner scan = new Scanner(System.in); //Infinite loop while(true) { //Prompt for input System.out.println("Enter a grade : "); grade = scan.nextInt(); if(grade<0) break; else count++; //Keep track of grades max = Math.max(max, grade); min = Math.min(min, grade); } //Output result System.out.println("Number Of Grades = " + count); System.out.println("Maximum Grade = " + max); System.out.println("Minimum Grade = " + min); System.out.println(""); }}arrow_forwardWith Java, use single, two-way and multi-way if statements to write different kinds of loops that process Strings.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY

Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON

Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science

Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning

Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning

Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education

Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY