By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

Student programmer - need help :(

Steel546
P: 25
Let me begin, I am a college student in a basic programming class and I honestly have a tough time learning Java. I'm here because I have a lab I'm trying to do and it's like I have a hard time filling in the blanks because our teachers don't teach us, they just say "go". So, any offering help is much obliged.

My lab link is located http://www.cse.unt.edu/~ecelikel/spr...ments/lab4.doc. It's a .doc file. I'm supposed to create two files for a jet fighter simulation and I'm a little stuck as I'm not very experience with the scanner class or with doing some methods. Yes, I've read the Sun Tutorials, I have them open, just I don't understand sometimes. It's like I'm trying to read latin. But here's what I have so far. The asterisks are my name, just choose not to display.

Expand|Select|Wrap|Line Numbers
  1. package *****;
  2.  
  3.  
  4.  
  5. public class fighterJet {
  6.  
  7.     double setRange=0; // number of miles flown before running out of fuel
  8.     double distanceTraveled=0; // how far jet has flown since last refuel
  9.     double xOffset=0; // how far east, west using negative numbers
  10.     double yOffset=0; // how far north, south using negative numbers
  11.     int numMisslesRemaining=0; // number of missles remaining
  12.  
  13.     public fighterJet (double setRange, int setNumMissles) {
  14.  
  15.  
  16.  
  17.     }
  18.  
  19.     private boolean outOfFuel(){
  20. if (setRange == 0) {
  21.     boolean outOfFuel = true;
  22.     System.out.println("Oh no, you ran out of fuel!");}
  23. if (setRange != 0){
  24.  
  25.     }
  26.  
  27.  
  28.   }
  29.     public void fly(double distanceX, double distanceY){
  30.  
  31.         double x = distanceX - xOffset;
  32.         double y = distanceY - yOffset;
  33.  
  34.         double current = Math.sqrt((x*x)+(y*y));
  35.         System.out.println(current);
  36.  
  37.         distanceTraveled += current;
  38.  
  39.     }
  40. }
  41.  
  42.  
  43. package *****;
  44.  
  45. import java.util.Scanner;
  46.  
  47.  
  48. public class *****SecretMission {
  49.  
  50.  
  51.     public static void main(String[] args) {
  52.  
  53. // create scanner for input
  54.         Scanner input = new Scanner (System.in);
  55. //obtain user input
  56.         System.out.println("Enter your destination coordinates");
  57.         double 
  58.  
  59.  
  60.    }
  61.  
  62. }
  63.  
At "private boolean outOfFuel()" it says I'm missing a return value, and I can't figure out how to put that in. Other than that, I'm still working on it as you read this trying to add more and figuring out how to organize all of this. Thank you very much for any help at all.
Mar 10 '09 #1
Share this Question
Share on Google+
13 Replies


Steel546
P: 25
Actually... I don't even think I need a scanner. I think they just want to see it inside the program... hmm.
Mar 10 '09 #2

10K+
P: 13,264
You are getting the error because you declare the method as returning a boolean value and yet you don't return anything in that method. The compiler doesn't like that.

P.S Read the posting guidelines about how to use code tags and how not to post your content in bold for no apparent reason.
Mar 10 '09 #3

Steel546
P: 25
Well I posted in bold or else everything just kind of runs together, so I thought I'd make it obvious. I dunno, it just bothered me. Heh.
Mar 10 '09 #4

10K+
P: 13,264
Like I said, it's not allowed here so don't do it again.
Mar 10 '09 #5

Expert 10K+
P: 11,448
I fixed it: added code tags and removed the darn bold attributes; they hurt my eyes. btw, when you type "boolean myMethod() { ... }" you promise that your method will return a boolean value (true or false); when there's no return statement to be seen your compiler will complain.

kind regards,

Jos (moderator)
Mar 10 '09 #6

Steel546
P: 25
So, my lab says...

"Create a private method called outOfFuel of type boolean. It returns true if the distance the jet has traveled has exceeded its range. It returns false otherwise."

Expand|Select|Wrap|Line Numbers
  1. private boolean outOfFuel()
  2. {
  3. .
  4. .
  5. .
  6. }
  7.  
But if I type that "template" in, it says "missing return value"... I'm pretty sure my lab is messing with me. So creating a private boolean method...

Expand|Select|Wrap|Line Numbers
  1.   }
  2.     private boolean outOfFuel(){
  3.         return (true);
  4.  
  5.     }
... Unfortunately, I don't know how that helps my project. At least I don't have any errors. :/... and I keep going.
Mar 10 '09 #7

10K+
P: 13,264
Don't keep going! Stop and take some time to go through Sun's tutorial first. Understand the basic principles of what Java programming is all about before you start working on your lab.
Mar 10 '09 #8

Expert 10K+
P: 11,448
@Steel546
I'd expect a method like this then:

Expand|Select|Wrap|Line Numbers
  1. private boolean outOfFuel() {
  2.    return range >= distanceTraveled;
  3. }
  4.  
btw, nothing is messing with you; read the tutorials because you're having basic language problems.

kind regards,

Jos
Mar 10 '09 #9

Steel546
P: 25
It's alright, pretty sure I got this figured out now.

Expand|Select|Wrap|Line Numbers
  1. package *****;
  2.  
  3.  
  4.  
  5. public class fighterJet {
  6.  
  7.   private double range; // number of miles flown before running out of fuel
  8.   private double distanceTraveled; // how far jet has flown since last refuel
  9.   private double xOffset; // how far east, west using negative numbers
  10.   private double yOffset; // how far north, south using negative numbers
  11.   private int numMisslesRemaining; // number of missles remaining
  12.  
  13.  
  14.  
  15.     public fighterJet(double setRange, int setNumMissles) {
  16.  
  17.       range = setRange;
  18.       numMisslesRemaining = setNumMissles;
  19.  
  20.     }
  21.     private boolean outOfFuel(){
  22.  
  23.         if (distanceTraveled > range){
  24.             return true;
  25.         }else{
  26.             return false;
  27.         }
  28.  
  29.   }
  30.  
  31.     public void fly(double distanceX, double distanceY){
  32.  
  33.         double x = distanceX - xOffset;
  34.         double y = distanceY - yOffset;
  35.  
  36.         double current = Math.sqrt((x*x)+(y*y));
  37.         System.out.println(current);
  38.  
  39.         distanceTraveled = distanceTraveled + current;
  40.  
  41.         if (outOfFuel()){
  42.             System.out.println("You are out of fuel. Goodbye.");
  43.         }else{
  44.             System.out.println("You have reached your destination");
  45.         }
  46.  
  47.         xOffset = distanceX;
  48.         yOffset = distanceY;
  49.  
  50.  
  51.     }
  52.  
  53.     public void fireMissile(){
  54.  
  55.         if (numMisslesRemaining > 0){
  56.             System.out.println("Missle away!");
  57.             numMisslesRemaining -= 1;}
  58.             else
  59.                if (numMisslesRemaining == 0){
  60.                    System.out.println("You have no missles remaining");
  61.                }
  62.  
  63.         }
  64.  
  65.  
  66. }
  67.  
  68.  
  69. package *****;
  70.  
  71.  
  72. public class *****SecretMission {
  73.  
  74.  
  75.     public static void main(String[] args) {
  76.  
  77.     fighterJet striker1 = new fighterJet(100,2);
  78.     fighterJet striker2 = new fighterJet(20,1);
  79.  
  80.     striker1.fly(20, 10);
  81.     striker1.fireMissile();
  82.  
  83.  
  84.     striker1.fly(15, 8);
  85.     striker1.fireMissile();
  86.  
  87.     striker2.fly(10,15);
  88.     striker2.fireMissile();
  89.  
  90.     striker2.fly(20, 20);
  91.     striker2.fireMissile();
  92.  
  93.  
  94.  
  95.  
  96.     }
  97. }
In my fly method, when I set the xOffset/yOffset equal to the distanceX/distanceY, will that update that x and y values if it runs through again?
Mar 12 '09 #10

10K+
P: 13,264
What do you mean?
You have
double x = distanceX - xOffset;

If you change any of xOffset or distanceX then surely your x value will change.
Mar 12 '09 #11

Steel546
P: 25
For instance if I run the program twice so the jet will "run out of fuel". When I place xOffset = distanceX after running the math portion, the next time I run the program, it'll be the "new" distanceX subtracting the previous distance as a "point"... man, this is hard to describe.
Mar 12 '09 #12

10K+
P: 13,264
Well you could simply make the changes you are trying to describe and run it to find out for yourself.
Mar 12 '09 #13

Expert 10K+
P: 11,448
@Steel546
This is the core of your problem: if you find it hard to describe it'll be even harder to implement. Try to find an easier description first.

kind regards,

Jos
Mar 12 '09 #14

Post your reply

Sign in to post your reply or Sign up for a free account.