CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2005
    Posts
    4

    '+' operator problem

    Hi all,

    I have to classes, class Distances and a main class TestDistances. In test distances I like to calculate the rate , so I called the method milesperhour. and I am getting an error at line 1. Can anybody please tell me why am I worng.

    Error : operator + cannot be applied to distances.Distances,distances.Distances



    package distances;

    public class Distances
    {
    static final int FEET_IN_YARDS = 3;
    static final int YARDS_IN_MILES = 1760;

    private static int feet;
    private static long yards;
    private static int miles;

    public Distances()
    {
    feet = 0;
    yards = 0;
    miles = 0;
    }
    public Distances(int initfeet, long inityards,int initmiles)
    {
    feet = initfeet;
    yards = inityards;
    miles = initmiles;
    }
    public float getFeet ()
    {
    return feet;
    }
    public float getYards ()
    {
    return yards;
    }
    public float getMiles ()
    {
    return miles;
    }

    public Distances addDistance(Distances two)
    {
    Distances add = new Distances();
    add.feet = feet + two.feet;
    add.yards = yards + two.yards;
    add.miles = miles + two.miles;
    return add;
    }
    public static Distances convertYards(long Yards)
    .
    {
    Distances testyards = new Distances();
    testyards.yards = Yards/3;
    testyards.feet = (int) (testyards.yards%3);//1 yard = 3 feet;
    testyards.miles = (int) (Yards/1760);// 1 mile =1760 yards.
    return testyards ;
    }

    public static Distances convertFeet(long Feets)
    {
    Distances testfeet = new Distances();
    testfeet.miles = (int) (Feets/5280); // 1 feet= 5280 miles
    testfeet.feet = (int) (Feets%5280);
    yards = feet/3; //1 feet = 3 yards;
    feet = feet%3;
    return testfeet;
    }

    public void printDistance()
    {
    System.out.println("Converting Feet to type Distances:" +Distances.convertFeet(6002).toString());
    System.out.println("Converting yards to type Distances:" +Distances.convertYards(5230).toString());
    }
    public String toString()
    {
    String actualDistance = miles+" miles, "+yards+" yards, "+feet+" feet";
    return actualDistance;
    }
    }


    -----------------------------------------------------------------------------------------------

    import distances.Distances;

    public class TestDistances
    {
    public static void main(String[] args)
    {
    Distances distance1;
    Distances distance2;
    Distances distance3;
    distance1 = new Distances(20,30,40);
    distance2 = new Distances(10,30,50);
    distance3 = distance1.addDistance(distance2);
    System.out.println(distance3.getFeet() +"feet"
    +distance3.getYards() + "yards" +distance3.getMiles()+"miles");
    distance3.printDistance();
    }
    public float milesPerHour(Distances distance,long time)
    {
    float rate;
    distance = Distances.convertFeet(15000) + Distances.convertYards(15000)+37;-----------------------------line (1)
    time = 45;
    return rate;

    }
    }


    Actual Question is : Write a helper mthd in TestDistances tht takes a varialbe of class Distances and a time(in minutes) and calculates miles per hour. Convert the variable of class Distances to miles(a float variable) before performing the calculation.The result should be float value representing miles per hour rather than a value of Distances.

  2. #2
    Join Date
    Dec 2003
    Posts
    593

    Re: '+' operator problem

    The methods convertFeet() and convertYards() both return a distance object. The plus operator can only be applied in specific instances (such as for primitives or for String objects), and it can't be applied to your Distance objects. You have to alter your code so instead of adding Distance objects you are instead adding the primitive values that they contain.

  3. #3
    Join Date
    Mar 2005
    Posts
    4

    Re: '+' operator problem

    Can you please tell me how to add the distance objects. Help me out with the code. If possible please explain me with an example.
    Thanks in advance.

  4. #4
    Join Date
    Dec 2003
    Posts
    593

    Re: '+' operator problem

    If you want to add two Distance objects together you're going to have to write a method in the class Distance to do this. for example...

    Code:
    public Distance add(Distance d)
    {
        Distance result;
        //do your adding here, that bit is up to you. Store answer in result.
    
        return result;
    }
    After looking at the method you had a problem with I can see another problem.

    Code:
    public float milesPerHour(Distances distance,long time)
    {
    float rate;
    distance = Distances.convertFeet(15000) + Distances.convertYards(15000)+37;-----------------------------line (1)
    time = 45;
    return rate;
    
    }
    If you take a look at rate, it is always going to be null. You never initialise it at all. I'm assuming you're meant to do some kind of calculation and store it in rate?

    Also you have a parameter called time that you never do anything with. You simply change it's value to be 45, so it will be 45 every time the method is called. Why would you want to do that? Surely you want to use the value of time that is passed into the method?

    After reading the problem that you're meant to be solving, i'm not sure why you're using the methods convertFeet and convertYards at all. You just need to extract the distance from the Distance object passed as a parameter, and then work out the speed.
    Last edited by mikeBarr81; April 2nd, 2005 at 04:19 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured