Student programmer - need help :(

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Steel546
    New Member
    • Mar 2009
    • 25

    Student programmer - need help :(

    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.

    Code:
    package *****;
    
    
    
    public class fighterJet {
    
        double setRange=0; // number of miles flown before running out of fuel
        double distanceTraveled=0; // how far jet has flown since last refuel
        double xOffset=0; // how far east, west using negative numbers
        double yOffset=0; // how far north, south using negative numbers
        int numMisslesRemaining=0; // number of missles remaining
    
        public fighterJet (double setRange, int setNumMissles) {
    
    
    
        }
    
        private boolean outOfFuel(){
    if (setRange == 0) {
        boolean outOfFuel = true;
        System.out.println("Oh no, you ran out of fuel!");}
    if (setRange != 0){
    
        }
    
    
      }
        public void fly(double distanceX, double distanceY){
            
            double x = distanceX - xOffset;
            double y = distanceY - yOffset;
    
            double current = Math.sqrt((x*x)+(y*y));
            System.out.println(current);
    
            distanceTraveled += current;
    
        }
    }
    
    
    package *****;
    
    import java.util.Scanner;
    
    
    public class *****SecretMission {
    
    
        public static void main(String[] args) {
    
    // create scanner for input
            Scanner input = new Scanner (System.in);
    //obtain user input
            System.out.println("Enter your destination coordinates");
            double 
    
    
       }
    
    }
    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.
    Last edited by JosAH; Mar 10 '09, 06:58 AM. Reason: fixed the [code] ... [/code] tags and removed the bold attribute
  • Steel546
    New Member
    • Mar 2009
    • 25

    #2
    Actually... I don't even think I need a scanner. I think they just want to see it inside the program... hmm.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      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.

      Comment

      • Steel546
        New Member
        • Mar 2009
        • 25

        #4
        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.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Like I said, it's not allowed here so don't do it again.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            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)

            Comment

            • Steel546
              New Member
              • Mar 2009
              • 25

              #7
              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."

              Code:
              private boolean outOfFuel()
              {
              .
              .
              .
              }
              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...

              Code:
                }
                  private boolean outOfFuel(){
                      return (true);
                      
                  }
              ... Unfortunately, I don't know how that helps my project. At least I don't have any errors. :/... and I keep going.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                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.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by Steel546
                  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."
                  I'd expect a method like this then:

                  Code:
                  private boolean outOfFuel() {
                     return range >= distanceTraveled;
                  }
                  btw, nothing is messing with you; read the tutorials because you're having basic language problems.

                  kind regards,

                  Jos

                  Comment

                  • Steel546
                    New Member
                    • Mar 2009
                    • 25

                    #10
                    It's alright, pretty sure I got this figured out now.

                    Code:
                    package *****;
                    
                    
                    
                    public class fighterJet {
                    
                      private double range; // number of miles flown before running out of fuel
                      private double distanceTraveled; // how far jet has flown since last refuel
                      private double xOffset; // how far east, west using negative numbers
                      private double yOffset; // how far north, south using negative numbers
                      private int numMisslesRemaining; // number of missles remaining
                    
                    
                    
                        public fighterJet(double setRange, int setNumMissles) {
                    
                          range = setRange;
                          numMisslesRemaining = setNumMissles;
                    
                        }
                        private boolean outOfFuel(){
                    
                            if (distanceTraveled > range){
                                return true;
                            }else{
                                return false;
                            }
                    
                      }
                    
                        public void fly(double distanceX, double distanceY){
                    
                            double x = distanceX - xOffset;
                            double y = distanceY - yOffset;
                    
                            double current = Math.sqrt((x*x)+(y*y));
                            System.out.println(current);
                    
                            distanceTraveled = distanceTraveled + current;
                    
                            if (outOfFuel()){
                                System.out.println("You are out of fuel. Goodbye.");
                            }else{
                                System.out.println("You have reached your destination");
                            }
                    
                            xOffset = distanceX;
                            yOffset = distanceY;
                     
                    
                        }
                    
                        public void fireMissile(){
                    
                            if (numMisslesRemaining > 0){
                                System.out.println("Missle away!");
                                numMisslesRemaining -= 1;}
                                else
                                   if (numMisslesRemaining == 0){
                                       System.out.println("You have no missles remaining");
                                   }
                    
                            }
                    
                        
                    }
                    
                    
                    package *****;
                    
                    
                    public class *****SecretMission {
                    
                    
                        public static void main(String[] args) {
                    
                        fighterJet striker1 = new fighterJet(100,2);
                        fighterJet striker2 = new fighterJet(20,1);
                    
                        striker1.fly(20, 10);
                        striker1.fireMissile();
                    
                     
                        striker1.fly(15, 8);
                        striker1.fireMissile();
                    
                        striker2.fly(10,15);
                        striker2.fireMissile();
                    
                        striker2.fly(20, 20);
                        striker2.fireMissile();
                    
                        
                    
                    
                        }
                    }
                    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?

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      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.

                      Comment

                      • Steel546
                        New Member
                        • Mar 2009
                        • 25

                        #12
                        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.

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Well you could simply make the changes you are trying to describe and run it to find out for yourself.

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by Steel546
                            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.
                            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

                            Comment

                            Working...