Results 1 to 3 of 3
  1. #1
    gwithey is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Question Comparing costs method

    I am doing a qestion in which i had to build a car class and create a series of simple functionality.

    However the method mostCostly is proving dificult.

    The desired functionality of this method is as follows:

    "The mostCostly method returns the greater cost of the two Cars by comparing the cost of itself with the cost of the Car passed in as a parameter."

    Just experiencing dificulties coparing the peramiter to itself. (presuming itself means (Class "Car"))

    My attempt at this is:

    Java Code:
    package assignment3;
    
    import java.awt.Color;
    
    public class Car {
    
        private String model;
        private Color color;
        private int cost;
    
        public Car(String model, Color color, int cost) {
            this.model = model;
            this.color = color;
            this.cost = cost;
        }
    
        public Car(String model) {
            this.model = model;
            cost = 10000;
            color = Color.black;
        }
    
        public String getModel() {
            return model;
        }
    
        public void setModel(String model) {
            this.model = model;
        }
    
        public Color getColor() {
            return color;
        }
    
        public void setColor(Color color) {
            this.color = color;
        }
    
        public int getCost() {
            return cost;
        }
    
        public void setCost(int cost) {
            this.cost = cost;
        }
    
        public int mostCostly(Car car) {
    
            int perameterCar = car.getCost();
            int largest1 = perameterCar - cost;
            int largest2 = cost - perameterCar;
            car.equals(car);
    
            return largest;
        }
    }
    i know the methods completely wrong and doesnt do anything but does anyone know how i would go about creating this method.

    Thanx George
    -Long time no c-
    -:eek:-

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Java Code:
    if (cost > car.cost)
    maybe?

  3. #3
    gwithey is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Default

    Thanx for the help just couldn't get my head round the class relationship side as im new to this.

    Java Code:
    public int mostCostly(Car car) {
    
            int largest = 0;
            if (cost > car.cost) {
                largest = cost;
            } else {
                largest = car.cost;
            }
    
            return largest;
        }
    I think this is the right idea now to return the higher value.
    -Long time no c-
    -:eek:-

Similar Threads

  1. Comparing two images
    By GhosT in forum Advanced Java
    Replies: 13
    Last Post: 04-25-2009, 12:37 AM
  2. Comparing Images
    By shaungoater in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 10:38 AM
  3. comparing
    By Feng in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 09:40 AM
  4. Comparing JavaWebFrameworks
    By pegitha in forum Web Frameworks
    Replies: 1
    Last Post: 05-18-2007, 06:23 PM

Posting Permissions

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