How To Generate A Ship On 10X10 Multidimensional Array

Sep 10, 2014

I am working on a project on bluej called battleship and being new to it I cant figure out how to generate a ship(3 co ordinate long) on a 10X10 multi dimensional array. nothing graphical just plain co ordinates.

View Replies


ADVERTISEMENT

How To Return Sum Of Multidimensional Array

Nov 21, 2014

what would the return be for adding two array's together,

public static int sum (int[][] a, int[][] b) {

int[] [] c = new int[a[0].length][b[0].length];
for (int i=0; i< a.length; i++){
for (int j=0; j<a[i].length; j++){
c[i][j] = a[i][j] + b[i][j];
}
}
return c[i][j];
}

My return is wrong...... I know, Any hints?

View Replies View Related

Dividing Multidimensional Array By Its Sum

May 5, 2015

At the bottom of this code snippet you will see the print statement. I am trying to divide the multidimensional array by its sum. Can this be done simply in the System.out statement, or do I need a separate method?

private static void Histogram() throws IOException {
int[][][] ch = new int[4][4][4]; /* 4 for 64 and 8 bins, 8 for 512 bins */
BufferedImage image = ImageIO.read(new File("airplane_training1.jpg"));
for(int x = 0; x < image.getWidth(); x++)
for(int y = 0; y < image.getHeight(); y++) {

[Code] .....

View Replies View Related

How To Add Numbers In A Column In Multidimensional Array

Mar 13, 2015

import java.util.Scanner;
public class ColumnSum {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int userpick = 0;
int sum = 0;
int [][] matrix = {{5, 9, 87, 74, 12, 7}, // row 1

[code]...

Right now my code gets all the numbers in a row and adds those up, but I want it to get the numbers in a column and add them instead. The problem is I don't know how to get the userpick (the number that the user picks to determine which column gets added) to be set to that particular column.

View Replies View Related

Multidimensional Array With Size Only In The First Square

Jan 14, 2014

I come to the point: I just started to learn java through various manuals and in one of them I came across a declaration of an array that I do not understand:

int[][] multiArr = new int[2][];

the manual says that you can allocate the multidimensional array multiArr by defining size in only the first square bracket but I can't undestand how you can use this array. Seems to be no way to store data with it!

View Replies View Related

How To Initialize Multidimensional Array Just After Declaration

Feb 13, 2014

I tried this:

String[][]f = new String[1][1] {{"Harry"}{"Hairy"}};

I also tried this:

String[][]f = new String[1][1] {{"Harry"},{"Hairy"}};

but I get an error

View Replies View Related

Finding Average Of Multidimensional Array

Mar 2, 2015

public class StuTest {
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args){
int[][] testScores; // [students][tests]
String[] stuNames;

[Code] ....

The method I am having issues with is the "printStudentReport" method. I am trying to calculate the average of a randomly generated number of students and tests. For my "printTestReport" method, I have to calculate the average of the test by test number.

View Replies View Related

Using Split Method With Multidimensional Array

Feb 2, 2015

I've found different examples on line, but none that use the split method with a multidimensional array. I would like a user to input coordinates (x,y) for city locations and store them into a 2-D array. For the user to input the x,y-coordinates on one line I need to use split(',') and then parse the string array to a double which will then be used to calculate the distances from one another.

My issue is how to store the String vales into the 2-D array. I do not want to store the x-value at even (cityArray[0]) and y-value at odd (cityArray[1]) 1-D locations.

View Replies View Related

Printing Each Value In Multidimensional Array Using For Loop

Dec 5, 2014

I want to print each value in the multi-dimensional array using a for loop but there is a problem.

Here is the script:

Java Code:

public class LearningJava
{
public static void main(String[] args) {
int[][] Grid = {
{12, 24, 36, 48, 60},
{1, 3, 5},
[Code] ....

Printing i: 0, x: 12
Printing i: 0, x: 24

Exception in thread "main" Printing i: 0, x: 36

Printing i: 0, x: 48
Printing i: 0, x: 60

java.lang.ArrayIndexOutOfBoundsException: 5
at LearningJava.main(LearningJava.java:11)

It's suppose to get the length of each array and print all the values in that array and then move to the next one.

I tried adding .length

Java Code: for(int x = 0; x < Grid[i][x].length; x++) mh_sh_highlight_all('java');

but then it doesn't work at all.

View Replies View Related

Counting In Multidimensional Array In Java?

Dec 19, 2014

I have to write a program that will read a picture and then print out the number of blocks inside it.I have to read the picture as a binary matrix of the size r c (number of rows times number of columns).The blocks are groups of one or more adjacent elements with the value 1.

- Blocks are built exclusively of elements with value 1
-Each element with value 1 is a part of some block
-Adjacent elements with value 1 belong to the same molecule.

We only take into account the horizontal and vertical adjacency but not diagonal.

INPUT:

In the first line of the input we have the integers r and c, separated with one space. Then we have the r lines, where each contains s 0's and 1's.The numbers inside the individual lines are NOT separated by spaces.The OUTPUT only print the number of blocks in the picture.

Example:

INPUT:
7 5
01000
00010
00000
10000
01000
00001
00100

OUTPUT:
6

THIS IS WHAT I CAME UP SO FAR:

import java.util.Scanner; 
class Blocks{ 
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
 
[code]...

View Replies View Related

Java Length Of One Dimension From A Multidimensional Array?

Apr 8, 2014

how you can do array.length, I have an array with [][][][], and what do I do to find the length of one section?

So let's say

Java Code: int array[][] = {
{5, 5, 5, 5},
{4, 4, 4, 4},
{3, 3, 3, 3}
} mh_sh_highlight_all('java');

How do I find out the length of the {5, 5, 5, 5} which is 4. Because there are 4 place-holders.Is there a way?

View Replies View Related

Replacing Null Values Within Multidimensional Array

Feb 18, 2014

I have a file which contains certain positions ([a][b]) that require to be placed in a certain multi-dimensional array. For example I might have an array String[][] that is a size of 6x6 but only have values in positions [2][1] and [3][2]. Because it is important for me to maintain the given array size and also do certain actions with the given positions I cannot modify the size. In addition I need to count the surrounding neighbors each element has (including elements that are null). However because some of my further code cant process with null elements I need to remove all null elements with " " (blank).

I am not sure how this is done or if it's even possible. If it is not possible how can I do something as close as possible to my needs?

View Replies View Related

Converting Multidimensional Array Into 1D Array?

Mar 22, 2015

import java.util.*;
import java.text.*;
public class Linearize {
public static void main(String[] args) {
// Create new Scanner and Random and Decimal objects
Scanner s = new Scanner (System.in);
Random g = new Random ();
DecimalFormat oneplaces = new DecimalFormat (".00");

[code]....

I am really close to finishing this program and my output is almost there, except it's only printing out the first half or so of the array. I have two for loops and two counters to determine the size of the array and then copy the multidimensional array's values into the 1D array, but it only prints out the first half of the array as such:

How many rows in the array? 4

How many columns in the array? 2

This 2D array contains:

35.23, 26.94,
99.48, 66.69,
7.31, 25.18,
64.53, 21.25,

Converted to a 1D array:

35.23, 26.94, 99.48, 66.69,

And yes, I realize that I should be formatting my Print statements with % but my instructor doesn't seem to care about it (he never taught it to us) so for the time being I am being stubborn and using .

View Replies View Related

Multidimensional Array - Show Values In Rows And Columns

Aug 20, 2014

I am trying to write a code for multidimensional array, allocate memory for variables and access the value of the arrays. I want them to be shown as rows and columns. but my code only shows one column and no rows. here is my code:

public static void main(String[] args) {
int[ ][ ] aryNumbers = new int[2][2];
aryNumbers [0][0] = 1;
aryNumbers [0][1] = 2;
aryNumbers [1][0] = 3;
aryNumbers [1][1] = 4;
int rows = 2;
int columns = 2;

[code]....

View Replies View Related

Accepting And Assigning User Input Into Multidimensional Array?

Apr 7, 2014

I have been asked to write a library program that will keep record of books and the year it was published. The program should ask the user how many rows he wants accept the string input from the user and display them in rows and columns. This is how i code it

package multidimension;
import java.util.Scanner;
public class bookrecords {
public static void main(String[]args){
//declaring a scanner variable
Scanner input =new Scanner(System.in);

[code]....

View Replies View Related

Multidimensional Array - Print Out 5 Names With Gender Type

Apr 19, 2013

I just started in java programming and into Arrays multidimensional. I started a simple 2 dimensional array with 5 names with genders. I 'm having issues because it does not want to print out the names with correct genders Male/ Female. The simple program should print out the 5 names with gender type.

example:

Jack - Male
Sally - Female
Dave - Male
Sue - Female
Brian - Male

Here is what I came up with so far:

public class Multiname
{
     public static void main(String[] args)
     {
          //String[][] multiname;
          String [][] multiname =

[Code] .....

This is the result:

multiname : Jack
multiname : sally
multiname : Dave
multiname : Brian
multiname : Sue

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
     at MultiD.main(MultiD.java:29)

View Replies View Related

Read Picture And Print Out Number Of Blocks - Counting In Multidimensional Array

Dec 19, 2014

I have to write a program that will read a picture and then print out the number of blocks inside it.

I have to read the picture as a binary matrix of the size r - c (number of rows times number of columns). The blocks are groups of one or more adjacent elements with the value 1.

- Blocks are built exclusively of elements with value 1
- Each element with value 1 is a part of some block
- Adjacent elements with value 1 belong to the same molecule.

We only take into account the horizontal and vertical adjacency but not diagonal.

INPUT:

In the first line of the input we have the integers r and c, separated with one space.
Then we have the r lines, where each contains s 0's and 1's.
The numbers inside the individual lines are NOT separated by spaces.

The OUTPUT only print the number of blocks in the picture.

Example:

INPUT:
7 5
01000
00010
00000
10000
01000
00001
00100

OUTPUT:
6

THIS IS WHAT I CAME UP SO FAR:

import java.util.Scanner;
class Blocks{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
char ch[][];
int rowNum=sc.nextInt();
int columnNum=sc.nextInt();

[Code] ....

View Replies View Related

Generate 10 Random Integers / Store In Array And Then Calling A Method To Display Array

Nov 8, 2014

So I need to generate 10 random integers in the range 1-20 but i have to store them in an array called numbers. Then I have to call a method called displayArray which displays the contents of the array and for the assignment i have to use a for loop to traverse the array. The method header for the displayArray method is:

public static void displayArray(int[] array)

This is what I have done
 
public class RandomIntegers {
 static int numbers = 0;
public static void displayArray(int[] array) {
System.out.println(numbers + "Numbers Generated");

[Code] .....

View Replies View Related

10x10 Grid Java - How To Add Numbers To Squares

Mar 12, 2015

So I need to create a 10x10 grid for a snakes and ladders board, the board loads but i cant figure out how to add numbers to the squares. Here is my code :

import java.awt.*;
import javax.swing.*;
public class Board
{
//Creating the array
int[][] numbers=new int[10][10];
public static int rows = 10;
public static int columns = 10;
public static Color col1 = Color.green;

[Code] .....

View Replies View Related

Random Generate Secret Number Into Array

Apr 1, 2014

Trying to generate random integers, based off user-input for amount of integers, and then sort them into an array. The problem is that the second method needs to be int[] but I cannot figure out what to make the return result. The instructions say it needs to be an int[] in the UML diagram, so I know it's not supposed to be void.

Java Code:

public void generateNewSecret() {
Random rand = new Random();{
for (int i=0; i<numDigitsSet; i++) {
secretNumber[i]= rand.nextInt(10);
System.out.println("" + secretNumber[i]);

[Code] .....

View Replies View Related

Generate Random 100 Numbers From 0 To 9 In Array - Count How Many Integers There Are

Mar 7, 2014

I'm trying to generate random 100 numbers, from 0 to 9, in an array using Math.random, but it only outputs 0s which is very confusing to me. Also, I need to be able to count how many different integers there are like 0s, 1s, 2s... 8s, 9s.

Here's my code, I only got as far as the array then got stumped on the counting part.

import java.util.Arrays;
public class countDigits {
public static void main(String[] args) { 
//Create random generator and values
int numbers = (int)(Math.random() * 10);
int arrayCount = 1;

[Code] ....

and my results

0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0

View Replies View Related

Generate Random Number Between 0 And 19 Which Represent Location In Array

Aug 2, 2014

To simulate it, the program will generate a random number between 0 and 19, which represents the location in the array (i.e. index number). Then, the 3 numbers to the left and right of this location should be reset to the value 0. If there isn't 3 numbers to the left and right you may assume a lesser number depending on the boundaries of the array.

How to reset the numbers to 0 in the final array ?

View Replies View Related

Generate 100 Numbers Using Arrays - Sort Even Numbers Into Separate Array And Display Both

Apr 24, 2014

I've just written a program that generates 100 numbers that range from 0 ~ 25 using arrays, the program calls another method that sorts the even numbers into a separate array and returns the array. I need it to display both arrays, however, when I run my program, the numbers of both arrays are mixed together, and I'm not sure how to separate them.

[ public class Array1
{
public static void main(String[] args)
{
int array [ ] = new int[100];
for (int i = 0; i < array.length; i++)
{
array[i] = (int) (Math.random() * 26);

[Code] .....

View Replies View Related

Does Either Of These Randomly Generate Both 0 And 1?

Nov 22, 2014

"which code is best for randomly generating integer 0 or 1". Haven't gotten my grade back yet but in reading these during the test I didn't think any of these would kick out both of those numbers, but there was no "none of the above" option on the test.This is exactly how it appeared on the test:

A) (int)Math.random() + 1
B) (int)(Math.random() + 0.2)
C) (int)Math.random()
D) (int)(Math.random() + 0.8)
E) (int)(Math.random() + 0.5)

I've tried all of them in the cs lab 100 times each and none of them generated both numbers. 'A' kicked out 1 every time. 'B,D and E' kicked out 0.2, 0.8 and 0.5 respectively each time, and 'C' kicked out 0. Did I just not run them enough times for the result to change or am I right in thinking there's a glitch on the test?

View Replies View Related

Generate API HTML Pages?

Feb 28, 2014

I have a large product with many maven projects, all of which can be hierarchically tracked back to the same parent. The projects have javadocs and whatnot.

Icommand line instructions (Windows 7) to generate the full API documents html on my local machine (of all the projects together). If there is a maven command for doing this from the CMD, more the better.

View Replies View Related

Generate Bar Graph From Data

May 22, 2014

I need to generate a bar chart given the data that is in main. The relative performance of each sorting algorithm with respect to time, number of comparisons, and number of swaps for each data set (e.g., 50,000 –400,000 random integers). Each algorithm will have 5 bars corresponding to the 5 data sets. The height of these bars will depend on the performance of the algorithm for that data set.

I know how to write a bar with normal values like integers but for this I'm not sure because the values for instance comparisons and swaps are not given they are calculated. I just need to know what steps to go about doing that, you can even express them in pseudocode or even just explaining it I don't need the code, I think. Here is the main

public class Main {
public static void main(String[] args){
int t= 50000;
int t2=100000;
int t3=200000;
int t4=300000;
int t5=400000;

[Code] .....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved