public class MakeQuilt {
public static void main (String[] args){
char [][] myBlock = new char [4][5];
char [][] myQuilt = new char [12][4];
for(int row = 0; row < myBlock.length; row++){
for(int column = 0; column < myBlock[row].length; column++){
if(column == 0 || row == 3)
myBlock[row][column]='X';
else if(row == column || (row == 2 && column == 1))
myBlock[row][column]='+';
else
myBlock[row][column]='.';
}
}
displayPattern(myBlock);
displayPattern(myQuilt);
}
public static void displayPattern(char[][] myBlock){
for(int row = 0; row < myBlock.length; row++){
for(int column = 0; column < myBlock[row].length; column++){
System.out.print(myBlock[row][column]);
}
System.out.println();
}
System.out.println();
}
public static void fillQuilt(char[][] myQuilt){
for(int row = 0; row < myQuilt.length; row++){
for(int column = 0; column < myQuilt[row].length; column++){
myQuilt[row][column] =('?');
}
}
}
}
Can't seem to figure out why my char array myQuilt won't fill with question marks but instead is filled with nothing? (output shows a bunch of 0's). Not sure how to change the displayPattern method to output ?'s in the myQuilt array.
Before calling displayPattern, you have to fill quilt somewhere. i.e.
displayPattern(myBlock);
fillQuilt(myQuilt);
displayPattern(myQuilt);
Question: You define the fillQuilt(...) method where you would fill an array with question mark characters, but where do you ever call this method?
Answer: You don't (at least you don't show it), and if it's never called, it will never do its thing. The solution is to call the fillQuilt method, passing in myQuilt where you need it to do its actions: fillQuilt(myQuilt);. Understand that programming takes things very literally: they only do what you explicitly program them to do, nothing less, nothing more.
I can't see any call to your fillQuilt() method in your main.
Don't you have to call fillQuilt() somewhere before printing?
Related
I am new to java and I am struggling immensely! I've written the following code but keep getting errors. All I am trying to do at the moment is fill a 5x5 matrix with the letter A. Here's what I have so far, I am not sure if I need to post the errors as well? Any help would be really greatly appreciated.
public class Encryption {
private String Unencoded, FiveLetterKeyword, EncryptedMessage;
//constructor method
public Encryption(String U, String F, String E)
{
Unencoded = U;
FiveLetterKeyword = F;
EncryptedMessage = E;
}
//create 2D string array 5 by 5
String Encrypt [][] = new String[5][5];
//create string filled with all letters of the alphabet
String String = new String
("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z");
//method for loop to print out the matrix
public static void matrix()
//for loop to create matrix rows and columns
{
for (int row = 1; row < Encrypt.length; row++)
{
for (int column = 1; column < Encrypt[row].length; column++)
System.out.print(Encrypt[row][column] + " ");
}
}
//filling the array with the letter A
public char Encrypt(char Encrypt[][])
{
//char[] alpha = alphabets.toCharArray;
//declaring variable to fill array with A
char aChar = "A";
for (int row = 1; row < Encrypt.length; row++)
{
for (int column = 1; column < Encrypt.length; column++)
return Encrypt;
}
}
}
Arrays in Java are zero-based, which means they start at index zero, and range until index array.length-1.
Your code starts the row and column at 1—which means you're skipping the initialization of row/column 0. That's probably where at least some of the problems are coming from, since you're using your 5x5 array (rows/columns 0,1,2,3,4) as a 4x4 array (rows/columns 1,2,3,4).
There's also the fact that your Encrypt method doesn't actually make any assignments to the array. You probably want to initialize it like this:
// NOTE: changed return type to void -- this is a side-effect-only method!
public void Encrypt(char Encrypt[][])
{
// NOTE: use single-quotes for chars. double-quotes are for strings.
char aChar = 'A';
// NOTE: changed the starting loop values from `1` to `0`
for (int row = 0; row < Encrypt.length; row++)
{
// NOTE: technically Encrypt.length works here since it's a square
// 2D array, but you should really loop until Encrypt[row].length
for (int column = 0; column < Encrypt[row].length; column++)
{
// NOTE: set each entry to the desired char value
Encrypt[row][column] = aChar;
}
}
}
There are several issues with your original code. Look at the NOTE entries in the comments for individual explanations.
You are missing the most crucial part of what you are trying to accomplish.
Where are you setting your matrix to the letter A?
Change your Encrypt function to the following:
//filling the array with the letter A
public void Encrypt(char arr[][])
{
//char[] alpha = alphabets.toCharArray;
//declaring variable to fill array with A
char aChar = 'A';
for (int row = 0; row < arr.length; row++)
{
for (int column = 0; column < arr[row].length; column++)
{
arr[row][column] = aChar;
}
}
}
I'm trying to test each row in my array to see if they are even or odd. If the row is even i want to change the random value in the elements of the row to 0. If the row is odd i want to change the elements to 1. Ive been able to create the element and print it out but im stuck on how to test the rows. I know to test if a number is even you use (i % 2 == 0) but im not sure what coding i should use.
public static void main(String[] args) {
int[][] box;
box = new int[2][2];
int row;
int column;
for (row = 0; row < box.length; row++) {
for (column = 0; column < box[row].length; column++) {
box[row][column] = (int) (Math.random() * 100);
}
}
//where im having issues
// i get error 'bad operand types for binary operator'
if (box[row] % 2 == 0) {
for (row = 0; row< box.length;row++){
box[row][column] = [0][];
}
}
else{
for(row = 0; row < box.length; row++){
box[row][column] = [1][];
}
}
for (row = 0; row < box.length; row++) {
for (column = 0; column < box[row].length; column++) {
System.out.print(box[row][column] + " ");
}
System.out.println();
}
}
}
you can use Arrays built in function fill(int[],value) that takes two argument, First 1d array and second a value to fill
//also if you are checking is row is even or odd divide row not box[row]
if (row % 2 == 0) {
Arrays.fill(box[row],0);//set 0 to every element in this wor
}
else{
Arrays.fill(box[row],1);//set 1 to every element in this wor
}
Say I have a program that creates a 4x8 board. Each cell in the board is either a colored object or an emptycell object. How do I find which row in my board is empty and nearest to row 0?
My attempt:
public int emptyRow() {
int emptyCells = 0;
int emptyRow;
for (int i = 0; i < getMaxRows(); i++){
for (int j = 0; j < getMaxCols(); j++){
if (getBoardCell(i,j).equals(BoardCell.EMPTY)){
emptyCells++;
if (emptyCells == getMaxCols()){
return i;
}
}
}
}
But I realised that this will count all the cells that are empty and I only want 8 empty cells in one row.
You will first need to create a variable for the inner for loop to count the number of items in that particular row, so you can then determine if it is empty or not. If you start at row 0, then the first row you find will be your closest row. Something like this.
int[][] myBoard = {{1,1,1},{0,0,0},{1,1,1}};
for(int i = 0; i < myBoard.length; i++) {
int count = 0;
//Loop through the columns of the row
for(int j = 0; j < myBoard[i].length; j++) {
//Check to see if the column for this row is empty if it is add
//to our empty cell count
if(myBoard[i][j] == 0) count ++;
}
//If our count is equal to the amount of columns in a row we return
//the row index.
if(count == myBoard[i].length()) return i;
}
I'm working on a game class project and got stuck.
Ive been looking everywhere on how to check if any column in a 2 dimensional Array is full and if so, the column be completely cleared.
I'm really new to Java so if you can please help me out with this!
This is the code I have so far.
//removes filled columns - added method
private boolean removeFullCol()
{
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[i].length; j++){
if(board[i][j] != occupied){
return false;
}
}
}
//empty square was never found - column is full
return true;
}
If you want to remove only full cols, check by cols, but you cant return a boolean if you want to check ALL columns. You have 2 options:
check a given column (return a boolean)
return first column full (return an int)
First option:
private boolean removeFullCol(int col)
for (int row=0; row<board[col].length; row++) {
// check if all are filled not sure which object is inside board...
if(board[row][col] == null){
return false;
}
}
return true;
}
Second option (returns first col full or data, -1 otherwise):
private int removeFullCol()
for (int row=0; row<board[col].length; row++) {
int filled = 0;
for (int row=0; row<board[col].length; row++) {
// check if all are filled not sure which object is inside board...
if(board[row][col] != null){
filled ++;
}
}
// when finished check how many rows are filled
if (board[col].length == filled)
return row;
}
return -1;
}
I wrote the code on the fly... Let me know if any errors or doubts...
i am trying some of the practice New Boston videos and can't get this to work! I am very new to java, this is my first class. I have an assignment to do a 2d array and I can't figure out how to get it to display on the screen. This practice is from Thenewboston's Java tutorials, video # 34 and it works for him!
public class inventory {
public static void main (String[] args) {
int firstarray[][]= {{8,9,10,11},{12,13,14,15}};
int secondarray[][]={{30,31,32,33},{43},{4,5,6}};
System.out.println("This is the first array");
display(firstarray);
System.out.println("This is the second array");
display(secondarray);
}
public static void display (int x[][])
{
for(int row=0;row<x.length;row++)
{
for(int column=0;column<x.length;column++);
System.out.print(x[row][column]+"\t");
}
{
System.out.println();
}
}
}
You've put a ; after the for loop, negating what you thought was its body. Get rid of
for(int column=0;column<x.length;column++); // <--- this ;
In this situation, the body of the for loop, where column variable is declared and has scope, is everything after the ) and before the ;. In other words, nothing. You actually need to replace the ; with a {.
Correct indentation would go a long way in helping you write syntactically correct code.
You have semicolon at the end of for cycle and not good formating. There are also two brackets, which are absolutely useless :). The right code can look like this :
public static void display(int x[][]) {
for (int row = 0; row < x.length; row++) {
for (int column = 0; column < x.length; column++) {
System.out.print(x[row][column] + "\t");
}
System.out.println();
}
}
Still the display function is not correct, it fails at the end, because the difference of length of rows and columns.
If you want to make this functional, at the second for cycle, you should consider the length of the actual ROW, not how many rows (which is x.length).
You only need change column < x.length to column < x[row].length
So the working code is this :
public static void display(int x[][]) {
for (int row = 0; row < x.length; row++) {
for (int column = 0; column < x[row].length; column++) {
System.out.print(x[row][column] + "\t");
}
System.out.println();
}
}