Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Exercise 137

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Exercise 137

    Alright, so as always, this is for school. Here is my exercise:

     
    Exercise 137
     
        Add a customized equals method to the Car class, defined below, so that it returns true if and only if the model identification of the argument is the same as that of the Car object whose instance method is being called.
        public class Car
        {
          private String myModel,
                         myColor;
     
          public Car( String color, String model )
          {
            myColor = color;
            myModel = model;
          }
     
          public String getModel()
          { 
            return myModel;
          }

    // add your equals method here

    This is where my code would go.

     
        }
     
        public class MainClass
        {
          public static void main( String[] args )
          {

    My code would also go here.

          }
        }
     
        What happens when your new equals method is applied to an argument that is an instance of Object or one of its subclasses, but not a Car object? Suggest a way to overcome this problem.

    Alright, I feel like a jerk for posting this previously so I'm sorry, but basically, here's my question. I'm pretty sure it would return true or false. So, if it does, am I supposed to take the instance of one class and sort of poly-morph it to make it fit the instance of another class or am I just supposed to make a new class all together.

    Thx, ghostheadx
    Last edited by ghostheadx; February 11th, 2014 at 08:52 PM. Reason: fixing it to make it forum level appriate and more polite, since I feel like a jerk for posting it the unedited way


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,483
    My Mood
    Hungover
    Thanks
    144
    Thanked 643 Times in 548 Posts

    Default Re: Exercise 137

    Did you have a question?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,848
    My Mood
    Mellow
    Thanks
    239
    Thanked 721 Times in 707 Posts

    Default Re: Exercise 137

    C'mon ghostheadx, you know better than to post a thread like this. Did you fall asleep and accidentally push the "Post" button?

  4. #4
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 137

    Yes. It was like I had to sleep in because I hadn't gotten enough sleep the night before. I'll edit it to make it a little more forum appropriate, ASAP.

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 137

    I should have gotten coffee or something. It's my fault. I take full responsibility for it.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,483
    My Mood
    Hungover
    Thanks
    144
    Thanked 643 Times in 548 Posts

    Default Re: Exercise 137

    Quote Originally Posted by ghostheadx View Post
    Alright, I feel like a jerk for posting this previously so I'm sorry, but basically, here's my question. I'm pretty sure it would return true or false. So, if it does, am I supposed to take the instance of one class and sort of poly-morph it to make it fit the instance of another class or am I just supposed to make a new class all together.
    It sounds like you're just supposed to add a single method (actually it sounds like the method is already there, you just have to fill in the code). The method takes a car instance as a parameter, and you have to return whether the parameter car's model is the same as the car's model for the instance you're "in" when the code is being run.

    Car one = new Car("blue", "Nissan");
    Car two = new Car("red", "Nissan");
    Car three = new Car("black", "Monster Truck");
     
    System.out.println(one.equals(two)); //prints true
    System.out.println(one.equals(three)); //prints false

    It's your job to define that equals method.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 137

    I get this error:

    "error <identifier> expected"

    --- Update ---

    When I try typing in your code.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,239
    Thanks
    52
    Thanked 2,297 Times in 2,268 Posts

    Default Re: Exercise 137

    Please copy full text of the compiler's error message and paste it here.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 137

    It's okay. I solved it. I'm posting the next things I need help with.

Similar Threads

  1. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 09:18 PM
  2. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 07:42 PM
  3. Exercise 86
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 7th, 2013, 10:31 PM
  4. Exercise
    By keepStriving in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 21st, 2013, 06:58 PM
  5. Help with exercise.
    By javapol in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 8th, 2013, 08:40 PM

Tags for this Thread