Toon posts:

[java] objects and classes :(

Pagina: 1
Acties:

Verwijderd

Topicstarter
Help, zit helemaal vast vlak voor mijn tentamens! Het probleem:

Ik moet dus een client program maken om een class te testen.

De class is:
public class Rectangle
{
private double width;
private double height;

// Default constructor
public Rectangle()
{
this(1.0, 1.0);
}

// Construct a rectangle with specified width and height
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}

// Getter method for width
public double getWidth()
{
return width;
}

// Setter method for width
public void setWidth(double width)
{
this.width = width;
}

// Getter method for height
public double getHeight()
{
return height;
}

// Setter method for height
public void setHeight(double height)
{
this.height = height;
}

// Implement the findArea method in GeometricObject
public double findArea()
{
return width*height;
}
}
en de 'zelfgemaakte' test programma:
public class One
{
Rectangle rect1 = new Rectangle();

rect1.setWidth(5.0);

System.out.println(rect1.getWidth());
}
Dit is de compiler output, als ik One.java probeer te compileren:
One.java:5: Type expected.

rect1.setWidth(5.0);

^

1 error
Ik heb alles al geprobeerd, maar ik kom er niet uit, kan iemand me alsjeblieft helpen?:'(
(komt uit het boek: introduction to java programming, schrijver: Y.D.Liang)

alvast bedankt

  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
code:
1
2
3
4
5
6
7
8
public class One
{
    Rectangle rect1 = new Rectangle();

    rect1.setWidth(5.0);

    System.out.println(rect1.getWidth());
}

Deze code staat direct in je klasse. Dat kan niet en is volkomen onlogisch. Die rectangle declaratie en assignment kan nog net, de statement rect1.bla kan echt niet.

Waarschijnlijk wil je dit in een public static void main(String[] ps) methode hebben... :)

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • stylee
  • Registratie: December 2000
  • Laatst online: 04-09-2021

stylee

blah zeg ik je

je bent je Main vergeten :+

die mbravenboer is gewoon irritant snel bij alle java topics >:)

  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
Als je het aanpast compileert het verder goed zie ik hier ;) .

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
stylee: die mbravenboer is gewoon irritant snel bij alle java topics >:)
Sorry :o .

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


Verwijderd

Topicstarter
hey, thanks!!!! :D, dat was het inderdaad. Ik ben gewoon de main method vergeten!! AAAAAAAAAAAAAAAA |:(
Pagina: 1