Printing a shape based on inputs from the user - java

I am trying to print a shape based on input; the shape is an "x". The inputs must be positive odd ints, and an arbitrary brush character. I have the code completed for the user input, but I need help with the code that actually prints the shape. Here is what I have so far:
public class TestProgram {
public static void main(String[] args) {
int height = 5;//Any positive odd int but 5 does not work correctly. Not sure what is going on.
char brush = '*';
for (int row = 0; row < height/2; row++) {
for (int i = row; i > 0; i--) {
System.out.print(" ");
}
System.out.print(brush);
for (int i = (height/2); i >= 2*row; i--) {
System.out.print(" ");
}
System.out.print(brush);
System.out.print("\n");
}
for (int row = 1; row < (height/2)+1; row++ ) {
System.out.print(" ");
}
System.out.print(brush);
System.out.print("\n");
for (int row = (height/2)-1; row >= 0; row--) {
for (int i = row; i > 0; i--) {
System.out.print(" ");
}
System.out.print(brush);
for (int i = (height/2); i >= 2*row; i--) {
System.out.print(" ");
}
System.out.print(brush);
System.out.print("\n");
}
for (int row = 1; row < (height/2)+1; row++ ) {
System.out.print(" ");
}
}
}

I would start with a routine to repeat your char n times. Something like
private static String repeat(char ch, int count) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++) {
sb.append(ch);
}
return sb.toString();
}
Then, I would prefer to build the String with a StringBuilder and the repeat routine. Like
int height = 5;
char brush = '*';
char space = ' ';
int half = height / 2;
StringBuilder sb = new StringBuilder();
for (int row = 0; row < half; row++) {
int cols = height - ((row + 1) / 2);
sb.append(repeat(space, row)).append(brush);
sb.append(repeat(space, cols)).append(brush);
sb.append(System.lineSeparator());
}
sb.append(repeat(space, half)).append(brush);
sb.append(System.lineSeparator());
for (int row = half - 1; row >= 0; row--) {
int cols = height - ((row + 1) / 2);
sb.append(repeat(space, row)).append(brush);
sb.append(repeat(space, cols)).append(brush);
sb.append(System.lineSeparator());
}
System.out.println(sb.toString());
Since you haven't learned about buffered io yet, that could also be expressed as
int height = 5;
char brush = '*';
char space = ' ';
int half = height / 2;
for (int row = 0; row < half; row++) {
int cols = height - ((row + 1) * 2);
System.out.print(repeat(space, row));
System.out.print(brush);
System.out.print(repeat(space, cols));
System.out.println(brush);
}
System.out.print(repeat(space, height / 2));
System.out.println(brush);
for (int row = (height / 2) - 1; row >= 0; row--) {
int cols = height - ((row + 1) * 2);
System.out.print(repeat(space, row));
System.out.print(brush);
System.out.print(repeat(space, cols));
System.out.println(brush);
}
And if you really don't know how to create a method,
int height = 5;
char brush = '*';
char space = ' ';
int half = height / 2;
for (int row = 0; row < half; row++) {
int cols = height - ((row + 1) * 2);
for (int t = 0; t < row; t++) {
System.out.print(space);
}
System.out.print(brush);
for (int t = 0; t < cols; t++) {
System.out.print(space);
}
System.out.println(brush);
}
for (int t = 0; t < height / 2; t++) {
System.out.print(space);
}
System.out.println(brush);
for (int row = (height / 2) - 1; row >= 0; row--) {
int cols = height - ((row + 1) * 2);
for (int t = 0; t < row; t++) {
System.out.print(space);
}
System.out.print(brush);
for (int t = 0; t < cols; t++) {
System.out.print(space);
}
System.out.println(brush);
}

Related

different output in conways game of life

I have to create a Java program for Conway's Game Of Life in procedural manner and I'm only one step away from finishing - all I have left to do is figure out how to make the output change itself(something like a GIF). Instead, my program outputs all the results one by one. I'm figuring it should be some kind of a loop, but I'm not sure. Here's the code:
import java.util.Random;
import java.util.Scanner;
class gameOfLife {
public static void main(String[] args) {
//User input
Scanner in = new Scanner(System.in);
System.out.println("How many rows?");
int rows = in.nextInt();
System.out.println("How many columns?");
int cols = in.nextInt();
//Declaring variables and grids
int[][] grid = new int[rows][cols];
int[][] nextGrid = new int[rows][cols];
int[][] temp = new int [rows][cols];
//Initializing first generation
initiateGrid(grid);
printGameBoard(grid);
//Looping through 10 generations
for (int x = 0; x < 20; x++) {
applyTheRules(grid, rows, cols, nextGrid);
temp = nextGrid;
nextGrid = grid;
grid = temp;
printGameBoard(grid);
}
}
//Initiating first generation grid randomly
static void initiateGrid(int[][] grid) {
Random r = new Random();
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
grid[i][j] = r.nextInt(2);
}
}
}
//Printing out the game board
static void printGameBoard(int[][] grid) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j] == 0)
System.out.print(" . ");
else
System.out.print(" ■ ");
}
System.out.println();
}
System.out.println();
}
//Applying the rules of the game
static void applyTheRules(int [][] grid, int rows, int cols, int [][] nextGrid) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int count = 0;
if(i-1>=0 && i+1<grid.length && j-1>=0 && j+1<grid[i].length) {
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
count += grid[i + x][j + y];
}
}
count -= grid[i][j];
} else{
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
count += 0;
}
}
}
//Alive cell becomes dead, if there are more than
//3 or less than 2 neighbouring cells
if((grid[i][j]==1)&&(count<2)||(count>3))
nextGrid[i][j]=0;
//Dead cell becomes alive if there are exactly 3 neighbouring cells
else if((grid[i][j]==0)&&(count==3))
nextGrid[i][j]=1;
//State stays the same
else
nextGrid[i][j]=grid[i][j];
}
}
}
}

Square board in Java - why is result different?

The first piece of code results in only one column and bunch of rows. The second piece of code results in 5x5 board. What is wrong with the first piece of code. It's probably something stupid and simple, but I couldn't find it.
int size = 5; // size of the board
for (int row = 1; row <= size; ++row) {
for (int col = 1; col <= size; ++col) {
System.out.println("# ");
}
System.out.println();
}
int height = 5;
int width = 5;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
System.out.print("# ");
}
System.out.println();
}
int size = 5;
for(int row=1;row<=size;++row)
{
for (int col = 1; col <= size; ++col)
{
System.out.print("# ");
}
System.out.println();
}
int height = 5;
int width = 5;
for(int i=0;i<height;i++)
{
for (int j = 0; j < width; j++)
{
System.out.print("# ");
}
System.out.println();
}

Magic Square Code (java)

I've been having an issue with my Magic Square code. It continually prints "This is a magic square" even when I'm positive it isn't.
You enter 16 integers, and then the code is supposed to run and determine whether the entered integers create a magic square (i.e. the sum of all rows, columns, and diagonals are all equal).
I can't figure out how to make it print false.
import java.util.*;
public class MagicSquare {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner (System.in);
int [] [] square = new int [4][4];
for (int row = 0; row < 4; row++)
{
for (int col = 0; col < 4; col++)
{
System.out.println("Input value for row " + (row+1) + " column " + (col+1));
square[row][col] = keyboard.nextInt();
}
}
int [] sumRow = new int [4];
int [] sumCol = new int [4];
int sum = 0;
for (int row = 0; row < 4; row ++)
{
for (int col = 0; col < 4; col ++)
{
sum = sum + square[row][col];
sumRow[row] = sum;
}
System.out.println("sum row " + row + "\n" + sumRow[row]);
sum = 0;
}
sum = 0;
for (int col = 0; col < 4; col ++)
{
for (int row = 0; row < 4; row ++)
{
sum = sum + square[row][col];
sumCol[col] = sum;
}
System.out.println("sum columns " + col + "\n" + sumCol[col]);
sum = 0;
}
int [] sumDiag = new int [4];
sum = 0;
for (int row = 0; row < 4; row++)
{
for (int col = 3; col > -1; col--)
{
sum = sum + square [row][col];
sumDiag[row] = sum;
}
System.out.println("sum diagonal " + row + "\n" + sumDiag[row]);
sum = 0;
}
int [] sumDiag2 = new int [4];
sum = 0;
for (int col = 0; col < 4; col ++)
{
for (int row = 3; row > -1; row --)
{
sum = sum + square[row][col];
sumDiag2[col] = sum;
}
System.out.println("sum diagonal 2 " + col + "\n" + sumDiag2[col]);
}
boolean bool = false;
int k = 0; int j = 1;
do
{
if (sumRow[k] == sumRow[j])
{
k = j;
j += 1;
bool = true;
}
else
{
bool = false;
System.out.println("Not a magic square");
break;
}
} while ((k < 4) && (j >- 1));
k = 0; j = 1;
do
{
if (sumCol[k] == sumRow[j])
{
k = j;
j += 1;
bool = true;
}
else
{
bool = false;
System.out.println("Not a magic square");
break;
}
} while ((k < 4) && (j >= -1));
String TorF = "";
if (bool = true)
{
TorF = "is";
}
else if (bool = false)
{
TorF = "is not";
}
System.out.println("This " + TorF + " a magic square.");
}
}
There is a number of issues in your code, as said in the comments.
First, you only need to verify the main and secondary diagonals.
Second, your code to compare the sums doesn't work for all cases and doesn't compare the diagonals.
Also, the if in the end is just wrong. Do this instead:
if (bool) {
} else {
}
And here is a solution:
int order = square.length;
int[] sumRow = new int[order];
int[] sumCol = new int[order];
int[] sumDiag = new int[2];
Arrays.fill(sumRow, 0);
Arrays.fill(sumCol, 0);
Arrays.fill(sumDiag, 0);
for (int row = 0; row < order; row++) {
for (int col = 0; col < order; col ++) {
sumRow[row] += square[row][col];
}
System.out.println("sum row " + row + "\n" + sumRow[row]);
}
for (int col = 0; col < order; col++) {
for (int row = 0; row < order; row ++) {
sumCol[col] += square[row][col];
}
System.out.println("sum columns " + col + "\n" + sumCol[col]);
}
for (int row = 0; row < order; row++) {
sumDiag[0] += square[row][row];
}
System.out.println("sum diagonal 0 " + "\n" + sumDiag[0]);
for(int row = 0; row < order; row++) {
sumDiag[1] += square[row][order - 1 - row];
}
System.out.println("sum diagonal 1 " + "\n" + sumDiag[1]);
boolean bool = true;
int sum = sumRow[0];
for (int i = 1; i < order; i++) {
bool = bool && (sum == sumRow[i]);
}
for (int i = 0; i < order; i++) {
bool = bool && (sum == sumCol[i]);
}
for (int i = 0; i < 2; i++) {
bool = bool && (sum == sumDiag[i]);
}
String tOrF = "";
if (bool) {
tOrF = "is";
} else {
tOrF = "is not";
}
System.out.println("This " + tOrF + " a magic square.");
Furthermore, there are some things you could do to optimize this code.

Trouble printing chars thru a 2 dim java array

I tried to run this code as a score table where I have chars and ints as below for heading;
A B c
1
2 65 //this is where I'm stuck again!
3
In order to print the score like (65) above in a particular place (matrix) but as soon as I try to add the print statements the table falls apart. Any help would be appreciated;
public class Table3 {
static int[][] list = new int[4][4];
//private char column = 'A';
//private int row = 1;
private static int row = 1;
public Table3(){
//column = 'A';
for (int i = 0; i < 4; i++) {
for (int j = 1; j < 4; j++)
list[i][j] = 0;
}
}
public static void table(char col, int row, int value) {
//System.out.printf("\n\n%s\n", "Table");
for (int i = 1; i < 4; i++) {
System.out.print(row + " ");
row++;
for (int j = 1; j < 4; j++)System.out.print(col + " ");
System.out.println("\n");
col++;
if (row >= 0 && row <= 4 && col >=0 && col <= 4)
System.out.print(list[col][row]=value);
System.out.println("\n");
}
}
}
Client
public class TableTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Table3 t = new Table3();
t.table('A', 5, 5);
}
}
Learn how to use System.out.printf. The docs are here.
public class Table3
{
static int numRows = 4;
static int numCols = 4;
static int[][] list = new int[numRows][numCols];
public Table3()
{
//column = 'A';
for (int i = 0; i < 4; i++)
{
//this is the row number so you don't have to print it manually
//just print the array
list[i][0] = i;
//initialize the list to 0
for (int j = 1; j < 4; j++)
{
list[i][j] = 0;
}
}
}
public static void table(char col, int row, int value)
{
list[row][col] = value;
int columnWidth = 5; //in characters
//empty space before first column header
for (int i = 0; i < columnWidth; i++)
{
System.out.print(" ");
}
//print the column headers (A through C)
for (int i = 1; i < numCols; i++)
{
System.out.printf("%-" + columnWidth" + "c", (char)(64 + i));
}
System.out.println(); //get off of the column header row
//print the rest of the table
for (int i = 1; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
if (list[i][j] == 0)
{
System.out.printf("%" + columnWidth + "s", " ");
}
else
{
System.out.printf("%-" + columnWidth + "d", list[i][j]);
}
}
System.out.println("\n");
}
}
}

How to perform despeckle operation without using JAI,ImageJ,jhlab libraries?

I am making an app in Netbeans using Java Swing. I want to achieve some image processing functionality (like in ImageJ) in my app without using the ImageJ, JAI and jhlab libraries.
For example: ImageJ>>Process>>Noise>>Despeckle.
So, how can I do this?
This might helpful
private void removeNoise() {
int iterator;
for (int row = 0; row < x1; row++) {
for (int column = 0; column < y1; column++) {
if (row == 0 || row == x1 - 1 || column == 0
|| column == y1 - 1) {
result[row][column] = result[row][column];
continue;
} else {
iterator = 0;
for (int r = row - 1; r < row + 2; r++) {
for (int c = column - 1; c < column + 2; c++) {
surround[iterator] = result[r][c];
iterator++;
}
}
result[row][column] =
sortMedian(surround, 9); //calls the sorting method
}
}
}
setPixel();
}
private int sortMedian(int[] surround1, int x) {
int i, j, t = 0;
for (i = 0; i < 9; i++) {
for (j = 1; j < (9 - i); j++) {
if (surround1[j - 1] > surround1[j]) {
t = surround1[j - 1];
surround1[j - 1] = surround1[j];
surround1[j] = t;
}
}
}
return surround1[4];
}
private void setPixel() {
int[] pixel = new int[1];
for (int x = 0; x < bi.getWidth(); x++) {
for (int y = 0; y < bi.getHeight(); y++) {
pixel[0] = (int) result[x][y];
rnImage.getRaster().setPixel(x, y, pixel);
}
}
}

Categories