Diamond Matrix Program?
Oct 11, 2014i want solution to my program
View Repliesi want solution to my program
View RepliesWrite a program that reads an integer and displays, using asterisks, a filled diamond of the given side length. For example, if the side length is 4 the program should display.
*
***
*****
*******
*****
***
*
I have it where it displays the top half of the diamond. But i cannot figure out how to get it to draw the bottom half.
import java.util.*;
public class E616 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter number of rows. ");
int N=input.nextInt();
[code]...
My son want to create a java program (Diamond shape) that can plot graph for with the following data.
Pattern type is:
* *
* *
*
* *
* *
*
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 RelatedI did a problem from my book which is "Write a method that displays an n-by-n matrix. Each element is 0 or 1, which is generate randomly. Write a test program that prompts the user to enter n and displays the n-by-b matrix".
So if a user would enter 3, and output could be 0 1 1
0 1 0
1 0 1
So here's my question... I was able to get the program to work in the way my book describes (by just writing the code in the class with the main), but for practice, I want to learn how to do this OOP-style...
I'm not sure how to do this, though, since the method is a void method, so I can't seem to call it within a toString method.
import java.util.Scanner;
public class MatrixTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an int");
int number = input.nextInt();
MatrixClass mc = new MatrixClass(number);
[Code] .....
I am currently trying to print a Diamond using for-loops but I seem to be confusing my self / over-complicating it.
This is what I have so far [URL]). When I run the code I get [URL].
I want to duplicate it on to the other side but I can't seem to be able to figure out a working for-loop to print it out.
Also, is there a faster/efficient method of doing something like this?
I am pretty close to finishing the diamond but I cant figure out how to print the bottom right. I have to use a for loop and if/else statements.
public static void main(String[] args) {
int input = 0;
input = getSize(input);
char[][] array = new char[input][input];
System.out.println("
Diamond:");
createDiamond(array, input);
print(array);
[code]....
I am new to java and i am trying to make a Java application which prints a diamond in a square grid of dots whose side length is input to the application.When you run the code is should be like this:
..*..
.*.*.
*...*
.*.*.
..*..
My java code print this:
..*..
.***.
*****
.***.
..*..
Here is my code:
class Main {
public static void main(String args[]) {
System.out.println("#Enter size of Diamond :");
int longestRow = BIO.getInt();
for(int row=1 ; row<=longestRow ; ++row)
[Code] .....
I'm currently trying to do a diamond using Polygon. I have done this so far. It is not drawing a diamond.
import java.applet.Applet;
import java.awt.Graphics;
public class Diamond extends Applet {
int[] a = {-30, 100 , 30, 100};
int[] b = {100, 40, 100, -40};
public void init()
{
}
public void paint(Graphics g)
{
g.drawPolygon(a, b, 4);
}
So I am learning HashMaps/Arraylists and I can't really understand the diamond operator or what it's for. Take the following code for example: Why could we not just do this without the diamond?
Java Code:
import java.util.HashMap;
class Untitled {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("California","Sacramento");
map.put("Oregon","Salem");
map.put("Washington","Olympia");
System.out.println(map);
}
} mh_sh_highlight_all('java');
I am new to learning Java and I am trying to print an unfilled diamond using asterisks. The height of the diamond has to be based on user input. My code currently prints out a filled diamond based on user input but I cannot figure out how to print an unfilled one. Every time I change one of the loops it messes something else up.
[public static void diamond(){
int i = 0;
int j = 0;
int k = 0;
int height = 0;
[code]...
I have to make a pattern that is of a diamond shape such as this:
Java Code:
1
1 2 3
1 2 3 4 5
1 2 3
1
mh_sh_highlight_all('java');
I wrote this (for the variable name, this is just a small part of my program and I'm trying to get the pattern right so I didn't mind my variables at the moment):
Java Code:
public class NewFile{
public static void main(String []args){
int k = 0;
for (int i=1 ; i<=5 ; i++) {
{ for (int h=2 ; h >= i ; h--)
[Code] ....
My output is the following:
Java Code:
1
123
12345
1234567
123456789
mh_sh_highlight_all('java');
I realize I should divide the code into a lower and upper triangle using two loops. However, I don't know how to break the first part. I did find the "trend" but I don't see how to implement it.
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]....
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]...
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]...
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]....
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]...
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] .....
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?
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
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] ....
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].....
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.
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]....
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]....
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 ?