0% found this document useful (0 votes)
90 views3 pages

Java Assignment1 Sample Solution

This Java program uses a do-while loop to input patient data, check for conditions requiring admission (a history of bleeding, high urea or low protein levels), and tally the numbers of patients admitted and discharged. It prompts the user to enter a patient's name, bleeding history, urea and protein levels, determines if admission is necessary, and repeats in a loop until the user chooses to stop entering patients. Finally, it prints the numbers of patients admitted and discharged.

Uploaded by

Sn Lau
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views3 pages

Java Assignment1 Sample Solution

This Java program uses a do-while loop to input patient data, check for conditions requiring admission (a history of bleeding, high urea or low protein levels), and tally the numbers of patients admitted and discharged. It prompts the user to enter a patient's name, bleeding history, urea and protein levels, determines if admission is necessary, and repeats in a loop until the user chooses to stop entering patients. Finally, it prints the numbers of patients admitted and discharged.

Uploaded by

Sn Lau
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

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

}
}

You might also like