Java :erreur de compilation

Fermé
domxaline - 2 avril 2012 à 12:39
KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 - 4 avril 2012 à 12:22
Bonjour,
j'ai une erreur lors de compilation,veuillez aidez moi svp
abstract public class Figure 
{
 int x,y;
 void changePosition (int newX,int newY)
 { }
 abstract void draw();
 {}
}

public class CirculeObject extends Figure
{
  void draw()
  {
	  System.out.println("Draw method called");
  }
}

public class RectangleObject extends Figure 
{
 void changePosition(int newX,int newY)
 {
	 System.out.println("change position method called");
 }
 public static void main(String[]args)
 {
	FigureObject f0=new FigureObject(20,15);
	f0.draw();
	CirculeObject f1=new CirculeObject();
	f1.draw();
    RectangleObject f2=new RectangleObject() ;
	f2.changePosition(10,20);
 }
}


Exception in thread "main" java.lang.Error: Unresolved compilation problems:
FigureObject cannot be resolved to a type
FigureObject cannot be resolved to a type

at RectangleObject.main(RectangleObject.java:10)

A voir également:

8 réponses

KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
2 avril 2012 à 13:34
Si draw est abstraite dans Figure alors :
1) Tu ne dois pas mettre d'accolades derrière.
2) Tu dois définir la méthode draw dans RectangleObject sinon elle sera également abstraite et tu ne pourra pas faire d'instanciation (new RectangleObject)
0
vous voulez que je fasse comme ça
abstract public class Figure 
{
 int x,y;
 void changePosition (int new x,int new y)
 {
	 
 }
 abstract void draw();
}

public class RectangleObject extends Figure 
{
    //void changePosition(int newX,int newY)
	void draw(int newX,int newY)
 {
	 System.out.println("change position method called");
 }
 public static void main(String[]args)
 {
	FigureObject f0=new FigureObject(20,15);
	f0.draw();
	CirculeObject f1=new CirculeObject();
	f1.draw();
    RectangleObject f2=new RectangleObject() ;
	f2.changePosition(10,20);
 }
}

j'ai toujours la même erreur
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
FigureObject cannot be resolved to a type
FigureObject cannot be resolved to a type
The method changePosition() in the type Figure is not applicable for the arguments (int, int)

at RectangleObject.main(RectangleObject.java:11)
0
KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
2 avril 2012 à 15:05
On ne sais pas ce que c'est FigureObject !
Tu as Figure, CirculeObject et RectangleObject, mais pas de classe FigureObject...
0
abstract public class Figure
{
int x,y;
void changePosition (int new x,int new y)
{

}
abstract void draw();

}
0
le correction
public class RectangleObject extends Figure
{
void changePosition(int newX,int newY)
void draw(int newX,int newY)
{
System.out.println("change position method called");
}
public static void main(String[]args)
{
Figure f0=new Figure(20,15);
f0.draw();
CirculeObject f1=new CirculeObject();
f1.draw();
RectangleObject f2=new RectangleObject() ;
f2.changePosition(10,20);
}
}
0
KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
2 avril 2012 à 15:29
Tu ne peux pas faire de "new Figure" car Figure est abstraite. De même pour RectangleObject qui se retrouve également abstraite puisque tu n'as pas définis la méthode abstraite draw()
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
en générale la sous classe devient abstraite l'orque elle contient une méthode abstraite au fait je vous demande est ce qu'il y a d'autre cas dans le qu'elle la sous classe sois abstraite
0
KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
2 avril 2012 à 17:41
Tu peux explicitement mettre le mot clé abstract sur une classe, même si elle n'a pas de méthode abstraite, et elle sera abstraite quand même.

Par exemple, cette classe n'a aucune méthode abstraite, mais elle est abstraite quand même...

public abstract class Test
{
}
0
abstract public class Figure 
{
 int x,y;
 
 void changePosition (int x,int y)
 {
	 
 }
  abstract void draw()
}

public abstract class CirculeObject extends Figure
{
  void draw()
  {
	  System.out.println("Draw method called");
  }
}

public abstract class RectangleObject extends Figure 
{
    void changePosition(int newX,int newY)
	void draw(int newX,int newY)
 {
	 System.out.println("change position method called");
 }
 public static void main(String[]args)
 {
	Figure f0=new Figure(20,15);
	f0.changePosition();
	CirculeObject f1=new CirculeObject();
	f1.draw();
    RectangleObject f2=new RectangleObject() ;
	f2.changePosition(10,20);
 }
}

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot instantiate the type Figure
The method changePosition(int, int) in the type Figure is not applicable for the arguments ()
Cannot instantiate the type CirculeObject
Cannot instantiate the type RectangleObject

at RectangleObject.main(RectangleObject.java:11)
0
KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
2 avril 2012 à 18:16
Tout est dit dans le message !
Tu fais f0.changePosition(); alors que changePosition attends deux arguments (int, int)
Apprends à lire les messages d'erreurs !!!
0
public abstract class RectangleObject extends Figure 
{
    void changePosition(int newX,int newY)
	void draw(int newX,int newY)
 {
	 System.out.println("change position method called");
 }
 public static void main(String[]args)
 {
	Figure f0=new Figure();
	f0.changePosition(20,15);
	CirculeObject f1=new CirculeObject();
	f1.draw();
    RectangleObject f2=new RectangleObject() ;
	f2.changePosition(10,20);
 }
}

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot instantiate the type Figure
Cannot instantiate the type CirculeObject
Cannot instantiate the type RectangleObject

at RectangleObject.main(RectangleObject.java:11)
0
KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
2 avril 2012 à 18:44
0
bonjour j'ai corrigé mon prg ainsi mais toujous j'ai une erreur
abstract public class Figure 
{
 int x,y;
 
 void changePosition (int newX,int newY)
 {
	 
 }
 abstract void draw();
}


public abstract class CirculeObject extends Figure
 {
  void draw()
  {
	  System.out.println("Draw method called");
  }
 }


public abstract class RectangleObject extends Figure 
{
    void changePosition(int newX,int newY)
	
     {
	    System.out.println("change position method called");
     }	
 }


public class AbstractEssaie 
{
	public static void main(String[]args)
	 {
		CirculeObject c=new CirculeObject();
		Figure figuref;
		figuref=c;
		System.out.println("CirculeObject is"+figuref.changePosition(newX,newY));
		
		RectangleObject r =new RectangleObject();
		figuref=r;
		System.out.println("RectangleObject is"+figuref.draw(x,y));
		
     }
}

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot instantiate the type CirculeObject
newX cannot be resolved to a variable
newY cannot be resolved to a variable
Cannot instantiate the type RectangleObject
x cannot be resolved to a variable
y cannot be resolved to a variable

at AbstractEssaie.main(AbstractEssaie.java:6)
0
KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
4 avril 2012 à 12:22
Il faut lire le message d'erreur qui est pourtant clair, tu utilises dans ton main des variables newX, newY, x et y, qui ne sont pas déclarés et qui n'ont donc aucune valeur !
0