Question

Java debugging in eclipse

You are to debug the given file FindTheErrors.java. The code has four individual problems Problems 2- 4 have been commented o

package edu.ilstu;
import java.util.Scanner;
/**
* The following class has four independent debugging
* problems. Solve one at a time, uncommenting the next
* one only after the previous problem is working correctly.
*/
public class FindTheErrors {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
/*
* Problem 1 Debugging
*
* This problem is to read in your first name,
* last name, and current year and display them in
* a sentence to the console.
*/
String firstName = "";
String lastName = "";
String school = "";
int year = 0;
System.out.print("Enter your first name: ");
firstName = keyboard.nextLine();
System.out.print("Enter the current year: ");
year = keyboard.nextInt();
System.out.print("Enter your last name: ");
lastName = keyboard.nextLine();
System.out.println("You are " + firstName + " "
+ lastName + " and it is the year " + year);
keyboard.nextLine();
System.out.println("\n");
/*
* Problem 2 Debugging
*
* This problem is to assign a value to num2 based on
* the input value of num1. It should then print both
* numbers.
*/
// int num1 = 0;
// int num2 = 0;
//
// System.out.print("Enter a number - 1, 2, or 3: ");
// num1 = keyboard.nextInt();
//
// if (num1 == 1);
// num2 = 2;
// else if (num1 == 2);
// num2 = 3;
// else if (num1 == 3);
// num2 = 4;
//
// System.out.println("num1 = " + num1
// + " and num2 = " + num2);
//
// System.out.println("\n");
/*
* Problem 3
*
* This problem is to read the name of the course
* you are taking and display a statement showing the
* answer.
*/
// String courseName = "";
//
// System.out.print(
"Enter your course name (IT168 or IT177): ");
// courseName = keyboard.nextLine();
// if (courseName == IT168))
// System.out.println("You are taking IT168.");
// else if (courseName == IT177))
// System.out.println("You are taking IT177.");
// else
// System.out.println("Invalid input.");
//
// System.out.println("\n");
/*
* Problem 4
*
* This problem is to read a test grade from the
* keyboard and assign the appropriate letter grade.
*/
// int score = 0;
// char grade = 'Z';
//
// System.out.println("Enter your test grade (1-100): ");
// score = keyboard.nextInt();
//
// switch(score)
// {
// case (score > 60):
// grade = 'A';
// break;
// case (score > 70):
// grade = 'b';
// break;
// case (score > 80):
// grade = 'C';
// break;
// case (score > 90):
// grade = 'D';
// break;
// default:
// grade = 'F';
// }
//
// System.out.println("The score " + score
// + " will have a grade of " + grade + ".");
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

explainations are added in comments on lines which are altered.

fixed Java code -

package edu.ilstu;
import java.util.Scanner;
/**
* The following class has four independent debugging
* problems. Solve one at a time, uncommenting the next
* one only after the previous problem is working correctly.
*/
public class FindTheErrors {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
/*
* Problem 1 Debugging
*
* This problem is to read in your first name,
* last name, and current year and display them in
* a sentence to the console.
*/
String firstName = "";
String lastName = "";
String school = "";
int year = 0;
System.out.print("Enter your first name: ");
firstName = keyboard.nextLine();
System.out.print("Enter the current year: ");
year = keyboard.nextInt();
System.out.print("Enter your last name: ");
lastName = keyboard.next(); /*nextLine parses input until it finds newline character('\n'),so the string left after reading by nextInt(),
so it is reading what's left of previous line,(that's why it is printing line without taking next input,
while next() parse the next valid string so it skips the previous input and scans the next string of valid characters*/
System.out.println("You are " + firstName + " "
+ lastName + " and it is the year " + year);
keyboard.nextLine();
System.out.println("\n");
/*
* Problem 2 Debugging
*
* This problem is to assign a value to num2 based on
* the input value of num1. It should then print both
* numbers.
*/
int num1 = 0;
int num2 = 0;

System.out.print("Enter a number - 1, 2, or 3: ");
num1 = keyboard.nextInt();

if (num1 == 1) //removed the ';' as it terminates the if condition here and would have any effect on further lines
num2 = 2;
else if (num1 == 2) //similarly removed ';'
num2 = 3;
else if (num1 == 3) //removed ';'
num2 = 4;

System.out.println("num1 = " + num1
+ " and num2 = " + num2);

System.out.println("\n");
/*
* Problem 3
*
* This problem is to read the name of the course
* you are taking and display a statement showing the
* answer.
*/
String courseName = "";

System.out.print("Enter your course name (IT168 or IT177): ");
courseName = keyboard.next(); //converted nextLine to next() because of reason previously stated in problem 1
if (courseName.equals("IT168")) // the '==' operator is not valid in case of strings, hence equals method is called to match strings
System.out.println("You are taking IT168.");
else if (courseName.equals("IT177")) //same case   
System.out.println("You are taking IT177.");
else
System.out.println("Invalid input.");

System.out.println("\n");
/*
* Problem 4
*
* This problem is to read a test grade from the
* keyboard and assign the appropriate letter grade.
*/
int score = 0;
char grade = 'Z';

System.out.println("Enter your test grade (1-100): ");
score = keyboard.nextInt();
// to convert the range of marks like(60-70) to single integers, we are dividing it by 10.
switch((int)score/10) //to convert the range to single digit
{
case 6: //cases from 60 to 69
grade = 'D';
break;
case 7: //cases from 70 to 79
grade = 'C';
break;
case 8: //cases from 80-89
grade = 'B';   
break;
case 9: //cases from 90-99
case 10: //case of 100
grade = 'A'; //this will be executed for both cases 9 and 10
break;
default:
grade = 'F'; //other cases (0-59)
}

System.out.println("The score " + score
+ " will have a grade of " + grade + ".");
}
}

output screenshot -

run: Enter your first name: adam Enter the current year: 2019 Enter your last name: apple You are adam apple and it is the ye

Add a comment
Know the answer?
Add Answer to:
Java debugging in eclipse package edu.ilstu; import java.util.Scanner; /** * The following class has four independent...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • DEBUGGING. JAVA. Identify the errors in the following. There are no logical errors in this one...

    DEBUGGING. JAVA. Identify the errors in the following. There are no logical errors in this one just syntax issues. //start import java.util.Scanner; public class NumbersDemo {    public static void main (String args[])    {       Scanner kb = new Scanner(System.in);       int num1, num2;             System.out.print("Enter an integer >> ");       num1 = kb.nextInt();       System.out.print("Enter another integer >> ");       num2 = kb.nextDouble();       displayTwiceTheNumber(num1);       displayNumberPlusFive(num1);       displayNumberSquared(num1)       displayTwiceTheNumber(num2);       displayNumberPlusFive(num2);       displayNumberSquared(num2);    }   ...

  • // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public...

    // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public class String { public static void main(String[] args) { double Sside, Rlength, Rwidth, Tbase, Theight, Area, Tarea, Rarea; String input; Scanner keyboard = new Scanner(System.in); System.out.print("To Calculate the are of Square Enter 'Square', For Rectangle Enter 'Rectangle', For Triangle Enter 'Triangle'"); input = keyboard.nextLine(); if (input.equalsIgnoreCase("SQUARE")) { System.out.println("Square Side"); Sside = keyboard.nextInt(); Tarea = Sside * Sside; System.out.println("Side = " + Tarea ); }...

  • Java // Topic 2c // Program reserves airline seats. import java.util.Scanner public class Plane {    //...

    Java // Topic 2c // Program reserves airline seats. import java.util.Scanner public class Plane {    // checks customers in and assigns them a boarding pass    // To the human user, Seats 1 to 2 are for First Class passengers and Seats 3 to 5 are for Economy Class passengers    //    public void reserveSeats()    {       int counter = 0;       int section = 0;       int choice = 0;       String eatRest = ""; //to hold junk in input buffer       String inName = "";      ...

  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • make this program run import java.util.Scanner; public class PetDemo { public static void main (String []...

    make this program run import java.util.Scanner; public class PetDemo { public static void main (String [] args) { Pet yourPet = new Pet ("Jane Doe"); System.out.println ("My records on your pet are inaccurate."); System.out.println ("Here is what they currently say:"); yourPet.writeOutput (); Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the correct pet name:"); String correctName = keyboard.nextLine (); yourPet.setName (correctName); System.out.println ("Please enter the correct pet age:"); int correctAge = keyboard.nextInt (); yourPet.setAge (correctAge); System.out.println ("Please enter the...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • Why isnt my MyCalender.java working? class MyCalender.java : package calenderapp; import java.util.Scanner; public class MyCalender {...

    Why isnt my MyCalender.java working? class MyCalender.java : package calenderapp; import java.util.Scanner; public class MyCalender { MyDate myDate; Day day; enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }    public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.println("Enter date below :"); System.out.println("Enter day :"); int day = sc.nextInt(); System.out.println("Enter Month :"); int month = sc.nextInt(); System.out.println("Enter year :"); int year = sc.nextInt(); MyDate md = new MyDate(day, month, year); if(!md.isDateValid()){ System.out.println("\n Invalid date . Try...

  • JAVA I need a switch so that users can input one selection then another. These selections...

    JAVA I need a switch so that users can input one selection then another. These selections are for a simple calculator that uses random numbers 1. + 2. - 3. * 4./ 5. RNG. This has to be simple this is a beginners class. Here is what I have import java.util.Scanner; import java.util.Random; import java.util.*; public class You_Michael02Basic_Calculator {    public static void main(String[] args) {        // Generate random numbers        int num1,num2;        num1 =(int)(Math.random()*100+1);...

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT