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.
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.
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();
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++) {
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.
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!
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.
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.
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?
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.
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:
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 .
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;
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);
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)
It's supposed to count all of the duplicates in an array and print out how many occurrences of the value starting at whatever index, or if there are no duplicates state that. Basically:
No duplicates with value 1 beyond Index 0
There are 3 more occurrences of value 2 starting at index 1
There are 2 more occurrences of value 2 starting at index 2....
This is what I've got so far:
Java Code:
public static void main(String[] args) { int[] arr = {1, 2, 2, 3, 4, 2, 4, 3, 0, 5, 3, 2}; for(int i = 0; i<arr.length; i++){ int count = 0; for(int j = i+1; j<arr.length; j++){ if((arr[j] == arr[i]) && (i!=j)){ count++; System.out.print("There are " + count + " more occurrences of "); System.out.println(arr[i] + " starting at index " + i); } } } } mh_sh_highlight_all('java');
I have this code but I can't seem to get it to work. It keeps saying that "count" cat be found and that it cannot return a value whose type is void.
Java Code: public class Cuantos { static int getPosition(double listOfValues[], double targetValue ) { int i,count, position = -1; for (i=0; i < listOfValues.length; i++) { if (listOfValues[i] == targetValue)
I have to take user input and then count how many times each number that the user input and print each one out. For some reason, I can't even get the for loop statement to print and it's pretty much the same as my other program except for the loop which is a little different.
//User inputs numbers between 1 and 100, program counts how many of each integer is and ends with a 0
import java.util.Scanner; public class occurrence { public static void main(String[] args) { //scanner/values Scanner input = new Scanner(System.in); int number = 0; int c = 0; //array count
So in my parallel array i read from a textfile of strings and if i enter the string into the string array and if strings are repeated i store it in a parallel array that counts repeated instances. I'm supposed to get 27 15 21 23 20 but instead i get 106 0 0 0 0....
I am trying to make a java program which should count the occurrences of a specific character in a string. I have 1 error - "cannot find method charAt(int)". Here is what I have.
import java.util.Scanner; public class ch4q5 { public static void main(String[] args) { String input ; String t ; int c = 0;
I have written a piece of code that takes a desired input file and calculates things such as words, characters, digits etc. I would like to make the program look better by counting palindromes.what I could add to my current code to count palindromes.My current code for counting other things that I would like to add plaindromes to.
// Loops through the file calculating the outcome. while (input.hasNextLine()) { lines++; String line = input.nextLine(); chars += line.length();
I'm having trouble creating a highly efficient algorithm for counting within a custom scale. This problem applies to futures trading, specifically treasuries contracts.
One specific treasury contract has 32 units before rolling over to the next whole number. So, the price scale looks something like this ...
If I pick a number (price) at random, let's say 1 28, and I want to add 8 units to that value, I should end up with 2 4. I can do this using brute force, calculating remainders, etc, etc....
Write a method compressDuplicates that accepts a stack of integers as a parameter and that replaces each sequence of duplicates with a pair of values: a count of the number of duplicates, followed by the actual duplicated number. For example, suppose a variable called s stores the following sequence of values:
This new stack indicates that the original had 5 occurrences of 2 at the bottom of the stack followed by 2 occurrences of -5 followed by 4 occurrences of 3, and so on. This process works best when there are many duplicates in a row. For example, if the stack instead had stored:
bottom [10, 20, 10, 20, 20, 10] top
Then the resulting stack after the call ends up being longer than the original:
bottom [1, 10, 1, 20, 1, 10, 2, 20, 1, 10] top
If the stack is empty, your method should not change it. You may use one queue as auxiliary storage to solve this problem. You may not use any other auxiliary data structures to solve this problem, although you can have as many simple variables as you like. You may not use recursion to solve this problem. For full credit your code must run in O(n) time where n is the number of elements of the original stack.
I wrote a code but still having a problem with it , am I allowed to use 3 while loops ?
public void compressDuplicates(Stack<Integer> s ){ Stack<Integer> backup= new Stack<Integer>(); int count = 1; while(!s.isEmpty()){ int temp = s.pop();
I was browsing around and I found a question asking to find how many times a word occurred in a sentence. I am using a hashtable, over kill yes but I need to use them more. However my counter is not working, not really sure why.You can see in the main method I have two repeating names but it returns 0.
package frequency; import java.util.Hashtable; public class CheckFrequency { hashtable<String, Word> words = new Hashtable<String, Word>();