JAVA - I am having trouble passing my variables (range and myRange) to other met
ID: 640010 • Letter: J
Question
JAVA -
I am having trouble passing my variables (range and myRange) to other methods in the same class. (This is not the whole code i just copied and pasted the methods that had to do with range and myRange)
public double setRange()
{
cannonCount++;
double myRange;
System.out.println("Enter the range from Cannon " + cannonCount);
Scanner in = new Scanner(System.in);
myRange=in.nextInt();
return myRange;
}
public double fire()
{
double initialVelocity = chargeSize * 10;
double range;
range = (Math.pow(initialVelocity, 2)/G)*Math.sin(2* launchAngle*(PI/180.0));
if ((ammo>=1)&&(powder>=5))
{
ammo--;
powder=powder-5;
System.out.println("Cannon " +cannonCount+ " Shots Fired! You now have :"+ ammo + " Cannon balls and " + powder + " of powder remaining!");
}
else if((ammo<1)||(powder<5))
{
System.out.println("You are OUT of AMMO and Powder!");
}
return range;
} public void cannonResults()
{
System.out.println("Nice Shot!!!" + range + "<-- Range iss");
myRange=myRange+50;
if(myRange==range)
{
System.out.println("Nice Shot!!!");
}
myRange = myRange-100;
if(myRange==range)
{
System.out.println("Nice Shot!!!");
}
else
{
System.out.println("YOU MISSED!!");
}
}
The Errors:
./Cannon.java:122: error: cannot find symbol
System.out.println("Nice Shot!!!" + range + "<-- Range iss");
^
symbol: variable range
location: class Cannon
./Cannon.java:123: error: cannot find symbol
myRange=myRange+50;
^
symbol: variable myRange
location: class Cannon
./Cannon.java:123: error: cannot find symbol
myRange=myRange+50;
^
symbol: variable myRange
location: class Cannon
./Cannon.java:124: error: cannot find symbol
if(myRange==range)
^
symbol: variable myRange
location: class Cannon
./Cannon.java:124: error: cannot find symbol
if(myRange==range)
^
symbol: variable range
location: class Cannon
./Cannon.java:128: error: cannot find symbol
myRange = myRange-100;
^
symbol: variable myRange
location: class Cannon
./Cannon.java:128: error: cannot find symbol
myRange = myRange-100;
^
symbol: variable myRange
location: class Cannon
./Cannon.java:129: error: cannot find symbol
if(myRange==range)
^
symbol: variable myRange
location: class Cannon
./Cannon.java:129: error: cannot find symbol
if(myRange==range)
^
symbol: variable range
location: class Cannon
9 errors
The WHOLE code:
import java.util.Scanner;
public class Cannon {
private final double PI = 3.1415926;
private final double G = 9.8; //gravity
private int launchAngle;
private double chargeSize;
private static int ammo;
private static double powder;
private int cannonCount=0;
public Cannon() {
this(45, 15.0);
}
public Cannon(int angle)
{
this(angle, 15.0);
}
public Cannon(int angle, double size)
{
if (angle > 90)
launchAngle = 90;
else if (angle < 0)
launchAngle = 0;
else
launchAngle = angle;
if (size > 20)
chargeSize = 20;
else if(size < 1)
chargeSize = 1;
else
chargeSize = size;
}
public double getPowder()
{
return powder;
}
public void setPowdernAmmo()
{
int myAmmo;
int myPowder;
System.out.println("Enter the number of Cannon Balls and Powder: ");
Scanner in = new Scanner(System.in);
myAmmo=in.nextInt();
myPowder=in.nextInt();
powder=myPowder;
ammo=myAmmo;
System.out.println("The amount of powder is now: " + myPowder + " and the number of Cannon Balls is: " + ammo);
System.out.println(" ");
System.out.println(" ");
}
public int getAmmo()
{
return ammo;
}
public double setRange()
{
cannonCount++;
double myRange;
System.out.println("Enter the range from Cannon " + cannonCount);
Scanner in = new Scanner(System.in);
myRange=in.nextInt();
return myRange;
}
public void anglenCharge()
{
int myAngle;
int myCharge;
System.out.println("Enter angle and charge for Cannon " + cannonCount + " :");
Scanner in = new Scanner(System.in);
myAngle=in.nextInt();
myCharge=in.nextInt();
}
public double fire()
{
double initialVelocity = chargeSize * 10;
double range;
range = (Math.pow(initialVelocity, 2)/G)*Math.sin(2* launchAngle*(PI/180.0));
if ((ammo>=1)&&(powder>=5))
{
ammo--;
powder=powder-5;
System.out.println("Cannon " +cannonCount+ " Shots Fired! You now have :"+ ammo + " Cannon balls and " + powder + " of powder remaining!");
}
else if((ammo<1)||(powder<5))
{
System.out.println("You are OUT of AMMO and Powder!");
}
return range;
}
public int getLaunchAngle()
{
return launchAngle;
}
public void setLaunchAngle(int angle)
{
if (angle > 90)
launchAngle = 90;
else if (angle < 0)
launchAngle = 0;
else
launchAngle = angle;
}
public void cannonResults()
{
System.out.println("Nice Shot!!!" + range + "<-- Range iss");
myRange=myRange+50;
if(myRange==range)
{
System.out.println("Nice Shot!!!");
}
myRange = myRange-100;
if(myRange==range)
{
System.out.println("Nice Shot!!!");
}
else
{
System.out.println("YOU MISSED!!");
}
}
public void setChargeSize(double size)
{
chargeSize = size;
}
public double getChargeSize()
{
return chargeSize;
Explanation / Answer
double myRange;
System.out.println("Enter the range from Cannon " + cannonCount);
Scanner in = new Scanner(System.in);
myRange=in.nextInt()
myrange=in.nextDouble();
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.