<java>wight affiche pas

domxaline -  
 domxaline -
Bonjour,
j'ai essayé ce prg,dans le résultat weight affiche pas
j'ai une erreur suivant
The method weight() is undefined for the type SpecialCube
The method weight() is undefined for the type SpecialCube

at Essaie.Rectangle.main(Rectangle.java:9)

mon prg est suivant:
public class Cube1 
{
 int length;
 int breadth;
 int height;
 
 public int getVolume()
 {
	 return (length*breadth*height);
 }
 
 Cube1()
 {
	 this(10,10);
	 System.out.println("Finished with default constructor");
 }
 Cube1(int l,int b)
 {
	 this(l,b,10);
	 System.out.println("Finished with parameterized constructor having 2 params");
 }
 Cube1(int l,int b,int h)
 {
	 length=l;
	 breadth=b;
	 height=h;
	 System.out.println("Finished with parameterized constructor having 3 params");
 }
}

package Essaie;

public class SpecialCube extends Cube1 
{
 int weight;
 SpecialCube()
 {
	 super();
	 weight=10;
 }
 SpecialCube(int l,int b)
 {
	 this(l,b,10);
	 System.out.println("Finished with parameterized constructor having 2 params of SpecialCube");
 }
 SpecialCube(int l, int b,int h)
 {
	 super(l,b,h);
	 weight=20;
	 System.out.println("Finished with parameterized constructor having 3 params of SpecialCube");
 }
 
}


package Essaie;
public class Rectangle 
{
public static void main(String[]args)
{
	SpecialCube specialObj1=new SpecialCube();
	SpecialCube specialObj2=new SpecialCube(10,20);
	System.out.println("volume of SpecialCube1 is:"+specialObj1.getVolume());
	System.out.println("weight of SpecialCube1 is:"+specialObj1.weight());
	System.out.println("volume of SpecialCube2 is:"+specialObj2.getVolume());
	System.out.println("weight of SpecialCube2 is:"+specialObj2.weight());
}
}
aidez moi s'il vous plait


3 réponses

  1. vylco
     
    je pense que tu dois faire un .get(weight) car weight est un attribut de special cuble
    0
  2. domxaline
     
    non, j'attends un résultat comme ceci:

    Finished with parameterized constructor having 3 params of SpecialCube
    Finished with parameterized constructor having 2 params of SpecialCube
    volume of SpecialCube1 is:1000
    weight of SpecialCube1 is:10
    volume of SpecialCube1 is:2000
    weight of SpecialCube1 is:20
    0
  3. vylco
     
    je suis d'accord mais tu n'a pas ecrit ta méthode getWeigt() faudrait rajouter dans specialcube :

    public int getWeight(){
    return weight;
    }

    et ensuite remplacer :

    System.out.println("weight of SpecialCube1 is:"+specialObj1.getWeight());

    System.out.println("weight of SpecialCube2 is:"+specialObj2.getWeight());
    0
    1. domxaline
       
      merci beaucoup,j'ai corrigé mon erreur
      0