In: Computer Science
JAVA PROGRAM
The Florida Dental Association needs a simple program to record what teeth the members of Florida families have. Different Floridians have different numbers of different types of teeth (the program must use this simplified categorization of types of teeth):
The program must record the teeth for one Florida family. When it starts the program asks how many people there are in the family (maximally 5), then for each family member gets their name, a string of tooth types for the uppers (maximally 10 teeth including missing teeth), and a string a tooth types for the lowers (maximally 10 teeth), e.g., the string "CMCBBBMCCM" would represent 10 teeth (of which three are missing). The names are recorded in an array of strings. The tooth information is recorded in a three dimensional array of characters, where each plane corresponds to a person, there are two rows for uppers and lowers, and each row has a column for each tooth. Once the information is recorded the program must offer a menu of four options:
The program must be reasonably idiot proof:
Here's what a sample run should look like (with the keyboard input shown in italics) ...
Welcome to the Floridian Tooth Records -------------------------------------- Please enter number of people in the family : -3 Invalid number of people, try again : 7 Invalid number of people, try again : 3 Please enter the name for family member 1 : Henrietta Please enter the uppers for Henrietta : ABCDEFGHIJ Invalid teeth types, try again : BBBCMCBBBMCCMBBB Too many teeth, try again : CMCBBBMCCM Please enter the lowers for Henrietta : CCMBBMCC Please enter the name for family member 2 : Stanley Please enter the uppers for Stanley : mbbm Please enter the lowers for Stanley : ccMMcc Please enter the name for family member 3 : Raul Please enter the uppers for Raul : CCBbbcC Please enter the lowers for Raul : ccbBBCC (P)rint, (E)xtract, (R)oot, e(X)it : T Invalid menu option, try again : p Henrietta Uppers: 1:C 2:M 3:C 4:B 5:B 6:B 7:M 8:C 9:C 10:M Lowers: 1:C 2:C 3:M 4:B 5:B 6:M 7:C 8:C Stanley Uppers: 1:M 2:B 3:B 4:M Lowers: 1:C 2:C 3:M 4:M 5:C 6:C Raul Uppers: 1:C 2:C 3:B 4:B 5:B 6:C 7:C Lowers: 1:C 2:C 3:B 4:B 5:B 6:C 7:C (P)rint, (E)xtract, (R)oot, e(X)it : E Which family member : Melanie Invalid family member, try again : stanley Which tooth layer (U)pper or (L)ower : M Invalid layer, try again : u Which tooth number : 27 Invalid tooth number, try again : 4 Missing tooth, try again : 2 (P)rint, (E)xtract, (R)oot, e(X)it : P Henrietta Uppers: 1:C 2:M 3:C 4:B 5:B 6:B 7:M 8:C 9:C 10:M Lowers: 1:C 2:C 3:M 4:B 5:B 6:M 7:C 8:C Stanley Uppers: 1:M 2:M 3:B 4:M Lowers: 1:C 2:C 3:M 4:M 5:C 6:C Raul Uppers: 1:C 2:C 3:B 4:B 5:B 6:C 7:C Lowers: 1:C 2:C 3:B 4:B 5:B 6:C 7:C (P)rint, (E)xtract, (R)oot, e(X)it : R One root canal at 0.40 Another root canal at -2.07 (P)rint, (E)xtract, (R)oot, e(X)it : X Exiting the Floridian Tooth Records :-)
Code
import java.util.Scanner;
public class Main {
static Scanner scnr=new Scanner(System.in);
final static int MAX_PERSON=5;
final static int MAX_TEETH=10;
public static void main(String[] args) {
String names[];
char teeth[][][];
int num_person;
char choice;
System.out.print("Please enter number of people in the family
:");
num_person=scnr.nextInt();
while(num_person<0 || num_person>MAX_PERSON)
{
System.out.print("Invalid number of people, try again : ");
num_person=scnr.nextInt();
}
names=new String[num_person];
teeth=new char[num_person][2][];
getInput(names,teeth);
do
{
choice=menu();
switch(choice)
{
case 'P':
printInfo(names,teeth);
break;
case 'E':
ExtractTooth(names,teeth);
break;
case 'R':
rootCanalIndices(teeth);
case 'X':
break;
}
}while(choice!='X');
System.out.println("\nExiting the Floridian Tooth Records
:-)");
}
//function that will get naame and teeth
private static void getInput(String[] names, char[][][]
teeth)
{
String t;
for(int i=0;i<names.length;i++)
{
System.out.print("Please enter the name for family member "+(i+1)+"
: ");
names[i]=scnr.next();
System.out.print("Please enter the uppers for "+names[i]+" :
");
t=scnr.next();
t=t.toUpperCase();
while(!isValidTeeths(t))
{
t=scnr.next();
t=t.toUpperCase();
}
teeth[i][0]=t.toCharArray();
System.out.print("Please enter the lowers for "+names[i]+" :
");
t=scnr.next();
t=t.toUpperCase();
while(!isValidTeeths(t))
{
t=scnr.next();
t=t.toUpperCase();
}
teeth[i][1]=t.toCharArray();
}
}
//this fucntion will validate the teeth input if its valid then
return true otherwise false
private static boolean isValidTeeths(String t) {
if(t.length()>MAX_TEETH)
{
System.out.print("Too many teeth, try again : ");
return false;
}
for(int i=0;i<t.length();i++)
{
if(t.charAt(i)=='M' || t.charAt(i)=='C' || t.charAt(i)=='B')
continue;
else
{
System.out.print("Invalid teeth types, try again :");
return false;
}
}
return true;
}
//functon that will print menu and ask user choice and validate it
return the user choice
private static char menu() {
char choice;
System.out.print("\n(P)rint, (E)xtract, (R)oot, e(X)it :");
choice=scnr.next().charAt(0);
choice=Character.toUpperCase(choice);
while(!(choice=='P' || choice=='E' || choice=='R' ||
choice=='X'))
{
System.out.print("Invalid menu option, try again : ");
choice=scnr.next().charAt(0);
choice=Character.toUpperCase(choice);
}
return choice;
}
//this function will print the info of evry member
private static void printInfo(String[] names, char[][][]
teeth)
{
System.out.println("");
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]);
System.out.print(" Uppers: ");
for(int j=0;j<teeth[i][0].length;j++)
{
System.out.print((j+1)+":"+teeth[i][0][j]+" ");
}
System.out.print("\n Lowers: ");
for(int j=0;j<teeth[i][1].length;j++)
{
System.out.print((j+1)+":"+teeth[i][1][j]+" ");
}
System.out.println("");
}
}
//this function will find the name provided by the user and return
it index in array if it will in array oterwise return -1
public static int findMember(String[] names, String findName)
{
for(int i=0;i<names.length;i++)
if(names[i].equalsIgnoreCase(findName))
return i;
return -1;
}
//this function exteact the tooth of memeber
private static void ExtractTooth(String[] names, char[][][] teeth)
{
String name;
char layer;
int toothNumber;
int upperLower;
System.out.print("Which family member : ");
name=scnr.next();
int pos=findMember(names, name);
while(pos<0)
{
System.out.print("Invalid family member, try again : ");
name=scnr.next();
pos=findMember(names, name);
}
System.out.print("Which tooth layer (U)pper or (L)ower : ");
layer=scnr.next().charAt(0);
layer=Character.toUpperCase(layer);
while(!(layer=='U' || layer=='L'))
{
System.out.print("Invalid layer, try again : ");
layer=scnr.next().charAt(0);
layer=Character.toUpperCase(layer);
}
if(layer=='U')
upperLower=0;
else
upperLower=1;
System.out.print("Which tooth number : ");
toothNumber=scnr.nextInt();
boolean valid=true;
while(true)
{
if(toothNumber<0 || toothNumber>MAX_TEETH)
{
System.out.print("Invalid tooth number, try again : ");
toothNumber=scnr.nextInt();
continue;
}
if(teeth[pos][upperLower][toothNumber-1]=='M')
{
System.out.print("Missing tooth, try again : ");
toothNumber=scnr.nextInt();
continue;
}
teeth[pos][upperLower][toothNumber-1]='M';
break;
}
}
//this funciton will print root canal indeices
private static void rootCanalIndices(char[][][] teeth)
{
int B=0,M=0,C=0;
for(int i=0;i<teeth.length;i++)
{
for(int j=0;j<teeth[i][0].length;j++)
{
if(teeth[i][0][j]=='M')
M++;
else if(teeth[i][0][j]=='C')
C++;
else
B++;
}
for(int j=0;j<teeth[i][1].length;j++)
{
if(teeth[i][1][j]=='M')
M++;
else if(teeth[i][1][j]=='C')
C++;
else
B++;
}
}
double determinant = C * C - 4 * B * (-M);
double root1, root2;
if(determinant > 0) {
root1 = (-C + Math.sqrt(determinant)) / (2 * B);
root2 = (-C - Math.sqrt(determinant)) / (2 * B);
System.out.printf("One root canal at %.2f%n",root1);
System.out.printf("Another root canal at %.2f%n",root2);
}
// Condition for real and equal roots
else if(determinant == 0) {
root1 = root2 = -C / (2 * B);
System.out.printf("One root canal at %.2f%n",root1);
}
}
}
output
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.