Neighbors Of 2D Array - Return SmartArray With Values
Feb 2, 2015
I have attempted on my own many times but I am not getting any closer to a solution.
/**
* Returns a SmartArray with eight values. The values are the values stored in the 8 neighbors of the array cell at the given location in the Smart2DArray.
* Start with the neighbor to the "north" of the given cell and proceed clockwise, using -1 as the value if the neighboring cell is outside the Smart2DArray.
For example, if the array is:
1 2 3
4 5 6
7 8 9
neighbors(1, 1) should return a SmartArray with the values:
2 3 6 9 8 7 4 1
in that order.
neighbors(2,1) should return a SmartArray with the values:
3 -1 -1 -1 9 8 5 2
in that order.
*/
public SmartArray neighbors (int col, int row) {
}
How do i take input values for TwoDPoint (which are objects) and return it back in numerical values also print them.
When i create an object in main method and pass values to my function of return type TwoDPoint,it gives error:- found int,int need TwoDPoiint,TwoDPoint.
// Here is what i tried to do:
Created class TwoDPoint that contains two fields x, y which are of type int. Defined another class TestTwoDPoint, where a main method is defined.In the main method created two TwoDPoint objects.
Then I modified the class TestTwoDPoint and add another function to it. This function takes two TwoDPoints as input and returns the TwoDPoint that is farthest from the point (0,0).
Then I added another function to TestTwoDPoint. This function takes two TwoDPoints as input and returns a new TwoDPoint whose x value is the sum of x values of the input TwoDPoint's and whose y value is the sum of the y values of the input TwoDPoint's.
class TwoDPoint { int x = 2; int y = 4; } class TestTwoDPoint { public static void main(String args[]) { TwoDPoint obj1 = new TwoDPoint(); System.out.println(obj1.x); System.out.println(obj1.y);
Here is my HashMap and a method for listing all the keys in it
HashMap<String, String> exampleOne = new HashMap<String, String>(); public void allKeys() { int i; i =0; for (String name: exampleOne.keySet())
[Code]....
Now I want to return all values that associated with one key. How do I do this? Or is it possible to other way round? I mean return All keys associated with a value?
I am trying to write a program that will take any comma separated data set and return the values in an arraylist without any commas. Like if I input "hello, world, program, java" it would output an arraylist [hello,world,program,java].
public void run(){ String line = readLine("Enter a CSV-formatted line of data: "); int lowerBound = 0; String entry = new String(""); ArrayList<String> string = new ArrayList<String>();
I am making a java swing program. I have a main window that opens a JForm window when the user clicks a menu option. Then I have a button on the JForm that closes the window on click. However, I would also somehow like to make the JForm return values to the main window (according to the states of the selector components) when it is closed. How would I go about this? Currently my jForm is a seperate class called "NewGame", and inside the menu item mouse clicked method, I have the following code:
//open new game jform window NewGame newGameWindow = new NewGame(); newGameWindow.setVisible(true); newGameWindow.setLocationRelativeTo(null); //center window
Is there something I can add here to get the object's values upon the window's close? Or is there a way I can add that into the button clicked method on the actual jForm?
Write method distance to calculate the distance between two points (x1, y1) and (x2, y2). All numbers and return values should be of type double. Incorporate this method into an application that enables the user to enter the coordinates of the points.
Hints:
- The distance between two points can be calculated by taking the square root of
( x2 - x1 )2 + ( y2 - y1 )2
- Use Math class methods to compute the distance.
- Your output should appear as follows:
Type the end-of-file indicator to terminate
On UNIX/Linux/Mac OS X type <ctrl> d then press Enter
On Windows type <ctrl> z then press Enter
Or Enter X1: 1
Enter Y1: 1
Enter X2: 4
Enter Y2: 5
Distance is 5.000000
Type the end-of-file indicator to terminate
On UNIX/Linux/Mac OS X type <ctrl> d then press Enter
I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is strong return a, if it is notstrong return b. How would you write that in a Code?
I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.
mlong should return an int depending on the X.moth. at the moment my code looks like this:
// File1: public class date { public int day; public int month; public int year; }
// File 2: public class monthlength { public int mlong(date X) { int t; t = X.month; if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12) { return 31; } if(t == 4 || t == 6 || t == 9 || t == 11) {return 30;} } }
I want to return values from arrays to the main and the problem is i cant use my variables from my constructor, I use new variables in my functions and i know this is no good, when I used the variables from the constructor in my function. I have a compilations errors,also i want to create un object in main and with this object i want to call the functions.
Java Code:
package javaapplication4; import java.util.Scanner; public class JavaApplication4 { public static int[] MyInt; public static double[] MyDouble; public static String[] MyString; public static char[] MyChar;
[Code] .....
The code is working when i run it and i have the right result in my screen but i know this is all wrong with the variables.
Write a method that returns a new array by eliminating the duplicate values in the array using the following method header: public static int[] eliminateDuplicates(int[] list). The thing is that I found the working solution that is written below, but how it works. How to eliminateDuplicates method done with flag and flag2.
Here is the code:
Java Code:
import java.util.Scanner; public class Exercise06_15 { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); int[] numbers = new int[10]; System.out.println("Enter ten numbers: ");
I am getting these errors with this code, I can not figure why this error is occuring.
what is the issue with this code?
Java Code: package ABC; import java.util.ArrayList; import java.util.Scanner; public class GetInputFromUser { private ArrayList<String> name = new ArrayList<String>();
I need to return all the object name of one class in an array. I have a class named country, and other classes with athletes and medals etc. I need to do a method that list an array with all the countries that participate, so all the objects created with the class country (i.e canada.country, usa.country, etc). Is there a way I can retrieve them?
I need to return all the object name of one class in an array. I have a class named country, and other classes with athletes and medals etc. I need to do a method that list an array with all the countries that participate, so all the objects created with the class country (i.e canada.country, usa.country, etc). Is there a way I can retrieve them?
I need to design a class named Location for locating a maximal value and its location in a two-dimensional array. The class should contain public data fields row, column, and maxValue that store the maximal value and its indices in a two dimensional array with row and column as int type and maxValue as double type.
I need to write the following method that returns the location of the largest element in a two-dimensional array: public static location locateLargest(double[][] a)
The return value is an instance of Location. Write a test program that prompts the user to enter a two-dimensional array and displays the location of the largest element in the array.
Here is what i get when I run
Enter the number of rows and columns of the array: 3 4 Enter the array: 23.5 35 2 10 4.5 3 45 3.5 35 44 5.5 9.6 The location of the largest element is at 00 The location of the largest element is at 01
But I need it to display this instead.
Enter the number of rows and columns of the array: 3 4 Enter the array: 23.5 35 2 10 4.5 3 45 3.5 35 44 5.5 9.6 The location of the largest element is 45 at (1,2)
public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Number of rows and columns: "); int row = input.nextInt(); int col = input.nextInt();
[Code] ....
Enter the number of rows and columns of the array: 3 4 Enter the array: 23.5 35 2 10 4.5 3 45 3.5 35 44 5.5 9.6 The location of the largest element is at 00 The location of the largest element is at 01
I need it to display this instead.
Enter the number of rows and columns of the array: 3 4 Enter the array: 23.5 35 2 10 4.5 3 45 3.5 35 44 5.5 9.6 The location of the largest element is 45 at (1,2)
filling out a Random array: An Array of Specific Length Filled with Random Numbers This time what I need to do is take the elements from this Random array and assign them to a new Byte array:
for(int i = 0; i < limit-10; i++) { Random dice = new Random(); int randomIndex = dice.nextInt(array.length); if (array[randomIndex] < 128) { System.out.print(array[randomIndex] + " "); } else if (array[randomIndex] >= 128) { System.out.print(array[i] + " "); } } byte[] noteValues = new byte[]
{ 64, 69, 72, 71, 64, 71, 74, 72, 76, 68, 76 }; //This is the byte array filled manually!
I've tried amending the manual input to fit in with the Random array, as follows:
byte[] noteValues = new byte[] { array[randomIndex] };
In this case, however, the Byte array can't interpret the int values. Also, if the Byte array is outside the 'for' loop, array[randomIndex] cannot be resolved.
I am trying to understand the following code.This return statement should actually return the char at myArray[index] first, then increments the index afterwords correct?
I am having an issue with a parallel array that I created that returns array values based on an input. The code only works for the first four array locations just fine. The remaining values are not found. I thought it might be a memory allocation issue with the array size, I tried to dimension the array, however, using the Eclipse editor get errors.
import java.util.*; import javax.swing.JOptionPane; public class StudentIDArray { static String[] studentNum = new String[] {"1234", "2345", "3456", "4567", "5678", "6789", "7890", "8901", "9012", "0123"}; static String[] studentName = new String[]
I'm trying to return an array back to main. The array returned to main should contain the reversed random numbers array. I believe I have the array correctly reversed within the 'reverseArray' method. I'm trying to send this array back to main, but it appears to contain empty data (a bunch of zeros).
Java Code:
class ArrToMain { public static void main(String[] args) { final int NUMBER_OF_ELEMENTS = 1000; double[] numbers = new double [NUMBER_OF_ELEMENTS]; //Invoke initialize method initialize(numbers);
Assignment: Given an array of scores sorted in increasing order, return true if the array contains 3 adjacent scores that differ from each other by at most 2, such as with {3, 4, 5} or {3, 5, 5}.
public boolean scoresClump(int[] scores) { for (int i = 0; i < scores.length; i++) { int a = scores[i]; int b = scores[i + 1]; int c = scores[i + 2]; if (Math.abs(b-a) + Math.abs (c-b) <=2) return true; } return false; }
I got it right for some of the input, but not one of them. I tried to use the for loop and if statement on a specific input that I got wrong:
I suspect it has something to do with the for loop, but I don't see the problem with it. It should work, shouldn't it? But anyway, here is the error for {4,5,8} :
Question - Given an specific integer and an array of integers, find and remove all occurrences of the given integer and return an amended array. I solved it. Here is my solution -
I have problems getting the right number of times for each number of the array. Here is my code:
public static void main(String[] args) { int[] a = { 3, 13, 5, 9, 13, 6, 9, 13, 2, 3 }; int num = 3; int count = numbers(a, num); for (int i = 0; i < a.length; i++) {