user1914491
user1914491

Reputation: 89

Confusion about constructors

Consider the following segment of code.

SomeClass someObject1 = new SomeClass("Computer Science");
SomeClass someObject2 = new SomeClass("Computer Science", "Rocks");
SomeClass someObject3 = new SomeClass("Computer", "Science", "Rocks");

Write complete constructors for each of these statements including the assignment of variables.

I'm not sure exactly what this problem is looking for. Can someone help me out with this?

Upvotes: 0

Views: 1812

Answers (2)

SRy
SRy

Reputation: 2967

so your answer will be

public SomeClass{

 String str1;
 String str2;
 String str3;

public SomeClass(String str1){
  this.str1=str1; 
}; 
public SomeClass(String str1,String str2){
this.str1=str1;
this.str2=str2;
}; 
public SomeClass(String str1,String str2,String str3){
this.str1=str1;
this.str2=str2;
this.str3=str3;
}; 

}

Upvotes: 1

Richard Owczarzy
Richard Owczarzy

Reputation: 21

I got similar questions in my previous Java course. You are writing constructor for SomeClass. You are supposed to overload the constructor, e.g. it can accept a different number of arguments (different signatures). Your second statement is missing quotes (").

Upvotes: 0

Related Questions