4 Replies - 3595 Views - Last Post: 14 April 2012 - 08:15 AM Rate Topic: -----

#1 lilVaratep  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 207
  • Joined: 09-October 11

OOP: actual argument Language cannot be converted to String...

Posted 13 April 2012 - 11:59 PM

Homework3.java:17: error: constructor Language in class Language cannot be appli
ed to given types;
                Language aaa = new Language(aaa);
                               ^
  required: String
  found: Language
  reason: actual argument Language cannot be converted to String by method invoc
ation conversion
Homework3.java:18: error: constructor Language in class Language cannot be appli
ed to given types;
                Language aaaa = new Language(aaaa);
                                ^
  required: String
  found: Language
  reason: actual argument Language cannot be converted to String by method invoc
ation conversion
Homework3.java:19: error: constructor Language in class Language cannot be appli
ed to given types;
                Language aaaaa = new Language(aaaaa);
                                 ^
  required: String
  found: Language
  reason: actual argument Language cannot be converted to String by method invoc
ation conversion
Homework3.java:20: error: constructor Language in class Language cannot be appli
ed to given types;
                Language aaaaaa = new Language(aaaaaa);
                                  ^
  required: String
  found: Language
  reason: actual argument Language cannot be converted to String by method invoc
ation conversion
4 errors


Homework3.java
class Homework3
{
	public static void main(String args[])
	{
		Rectangle one = new Rectangle();
		Rectangle two = new Rectangle(4,40);
		Rectangle three = new Rectangle(6.5, 35.4);
		one.getArea();
		one.getPerimeter();
		two.getArea();
		two.getPerimeter();
		three.getArea();
		three.getPerimeter();
		
		Language aaa = new Language(aaa);
		Language aaaa = new Language(aaaa);
		Language aaaaa = new Language(aaaaa);
		Language aaaaaa = new Language(aaaaaa);
		aaa.isPrime();
		aaa.getResult();
		aaaa.isPrime();
		aaaa.getResult();
		aaaaa.isPrime();
		aaaaa.getResult();
		aaaaaa.isPrime();
		aaaaaa.getResult();
	}
}


Language.java
class Language
{
	String word;
	
	Language(String word)
	{
		this.word = word;
		
	}
	
	boolean isPrime()
	{
		if (word.length() == 1) return false;
		for (int k = 2; k * k <= word.length(); k++)
			if (word.length() % k == 0) return false;
		return true;
	}
	
	boolean isLanguage()
	{
		for (int i = 1; i < word.length(); i++)
			if (word.charAt(i) != 'a') return false;
		return true;
	}
	
	void getResult()
	{
		System.out.println("Word:  " + word + "\nResult:  " + (isPrime() && isLanguage()));
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: OOP: actual argument Language cannot be converted to String...

#2 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: OOP: actual argument Language cannot be converted to String...

Posted 14 April 2012 - 12:05 AM

aa, aaa, aaaa, aaaaa are not words or rather 'Strings', you need to enclose them in ""s, like "aa" to make them strings.

Also, are you trying to do something with this code, or testing stuff? I can't see any purpose to the code. Prime languages?

This post has been edited by Mylo: 14 April 2012 - 12:06 AM

Was This Post Helpful? 1
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: OOP: actual argument Language cannot be converted to String...

Posted 14 April 2012 - 12:11 AM

Somehow I think you're not kidding. Are you kidding? Maybe you just don't understand how constructors work.

Your statements:

Language aaa = new Language(aaa);

make no sense on several levels.

Since the Language() constructor expects a String object as an argument, maybe you meant:

Language aaa = new Language("aaa");

Even so, naming your variables some number of 'a's is not good practice. Give your variables meaningful names.
Was This Post Helpful? 2
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,688
  • Joined: 19-March 11

Re: OOP: actual argument Language cannot be converted to String...

Posted 14 April 2012 - 08:11 AM

I'm curious about that error language. Is it new to 7? Are you running 7? When I do something like this in 6 I get "variable foo might not have been initialized"

Obvious problems are obvious: you can't use an object as an argument to its own constructor.

EDIT: Rethink. Yes, you can, but not when it's creating itself for the first time.

This works:

      ArrayList l = new ArrayList();
      l = new ArrayList(l);


But not this:

      ArrayList l = new ArrayList(l);

This post has been edited by jon.kiparsky: 14 April 2012 - 08:13 AM

Was This Post Helpful? 0
  • +
  • -

#5 lilVaratep  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 207
  • Joined: 09-October 11

Re: OOP: actual argument Language cannot be converted to String...

Posted 14 April 2012 - 08:15 AM

Oh wow... what a stupid mistake. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1