What's new

Java Help di ko po gets yung sinasabi na use the setter methods to accept user input???

najamjam

Grasshopper
Joined
Sep 23, 2021
Posts
51
Solutions
9
Reaction
13
oop 05 handson 1.JPG


Ito yung codes before pa i-modify so wala pa yung encapsulation dito:

Java:
class BloodData {
    static String bloodType;
    static String rhFactor;

    public BloodData() {
        bloodType = "O";
        rhFactor = "+";
    }
    public BloodData(String bt, String rh) {
        bloodType = bt;
        rhFactor = rh;
        }
    public void display() {
        System.out.print(bloodType + rhFactor + " is added to the blood bank.");
    }
}

Java:
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 bt = sc.nextLine();
        System.out.print("Enter the Rhesus factor (+ or -): ");
        String rh = sc.nextLine();

            if(bt.equals("") && rh.equals("")) {
                BloodData bd = new BloodData();
                bd.display();
            }
            else {
                BloodData bd = new BloodData(bt, rh);
                bd.display();
            }
    }
}
 

Attachments

Last edited:

2pepot

Addict
TS, please study "encapsulation" first for better understanding of the use of getters and setters.

example of getters and setters:

Java:
public class BloodData {

  private String bloodType; // private = restricted access
  private String rhFactor;  // private = restricted access

  // Getters
  public String getBloodType() {
    return bloodType;
  }

  public String getRhFactor() {
    return bloodType;
  }

  // Setters
  public void setBloodType(String newBloodType) {
    this.bloodType = newBloodType;
  }

  public void setRhFactor(String newRhFactor) {
    this.rhFactor = newRhFactor;
  }

}
 

najamjam

Grasshopper
TS, please study "encapsulation" first for better understanding of the use of getters and setters.

example of getters and setters:

Java:
public class BloodData {

  private String bloodType; // private = restricted access
  private String rhFactor;  // private = restricted access

  // Getters
  public String getBloodType() {
    return bloodType;
  }

  public String getRhFactor() {
    return bloodType;
  }

  // Setters
  public void setBloodType(String newBloodType) {
    this.bloodType = newBloodType;
  }

  public void setRhFactor(String newRhFactor) {
    this.rhFactor = newRhFactor;
  }

}
Java:
class BloodData {
    private String bloodType;
    private String rhFactor;

    public void setBloodType(String bloodType) {
        this.bloodType = bloodType;
    }
    public void setRhFactor(String rhFactor) {
        this.rhFactor = rhFactor;
    }
    public String getBloodType() {
        return bloodType;
    }
    public String getRhFactor() {
        return rhFactor;
    }
    public void readInput(String bloodType, String rhFactor) {
        setBloodType(bloodType);
        setRhFactor(rhFactor);
    }
    public void writeOutput() {
        System.out.println(getBloodType() + getRhFactor() + " is added to the blood bank.");
    }
    public BloodData() {
        this.bloodType = "O";
        this.rhFactor = "+";
    }
    public void display() {
        System.out.println(bloodType + rhFactor + " is added to the blood bank.");
    }
}

Ganito po ginawa ko gumana na siya pero di ko lang alam kung nasunod ko instructions hahahaha
 
Good job TS! ;)(y)
Lodi ano Yung code sa RunBlood data Ngay setter at getter na

Lo
Java:
class BloodData {
    private String bloodType;
    private String rhFactor;

    public void setBloodType(String bloodType) {
        this.bloodType = bloodType;
    }
    public void setRhFactor(String rhFactor) {
        this.rhFactor = rhFactor;
    }
    public String getBloodType() {
        return bloodType;
    }
    public String getRhFactor() {
        return rhFactor;
    }
    public void readInput(String bloodType, String rhFactor) {
        setBloodType(bloodType);
        setRhFactor(rhFactor);
    }
    public void writeOutput() {
        System.out.println(getBloodType() + getRhFactor() + " is added to the blood bank.");
    }
    public BloodData() {
        this.bloodType = "O";
        this.rhFactor = "+";
    }
    public void display() {
        System.out.println(bloodType + rhFactor + " is added to the blood bank.");
    }
}

Ganito po ginawa ko gumana na siya pero di ko lang alam kung nasunod ko instructions hahahaha
Lodi ano Po Ang code sa Runblooddata Ng may getter at setter na
 
Top