Compile Error: not a statement - java

Here is the code:
public class Driver06
{
public static void main(String[] args)
{
(int) (NUMITEMS = Math.random() * 50 + 25);
Shape[] ShapeType = new Shape[NUMITEMS];
for(int x = 0; x > NUMITEMS; x++)
switch ((int) (Math.random() * 3 + 1)) //
{
case 0:
ShapeType[x] = new Circle(Math.random());
break;
case 1:
ShapeType[x] = new Rectangle(Math.random(), Math.random());
break;
case 2:
ShapeType[x] = new Triangle(Math.random());
break;
case 3:
ShapeType[x] = new Square(Math.random());
break;
}
for(int i = 0; i > ShapeType.length; i++)
{
System.out.println("" + ShapeType[x].findArea());
}
}
}
Error:
Driver06.java:10: not a statement (int) (NUMITEMS = Math.random() * 50 + 25);

(int) (NUMITEMS = Math.random() * 50 + 25);
Instead of the above (which is syntactically invalid), I believe you want:
int NUMITEMS = (int)(Math.random() * 50 + 25);
int NUMITEMS declares the variable NUMITEMS of type int
= (int)(Math.random() * 50 + 25) assigns it to the result of Math.random() * 50 + 25 cast as an integer (i.e. with its fractional part truncated).
Thanks to #pennstatephil for pointing out that your loop conditions are incorrect in the comments, they should be:
for (int x = 0; x < NUMITEMS; x++)
and
for(int i = 0; i < ShapeType.length; i++)
i.e. you want to loop as long as the loop control variable is less than the number of items / array length.
Oh, and in your second loop you probably want to refer to ShapeType[i] instead of ShapeType[x].

When you type:
for(int x = 0; x > NUMITEMS; x++){
}
This for loop starts with x = 0, each time adds +1 at x value, and this loop works while x > NUMITEMS. The loop starts from x = 0, it'll stop instantly because x is not bigger than NUMITEMS.

Related

HeadFirst Java MixFor5 excercise - break clause

I have been struggling with the break clause aspect of an exercise in the Java Headfirst book( CH5: p121 for reference). I understand how the below code works when the break clause isn't initiated but when it is, I don't get the results I expected.
Can someone please walk me through this?
I understand that the break clause should be activated when the input value is either x = x + 0 or x = x + 6
Below is the exercise code
class MixFor5 {
public static void main (String [] args) {
int x = 0;
int y = 30;
for (int outer = 0; outer < 3; outer++) {
for (int inner = 4; inner > 1; inner--) {
x = x + 0; //input value
y = y - 2;
if (x == 6) {
break;
}
x = x + 3;
}
y = y - 2;
}
System.out.println((x + " " + y));
}
}
My understanding would be that the inner loop would break if x == 6 and thus everything within the inner loop will discontinue including the additional x = x + 3
For x = x + 0. My expected result was x = 6 | y = 18 (actual result x = 6 | y = 14)
For x = x + 6. My expected result was x = 6 | y = 22 (actual result x = 60 | y = 10)
Thanks
For x = x + 0
outer = 0;
inner = 4;
x=x+0, x!=6;
x=x+3=3;
y=30-2=28;
inner = 3;
x=3;
y=28-2=26;
3 != 6;
x=3+3;
inner = 2;
x==6;
y=24;
break;
y=22;
outer=1;
inner=4;
x==6;
y=20;
break;
y=18;
outer=2;
inner=4;
x==6;
y=16;
break;
y=14;
for x=x+6
outer=0;
inner=4;
x=6;
y=28;
break;
y=26;
outer=1;
inner=4;
x=6+6+3;
y=24;
inner=3;
x=15+6+3;
y=22;
inner=2;
x=33;
y=20;
y=18;
outer=2;
inner=4;
x=42;
y=16;
inner=3;
x=51;
y=14;
inner=2;
x=60;
y=12;
y=10;
hope this would help.

Java: How to add variable in a loop together, multiple times

public class R {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int trials = Integer.parseInt(args[1]);
int x = 0;
int y = 0;
int j = 0;
int distance = 0;
while (trials>j) {
j = j + 1;
int i = -1;
double counter = 1.0 * distance;
double sum = (distance + counter);
while (i<=n) {
i = i + 1;
if (i == n) {
distance = ((x*x) + (y*y));
}
if (i<n) {
int random = (int )(Math.random() * 4 + 1);
if (random == 1) x = x + 1;
if (random == 2) y = y + 1;
if (random == 3) x = x - 1;
if (random == 4) y = y - 1;
}
}
}
double average= (sum)/(trials);
System.out.println("mean " + "squared " + "distance " + "= " + average);
}
}
Hey guys I'm wondering how it's possible to compute a value within a loop, and then every single time the loop finishes (and the value in computed) to average them together. I can't wrap my head around the concept and I tried doing it in the code above but I can't quite figure it out.
As you can see there are two while loops, and inside one of them a random value (distance) is computed. So essentially I need to average the distances together, but I can't imagine how it's possible to add the distances that are computed each time together into one number. Let's say the loop goes through one time and outputs a singular distance, how would I go about adding a new distance (for the new loop) together with the old one, and then keep doing that for each trial?
You just have to divide the total distance per trials.
public class R {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int trials = Integer.parseInt(args[1]);
int x = 0;
int y = 0;
int j = 0;
int distance = 0, distance_total = 0;
while (trials>j) {
j = j + 1;
int i = -1;
distance = 0;
while (i<=n) {
i = i + 1;
if (i == n) {
distance += ((x*x) + (y*y));
}
if (i<n) {
int random = (int )(Math.random() * 4 + 1);
if (random == 1) x = x + 1;
if (random == 2) y = y + 1;
if (random == 3) x = x - 1;
if (random == 4) y = y - 1;
}
}
distance_total += distance;
}
System.out.println(distance_total/j);
}
}

Breakpoint doesn't work

My Code :
class MixFor5 {
public static void main (String [] args) {
int x = 0;
int y = 30;
for (int outer = 0; outer < 3; outer++) {
for (int inner = 4; inner > 1; inner--) {
x = x + 3;
y = y - 2;
if (x == 6) {
break; // *Useless break;*
}
x = x + 3;
}
y = y - 2;
}
System.out.println(x + " " + y);
}
}
My output:
54 6
Can someone explain to me. Why when I remove break; my output data don't change at all.
You are never fulfilling the if(x==6)
lets take a look at the first loop:
int x = 0;
//....
x = x + 3; // x = 3;
if( x == 6 ) //false
break;
x = x + 3; // x = 6
now the second loop
x = x + 3 // x = 9
if( x == 6 ) //false x = 9
break;
x = x + 3; //x = 12
so you never are equal to 6 when comparing.

Java array out bounds can't find the index out of bounds

This is an eulers method program and i think i pretty much got it, but i keep getting arrayIndex out of bounds between my x and y arrays and its counter i, I think i know what arrayIndex out of bounds mean but i cant seem to get where the index went out of bounds. Can anyone help?
import java.util.Scanner;
import static java.lang.System.out;
import static java.lang.System.in;
public class INFINITE_EULER {
/**
* #param args
*/
public static float functionof(float b,float c){
return (float) ((float) Math.pow(b+0.1, 2)+ Math.pow(c+0.1, 2)); //to return the function of x and y
}
public static void main(String[] args) {
Scanner myScanner = new Scanner(in);
out.println("Programme to implement Eulers method");
float h;
float y[] = new float[100]; //initialize the value of x from 0 to 100
float x[] = new float[100]; // initialize the value of y from 0 to 100
int i; //variable i is the counter for the array
out.println("enter the value of h");
h = myScanner.nextFloat();
out.println("Enter the first and second interval");
x[0]=myScanner.nextFloat(); //take the value of x0
y[0]=myScanner.nextFloat(); //take the value of y0
for(i = 0 ; i < 100 ; i ++);{ // for x0 to x100
y[i+1] = y[i] + h * Math.abs(functionof(x[i],y[i])); //do yi+1 = yi + h * function of current x and current y through the loop
out.print("y");
out.print(i);
out.print("=");
out.print(y[i]);
}
}
}
Your problem:
in
y[i+1] = y[i] + h * Math.abs(functionof(x[i],y[i])); //do yi+1 = yi + h * function of current x and current y through the loop
when looping
for(i = 0 ; i < 100 ; i ++)
which means
for(i=0 to 99)
y[i+1] -- > when i=99, you will try to acess y[99+1] i.e, y[100] that doesn't exist
EDIT :
change your code to :
for(i = 1 ; i < 100 ; i ++){ // for x1 to x99
y[i] = y[i-1] + h * Math.abs(functionof(x[i-1],y[i-1]));
out.print("y");
out.print(i-1);
out.print("=");
out.print(y[i-1]);
}

Java creation of a spiral [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Looping in a spiral
I'm creating a program to populate a 3 by 3 matrix. I want to result in something looking like this
5 4 3
6 1 2
7 8 9
As you have probably noticed it is a spiral.
Now the algorithm I'm using is this: I have a 2-d array where the values represent the coordinates of the number. First I assign that every number coordinate in this array will have a value of 10. Then starting at 9 I decrease my x coordinate and assign the value of the coordinate to currentnum - 1 until it reaches the end or its value is not 10; Then I do the same thing except I increase the value of Y; Then decrease the value of x; Then of Y;
The reason I assign 10 to every number is so like it acts as a road for my program. Since current num will never exceed 9. If the value of a square is 10 it is like a green light. If it is not 10 meaning a value has been assigned to that square it breaks out of it.
Here is my code, please note it is written in Java
public class spiral {
/**
* #param args
*/
public static void main(String[] args) {
int spiral [] [] = new int[3][3];
for(int i = 0; i <= 2; i++){
for(int j = 0; j <= 2; j++){
spiral[i][j] = 10;
}
}
//0 is x value, 1 is y value
spiral[0][0] = 9;
int x = 1;
int y = 1;
int counter = 1;
int currentnum = 9;
int gridsquare = 3;
for(int i = 0; i <= 8; i++){
if(counter == 5){
counter = 1;
}
if(counter == 1){
System.out.println(x + " " + y);
for(int j = 0;j <= 1;j++){
if(spiral[x][y] == 10){
spiral[x][y] = currentnum;
currentnum--;
x += 1;
}
else{
y += 1;
break;
}
}
}
if(counter == 2){
for(int k = 0; k <= 0; k++){
System.out.print(x + " " + y);
if(spiral[x][y] == 10){
spiral[x][y] = currentnum;
currentnum--;
y += 1;
}
else{
x -= 1;
break;
}
}
}
if(counter == 3){
for(int z = 0; z <= 0; z++){
if(spiral[x][y] == 10){
spiral[x][y] = currentnum;
currentnum--;
x -= 1;
}
else{
y -= 1;
break;
}
}
}
if(counter == 4){
for(int b = 0; b <= 0; b++){
if(spiral[x][y] == 10){
spiral[x][y] = currentnum;
currentnum--;
y -= 1;
}
else{
x += 1;
break;
}
}
}
counter++;
}
System.out.print(currentnum);
}
}
I'm getting this error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at spiral.main(spiral.java:44)
Since I'm new to Java would someone please suggest a posible fix for this. Also if you see any problems with my algorithm please do inform me.
You do not need to pre-fill with 10: zero works just as well.
I think the best approach to solving the spiral is to think of how you do it manually: start in a corner, and go horizontally until you hit non-zero or an edge of the array. Then you turn right. Stop when the current number goes past N*N.
Now let's look at what each part of the algorithm means:
Starting in the corner means setting x=0 and y=0.
Going in a straight line means x=x+dx, y=y+dy, where either dx or dy is zero, and dy or dx is 1 or -1.
Turning right means assigning dx to dy and -dy to dx.
Here is how it looks in the code:
int current = 1;
// Start in the corner
int x = 0, y = 0, dx = 1, dy = 0;
while (current <= N*N) {
// Go in a straight line
spiral[x][y] = current++;
int nx = x + dx, ny = y + dy;
// When you hit the edge...
if (nx < 0 || nx == N || ny < 0 || ny == N || spiral[nx][ny] != 0) {
// ...turn right
int t = dy;
dy = dx;
dx = -t;
}
x += dx;
y += dy;
}
You've incremented x or y to 3 which is past the end of one of your arrays.
Step through your program with the debugger or add System.out.println statements before each if (counter) to find out where you're doing this.

Categories