How do you turn a psuedocode into an appropriate code for java to understand?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • k3lvynn
    New Member
    • Feb 2014
    • 2

    How do you turn a psuedocode into an appropriate code for java to understand?

    i do not understand how to return a range method from this code i made. i have the psuedocode for it, but i do not know how to turn it into a legit code. Also, can someone double check my other methods as well?

    Code:
    /**
     * Class to determine Car's range of miles. 
     * 
     * @author Kelvynn Cayanan 
     * @version 2/2/2014
     */
    public class Car
    {
        // instance variables - 
        private double miles;
        private double gallons;
        private double gas;
    
        /**
         * Constructor for objects of class Car
         */
        public Car()
        {
            
        }
        public Car(double initialGas)
        {
            gas = initialGas;
        }
        
        public void addGas(double gas)
        {
            // Increases amount of gas in gas tank.
            gallons = gallons + gas;
        }
        
        public void drive(double drive)
        {
            // Decreases amount of gas in gas tank.
            double newdrive = (drive/miles) - gas;
            drive = newdrive;
        }
        
        public double range(double range)
        {
            //**calculates range, the number of miles the car can travel until the gas tank is empty */
            double newrange = miles * gas;
            range = newrange;
            return range;
        }
    }
    
    
    here is a class that i am supposed to implement with the class i made above.
    
    /**
     * Uses Cars.
     * 
     * @author Anthony W. Smith 
     * @version 6/15/2009
     */
    public class CarUser
    {
        /**
         * Constructor for objects of class CarUser
         */
        public CarUser()
        {
            Car honda = new Car(30.0);      // 30 miles per gallon
            
            honda.addGas(9.0);              // add 9 more gallons
            honda.drive(210.0);             // drive 210 miles
            
            // print range remaining
            System.out.println("Honda range remaining: " + honda.range());
    
            Car toyota = new Car(26.0);      // 26 miles per gallon
            
            toyota.addGas(4.5);              // add 4.5 more gallons
            toyota.drive(150.0);             // drive 150 miles
            
            // print range remaining
            System.out.println("Toyota range remaining: " + toyota.range());
        }
    }
    Last edited by Rabbit; Feb 3 '14, 05:16 AM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
  • k3lvynn
    New Member
    • Feb 2014
    • 2

    #2
    my first class comes out clean with no syntax errors, but when i try to compile the second class, it says
    " method range in class Car cannot be applied to given types;
    required: double; found: no argument: reason: actual and formal argument lists differ in length

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Hi k3lvynn and welcome to bytes.com!

      In your Car class you have the following function:
      Code:
      public double range(double range)
      {
          //**calculates range, the number of miles the car can travel until the gas tank is empty */
          double newrange = miles * gas;
          range = newrange;
          return range;
      }
      and in your CarUser class you try to call it like this:
      Code:
      // print range remaining
      System.out.println("Honda range remaining: " + honda.range());
      So, you're trying to call the function without arguments while you defined it with an argument. From what your function definition looks like you don't really need a value to be passed though as you never read it; so you probably want to change your function definition to this:
      Code:
      public double range()
      {
          //**calculates range, the number of miles the car can travel until the gas tank is empty */
          double newrange = miles * gas;
          double range = newrange;
          return range;
      }
      or alternatively (and shorter) this:
      Code:
      public double range()
      {
          //**calculates range, the number of miles the car can travel until the gas tank is empty */
          double newrange = miles * gas;
          return newrange;
      }
      or even this:
      Code:
      public double range()
      {
          //**calculates range, the number of miles the car can travel until the gas tank is empty */
          return miles * gas;
      }
      If your function signature (public double range(double) in this case) has values between the braces that means you'll be handing input into the function; something you don't have to here. Also, you couldn't change the value of range within the function as Java has a mixture of pass-by-reference and pass-by-value which stops you from doing stuff like that.

      Comment

      Working...