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 10 of 10

Thread: What do I do?

  1. #1
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default What do I do?

    15.05 Assignment Instructions
    1. Create a folder called 15.05 Assignment in your module 15 assignments folder.
    2. Create an interface named Product .
    a. Add a method called getName() that returns a string.
    b. Add a method called getCost() that returns a double.
    3. Create abstract class Vehicle that implements Product.
    a. It should have string variable name and double cost, that are initialized in the constructor.
    b. Add appropriate getName() and getCost() methods
    4. Create classes Car and Truck that extend Vehicle.
    a. No other methods are needed.
    5. Create class Tool that implements Product and Comparable<T> .
    a. It should have string variable name and double cost that are initialized in the constructor.
    b. Add appropriate getName() and getCost() methods.
    c. Add a compareTo() method that compares tools based upon cost .
    6. Create class InventoryDemo.
    a. Test your classes by using ArrayList products of following products (Remember to declare it
    properly using List):
    Name Cost
    Jaguar 1000000.00
    Neon 17000.00
    JigSaw 149.18
    Jaguar 110000.00
    Neon 17500.00
    Neon 17875.32
    RAM 35700.00
    CircularSaw 200.00
    CircularSaw 150.00

    b. Create a static method takeInventory that, when passed the name of a product, will go
    through the list and print out <item name>: Quantity = <quantity>, Total cost = <totalcost>.
    <item name> is the name of the product, <quantity> and <totalcost> are the values you
    calculate by going through the list for the product with name that was passed to takeInventory.
    This is what I have.
    public interface Product
    {
      String getName();
      double getCost();
    }
    public abstract class Vehicle implements Product
    {
      private String name;
      private int cost;
      public Vehicle()
      {
        name = "";
        cost = 0;
      }
      public abstract String getName();
      public abstract double getCost();
    }
     public class Car extends Vehicle 
    {
       public Car()
       {
         super();
       }
     
    }
    public class Truck extends Vehicle
    {
      public Truck()
      {
        super();  
      }
     
    }
    The latter two throw errors and I know why, but how can I fix probleems using guidelines given, and dont I need setters for the vehicle class.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: What do I do?

    throw errors
    Please copy the full text and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: What do I do?

    Simply: Truck and car arent abstract and dont overide the abstract methods

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: What do I do?

    If that is part of the error message, do you have any questions about what it is saying?
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    llowe29 (February 26th, 2014)

  6. #5
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: What do I do?

    Quote Originally Posted by llowe29 View Post
    3. Create abstract class Vehicle that implements Product.
    a. It should have string variable name and double cost, that are initialized in the constructor.
    b. Add appropriate getName() and getCost() methods
    4. Create classes Car and Truck that extend Vehicle.
    a. No other methods are needed.
    [...]
    public interface Product
    {
      String getName();
      double getCost();
    }
    public abstract class Vehicle implements Product
    {
      private String name;
      private int cost;
      public Vehicle()
      {
        name = "";
        cost = 0;
      }
      public abstract String getName();
      public abstract double getCost();
    }
     public class Car extends Vehicle 
    {
       public Car()
       {
         super();
       }
     
    }
    Step (3b) states you need to add the getName() and getCost() methods. In addition, Step (4a) states no other methods are needed for the Car and Truck concrete classes.

    I interpret this as you need to implement the 2 methods in your abstract Vehicle class. If not, you'll need to implement these methods in your Car and Truck concrete classes but step (4a) suggests that you're not to do so.

    There are 2 ways to set field values in a class: (1) via setters, and (2) via the constructor. Your Car and Truck can each have a constructor that accepts 'name' and 'cost' parameters, which in turn sets the corresponding field values within the constructor. To do so you'll need to change the 'name' and 'cost' fields' access modifier in the Vehicle abstract class to either default (blank) or protected so that they can be inherited by the Car and Truck classes.

    Hth!

  7. The Following User Says Thank You to jashburn For This Useful Post:

    llowe29 (February 26th, 2014)

  8. #6
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: What do I do?

     public class Car extends Vehicle
    {
       public Car(String name, int cost)
       {
          super(name, cost);
       }
     
    }
    i changed my car but confused about what to do with veichle.
    Here is it.
    public abstract class Vehicle implements Product
    {
      private String name;
      private int cost;
      public Vehicle(String name, int cost)
      {
        this.name = name;
        this.cost = cost;
      }
      public abstract String getName();
      public abstract double getCost();
     
    }

  9. #7
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: What do I do?

    Implement the getName() and getCost() methods in Vehicle.

    Btw, note the bug where getCost() returns a double, whereas the cost field is an int...

  10. The Following User Says Thank You to jashburn For This Useful Post:

    llowe29 (February 26th, 2014)

  11. #8
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: What do I do?

    by implement you mean dont writte as abstract methods.

  12. #9
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: What do I do?

    Yup, that's how I interpret step (3b), i.e., write "concrete" rather than abstract methods. The Vehicle class itself can remain abstract so that it cannot be instantiated directly.

  13. The Following User Says Thank You to jashburn For This Useful Post:

    llowe29 (February 26th, 2014)

  14. #10
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: What do I do?

    and then all id have to do is call the super methods through the car and truck construstors respectively, which works as Ive already fixed the problem with double/int congruence, thanks.