Check alle échte Black Friday-deals Ook zo moe van nepaanbiedingen? Wij laten alleen échte deals zien

[Java] Krijg mijn programma niet werkend

Pagina: 1
Acties:
  • 496 views

  • jan.manders
  • Registratie: Februari 2012
  • Laatst online: 31-07-2021
Beste mensen,

Ben bezig geweest met een project voor school waarbij ik het bewegen van een biljartbal
over het speelveld beschrijf. Ik heb een hele class geschreven voor de biljartbal.
Maar ik krijg het niet voor elkaar dat het programma de method gebruikt op de manier die ik wil.
Dat is dus bij elke verplaatsing van 0.1 x de nieuwe y waarde en de nieuwe snelheid berekenen.

Ben nog niet heel ervaren met programmeren dus denk dat ik ergens een grote fout heb gemaakt.
Graag hulp bij het vinden van deze fout.

de error (in de regel: Biljartbal rood = new Biljartbal(0.38,0.12).shoot(15.0, 33.6);) :

incompatible types
required: alfa05.Biljartbal
found: void
-----------------------


bij voorbaat dank




/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package alfa05;

/**
*
* @author Manders
*/
public class Alfa05 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

}

Biljartbal rood = new Biljartbal(0.38,0.12).shoot(15.0, 33.6);


}

class Biljartbal {
double speed = 0.0; //ms^-1
double mass = 0.22; //kg
double radius = 0.036; //m
double Elasticity = 0.99; // on a scale from 0 to 1
double xCoordinate; // length of the playing field
double yCoordinate; // width of the playing field
double direction;
double b; // from: y = ax + b
double s; // distance traveled (m)
double F = 0.2; // force of friction between Ball and surface (N)
//constructor
public Biljartbal(double x, double y) {
x = xCoordinate;
y = yCoordinate;

}
// method
// for a shot
void shoot(double increment, double angle){
direction += angle;
speed += increment;

do {

// calculating the new Coordinates of the ball
// depending on the increasement of the x-Coordinate
do {
b = (yCoordinate - Math.tan(direction) * xCoordinate);
xCoordinate += 0.01;
yCoordinate = (Math.tan(direction) * xCoordinate + b);
System.out.println("x: " + xCoordinate +
" y: " + yCoordinate);

// calculating the distance traveled by the ball
// .sqrt = square root
// .pow(a,b) = a raised to the power of b
s = Math.sqrt(Math.pow(Math.abs((yCoordinate
- ((xCoordinate - 0.1)
* Math.tan(direction)+ b))), 2.0) +
Math.pow(0.1, 2.0));
// calculating the new speed:
// speed = Math.sqrt(v² - 2*F*s/mass)
speed = Math.sqrt(Math.pow(speed, 2.0) - (2.0*F*s/mass));
System.out.println("speed: " + speed);

// new direction when ball hits north or south bumper:
if ( (yCoordinate + radius) >= 1.4225 ||
(yCoordinate - radius) <= 0.0) {
direction = (360.0 - direction);
}
// new direction when ball hits east bumper:
if ( (xCoordinate + radius) >= 2.845 &&
(direction < 90.0) ) {
direction = (180.0 - direction);
}
if ( (xCoordinate + radius) >= 2.845 &&
(direction > 270.0) ) {
direction = (540.0 - direction);
}


} while (direction < 90.0 || direction > 270.0);


do {
b = (yCoordinate - Math.tan(direction) * xCoordinate);
xCoordinate -= 0.01;
yCoordinate = (Math.tan(direction) * xCoordinate + b);
System.out.println("x: " + xCoordinate +
" y: " + yCoordinate);

// calculating the distance traveled by the ball
// .sqrt = square root
// .pow(a,b) = a raised to the power of b
s = Math.sqrt(Math.pow(Math.abs((yCoordinate -
((xCoordinate + 0.1)
* Math.tan(direction)+ b))), 2.0)
+ Math.pow(0.1, 2.0));
// calculating the new speed:
// speed = Math.sqrt(v² - 2*F*s/mass)
speed = Math.sqrt(Math.pow(speed, 2.0) - (2.0*F*s/mass));
System.out.println("speed: " + speed);

//new direction when ball hits north or south bumper
if ( (yCoordinate + radius) >= 1.4225 ||
(yCoordinate - radius) <= 0.0) {
direction = (360.0 - direction);
}

//new direction when ball hits west bumper
if ( (xCoordinate - radius) <= 0.0 &&
(direction > 180.0) ) {
direction = (540.0 - direction);
}
if ( (xCoordinate - radius) <= 0.0 &&
(direction < 180.0) ) {
direction = (180.0 - direction);
}

} while (direction > 90.0 && direction < 270.0 &&
direction != 180.0);

do {
yCoordinate += 0.1;
System.out.println("x: " + xCoordinate +
" y: " + yCoordinate);
s = 0.1;
speed = Math.sqrt(Math.pow(speed, 2.0) - (2.0*F*s/mass));
System.out.println("speed: " + speed);

if ((yCoordinate + radius) >= 1.4225) {
direction = 270.0;
}

} while (direction == 90.0);


do {
yCoordinate -= 0.1;
System.out.println("x: " + xCoordinate +
" y: " + yCoordinate);
s = 0.1;
speed = Math.sqrt(Math.pow(speed, 2.0) - (2.0*F*s/mass));
System.out.println("speed: " + speed);

if ((yCoordinate - radius) <= 0.0) {
direction = 90.0;
}

} while (direction == 270.0);

do {
xCoordinate -= 0.1;
System.out.println("x: " + xCoordinate +
" y: " + yCoordinate);
s = 0.1;
speed = Math.sqrt(Math.pow(speed, 2.0) - (2.0*F*s/mass));
System.out.println("speed: " + speed);
if ((xCoordinate - radius) <= 0.0) {
direction = 0.0;
}
} while (direction == 180.0);

do {
xCoordinate += 0.1;
System.out.println("x: " + xCoordinate +
" y: " + yCoordinate);
s = 0.1;
speed = Math.sqrt(Math.pow(speed, 2.0) - (2.0*F*s/mass));
System.out.println("speed: " + speed);
if ((xCoordinate - radius) <= 0.0) {
direction = 180.0;
}
} while (direction == 0.0);

} while (speed > 0.0);

}

}


class Speelveld {
double length = 2.845; //m
double width = 1.4225; //m
double bumperHeight = 0.036; //m
double bumperElasticity = 0.7; //scale of 0 to 1

//constructor
public Speelveld () {
};
}



  • Haan
  • Registratie: Februari 2004
  • Laatst online: 21:08

Haan

dotnetter

Als dit letterlijk je code is, zou ik die regel eens in de body van je main method zetten ipv er buiten..

Kater? Eerst water, de rest komt later


  • RobIII
  • Registratie: December 2001
  • Niet online

RobIII

Admin Devschuur®

^ Romeinse Ⅲ ja!

(overleden)
Sorry, maar zo werkt het hier niet; het is niet de bedoeling dat je hier een lap code dumpt (en al helemaal niet zonder code tags die het tenminste nog leesbaar maken ;) ) en dat wij dan even de oplossing aandragen of de fout spotten. Zie daarvoor ook Kan iemand even...?.

Open gerust een nieuw topic mocht dat nog nodig zijn na bovenstaand post (*hint*), maar hanteer dan even onze Quickstart waarin je precies leest wat we van een topicstart verwachten hier ;)

[ Voor 4% gewijzigd door RobIII op 01-04-2012 20:59 ]

There are only two hard problems in distributed systems: 2. Exactly-once delivery 1. Guaranteed order of messages 2. Exactly-once delivery.

Je eigen tweaker.me redirect

Over mij


Dit topic is gesloten.