Do-While loop in Java - java

So, as far as I understand it, a do while loop will always run at least once? But if this is the case why do we need to declare and initialise variables outside of the loop?
Take for instance the following code:
do {
int a = (int) (Math.random() * 13);
int b = (int) (Math.random() * 13);
int c = (int) (Math.random() * 13);
int d = (int) (Math.random() * 13);
}
while (a + b + c + d != 24);
Which will throw a compile error that a, b, c, d may not have been initialised. As I'm a java beginner I'm sure there's a simple reason for this, but I cannot seem to find it?!
Much thanks for any help with this.

Look up variable scope because that is your problem: you're trying to access variables outside of their declared scope, here the do-while loop, and this cannot be done.
Note your code will work if you introduce one more variable:
int sum = 0; // scope is *outside* of do-while loop
do {
int a = (int) (Math.random() * 13);
int b = (int) (Math.random() * 13);
int c = (int) (Math.random() * 13);
int d = (int) (Math.random() * 13);
sum = a + b + c + d;
} while (sum != 24);
But note that now if you still need to access the a, b, c, and d values, you cannot. To allow this, again, you should declare your variables before the loop.

This can be re written like this
int a = (int) (Math.random() * 13);
int b = (int) (Math.random() * 13);
int c = (int) (Math.random() * 13);
int d = (int) (Math.random() * 13);
while (a + b + c + d != 24){
a = (int) (Math.random() * 13);
b = (int) (Math.random() * 13);
c = (int) (Math.random() * 13);
d = (int) (Math.random() * 13);
//do something
}

do {
int a = (int) (Math.random() * 13);
int b = (int) (Math.random() * 13);
int c = (int) (Math.random() * 13);
int d = (int) (Math.random() * 13);
}
while (a + b + c + d != 24);
This is a scoping issue. Look at jls 6.3. Scope of a Declaration
You want to re-write the code as so:
int a = 0; //I am being explicit here
int b = 0;
int c = 0;
int d = 0;
do {
a = (int) (Math.random() * 13);
b = (int) (Math.random() * 13);
c = (int) (Math.random() * 13);
d = (int) (Math.random() * 13);
}
while (a + b + c + d != 24);

Related

Moving A JLabel Within A JPanels Limit (Think Its A Math Problem) JAVA

Also this might help others wanting to move JLabels around too.
So here is the situation:
1 JPanel with an item in it.
1 JLabel with an Img in it as the ICON.
I want to move the JLabel around the panel but not out of the bounds of the panel (So the user can see ALL of the image on the screen)
Here is the code:
I initialize there:
int randH = 0;
int randW = 0;
int targetHeight = 0;
int targetWidth = 0;
Then when the panel opens depending on which (target / IMG) is loaded:
targetHeight = jLabel1.getHeight();
targetWidth = jLabel1.getWidth();
Then when the label is clicked I called this code to move it around:
int posH = jPanel1.getHeight() - targetHeight;
int posW = jPanel1.getWidth() - targetWidth;
randH = new Random().nextInt(posH) - jLabel1.getHeight();
randW = new Random().nextInt(posW) - jLabel1.getWidth();
if (randH <= 0) {
int num = (int) (Math.random() * 5);;
randH = num;
}
if (randW <= 0) {
int num = (int) (Math.random() * 5);;
randW = num;
}
if (randH >= posH) {
int num = (int) (Math.random() * 5);;
randH = posH - num;
}
if (randW >= posW) {
int num = (int) (Math.random() * 5);;
randW = posW - num;
}
jPanel1.setLayout(null);
jLabel1.setBounds(new Rectangle(new Point(200, 300), jLabel1.getPreferredSize()));
jLabel1.setLocation(randW, randH);
jLabel1.setVisible(true);
so sometimes the IMG is partially out of the panel when the value is greater than the dimension of the JPanel
Any reason for this?
So after checking it all and thanks to everyone for the help!
randH = new Random().nextInt((posH - 0) + 1) + 0;
seems to have gotten me to where I need to be
posH = is the max value
0 = is the min value
+1 = include max
+0 = include min
and to explain a little more, the following code is to make sure the IMG isn't hugging the panel border so there are at least 5 pixels between the image and border.
if (randH <= 0) {
int num = (int) (Math.random() * 5);;
randH = num;
}
if (randW <= 0) {
int num = (int) (Math.random() * 5);;
randW = num;
}
if (randH >= posH) {
int num = (int) (Math.random() * 5);;
randH = posH - num;
}
if (randW >= posW) {
int num = (int) (Math.random() * 5);;
randW = posW - num;
}
this is to the best of my knowledge, maybe didn't do it well :D

Having trouble solving cubic equations in Java

I'm attempting to follow some psuedo code for solving cubic equations in Mathematics and Physics for Programmers, Chapter 3, as far as I can see I've followed it accurately, but I don't seem to be getting correct outputs.
For example: According to Wolfram Alpha 5x^3 + 4x^2 + 3x + 2 = 0 should give a root of x≈-0.72932, but I'm getting back -1.8580943294965526 from my script.
Can someone help me to work out what the hell I'm doing? I'm following the scripts to get a better understanding of maths and converting equations to code. But this is at the brink of my understanding so I'm finding it troublesome to debug. Coupled with the fact the book has no errata and many online reviews state the book has many errors, I'm struggling to see whether the issue is with my code, the books explanation or both.
The equation given in the book is:
Then if the discriminant > 0 then root has value of r+s:
if discriminant == 0 then there are two roots:
if discriminant < 0 you can find three roots as follows:
After finding t you can transform it to x by taking:
package com.oacc.maths;
public class SolveCubic {
public static double[] solveCubic(double a, double b, double c, double d) {
double[] result;
if (d != 1) {
a = a / d;
b = b / d;
c = c / d;
}
double p = b / 3 - a * a / 9;
double q = a * a * a / 27 - a * b / 6 + c / 2;
double D = p * p * p + q * q;
if (Double.compare(D, 0) >= 0) {
if (Double.compare(D, 0) == 0) {
double r = Math.cbrt(-q);
result = new double[2];
result[0] = 2 * r;
result[1] = -r;
} else {
double r = Math.cbrt(-q + Math.sqrt(D));
double s = Math.cbrt(-q - Math.sqrt(D));
result = new double[1];
result[0] = r + s;
}
} else {
double ang = Math.acos(-q / Math.sqrt(-p * p * p));
double r = 2 * Math.sqrt(-p);
result = new double[3];
for (int k = -1; k <= 1; k++) {
double theta = (ang - 2 * Math.PI * k) / 3;
result[k + 1] = r * Math.cos(theta);
}
}
for (int i = 0; i < result.length; i++) {
result[i] = result[i] - a / 3;
}
return result;
}
public static double[] solveCubic(double a, double b, double c) {
double d = 1;
return solveCubic(a, b, c, d);
}
public static void main(String args[]) {
double[] result = solveCubic(5, 4, 3, 2);
for (double aResult : result) {
System.out.println(aResult);
}
}
}
I also found this code example from the book site(n.b. this is not the psuedo code from the book): http://www.delmarlearning.com/companions/content/1435457331/files/index.asp?isbn=1435457331
on solveCubic(a,b,c,d)
--! ARGUMENTS:
a, b, c, d (all numbers). d is optional (default is 1)
--!
RETURNS: the list of solutions to dx^3+ax^2+bx+c=0
-- if d is defined then divide a, b and c by d
if not voidp(d)
then
if d=0 then return solveQuadratic(b,c,a)
set d to float(d)
set a to a/d
set b to b/d
set
c to c/d
else
set a to float(a)
set b to float(b)
set c to float(c)
end if
set p to b/3 - a*a/9
set q to a*a*a/27 - a*b/6 + c/2
set disc to p*p*p + q*q
if abs(disc)<0.001 then
set r to cuberoot(-q)
set ret to [2*r, -r]
else if disc>0 then
set r to cuberoot(-q+sqrt(disc))
set s to cuberoot(-q-sqrt(disc))
set ret to [r+s]
else
set ang to acos(-q/sqrt(-p*p*p))
set r to 2*sqrt(-p)
set ret to []
repeat with k=-1 to 1
set theta
to (ang - 2*pi*k)/3
ret.add(r*cos(theta))
end repeat
end if
set ret to ret-a/3 --NB: this adds the value to each
element
return ret
end
The error appears to be with the names of the parameters of your solveCubic method.
It seems your book is explaining how to solve the equation x3 + ax2 + bx + c = 0. You are calling your method thinking that the parameters a, b, c and d are for the equation ax3 + bx2 + cx + d = 0. However, it turns out that the body of your method is actually finding solutions to the equation dx3 + ax2 + bx + c = 0.
Aside from this naming error, the calculations appear to be correct. Try plugging your value ≈-1.858 into 2x3 + 5x2 + 4x + 3 if you don't believe me.
If you instead declare your solveCubic method as
public static double[] solveCubic(double d, double a, double b, double c) {
the parameters then correspond to the equation dx3 + ax2 + bx + c. You should then find that your method gives you the answer you expect.
Okay. So, first off the equations from the book seem to be referring to this idea: If you have an equation of the form:
Then by defining t as x - (a/3) you can transform this into an equation with no quadratic term, as verified by a bit of Mathematica:
Once you have no quadratic term, you can then apply the method given; let q be half the constant term, let p be one third the coefficient on the first power term, and define D (discriminant) as p*p*p - q*q.
All else then follows.
So why does your code not work? Because you've mislabeled the variables. a is the coefficient on x^2, not on x^3. If you call your method with the arguments (0.8, 0.6, 0.4, 1) or equivalently with (4, 3, 2, 5), you'll get the right answer.
(Or do as the other answer suggests and more around the variable names in the method declaration)
This works 100%. It has been tried and tested for all combinations.
public void solveCubicEquation(int A, int B, int C, int D) {
double a = (double) B / A;
double b = (double) C / A;
double c = (double) D / A;
System.out.println("Double values: ");
System.out.println(a + " " + b + " " + c);
double p = b - ((a * a) / 3.0);
double q = (2 * Math.pow(a, 3) / 27.0) - (a * b / 3.0) + c;
double delta = (Math.pow(q, 2) / 4) + (Math.pow(p, 3) / 27);
if (delta > 0.001) {
double mt1, mt2;
double t1 = (-q / 2.0) + Math.sqrt(delta);
double t2 = (-q / 2.0) - Math.sqrt(delta);
if (t1 < 0) {
mt1 = (-1) * (Math.pow(-t1, (double) 1 / 3));
} else {
mt1 = (Math.pow(t1, (double) 1 / 3));
}
if (t2 < 0) {
mt2 = (-1) * (Math.pow(-t2, (double) 1 / 3));
} else {
mt2 = (Math.pow(t2, (double) 1 / 3));
}
x1 = mt1 + mt2 - (a / 3.0);
} else if (delta < 0.001 && delta > -0.001) {
if (q < 0) {
x1 = 2 * Math.pow(-q / 2, (double) 1 / 3) - (a / 3);
x2 = -1 * Math.pow(-q / 2, (double) 1 / 3) - (a / 3);
} else {
x1 = -2 * Math.pow(q / 2, (double) 1 / 3) - (a / 3);
x2 = Math.pow(q / 2, (double) 1 / 3) - (a / 3);
}
} else {
System.out.println("here");
x1 = (2.0 / Math.sqrt(3)) * (Math.sqrt(-p) * Math.sin((1 / 3.0) * Math.asin(((3 * Math.sqrt(3) * q) / (2 * Math.pow(Math.pow(-p, (double) 1 / 2), 3)))))) - (a / 3.0);
x2 = (-2.0 / Math.sqrt(3)) * (Math.sqrt(-p) * Math.sin((1 / 3.0) * Math.asin(((3 * Math.sqrt(3) * q) / (2 * Math.pow(Math.pow(-p, (double) 1 / 2), 3)))) + (Math.PI / 3))) - (a / 3.0);
x3 = (2.0 / Math.sqrt(3)) * (Math.sqrt(-p) * Math.cos((1 / 3.0) * Math.asin(((3 * Math.sqrt(3) * q) / (2 * Math.pow(Math.pow(-p, (double) 1 / 2), 3)))) + (Math.PI / 6))) - (a / 3.0);
}
}
Note: will not work for imaginary values

Can someone please help me with the code I am writing? (Java)

I am writing a small program.
I am a beginner at programming java.
Here is the code:
package dnd;
public class Monster {
static String name;
static int level;
static String rang;
static String Class;
static double[] Strength = {1, 1, 0};
static double[] Endurance = {1, 1, 0};
static double[] Agility = {1, 1, 0};
static double[] Precision = {1, 1, 0};
static double[] Wisdom = {0, 1, 1};
static double[] Intelegence = {0, 1, 1};
public static void Monster(String nameC, int levelC, String classesC, int rangC){
name = nameC;
level = levelC;
switch(rangC){
case 1:
rang = "Civillian";
case 2:
rang = "Soldier";
case 3:
rang = "Veteran";
case 4:
rang = "Commander";
case 5:
rang = "Boss";
case 6:
rang = "Elite";
default:
rang = "Civillian";
}
Class = classesC;
switch(Class){
case "Warrior":
Strength[1] = 2;
Endurance[1] = 2;
Intelegence[1] = 0.5;
case "Archer":
Agility[1] = 2;
Precision[1] = 2;
Endurance[1] = 0.5;
case "Rouge":
Agility[1] = 2;
Intelegence[1] = 2;
Endurance[1] = 0.5;
case "Mage":
Wisdom[1] = 2;
Intelegence[1] = 2;
Strength[1] = 0.5;
case "Priest":
Wisdom[1] = 2;
Strength[1] = 0.5;
case "Druid":
Intelegence[1] = 2;
Agility[1] = 0.5;
}
}
public static void defaultStaringPoints(){
int StP;
int EnP;
int InP;
int AgP;
int PrP;
int WiP;
switch(Class){
case "Warrior":
//Strength
//Endurance
//-Intelegence
StP = (int) (Math.random() * 2);
EnP = (int) (Math.random() * (3-StP));
InP = (int) (Math.random() * (3-(StP+EnP)));
Strength[0] = Strength[0] + StP;
Endurance[0] = Endurance[0] + EnP;
Intelegence[0] = Intelegence[0] + InP;
case "Archer":
//Agility
//Precision
//-Endurance
AgP = (int) (Math.random() * 2);
PrP = (int) (Math.random() * (3-AgP));
EnP = (int) (Math.random() * (3-(AgP+PrP)));
Agility[0] = Agility[0] + AgP;
Precision[0] = Precision[0] + PrP;
Endurance[0] = Endurance[0] + EnP;
case "Rouge":
//Agility
//Intelegence
//-Endurance
AgP = (int) (Math.random() * 2);
InP = (int) (Math.random() * (3-AgP));
EnP = (int) (Math.random() * (3-(AgP+InP)));
Agility[0] = Agility[0] + AgP;
Intelegence[0] = Intelegence[0] + InP;
Endurance[0] = Endurance[0] + EnP;
case "Mage":
//Wisdom
//Intelegence
//-Strength
WiP = (int) (Math.random() * 2);
InP = (int) (Math.random() * (3-WiP));
StP = (int) (Math.random() * (3-(WiP+InP)));
Wisdom[0] = Wisdom[0] + WiP;
Intelegence[0] = Intelegence[0] + InP;
Strength[0] = Strength[0] + StP;
case "Priest":
//Wisdom
//-Strength
WiP = (int) (Math.random() * 3);
StP = (int) (Math.random() * (3-WiP));
Wisdom[0] = Wisdom[0] + WiP;
Strength[0] = Strength[0] + StP;
case "Druid":
//Intelegence
//-Agility
InP = (int) (Math.random() * 3);
AgP = (int) (Math.random() * (3-InP));
Intelegence[0] = Intelegence[0] + InP;
Agility[0] = Agility[0] + AgP;
}
}
}
Then I create a new Monster,
run Goblin.Monster("Goblin", 1, "Rouge", 2)
and Goblin.defaultStaringPoints()
the output for Arrays.toString(Goblin.Wisdom) should be [0, 1, 1],
but instead it is [1, 2, 1], [2, 2, 1] or even [3, 2, 1].
I feel, that I am just overseeing something,
but I checked over 10 times.
Thank You in advance!
In each of your switch statements, you need a break; statement at the end of each case. For example:
switch(rangC){
case 1:
rang = "Civillian";
break;
case 2:
rang = "Soldier";
break;
...
}
Without the break;, execution simply falls through to the next case.
Also, you don't have a case for "Goblin" in any of your switch statements.
When you call defaultStartingPoints(), it randomizes the stats so that the Wisdom stat is no longer [0, 1, 1].

Image over another already drawn image in CAPTCHA

I want to generate a captcha by myself without using any other API, but here are some issues. In my code I have treated each character as an image and then form some operation on them but now I want to rotate whole image with respect to its midpoint.
Here is my sample code:
for (int i = 0; i < charsToPrint; i++)
{
double randomValue =Math.random();
int randomIndex = (int) Math.round(randomValue * (chars.length - 1));
char characterToShow = chars[randomIndex];
finalString.append(characterToShow);
int charWidth = fontMetrics.charWidth(characterToShow);
int charDim = Math.max(maxAdvance, fontHeight);
//int halfCharDim = (int) (charDim / 2);
charImage = new BufferedImage(charDim, charDim,BufferedImage.TYPE_INT_ARGB);
charGraphics = charImage.createGraphics();
charGraphics.setColor(textColor);
charGraphics.setFont(textFont);
int rn = ran.nextInt((20 - 10) + 1) + 10;
x1=charDim/rn;
int rn1 = ran.nextInt((20 - 10) + 1) + 10;
y1=charDim/rn1;
int charX = (int) ((0.5 * charDim - 0.5 * charWidth)+x1);
int charY=(int) ((charDim - fontMetrics.getAscent()) / 2 + fontMetrics.getAscent()+y1);
charGraphics.drawString("" + characterToShow, charX, charY);
float x = horizMargin + spacePerChar * (i) - charDim / 2.0f;
int y = (int) ((height - charDim) / 2);
g.setColor(Color.GREEN);
g.drawImage(charImage, (int) (x+x1), (int)(y+y1), charDim,charDim, null,null);
charGraphics.dispose();
}

Math.ceil not rounding the value

double totalInches = d * 0.3937;
double feetPart = totalInches / 12;
int inchesPart = (int) Math.ceil(totalInches - (feetPart * 12));
return (feetPart) + "' " + inchesPart + "''";
I am getting a value 6.9999999 ' 0". I am returning a string, is it the reason why the decimals values in feet is not getting rounded off.
I tried without casting too. double inchesPart = Math.ceil(totalInches - (feetPart * 12));, but still i get the same result.
Surely you need:
int feetPart = (int)Math.floor(totalInches / 12);
or just:
int feetPart = (int)(totalInches / 12);
To get the two parts you can use
int totalInches = (int) (d * 0.3937);
int feetPart = totalInches / 12;
int feetInchPart = totalInches % 12;

Categories