In: Computer Science
// staff should be coming in to
// campus, or staying at home. Only part of the code is here.
// It bases the decision based on a points system by asking
the
// user a set of questions and keeping track of how many
times
// it gets a yes response back.
//
// The code implements policy by assigning a weight to each of
the
// categories of symptoms/risk factors
// 1) Mild Symptoms (1pt each)
// 2) Severe Symptoms (2pt each)
// 3) Recent exposure (3pt each)
// 4) Risk factors (3pt each)
//
// This file implements only the checks FOR ( 1
MILD) symptoms.
//
// There are 5 errors in this code.
//
// ~-~-~- ~-~-~-~-~ ~-~-~-~-~ ~-~-~-~-~ ~-~-~-~-~ ~-~-~-~-~
~-~-~-~-~ ~-~-~-~-~|
import java.util.Scanner;
public class Fragment1 {
public static final int MILD_SYMPTOMS = 1;
// This method gets the next line of input from the
scanner and
// Checks to see if it is "yes" (in either upper or
lower case)
// If it is, it returns the number provided as the
second argument
// else, it returns 0.
public static int checkAndAdd(Scanner input, int
score){
String answer = input.nextLine();
if (answer.equalsIgnoreCase("yes")){
return 0;
} else {
return score;
}
}
// This method asks the user about a list of symptoms
and uses
// the checkAndAdd method to check the answers.
// Tt sums up a score based on MILD_SYMPTOMS for each
yes answer
// and returns the total.
public static int checkMildSymptoms(String name,
Scanner input) {
int score = 0;
System.out.println(name+ ", do you have chills? (yes/no)");
score--;
System.out.println(name+ ", do you have diarrhea? (yes/no)");
score += checkAndAdd(input, MILD_SYMPTOMS);
System.out.println(name+ ", do you have sore throat?
(yes/no)");
score += checkAndAdd(input, MILD_SYMPTOMS)
System.out.println{name+ ", do you have body aches?
(yes/no)");
score += checkAndAdd(input, MILD_SYMPTOMS);
System.out.println(name+ ", do you have headache? (yes/no)");
score += check&Add(input, MILD_SYMPTOMS);
return score;
}
public static void main(String[] args) {
int totalScore = 0; // Tabulates the likelihood of infection
int totalRisk = 0; // Tabulates the high risk categories
String name=""; // Stores the name so we can be friendly
// Setup the scanner so we can prompt the user for answers
Scanner input = new Scanner(System.in);
System.out.println("Your total score is "+
checkMildSymptoms(name,input)
+ " points");
}
}
Hi,
Hope you doing fine. You have done a great job with the code. I have debugged you r code and have found a few errors. Some of them are syntactical errors while some are logical. Below are the errors:
Syntactical errors:
System.out.println(name+ ", do you have sore throat?
(yes/no)");
score += checkAndAdd(input,
MILD_SYMPTOMS)
Here you have missed in semicolon in the second statement of the above two lines.
Corrected line: score += checkAndAdd(input, MILD_SYMPTOMS);
System.out.println{name+ ", do you have body aches? (yes/no)");
Here you have used a curly opening brace for the print staement.
Correction: System.out.println(name+ ", do you have body aches? (yes/no)");
Here the method name check&Add is not defined, it must be checkAndAdd.
Correction: score += checkAndAdd(input, MILD_SYMPTOMS);
Logical errors:
In the above code of checkAndAdd() method, as per mentioned logic if the input entered is "yes" then irrespectve of lowercase or uppercase the method should return the score that was passed to the method or else it returns 0. But if you look at the code above it is written exactly opposite. According to the above logiv, if the input is not "yes" then the score is returned and is it is "yes" then 0 is returned.
Correction:
if (answer.equalsIgnoreCase("yes")){
return score;
} else {
return 0;
}
System.out.println(name+ ", do you have chills?
(yes/no)");
score--;
Correction:
System.out.println(name+ ", do you have chills?
(yes/no)");
score += checkAndAdd(input, MILD_SYMPTOMS);
Corrected code:
import java.util.Scanner;
public class Fragment1 {
public static final int MILD_SYMPTOMS = 1;
// This method gets the next line of input from the
scanner and
// Checks to see if it is "yes" (in either upper or lower
case)
// If it is, it returns the number provided as the second
argument
// else, it returns 0.
public static int checkAndAdd(Scanner input, int score){
String answer = input.nextLine();
if (answer.equalsIgnoreCase("yes")){
return score;
} else {
return 0;
}
}
// This method asks the user about a list of symptoms
and uses
// the checkAndAdd method to check the answers.
// Tt sums up a score based on MILD_SYMPTOMS for each yes
answer
// and returns the total.
public static int checkMildSymptoms(String name, Scanner input)
{
int score = 0;
System.out.println(name+ ", do you have chills?
(yes/no)");
score += checkAndAdd(input, MILD_SYMPTOMS);
System.out.println(name+ ", do you have diarrhea?
(yes/no)");
score += checkAndAdd(input, MILD_SYMPTOMS);
System.out.println(name+ ", do you have sore throat?
(yes/no)");
score += checkAndAdd(input, MILD_SYMPTOMS);
System.out.println(name+ ", do you have body aches?
(yes/no)");
score += checkAndAdd(input, MILD_SYMPTOMS);
System.out.println(name+ ", do you have headache?
(yes/no)");
score += checkAndAdd(input, MILD_SYMPTOMS);
return score;
}
public static void main(String[] args) {
int totalScore = 0; // Tabulates the likelihood of
infection
int totalRisk = 0; // Tabulates the high risk
categories
String name=""; // Stores the name so we can be
friendly
// Setup the scanner so we can prompt the user for
answers
Scanner input = new Scanner(System.in);
System.out.println("Your total score is "+
checkMildSymptoms(name,input)
+ " points");
}
}
Executable code snippet:
Output: