How To Swap Matrix From Index
Jan 2, 2015
so I have this matrix
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
If the user for example enter the number 2 for index I need to swap the matrix from this index two lines with two line so this is the result what I need
1 2 5 6
3 4 7 8
9 10 13 14
11 12 15 16
View Replies
ADVERTISEMENT
Dec 7, 2014
I am attempting to get the x and y coordinate of a matrix of buttons. I have googled my way to failure and read the docs and I think I am traveling of track.
Below is where I create a matrix of buttons via Swing
public class View extends Mybuttons{
private static final long serialVersionUID = 1L;
JFrame frame;
JPanel matrixPanel, optionsPanel;
Mybuttons[][] matrixBtn;
Later in this class:
JPanel matrixPan(){
matrixBtn = new Mybuttons[25][25];
JPanel panel = new JPanel();
panel.setSize(550,550);
panel.setLayout(new GridLayout(25,25));
//creating a 25x25 matrix of buttons
[Code]...
In the controller class I am trying to get the index of each button in the getUnvisitedChildNode method. I need to do this so I can search the buttons around the button passed to it and check if they are been visited yet. Below getUnvisitedChildNode you will be bfs (breadth first search).
private Mybuttons getUnvisitedChildNode(Mybuttons b){
//example of some of the things I have tried
int x= Mybuttons.getComponentAt(b);
int y= Mybuttons.indexOf(b);
int j=0;
return b;
}
[Code]...
View Replies
View Related
Dec 5, 2014
java program that will determine if the matrix is a lower or upper triangular matrix. I only need to use java.util.Scanner;.
View Replies
View Related
Jul 8, 2014
i want to swap the word. how could i be able to swap..
for example:-
I/P:-
i want to be a java developer
O/P:-
developer a be to java want
means swap first and last word and all alternate word.
View Replies
View Related
Nov 13, 2014
So as of right now, I'm trying to swap my bar arrays and repaint it. For now, I'm using the index 5 and swapping it with index 23. When I click the shuffle button, It swaps, but the highest bar still remains the same.
Here's my code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class AssortedAssortmentsOfSorts extends JFrame
[Code] ......
View Replies
View Related
Apr 24, 2014
Im trying to get the user to input the rows one by one but my input stops after the first. the examples i looked at in my book had this way i used as well. i are my rows and j are my columns. What is the correct way to do it?
import java.util.Scanner;
public class Exercise08_01 {
public static void main(String[] args) {
[Code]....
View Replies
View Related
Aug 1, 2014
Ok I am trying to at this point read in a matrix from a txt file in the format with order of the matrix leading the matrix. Like this :
1
5
2
2 3
5 9
3
3 -2 4
-1 5 2
-3 6 4
4
2 4 5 6
0 3 6 9
0 0 9 8
0 0 0 5
4
2 4 5 6
0 0 0 0
0 0 9 8
0 0 0 5
My code then is supposed to read it in and store it as an Array of singly linked lists. I am having trouble with my code, I am only getting outputs where it is only storing the first line of the txt matrixs like so:
5.0
2.0 3.0
2.0 3.0
3.0 -2.0 4.0
3.0 -2.0 4.0
3.0 -2.0 4.0
2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0
Is there something obvious in my code that is making it only save the first lines and how can i fix it.
//import java tools
import java.io.*;
import java.util.Scanner;
//Test.java
//See readMe.txt file for implementation instructions
//input: "input.txt"
[code]...
View Replies
View Related
Feb 28, 2014
why my code is not printing the matrix and sum?
The Java-program Matrix below first asks the user for the number of rows and columns in a matrix. After this the program asks for the values of the elements. Finally, program prints the elements of the matrix and their sum on screen. Your task is to create the missing methods. Check the example print to see how to modify the print. When printing the matrix, values on the same row are separated using tabulator.
Program to complete:
import java.util.Scanner;
public class Matrix {
public static void main(String[] args) {
int rows, columns;
Scanner reader = new Scanner(System.in);
System.out.print("Type in the number of rows: ");
[code]...
Write the missing methods here / Methods are written in the text box below.
Example output
Type in the number of rows: 3
Type in the number of columns: 4
Type in the element 1 of the row 1: 1
Type in the element 2 of the row 1: 2
Type in the element 3 of the row 1: 3
Type in the element 4 of the row 1: 4
Type in the element 1 of the row 2: 5
[code]...
Matrix:
1 2 3 4
5 6 7 8
9 10 11 12
Sum of the elements of the matrix: 78
my code
import java.util.Scanner;
public class apples {
public static void main(String[] args) {
int rows, columns;
Scanner reader = new Scanner(System.in);
System.out.print("Type in the number of rows: ");
[code]...
View Replies
View Related
Nov 16, 2014
I am making a Sudoku game and creating a matrix of JTextFields. However I am getting the following errors
Exception in thread "main" java.lang.NullPointerException
at SudokuView.board(SudokuView.java:30)
at SudokuView.<init>(SudokuView.java:18)
at SudokuMain.main(SudokuMain.java:5)
I know the problem is with this code
box[i][j] = new JTextField();
panel.add(box[i][j]);
I know this because when I do this:
panel.add(new JTextField());
It works. However it puzzles me why it is not working.
Whole Code:
import java.awt.GridLayout;
import javax.swing.*;
public class SudokuView {
JFrame frame;
JPanel panelBoard;
JTextField[][] box;
int row=10; int col=10;
SudokuView(){
frame = new JFrame("Play Sudoku GOOD LUCK");
[code]....
View Replies
View Related
Nov 21, 2014
I have a programming assignment in which I have to make a program which loads a crossword from a Properties file and then, when the user press a button, the program checks if the letters the user has typed in the interface are the same as in the correct matrix. If all the letters from a word are correct, the program must paint every letter of the word green.
I'm using two matrix, one that is called "mundo" (I 'm from Latin America) , which has the correct letter for each box. The other one is a JtextField matrix which ='ve created using a Gridllayout. I called this one crucigrama (crossword in Spanish)
This is the code I wrote to validate: So you can understand, here is a translation:
numero = number
fila = row
column = columna
bien - a boolean I used to check if the letter I've checked before are the same as the correct ones
.darLetra() - returns a string with the correct letter
[Code] .....
View Replies
View Related
Mar 8, 2014
I have string abcdef.I need to convert that string into 2 dimensional matrix.For each 2 characters in the string will be a matrix.For instance:
String: abcdef
Matrix will be: [a b],[c d],[e f]
How to do that in java?
View Replies
View Related
May 10, 2015
I have to create a square matrix that has a min and max value as well as a size value which is given a integer value in the main method. The matrix has to be filled with random values. Also I have to add that matrix to another one in an addMatrix method and I have to subtract both in a subMatrix method. These are the requirements:
Methods:
Constructor() - receives the row and col size for myMatrix and a max and min values for range of random fill values for the matrix.
RandFill() - fills matrices with random numbers
addMatrix() - receives a matrix object. adds its myMatrix with the received object's myMatrix. The result is placed in this object's myResultMatrix.
subMatrix() - subtracts both matrices
I typed up this code but I'm not sure about some parts of it and I would creating min and max values in the Random method and in the main printing the separate 2 matrices and adding and subtracting them:
import java.util.*;
public class SquareMatrix2 {
public int size;
public int myMatrix [][];
public int myResultMatrix;
public int [][] ResultStatus;
[Code] ....
View Replies
View Related
Oct 11, 2014
i want solution to my program
View Replies
View Related
Apr 16, 2015
I have a 5x5 array of which I read from an external file and I am supposed to print out the original matrix and then add up each row and column, proceeding to store them into the sixth positions in my matrix. After that I print out the new matrix.
For example this just using a 2 by 2,
Original
1 2 3 4 5
1 2 3 4 5
New 3 by 3
1 2 3 4 5 15
1 2 3 4 5 15
2 4 6 8 10 30
I am confused as to how to isolate rows and print them out. I know how to add the entire matrix up but isolation is my issue.
Here is what I have including how to read the matrix and the sum of the whole thing
import java.io.*;
import java.util.*;
public class prog470cAddingRandC {
public static void main (String [] args) {
//read the file
Scanner inFile=null;
[code].....
View Replies
View Related
Feb 18, 2015
I have some N*M matrix or N*N matrx , and there's a "worm" that can start from any index in the first column, or, any index in the first row. i need to choose the longest worm that satisfying this :
The index that comes after the index before him must be greater then 1. and i must use recursion, also for helper methods. no loops at all. that's it. i'm having a problem to find the longest worm in the matrix.
My idea was to create an helper array and assign to the array indexes a variable that will count the steps of the worm and finally assigns in to the helper array index. then i used findMax method to find the max value in an index. and thats will be the longest worm. i wrote a lot of code so i wont put it here. i will say that i'm close. if the longest worm is 6 i get in my output 7.
View Replies
View Related
May 2, 2014
I'm trying to get my matrix1 and matrix2 to add up to create a third matrix, I've gotten that down, but now I'm just having some trouble getting it to print out the matrix1, matrix2 and then the final matrix3 or the result matrix?
import java.util.Scanner;
public class Exercise_7_5{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter Matrix 1: ");
double[][] matrix1 = new double [3][3];
[code]....
View Replies
View Related
Dec 22, 2014
I am trying to print out a rubix cube nicely so that it looks something along the lines of this:
** ** ** R1 R2 R3 ** ** **
** ** ** R4 R5 R6 ** ** **
** ** ** R7 R8 R9 ** ** **
B1 B2 B3 Y1 Y2 Y3 G1 G2 G3
B4 B5 B6 Y4 Y5 Y6 G4 G5 G6
B7 B8 B9 Y7 Y8 Y9 G7 G8 G9
** ** ** O1 O2 O3 ** ** **
** ** ** O4 O5 O6 ** ** **
** ** ** O7 O8 O9 ** ** **
** ** ** W1 W2 W3 ** ** **
** ** ** W4 W5 W6 ** ** **
** ** ** W7 W8 W9 ** ** **
however, when I try to print out the entire cube, I get this:
** ** **
** ** **
** ** **
R1 R2 R3
R4 R5 R6
R7 R8 R9
[code]....
I have found out the area that causes this, my toString, but I want to know if there is something built in or not that will allow me to print it such that I can continue printing it at the same row level as its neighbor, even though I create a new line for each individual column? Here is the code that I try to print out the entire cube:
void printEntireCube(){
for(int row = 0; row < 4; row++){
for(int col = 0; col < 3; col++){
System.out.print(cube[col][row] + " ");
}
System.out.println();
}
}
void createCube(){
[code]....
View Replies
View Related
Apr 3, 2015
Code in java in which if i want to list particular row in matrix it should be displayed using for loop.
For example in 3 X 3 matrix:
1 2 3
1 1 1
1 0 1
If i want to see only 1st row i.e. 1 2 3 then how to do i do this ?
View Replies
View Related
Nov 4, 2014
Code for sparse matrix addition and Multiplication...
View Replies
View Related
Apr 30, 2015
I'm working on making a game in JAVA. To be brief, the game is on a 6x6 matrix called "blocks[][]". The game has two players and the objective is to capture the enemies pieces by moving your piece onto there's. The players do this by moving one piece per turn either forward/backward (Vertically) or right/left (horizontal) movements. Essentially checkers without jumps.I'm not very far into the development process here. So far I have the game board created and the pieces load by clicking the "Start" button (handled by the refresh function). However I'm having a few problems... Namely moving the pieces effectively at the moment.
Right now there's a lot to do:
(1) - Implement a turn system,
(2) - Only allow a player to click their pieces (I've a validation function already in place to only permit valid moves [horizontal/vertical only]),
(3) - getting the pieces to actually "move" what I mean by this is a player selects the piece they want to move, then they select the spot they want to move to and the icon I have placed on the button is supposed to be added to the new square and the icon for its previous position is to be made null.
Please see the "actionPerformed" section to see exactly what I'm trying to do and hopefully as well see what I'm missing as to why it is not working at the moment... Also see under the Refresh function where I commented out the for loops to add the pieces. I did this because it was resulting in an ArrayOutofBounds error so I just wrote each spot out manually where I wanted them to start.
/* Ghosts *
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.border.*;
import java.io.*;
[code].....
View Replies
View Related
Apr 29, 2014
Write the Java code of the method Transpose2D that takes one 2-dimentional array (matrix) A and returns one 2-dimentional array B that is transpose of matrix A. (assume that all the rows are of the same length)
public int[][] transpose (int[][] array) {
if (array == null || array.length == 0)//empty or unset array, nothing do to here
return array;
int width = array.length;
int height = array[0].length;
[Code] ....
View Replies
View Related
Mar 9, 2014
Assignment: Develop a GUI-based program to read the entries of a matrix from a text file. The first number is the number of rows; the second number is the number of columns. The remaining numbers are integers between 1 and 9 in row by row order. Scan the matrix, highlight (display the entries in different color) all cells that form a group of five cells with the same value horizontally, vertically or diagonally.
Example of text(.txt) file:
7 7
3623161
3666669
3936293
3594955
3289867
2493998
1399998
Example of output:
3623161
3666669
3936293
3594955
3289867
2493998
1399998
* ^^this would be what it would look like when displayed in the GUI. But it would be colored. Anything that has 5 in a row ...My Class File:
// GUI-related imports
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Scanner;
// File-related imports
[code]....
EX.
0=black(when there is no combo. of 5)1=blue(horizantal combo of 5)2=red(vertical combo of 5)3=green(right diagnol combo of 5)4=purple(left diagol combo of 5)
View Replies
View Related
Jan 12, 2015
I am making a program that reads a set of numbers (e.g.: 8 1 6 3 5 7 9 4 2) and prints them into a matrix.For example:
8 1 6 3 5 7 9 4 2, will produce:
8 1 6
3 5 7
9 4 2
When taking in these numbers from the user, I am not allowed to let the user state what size the matrix is (e.g.;3 x 3). Instead the program needs to determine that itself. I have written the code below to count the number of numbers inputted, but now I am stuck as to how to get them into the array. I have what is suppose to be my array written, but it is not function yet.
import java.util.*;
public class MagicSquare {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
[code]....
View Replies
View Related
Sep 18, 2014
I am able to perform column operation but not able to perform row operation because i am not able to store data say a 2 matrix [][]. I need to store the data into a 2-D matrix. Here is my code:
Java Code:
import java.awt.List;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class colRowRead {
[Code] ....
I tried something like that:
for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++){
matrix [i][j]=textFile.get(i).split(" ");
//System.out.println(matrix[i][j]);
}
}
*/ mh_sh_highlight_all('java');
File col.txt is like this:
Java Code:
5 9 7 1 5
3 6 8 6 8
4 6 7 8 9
9 8 3 5 7 mh_sh_highlight_all('java');
View Replies
View Related
Mar 19, 2015
I am continuing on in trying to build up the basics of matrix algebra from scratch.
I have created an object class called Matrix
import java.util.ArrayList;
public class Matrix {
public int NumRows;
public int NumColumns;
// This array contains the entries of our matrix.
ArrayList<Double> entry = new ArrayList<Double>();
[Code] ......
Bottom line: a matrix has a number of rows and a number of columns, and for each pair of row and column, we have a number in our matrix. The DisplayMatrix method prints my matrix to the screen, and the GetEntry method returns a particular value. This all works fine and dandy as far as I can tell.
A fundamental operation done to matrices to obtain a special matrix called the RREF is the process of switching 2 rows. Here is a method I have written that switches two rows of a matrix, and outputs the result as a new matrix. Note that I want the original matrix to be unchanged.
// Switch two rows
public static Matrix SwapRows(Matrix A, int r1, int r2){
if(r1<1 || r1>A.NumRows || r2<1 || r2>A.NumRows)
PRINTLN("illegally switching rows");
Matrix C = A;
double dummy[] = new double[A.NumColumns];
[Code] ....
How I call this, inside a public static void main(String[] args), is as follows:
// Declares that A is a 2 by 2 matrix.
Matrix A = new Matrix(2,2);
// We now add values in. The top left entry of A is 4, the top right entry of A is 1, the bottom left entry of A is 2, and the bottom right entry of A is 6.
double pony[]= new double[4];
pony[0]=4;
pony[1]=1;
pony[2]=2;
pony[3]=6;
A.AddEntries(pony);
// We can display the matrix in the output, and it looks exactly as expected!
A.DisplayMatrix();
// I am now going to create a new matrix called B. It is going to be obtained by flipping the first and second rows of A.
//Note that I want A is stay as I initialized it.
//I dont want A to have it's 2 rows switched. I want B to the matrix obtained by switching two rows of A.
Matrix B=SwapRows(A,1,2);
B.DisplayMatrix();
// Displaying B gives me the desired result. However, if I now display A again, it is the same as B.
A.DisplayMatrix();
Why is my matrix A being modified? Of course, I am more than capable of providing more details/comments if requested. I suspect that this is a super silly mistake.
View Replies
View Related
Apr 22, 2015
I've created several methods in another class to perform certain matrix calculations. Do I need to have mutator method in this calculation class to define the column length of the matrix object? I understand why the following code is not working, but I was hoping to get a quick and easy workaround. I'm not sure how to initialize the size of the column for newMatrix within the method.
public int [][] sum(int matrixOne[][], int matrixTwo[][]) {
int sumMatrix [][] = new int[matrixOne.length]["WHAT TO PUT HERE"];
for (int i = 0; i < matrixOne.length; i++) {
for (int j = 0; j < matrixOne[i].length; j++) {
sumMatrix[i][j] = matrixOne[i][j] + matrixTwo[i][j];
System.out.println("matrixSum" + sumMatrix[i][j]);
}
}
return sumMatrix;
}
Then I will use this method to print the new matrix:
public void printMatrix(int newMatrix [][]) {
for (int i = 0; i < newMatrix.length; i++) {
for (int j = 0; j < newMatrix[i].length; j++) {
System.out.print(newMatrix[i][j] + " ");
}
System.out.println("");
}
}
Is all of this overkill and should I just define all the methods within the same class to avoid these issues?
View Replies
View Related