Primitives and Java
Boolean - boolean
Character - char
Integer - byte, short, int, long, float
Floating-Point - float, double Float has a precision of 7 total numbers
Capitalized data types are not primitives (String, Arrays, Structures, etc.) These are Objects, primitives are not objects They have setters, getters, and manipulation
Hard Coded: Variables are predetermined and unchangeable outside of the code.
Dynamic: Are variable, like by user input.
import java.util.Scanner;
import java.util.ArrayList;
public class MedicalRecord{
public String name(){ //String is used here, because a name is a array of characters,
//therefore making String the most convenient to capture a name
Scanner input;
input = new Scanner(System.in); //Creates new scanner object
String inputName = "N/a"; //Initializes inputName as a String wrapper class.
System.out.println("Input name of patient:");
try {
inputName = input.nextLine(); //Assigns the next inputted String into inputName
} catch (Exception e) {
System.out.println("Not a valid name, " +e);
}
input.close(); //Closes input
return inputName; //Returns String inputName
}
public int age(){ //An age is typically counted in whole numbers. So is an integer,
//therefore making it the best to use.
Scanner input;
input = new Scanner(System.in);
int inputAge = 0; //Initializes integer inputAge as 0
System.out.println("Input age of patient:");
try {
inputAge = input.nextInt(); //Assigns next inputted integer to inputAge
} catch (Exception e) {
System.out.println("Not a valid age, " +e);
}
input.close();
return inputAge; //returns integer inputAge
}
public float weight(){ //Weight is often caught using 2 decimal points
//and will typically only be 5 numbers, a float has a precision of 7
//making it suitable to capture weight.
Scanner input;
input = new Scanner(System.in);
float inputWeight = 0;
System.out.println("Input weight of patient:");
try {
inputWeight = input.nextFloat();
} catch (Exception e) {
System.out.println("Not a valid weight, " +e);
}
input.close();
return inputWeight;
}
public float height(){
Scanner input;
input = new Scanner(System.in);
float inputHeight = 0;
System.out.println("Input Height of patient (Centimeters):");
try {
inputHeight = input.nextFloat();
} catch (Exception e) {
System.out.println("Not a valid Height, " +e);
}
input.close();
return inputHeight;
}
private boolean insurance(){ //Status of insurance is a yes or no parameter,
//therefore the true or false boolean can easily contain this information.
Scanner input;
input = new Scanner(System.in);
boolean inputInsurance = false;
System.out.println("Input state of insurance of patient:");
try {
inputInsurance = input.nextBoolean();
} catch (Exception e) {
System.out.println("Not true or false, " +e);
}
input.close();
return inputInsurance;
}
public ArrayList<String> condition(){ //A person can have multiple symptoms and
//conditions, therefore multiple strings in the form of an ArrayList
//contains this data easily.
ArrayList<String> symptoms = new ArrayList<String>();
Scanner input;
input = new Scanner(System.in);
boolean check0 = true;
String temp = "N/a";
while (check0 == true){
input = new Scanner(System.in);
System.out.println("Enter a Symptom, 0 to finish:");
temp = input.nextLine();
if (temp.contains("0")){
check0 = false;
break;
} else {
symptoms.add(temp);
}
}
input.close();
return symptoms;
}
public static void main(String[] args){
MedicalRecord record = new MedicalRecord(); //Creates new object record as an
//object of type MedicalRecord
String name = record.name(); //
int age = record.age();
float weight = record.weight();
float height = record.height();
boolean insured = record.insurance();
ArrayList<String> symptoms = record.condition();
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Weight: " + weight + " lbs.");
System.out.println("Height: " + height + " cm.");
System.out.println("Insured: " + insured);
System.out.println("Symptoms: " + symptoms);
}
}
MedicalRecord.main(null);
public static void main(String[] args){
int test;
double test1 = 30.99;
test = (int)test1; //This type casts double test1 to an integer, forcing 30.99 to become an integer, losing the .99.
//This type of code would be useful when you need a whole number result
//but input can be of type double or float.
System.out.println(test);
}
main(null);