Question

In: Computer Science

// staff should be coming in to // campus, or staying at home. Only part of...

// 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");

   }

}

Solutions

Expert Solution

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)");

  • score += check&Add(input, MILD_SYMPTOMS);

Here the method name check&Add is not defined, it must be checkAndAdd.

Correction: score += checkAndAdd(input, MILD_SYMPTOMS);

Logical errors:

  • if (answer.equalsIgnoreCase("yes")){
       return 0;
    } else {
       return score;
    }

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;
}

  • Now, this may or maynot be an error as you have only provided a part of code. But the line score-- in after the first print statement just didn't add up to the logic of the question. As per the data provided by you, there is no mention of any special operation that is supposed to be performed for this print statement. According to the conditions specified, this just seemed like any other print stements.

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:


Related Solutions

YOUR PARENTS ARE COMING TO VISIT YOU IN VANCOUVER AND ARE STAYING FOR A WEEK, WHICH...
YOUR PARENTS ARE COMING TO VISIT YOU IN VANCOUVER AND ARE STAYING FOR A WEEK, WHICH TOUR COMPANY WILL YOU CHOOSE AND WHY? THEY HAVE AN UNLIMITED BUDGET AND WANT TO SEE THE BEST OF THE BEST IN LUXURY HOTELS AND RESTAURANTS, HENCE PLEASE INCLUDE YOUR BUDGET AND DISCUSS YOUR REASON WITH REAL LIFE EXAMPLES. - WRITE AN ESSAY OF 250 - 300 WORDS.
Staff retention and staying power: Nissan builds on loyalty at Sunderland plan Some of carmaker’s earliest...
Staff retention and staying power: Nissan builds on loyalty at Sunderland plan Some of carmaker’s earliest recruits are now among its most senior executives. Since the first Bluebird rolled off the production line in July 1986, the Nissan plant in Sunderland has grown from a £50m assembly operation into the UK’s biggest car production site. Now a £3.7bn investment employing 6,800 people, it is also north-east England’s biggest private sector employer, offering relatively good pay and secure work in an...
Billy has been staying home and watching a lot of movies lately, and based on his...
Billy has been staying home and watching a lot of movies lately, and based on his experience, he believes that the average running time for movies is equal to 140 minutes. To test this, he creates the following hypotheses: H0: μ = 140 Ha: μ ≠ 140 ...and measures the running time for 4 movies and finds the following:   150 150 180 170 PART 1: If he uses the the p-value method to test his hypothesis, what would his p-value...
You are a student staying home during the COVID-19 pandemic. You discover a need in the...
You are a student staying home during the COVID-19 pandemic. You discover a need in the market for a new product. Suppose you want to start a company to sell this new product and fill the market gap. a) How will your company create and capture value in such uncertain times? b) What do you foresee for the future of your company as the pandemic slows? How will you maintain market share?
3. Currently, people are staying home due to COVID-19, which clearly limits a lot of the...
3. Currently, people are staying home due to COVID-19, which clearly limits a lot of the activities that people would normally do. While some people will spend more on online shopping, in general, it is reasonable to think that consumption has fallen. (There are OTHER changes going on as well, but just focus on this one for this question.) a. Draw an AD-AS graph. Assume that the economy was at full employment before the virus. Then show the SHORT RUN...
Addressing Key Human Resource Functions Part 1 : Do you think employers should invest in staff...
Addressing Key Human Resource Functions Part 1 : Do you think employers should invest in staff development? Why or why not? In what ways can decisions about staff development affect a public health organization's staffing plan? What strategies would you recommend to balance the benefits of staff development with budgetary constraints?
Below you will find part of a job description for a part-time position in a campus...
Below you will find part of a job description for a part-time position in a campus bookstore. Read each task in the job description and identify those tasks for which training (rather than employee selection techniques) would be appropriate. In the next exercise, you will be asked to determine how to train the employees for each of the tasks you identify. Textbook Clerk Job Summary The Textbook Clerk is a university work-study position. The student hired for this job is...
The code should be written in c++. It should be in only OpenGL // ***** ONLY...
The code should be written in c++. It should be in only OpenGL // ***** ONLY OpenGL PLEASE ******// write a program snippet that accepts the coordinates of the vertices of a rectangle centered around the origin, with edges that are parallel to the X and Y axes, and with aspect ratio W and converts it into a rectangle centered around the origin with aspect ratio of 1/W. The program has to figure W from the given points. You MUST...
Part I On a certain university campus there is an infestation of Norway rats. It is...
Part I On a certain university campus there is an infestation of Norway rats. It is estimated that the number of rats on campus will follow a logistic model of the form P(t)=50001+Be−ktP(t)=50001+Be−kt. A) It is estimated that there were 500 rats on campus on January 1, 2010 and 750 on April 1, 2010. Using this information, find an explicit formula for P(t)P(t) where tt is years since January 1, 2010. (Assume April 1, 2010 is t=.25t=.25.) P(t)= P(t)=  . B)...
Staff professional development is part of the role of themanager. In this assessment, assume the...
Staff professional development is part of the role of the manager. In this assessment, assume the role of the manager. On the company’s internal website, there is a staff blog for updates on policies and procedures. Create a post to inform employees regarding current healthcare policies and managed care.Instructions:Write a blog post the includes the following:A description of one current healthcare policy and expected outcomes.An explanation of how the policy impacts MCO.An analysis of the impact managed care has on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT