Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 days ago.
Improve this question
I am trying to create a code that calculates the y value based on the value of X. Here is a segment.
for(x = -3.0; x <= 4.0; x += 0.50) {
y= (9*x*x*x - 27*x*x - 4*x + 12)/(Math.sqrt(3*x*x + 1) + Math.abs(5 - x*x*x*x));
System.out.printf("\n\tX = %9.1f\t\tY = %13.3f",x,y);`
I tried to run this code, and the table came out good, however the X and Y values are printing far away from the X = and Y =. I was expecting the values to print right next to the = sign.
To left-justify the values you can prefix the placeholder with a dash, like so:
System.out.printf("\n\tX = %-9.1f\t\tY = %-13.3f", x, y);
Difference:
%-9.1f instead of %9.1f
%-13.3f instead of %13.3f
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
public static int flood(int x, int y) {
if(x<0||y<0||x>101||y>101||went[x][y]) return 0;
System.out.println(x + " " + y);
went[x][y] = true;
if(grid[x][y] == 1) return 1;
int result = 0;
result += flood(x+1,y);
result += flood(x,y+1);
result += flood(x-1,y);
result += flood(x,y-1);
return result;
}
The code never came back to the same coordinate, but it is still somehow crashing.
P.S. went is a 2d boolean array.
What you wrote is a recursive function, it calls itself.
At first sight, it looks ok, but it "expands very fast".
In the first call to flood(0,0), it will "stack" flood(1,0) which will then "stack" flood(2,0) ... which will then stack flood(100,100) and only at this point will the method return for the first time!
That means that the stack size is roughly 10000 before the stack (of methods to continue processing once the current one is done) starts to "shrink back" after that point.
The basics of your method is correct, and I suppose it will work for a small array size, it's just a too big stack for a default JVM.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am making a Java game and am trying to get blocks to spawn alternating back and forth on the x-axis using a for loop.
Just do something like this:
spawn(0);
for(int i = 1; i <= 3; ++i)
{
spawn(-i);
spawn( i);
}
Method createBlock(): //creates the blocks at desired x axis
createBlock(0); //initial block placed at 0
int howMany= input
for (i=0; howMany;i++){
createBlock(-i)
createBlock(i)
}
IntStream.iterate(0, i -> i >= 0 ? -i - 1 : -i). limit(100).forEach(i -> System.out.println(i));
System.out.print(0);
for (int i = 1; i<=3; i++) {
System.out.print(String.format(",%s,%s", -i, i));
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a list of Portfolio objects in java collection. This Portfolio object has a property called scoringRule. I want to calculate how much percentage of Portfolio objects are present in the list for each type of scoringRule. Actually, I have only 2 different types of scoring rules. The scoringRUle property can be either "single" or "double". How can I do this?
Class Portfolio{
private String name;
private String type;
private String scoringRule;
}
List<Portfolio> portfolios = new ArrayList<Portfolio>();
This is your solution.
double single = 0;
double doublee = 0;
for (Portfolio pobj : portfolios) {
if(pobj.scoringRule.equals("single"))
single++;
else
doublee++;
}
System.out.println("single Percentage : " +(single / list.size() )*100);
System.out.println("doublee Percentage : " + (doublee / list.size())*100);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a Java application and want to press a button to generate a 7 digit random number and put it into a text area.
Here is what I have so far:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.setText();
}
How do I do this?
i don't know what to write inside the brackets to get this random number of seven digits on the text area.
You can use a StringBuilder and append 7 random numbers between 0 and 9 using the nextInt(int n) method :
Random r = new Random();
StringBuilder sb = new StringBuilder();
for(int i = 0; i < 7; i++)
sb.append(r.nextInt(10));
jTextArea1.setText(sb.toString());
first you'll need to inport the java random number library at the top of your code, like this:
import java.util.Random;
this code will get you a random number from 1000000 to 9999999, it is a little weird, but take some time trying to figure it out
1000000 + (int)(Math.random() * ((8999999) + 1))
Try putting that between the parenthasis after setText, like this:
jTextArea1.setText(1000000 + (int)(Math.random() * ((8999999) + 1)));
For more information about the random function and how it works, looks here
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to implement a Mean Filter on a coloured image for an Android application. When I apply the filter on a chosen image.
The original image:
The filtered image
At each pixel you read the color value from pixels[index], but the index variable is an index to the filtered array, not the original image. You should be reading the pixel data at index x+filterX+width*(y+filterY), and be careful at the edges of the image.
A bigger problem is that the sums of red, green, and blue never get reset to 0, which means they'll keep accumulating pixel values. This should explain the discoloring. Add:
sumR = 0;
sumG = 0;
sumB = 0;
for (int i = 0; i < RArray.length; i++) {
sumR += RArray[i];
sumG += GArray[i];
sumB += BArray[i];
}