I have the following code(after the paragraph), this calculate how long a object has fallen - give time / distance.
The code are going to do following when I run it:
Denote time in second: 10.5 s
Distance after 10.5s in free fall: 540.8meter
Time interval in second: 20.00
Conversion table: Time [sec] | Distance [m]
20,0 | 1962,0
40,0 | 7848,0
60,0 | 17658,0
80,0 | 31392,0
100,0 | 49050,0
The code:
import java.util.Scanner; // <- metode for å konvertere bytes til primitive verdier.
public class Distansee {
public static void main(String[] args) {
Scanner tastatur = new Scanner(System.in);
System.out.println("Angi tid i sekunder: "); // <- skriver ut
final double g = 9.81; //konstant til g
int test = 0;
double[][] dblArrUtskrift = new double[6][2];
while (test <=5) {
test ++;
double tid = tastatur.nextDouble(); //leser tastaturet
double distanse = (g* Math.pow(tid,2)/2.0); //Gjør regneoperasjoner. Math.pow, opphøyer verdien i tid i 2 (tid^2)
dblArrUtskrift[test][5] = tid; // her velger du seff selv noe annet
dblArrUtskrift[test][1] = distanse; // ----------"----------------
}
for (int i = 0; i <= 5; i++) {
System.out.println(dblArrUtskrift[i][0] + "|" + dblArrUtskrift[i][1]);
}
System.out.println(". Angi distanse i meter:"); //skriver ut
double distanse2 = tastatur.nextDouble(); //leser tastaturet
double tid2 = Math.sqrt((2*distanse2)/g); //gjør regneoperasjon
System.out.printf("Tiden i fritt fall: %.1f sekunder", tid2); //skriver ut tekst og svaret på regneoperasjonen
}
}
First question:
I can compile the code, but i get the following error when i run it :
java.lang.ArrayIndexOutOfBoundsException: 5
at Distansee.main(Distansee.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
Whats the reason?
Thanks for the help!


Sign In
Create Account

Back to top









