Java Assignment1 Sample Solution
Java Assignment1 Sample Solution
*;
/*
* Sample Solution for Assignment 1
* BIT106/DIP215 Semester 3, 2014
*/
public class Assignment1Dengue {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int numAdmitted = 0;
int numDischarged = 0;
char another;
do // main loop to get a patient
{
String name;
do // input and validate name
{
System.out.print("Enter patient name :");
name= sc.nextLine();
if (name.equals(""))
System.out.println("Name cannot be blank");
} while (name.equals(""));
boolean needToAdmit = false; // assume that
by default, no need to admit
// input and validate history of bleeding
System.out.print("Does patient have history of
bleeding? ");
char bleeding = sc.nextLine().charAt(0);
while (!(bleeding =='y' || bleeding=='Y' ||
bleeding=='N' ||bleeding == 'n'))
{
System.out.println("Please enter Y or N");
System.out.print("Does patient have history of
bleeding? ");
bleeding = sc.nextLine().charAt(0);
}
if (bleeding == 'Y' || bleeding == 'y')
needToAdmit = true;
else // no bleeding history
{ // check urea
System.out.print("Enter patient's urea level");
double urea = sc.nextDouble();
while (urea < 0 || urea > 10)
{
System.out.println("Error. Urea level must
be between 0 and 10");
System.out.print("Enter patient's urea
level");
urea = sc.nextDouble();
}
sc.nextLine();
if (urea > 4)
needToAdmit = true;
else // urea is less than or equal to 4
{ // check protein
System.out.print("Enter patient's protein
level");
double protein = sc.nextDouble();
while (protein < 0 || protein > 150)
{
System.out.println("Error. Protein
level must be between 0 and 150");
System.out.print("Enter patient's
protein level");
protein = sc.nextDouble();
}
sc.nextLine();
if (protein <= 67)
needToAdmit = true;
}
}
// check if need to admit
if (needToAdmit)
{
System.out.println("Please admit the patient
immediately");
numAdmitted++;
}
else
{
System.out.println("Patient can be discharged");
numDischarged++;
}
do
{
System.out.print("Do you want to enter another
patient?");
another = sc.nextLine().charAt(0);
} while (!(another =='y' || another=='Y' ||
another=='N' ||another == 'n'));
} while (another=='y'|| another == 'Y');
// print summary
System.out.println("Number of patients admitted :" +
numAdmitted);
System.out.println("Number of patients discharged :" +
numDischarged);
}
}