Splitting into Quandrants - java

I am trying to split my grid into four quadrants, but I keep getting errors. Basically, I am trying to say that my x coordinates are between 0 and 50 and same with my y coordinates.
Then from there, I want to split the grid with my x and y coordinates. My second x in the for code is saying I need to create a method(int,boolean). And all my integers after the returns have errors as well. I am brand new to Java, so any help or insight would be awesome!
GridValueLayer habitat = new GridValueLayer("habitat", 0.00, true, 50, 50);
context.addValueLayer(habitat);{
for (int x = 0; x >= 0, x <= 50){
for (int y = 0; y >= 0; y <= 50){
if(x > 0 && y > 0)
return 1;
else if(x < 0 && y > 0)
return 2;
else if(x < 0 && y < 0)
return 3;
else if (x<0 && y >0)
return 4;
}
}

First of all, there is no purpose in having the for loop because once you do return 1;, or any return, the whole method stops. What you can do is simply output the results there instead of doing return, or populate an object in an arraylist with the x and y coordinates. In any case, remove the return and output instead and it should work.

I am not certain what the errors are (or exactly what you want the code to do), but I don't think the code you have there will compile. The for loop syntax is an initial condition, when to stop, and an increment separated by semi-colons. For the increment you gave a condition. You also left out a semi-colon and have an extra curly bracket by addValueLayer. I still don't think this code will do what you want it to but this is a place to try to work from.
GridValueLayer habitat = new GridValueLayer("habitat", 0.00, true, 50, 50);
context.addValueLayer(habitat);
for (int x = 0; x >= 0; x++) {
for (int y = 0; y >= 0; y++) {
if(x > 0 && y > 0)
return 1;
else if(x < 0 && y > 0)
return 2;
else if(x < 0 && y < 0)
return 3;
else if (x<0 && y >0)
return 4;
}
}

Related

Printing rectangle in java (there is hole in center)

I want create a rectangle which has hole in the middle. How can I import wallThick :) I define width, height, wallThick but I just write a rectangle. I can't press any hole. Could you help me please... Thanks for all...
if (width <= 0 || height <= 0 || wallThick <= 0)
{
System.out.println("Invalid value! Please enter positive integer.");
}else {
for ( y = 1; y <= height; y++)
{
for(x = 1; x <= width; x++)
{
System.out.print("*");
}
System.out.println();
}
what I want to do
Simplest solution: calculate the start and end "coordinates" of the hole. If you are within the hole coordinates, print a blank space.
int holeStartRow = wallThick + 1;
int holeStartCol = wallThick + 1;
int holeEndRow = height - wallThick;
int holeEndCol = width - wallThick;
Check if you're within the hole using:
if (y >= holeStartRow && y <= holeEndRow && x >=holeStartCol && x <= holeEndCol)
Sample code: here

How to solve for x when observing through a for loop?

Assume x is an integer and has been initialized to some value. Consider the code
for (int a = 1; a < 20; a++)
if (x < 0)
x = a;
Which statement will have the same effect on the value of x?
a.if (x < 0) x = 1;
b.if (x < 20) x = 19;
c.if (x < 0) x = 19;
d.if (x < 20) x = 20;
e.x = 1;
For this problem, I was a little confused on what the question was asking because the answer was not what I was expecting.
I chose answer choice b.if (x < 20) x = 19; because 19 is less than 20 and the other answer choices are false when plugging into 'x'. The answer to this question is actually a.if (x < 0) x = 1;. I am confused on why answer choice a. is the answer.
The answer is a. if x is less than 0, then the loop will assign 1 to x in the first iteration. The subsequent iterations will have no effect as x will be more than 0. if x is already more than 0, then both approaches will not alter the value of x.
Assuming x is not initialized, then on the first loop, x will only ever be assigned if it is less than 0. On the first loop a = 1 and so x will be assigned to 1, and never again after that, since 1 > 0.
Well if x has been initialized to 5 (or anything greater than 0) - then that statement would never be true. Ex:
int x = 3;
for (int a = 1; a < 20; a++)
if (x < 0)
x = a; // never reached
If x were initialized to anything less than 0 then that statement would evaluate only once and x would equal 1. Because on the second iteration:
if(1 < 0) is false so x would never get assigned again.
int x = -5;
for (int a = 1; a < 20; a++)
if (x < 0)
x = a; // reached once only - x == 1

Trying to get my for loop working

So at this point I have no idea why my for loop doesn't work. It's probably just an minor mistake but I cant find it so I'd appreciate your help. This is how it looks like:
int[] values = new int[array1.length];
for (int i = 0; i < array1.length; i++) {
for (int x = 0; x >= array1[i][0] && x <= array1[i][2]; x++) {
for (int y = 0; y >= array1[i][1] && y <= array1[i][3]; y++) {
values[i] += array2[x][y];
}
}
}
Numbers I'm using to test it on and some context:
Both arrays are 2D,
array1 looks like this:
(array1.length = 8) and array2 like this:
array1 holds coordinates of left-top and right-bottom corner of the area in array2 so for example 0 0 2 1 are coordinates for array2[0][0] and array2[2][1] and I'm trying to get my for loop to add all numbers in the area. So for this example it would add those four numbers:
My output is: 3 0 0 0 0 0 0 0
You should use explicit variable names not to do errors in meaning.
Moreover, start your loop at expected coordinates. So your loop can be written like this:
int[] values = new int[array1.length];
for (int i = 0; i < values.length; i++) {
int fromX = array1[i][0];
int fromY = array1[i][1];
int toX = array1[i][2];
int toY = array1[i][3];
for (int x = fromX; x <= toX; x++) {
for (int y = fromY; y <= toY; y++) {
values[i] += array2[x][y];
}
}
}
You can check the result:
System.out.println( Arrays.toString( values) );
// Output is [3, 0, 4, -2, 0, 1, -2, 3]
I think you should do this (add <= and >=) and change statring value. Also consider filling "values" with zeros since you're summing it up.
for (int x = array1[i][0]; x >= array1[i][0] && x =< array1[i][2]; x++) {
for (int y = array1[i][1]; y >= array1[i][1] && y =< array1[i][3]; y++) {
values[i] += array2[x][y];
}
}

Drawing a sqaure on an axis in Java

I'm currently trying to write a program that draws a sqaure.
The user will enter the size (in the length of the sides), the x-coordinate, and the y-coordinate of the bottom-left corner of the square on the grid as command-line parameters to program, in that order.
So an input of "run Question2Square 5 1 1"
draws a square of side length 5 whose bottom left corner is at the position (1, 1).
I've spent a few hours trying to just get the axes to show up correctly. I'm not even on the square yet.
My current code I have is this, but it's wrong:
import java.util.Scanner;
public class Question2square {
public static void main(String[] args) {
// Axis variables
int yAxismin = 0;
int yAxismax = 15;
int xAxismin = 0;
int xAxismax = 15;
//Loop through all coordinates on plane using for loops
for(int y = yAxismin; y >= yAxismin; y++)
{
for(int x = xAxismin; x >= xAxismin; x++)
{
//Draw the axis
if (Axis(x,y) != "") {
System.out.print(Axis (x,y));
}
}
System.out.println("");
}
}
// This method draws the 15x15 axis
public static String Axis(int x, int y)
{
// Each if and else if statement dictates what symbol needs to go where for the axes
// If there is nothing to be drawn, there will simply be a blank space
if (x == 15 && y== 0) return ">";
else if(x == 0 && y == 15) return "^";
else if (x == 0 && y == 0 )return ".";
else if(x == 0 && y >= 0) return "|";
else if(x >= 0 && y==0) return "-";
else return "";
}
}
All that does is run an infinite loop of '-' and I don't know exactly what's wrong.
Also, I need to figure out how I can extend the axes in either direction should an input that is greater than the 15x15 axes is entered.
If anyone could coach me through this, I would really appreciate it. I'm not asking for it to be done for me. I really want to figure this out but I'm a bit stuck right now and very frustrated.
Thanks in advance for any help!
Presumably you want to loop from yAxismin to yAxismax (and ditto for x)? Try
for(int y = yAxismin; y <= yAxismax; y++)
{
for(int x = xAxismin; x <= xAxismax; x++)
{
As you have it, the loop for(int y = yAxismin; y >= yAxismin; y++) will just go on for a very very long time. This as your expression says "start at yAxismin; loop while y is more than yAxismin; and on each iteration add one to y". You need it to stop when y reaches yAxismax.
Also, don't compare strings with == and !=. Use s1.equals(s2). You make this mistake on the line
if (Axis(x,y) != "") {
which should be
if (!Axis(x, y).equals("")) {
To extend the axes, just pass the limits to the Axis function:
public static String Axis(int x, int y, int maxX, int maxY)
{
if (x == maxX && y== 0) return ">";
else if(x == 0 && y == maxY) return "^";
else if (x == 0 && y == 0 )return ".";
else if(x == 0 && y >= 0) return "|";
else if(x >= 0 && y==0) return "-";
else return "";
}
...
// call it with
String drawThis = Axis(x, y, xAxismax, yAxismax);

Java minesweeper method to add numbers around bombs not working right

I wrote this method for my java minesweeper game, it is supposed to check spots surrounding a set of coordinates and then calculate how many bombs are near by.
public void numberMines(){
int count = 0;
int x = 0;
int y = 0;
int xMin = x-1;
int xMax = x+1;
int yMin = y-1;
int yMax = y+1;
if (x == 0){
xMin = 0;
}
if (y == 0){
yMin = 0; //these restrictions take care of the spots in the edges
}
if (x == rows){
xMax = rows;
}
if (y == columns){
yMax = columns;
}
//first 2 loops go through every spot on the board
for (x=0; x<rows; x++){
for (y=0; y<columns; y++){
//if the spot selected is not a bomb, for loops check spaces surrounding it
if (mineBoard[x][y] != bomb){
for (int i = xMin; i <=xMax; i++){
for (int j = yMin; j <=yMax; j++){
if (mineBoard[i][j] == bomb){
count++;
}
}
}
}
if (count > 0){ //converts them characters
mineBoard[x][y] = (char)(count + '0');
count = 0;
}
}
}
}
Every time I run this method it returns 3,2,1, or empty so it does count how many bombs are around, but for some reason it is over looping and returning the same thing for every spot that is not a bomb after the first one. I really cant see where I messed up, please help!
Move this block of code:
int xMin = x-1;
int xMax = x+1;
int yMin = y-1;
int yMax = y+1;
if (x == 0){
xMin = 0;
}
if (y == 0){
yMin = 0; //these restrictions take care of the spots in the edges
}
if (x == rows){
xMax = rows;
}
if (y == columns){
yMax = columns;
}
Inside of your for loops:
for (x=0; x<rows; x++){
for (y=0; y<columns; y++){
//Insert code here <---
Because at the moment, you're doing these calculations once, for x=0, y=0.
The code would probably also look cleaner if you moved the setting of count to 0 just before the i,j, loops, and not having it done once before all loops start, and again inside the conditional that displays the result.
Based on your comment - I think your valid indexes range from 0..(rows-1) and 0..(columns-1) - so you have a fencepost error also. Modify these lines:
if (x == rows-1){
xMax = rows-1;
}
if (y == columns-1){
yMax = columns-1;
}
But still have this entire block inside of your x/y loops. You don't get the out of bounds error when they're outside because you never calculate xMax and yMax when x and y are at their maximum values.
Avoid to declare all variables at the beginning of a method, better declare them close to when they are used. To fix your problem, you need to compute count, xMin, xMax, yMin and yMax in the loops like this:
public void numberMines(){
//first 2 loops go through every spot on the board
for (int x=0; x<rows; x++){
for (int y=0; y<columns; y++){
int count = 0;
//if the spot selected is not a bomb, for loops check spaces surrounding it
if (mineBoard[x][y] != bomb){
for (int i = (x == 0 ? 0 : x-1); i <= (x == rows ? rows : x+1); i++){
for (int j = (y == 0 ? 0 : y-1); j <= (y == rows ? rows : y+1); j++){
if (mineBoard[i][j] == bomb){
count++;
}
}
}
}
if (count > 0){ //converts them characters
mineBoard[x][y] = (char)(count + '0');
}
}
}
}
I have inlined the boundary checks, which is not necessary, but makes the code shorter to present here.

Categories

Resources