Click here to Skip to main content
15,918,303 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
import java.util.Scanner;
class BloodData{
	 private String bloodType;
	 private String rhFactor;
	 
	 public BloodData(){
		bloodType = "O";
		rhFactor = "+";
	 }
	 public BloodData(String bt, String rh){
		 this.bloodType = bt;
		 this.rhFactor = rh;
	 }
	 public void display() {
		 System.out.println(bloodType+rhFactor+" is added to the blood bank");
	 }
	 public static void main(String[] args) {
		 Scanner in = new Scanner(System.in);
		 System.out.println("Enter Blood Type(O, A, B, AB)");
		 System.out.println("Enter rhFactor('+' or '-')");
		 String x = in.nextLine();
		 String y = in.nextLine();
		 BloodData bd = new BloodData(x, y);
		 bd = new BloodData();
		 bd.display();	 
	} 
}


What I have tried:

So when i run the code i want the user to enter two strings so that the second constructor would run and if they enter nothing the default constructor would run.

But when im trying to run the code even and enter two strings the default constructor always run... Hope someone helps thank you
Posted
Updated 21-Oct-20 20:33pm
v2

Quote:
i want the user to enter two strings so that the second constructor would run and if they enter nothing the default constructor would run

Java
String x = in.nextLine();
String y = in.nextLine();
BloodData bd = new BloodData(x, y);
bd = new BloodData();
bd.display();

Based on your requirement, above code does nothing of sorts. You have no logic to check if strings were entered or not. Instead, you use same variable to define the BloodData object. Default without parameters being the last call. So, display does the hardcoded O+ve always irrespective of what you enter or not.

It's not case of constructor chaining. It's simple logic problem and your constructors would be called as expected. You seem to be missing a key aspect of programming - usage of DEBUGGER. Using it in the IDE, it will help you understand every line and how it gets executed.

For learning on debugging:
jdb - The Java Debugger[^]
Debugging the Eclipse IDE for Java Developers | The Eclipse Foundation[^]
 
Share this answer
 
Comments
CPallini 22-Oct-20 2:33am    
5.
Quote:
BloodData bd = new BloodData(x, y); // here the two parameters ctor is called, the created corresponding object is assigned to bd
bd = new BloodData(); // here the default ctor is called, the newly created object is assigned to bd (previous value of bd, the former object, is lost)

You are reassigning the same variable with a different instance of the class.
Trying
Java
BloodData bd = new BloodData(x, y);
 bd.display(); // show the object created via the 2 params ctor
 bd = new BloodData(); // reassign bd
 bd.display(); // show the newly created object via the default ctor

(or using, as suggested, the debugger) will probably shed some light for you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900