Our prof give us an activity to create a overloaded constructor that stores blood type. I've already Created two (2) classes named BloodData (no class modifier) and RunBloodData (public). My problem is I don't how to apply the if else statements to this 2 objects in the main method, if the user does not input anything Hence, the values stored in the default constructor are displayed.
public class RunBloodData {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter blood type of patient: ");
String input1 = input.nextLine();
System.out.print("Enter the Rhesus Factor (+ or -): ");
String input2 = input.nextLine();
//How to use conditions in this 2 objects with agruments and w/out arguments?
BloodData bd = new BloodData(input1,input2);
bd.display();
System.out.println(" is added to the blood bank.");
// if the user did not input values, the values stored in the default constructor should display this.
BloodData bd = new BloodData();
bd.display();
System.out.println(" is added to the blood bank.");
//constructor
class BloodData {
static String bloodType;
static String rhFactor;
public BloodData() {
bloodType = "O";
rhFactor = "+";
}
public BloodData(String bt, String rh) {
bloodType = bt;
rhFactor = rh;
}static
public void display(){
System.out.print(bloodType+rhFactor);
}
}
// this is the output of it
Enter blood type of patient: B
Enter the Rhesus Factor (+ or -): -
B- is added to the blood bank.
O+ is added to the blood bank.//this should not be displayed since the user input values. How to fix it?
This is a part of the instruction in the activity. --In the main method, add statements to ask the user to input the blood type and the Rhesus factor (+ or -). Instantiate a BloodData object name with arguments based on the user input. For example, BloodData bd new BloodData(inputl, input2); where input1 and input2 are String variables that stored what the user entered. If the user does not input anything, instantiate a BloodData object without an argument. Print the confirmation message by invoking the display method through the object you created For example, bd. disp1ay;
Aucun commentaire:
Enregistrer un commentaire