The document discusses encapsulation and constructors in Java. It contains code for a RunBloodData2 class that encapsulates blood type and Rh factor data by using private fields and public getter/setter methods. It also contains code for a RunBloodData class that uses two constructors - a no-arg constructor that sets default values, and a constructor that accepts blood type and Rh factor as arguments. The code takes user input for blood type and Rh factor and adds it to a "blood bank" by creating instances of the BloodData class using the appropriate constructor.
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 ratings0% found this document useful (0 votes)
2K views2 pages
Bryan (Encap)
The document discusses encapsulation and constructors in Java. It contains code for a RunBloodData2 class that encapsulates blood type and Rh factor data by using private fields and public getter/setter methods. It also contains code for a RunBloodData class that uses two constructors - a no-arg constructor that sets default values, and a constructor that accepts blood type and Rh factor as arguments. The code takes user input for blood type and Rh factor and adds it to a "blood bank" by creating instances of the BloodData class using the appropriate constructor.
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/ 2
Bryan Kyle A.
Ramos September 14, 2019
BT302
Encapsulation
package runblooddata2; import java.util.Scanner; public class RunBloodData2 {
public static void main(String[] args) {
RunBloodData_Sub bd = new RunBloodData_Sub(); Scanner sc = new Scanner(System.in); System.out.print("Enter blood type of patient: "); String z = sc.nextLine(); System.out.print("Enter the Rhesus factor (+ or -): "); String x = sc.nextLine();
if(z.equals("") && (x.equals(""))){
bd.setBloodType("O"); bd.setRhFactor("+"); System.out.println(bd.getBloodType()+ bd.getRhFactor()+" is added to the blood bank."); } else if (z.isEmpty() && x.equals("+") || x.equals("-")){ System.out.println("Wrong Input");} else if (z.equalsIgnoreCase("O")|| z.equalsIgnoreCase("A")|| z.equalsIgnoreCase("B")|| z.equalsIgnoreCase("AB")&& x.equals("+") || x.equals("-") ) { bd.setBloodType(z); bd.setRhFactor(x); System.out.println(bd.getBloodType()+ bd.getRhFactor()+" is added to the blood bank."); } else {System.out.println("Wrong Input");} } }
public class RunBloodData_Sub {
private String bloodType, rhFactor;
public void setBloodType(String bloodType){
this.bloodType = bloodType;}
public String getBloodType(){
return bloodType.toUpperCase(); }
public void setRhFactor (String rhFactor){
this.rhFactor = rhFactor; }
public String getRhFactor(){
return rhFactor; } } Constructor
package labexer5; import java.util.Scanner; public class RunBloodData { public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter blood type of patient: "); String z = sc.nextLine(); System.out.print("Enter the Rhesus factor (+ or -): "); String x = sc.nextLine();
if(z.equals("") && (x.equals(""))){
BloodData bd = new BloodData(); } else if (z.equalsIgnoreCase("O")|| z.equalsIgnoreCase("A")|| z.equalsIgnoreCase("B")|| z.equalsIgnoreCase("AB")&& x.equals("+") ||x.equals("-") ) { BloodData bd2 = new BloodData(z,x); } else {System.out.println("Wrong Input");} } }
public class BloodData {
static String bloodType, rhFactor; public BloodData(){ bloodType = "O"; rhFactor = "+"; System.out.println( bloodType + rhFactor + " is added to the blood bank.");}
public BloodData(String bt, String rh){
bloodType = bt; rhFactor = rh; System.out.println(bloodType + rhFactor + " is added to the blood bank. ");