0

I'm currently working on a very simple script and I'm getting an error that doesn't pop up in the example provided. I have a set of code and a client to test it. I'm getting this error when I try to run the code. I get this for all three instances where I try to use "new Class" in the client.

ClassClient.java:5: error: constructor Class in class Class cannot be applied to given types;

     Class code = new Class("1234");
                  ^
required: String,String,String
found: String
reason: actual and formal argument lists differ in length

Here is the first set of code:

public class Class
{
//attributes
public  String code;
private String name;
private String credit;

//constructor
public Class(String newCode, String newName, String newCredit)
{
  setCode(newCode);
  setName(newName);
  setCredit(newCredit);
}

//get|sets

public String getCode()
{
   return code;
}

public void setCode(String newCode)
{
   code = newCode;
}

public String getName()
{
   return name;
}

 public void setName(String newName)
{
   name = newName;
}

public String getCredit()
{
   return credit;
}

public void setCredit(String newCredit)
{
   credit = newCredit;
}

public String toString()
{
return ("The code for this class is: "+code + "\n" + "The name for this class is :"+name + "\n" +  "The number of credits this course has is :"+credit);
}

public boolean equals(Object o)
{
if(!(o instanceof Class))
   return false;
else
{
   Class x = (Class) o;
   Class y = (Class) o;
   Class z = (Class) o;

   return(code.equals(x.code) || name.equals(y.name) || credit.equals(z.credit));
}


}//endelse
}//end class

And here is the client that tests the code:

public class ClassClient
{
public static void main(String [] args)
  {
     Class code = new Class("1234");
     Class name = new Class("Java");
     Class credit = new Class("4");
     System.out.println("The code for this class is: "+code + "\n" + "The name for this class is :"+name + "\n" +  "The number of credits this course has is :"+credit);
  }
}
2
  • Check your constructor parameters number. Commented Jan 23, 2015 at 9:27
  • I would suggest to please read the compile time error which the compiler showed. Clearly mentioned there what it needs. Commented Jan 23, 2015 at 9:32

2 Answers 2

1

Constructor in class accept 3 String arguments not 1.

You can change

 Class code = new Class("1234"); // you are calling single argument
 Class name = new Class("Java"); // but there is no matching 
 Class credit = new Class("4"); // constructor for this

To

 Class cl = new Class("1234","Java","4");

Additionally to get values you should use getters.

You can change

System.out.println("The code for this class is: "+code + "\n" 
      + "The name for this class is :"+name + "\n" 
             +  "The number of credits this course has is :"+credit);

To

System.out.println("The code for this class is: "+cl.getCode() + "\n" 
      + "The name for this class is :"+cl.getName() + "\n" 
             +  "The number of credits this course has is "+cl.getCredit());
Sign up to request clarification or add additional context in comments.

4 Comments

Additionally, the println statement should use the getter methods to get the code, name and credit of the created Class object.
I'm actually looking for my client to test all 3 inputs of "code" "name" and "credit" individually. Using this method doesn't it just set the strings of "1234" "java" and "4" to code instead of using all 3 predefined strings? Would I need to change how my constructor is laid out?
@MattiasBuelens yes. I added that part to answer.
Ah I see, thank you very much. I'm a bit inexperienced so it didn't make sense at first to me haha
0

You are correct in your ClassClient if you have a constructor in Class class like this.

  //constructor

 public Class(String somethig)
   {
     // set somethig
  }

but in you constructor it expecting three arguments. but you have providded only one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.