Uploaded by mashaalmalik

2021 Quiz 2 Fall - Knighton - Solution (2)

advertisement
Name _____________________________________________________ ID # _______________________________________
MIS3370 Quiz 2
Fall 2021
Version
A
This is an open book, open note quiz. Your objective is to generate the code as describe below,
test it in your Java Development environment, and then paste and submit it in this document to
Blackboard within the allotted time period. Please put your name and ID on all pages.
Grading Criteria: (1) works according to the problem statement, (2) good style (indentation, naming, etc.), (3) appropriate
data types, (4) no nonworking or extraneous code, (5) only includes concepts & techniques introduced thus far by text or
lecture, (6) if not legible, it is wrong, (7) the solution should be able to be compiled and executed. NOTE: Partial credit will be
given.
Документ1
1
6/27/2022
Name _____________________________________________________ ID # _______________________________________
Problem Statement: You are to create an application to track the patients that have reveived their COVID-19 Vaccines at the
various locations on the University of Houston campus.
The Medical Office class (described to the right) is used in this assignment. You
MAY NOT change this class:
public class MedicalOffice
{
protected String officeName;
private static int noOfVaccinations;
MedicalOffice (String officeName)
{
this.officeName = officeName;
noOfVaccinations++;
}
Instructions for the code that you will need to create:
1.
Create a new class called Patient. This will be an extension of the MedicalOffice class. In
it there are 3 attributes. These attributes are Patient name, A Boolean indicating the
vaccine shot status (is this the first dose or not), and vaccine type. This class is an
extension of the MedicalOffice class described on the left.
a. This class will have one constructor. It will receive as parameters the office name,
patient name, the shot status (Boolean indicating if the shot was the second
dose), and the vaccine type administered to the patient. It will do the following:
a. Pass the office name to the parent constructor.
b. Store the other received parameters into their corresponding named
attributes.
b. There will be one method to print the office name, patient name vaccine type and
status (the detail patient information as shown in the black box below).
public String getOfficeName()
{
return officeName;
}
public static int getNoOfVaccinations()
{
return noOfVaccinations;
}
}
2. The next class to create is the MIS3370AFall2021 class. In it, there are 6 methods. There are also 6 attributes as defined in the box below,
use this code to create the parallel arrays, allocate memory to the array of objects ,and the count of patients being treated.
private static String[] name = new String [10];
private static String[] vaccine = new String [10];
private static boolean[] status = new boolean [10];
private static String[] office = new String [10] ;
// parallel array for patient name
// parallel array for vaccine patient received
// parallel array for 1st or 2nd dose
// parallel array for location where shot given
private static Patient[] patientObject = new Patient[10]; //setup memory for array of objects
private static int count = 0;
Method descriptions:
a. Method 1 (this is the main method) will perform the following:
i. Prompt the user for the number of patients vaccinated as shown below and save this number in the count attribute listed
above.
ii. Call method 2 below to populate the data in the parallel arrays listed above.
iii. Call method 5 below to instantiate the array of objects from the data in the parallel arrays listed above.
iv. Print the column header labels as shown below (first two lines in the black box shown below).
v. Call method 6 below to print the detail lines as shown in the remaining lines in the black box shown below.
vi. Print the summary line as shown in the black box below listing the number of vaccine shots administered.
b. Method 2. For each of the patients treated, do the following: (make sure that each patient’s information is stored in the same index value
of the parallel arrays (i.e. first patient’s data will be in the first element, etc.))
i. Prompt for the office where the vaccine was administered. This needs to be saved in the appropriate array listed above.
ii. Validate the office by passing the value entered to the Method 4 described below.
iii. If this office is valid, then prompt for the patient’s name and save it in the parallel array for name listed above
iv. The name will need to be “cleaned” by passing this value to Method 3 described below.
v. Prompt the user for the vaccine name as shown below. This will be saved in the appropriate array listed above.
vi. Using a “showConfirmDialog”, prompt the user to determine if this was a first dose. If the user selects “yes”, then make the
shotStatus entry True, else use false.
c. Method 3. The users tend to include random entries in the name field of “XYZ”, or “xyz” or “xYZ”. In this method you will receive as a
parameter the name that needs to be “cleaned” (i.e. remove the XYZ from the string). Create this code to eliminate them while
preserving the rest of the name. This method will return a string containing the cleaned name. See the dialog boxes below for examples.
d. Method 4. This method will be used to validate the office name entered. Using a “switch” statement, validate that the entries are only
“DOWNTOWN” or “MAIN CAMPUS”. Please note that the user may enter this using all lower case letters or a mix of upper or lower case
letters. This will return a Boolean indicating that the entry is valid (or not).
e. Method 5. This method will instantiate the array of objects based on the data in the parallel arrays listed above. This will only create an
object for the actual patients entered. So in this example it will only create 3 objects even though there are 10 possible.
f. Method 6. This method will generate the report as shown in the black box listed below by calling the method in the patient class that
displays the patient information.
Документ1A
2
06/27/22 7:49 AM
Your output should match these examples exactly. Please note that there are 5 different dialog boxes.
Please paste your code here when complete and submit this document.
public class MedicalOffice
{
protected String officeName;
private static int noOfVaccinations;
MedicalOffice (String officeName)
{
this.officeName = officeName;
noOfVaccinations++;
}
public String getOfficeName()
{
return officeName;
}
public static int getNoOfVaccinations()
{
return noOfVaccinations;
}
}
import javax.swing.*;
Документ1A
3
06/27/22 7:49 AM
public class Patient extends MedicalOffice
{
private String patientName;
private boolean vaccineStatus;
private String vaccineType;
public Patient (String officeName, String patientName, boolean vaccineStatus, String vaccineType)
{
super (officeName);
this.patientName = patientName;
this.vaccineStatus = vaccineStatus;
this.vaccineType = vaccineType;
}
public void printPatientData()
{
System.out.println(officeName + "\t" + patientName + "\t\t" + vaccineType + "\t\t" +
vaccineStatus);
}
}
import javax.swing.*;
public class MIS3370AFall2021
{
private static String[] name = new String [10];
private static String[] vaccine = new String [10];
private static boolean[] status = new boolean [10];
private static String[] office = new String [10];
private static Patient[] patientObject = new Patient[10];
private static int count = 0;
public static void main (String[] args)
{
count = Integer.parseInt(JOptionPane.showInputDialog
(null, "How many patients received Vaccines?"));
getInput();
buildPatientObjects();
System.out.println("Vaccine shots administered");
System.out.println("Office\t\tPatient\t\tVaccine\tReceived 2nd Dose");
generateReport();
System.out.println("\ntotal number of vaccines administered - " + Patient.getNoOfVaccinations());
}
public static void getInput()
{
boolean valid = false;
for (int i = 0; (i < count);++i)
{
office[i] = JOptionPane.showInputDialog (null,"Enter the office that did the vaccination");
valid = validateOffice(office[i]);
if (valid)
{
name[i] = JOptionPane.showInputDialog(null, "Enter the patient name");
name[i] = cleanName(name[i]);
vaccine[i] = JOptionPane.showInputDialog(null, "Which vaccine did " +
name[i] + " receive?");
int noOfShots = JOptionPane.showConfirmDialog(null,
"Was this a first dose?");
if (noOfShots == JOptionPane.YES_OPTION)
status[i] = false;
else
status [i] = true;
}
else
--i;
}
}
public static String cleanName(String badName)
{
Документ1A
4
06/27/22 7:49 AM
for (int i = 0; i<badName.length(); ++i)
{
badName = badName.replace("XYZ"," ");
badName = badName.replace("xyz"," ");
badName = badName.replace("xYZ"," ");
}
for (int i = 0; i< badName.length();++i)
{
badName = badName.replace(" "," ");
}
if (badName.charAt(0) == ' ')
badName = badName.substring(1,badName.length());
return badName;
}
public static boolean validateOffice(String office)
{
boolean valid = false;
switch(office.toUpperCase())
{
case "DOWNTOWN":
valid = true;
return valid;
case "MAIN CAMPUS":
valid = true;
return valid;
}
return valid;
}
public static void buildPatientObjects()
{
for(int i = 0; i < count ;++i)
{
patientObject[i] = new Patient( office[i],name[i],status[i],vaccine[i]);
}
}
public static void generateReport()
{
for(int i = 0; i<count;++i)
patientObject[i].printPatientData();
}
}
Документ1A
5
06/27/22 7:49 AM
Download