• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Abstract Classes and Comparable<T>  RSS feed

 
Jordan Richt
Greenhorn
Posts: 2
C++ Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone! My first post I am working on an assignment for an on-line AP Computer Science Course. The instructions are attached. Basically I am putting a lot of what I learned together.

Here is my code so far:

Vehicle Class


Product Class



Car Class



Truck Class



Now I am hung up on the instructions to "Create class Tool that implements Product and Comparable<T>. "

Here is what I have for the tool Class so far:



Should it be an abstract class? Do I need to restate the variable names and reference in the constructor? Right now it gives the error: "Cannot find symbol-Variable cost" when I try to compile the tool class. Could anyone help me a little? Thank you for your time!
a.png
[Thumbnail for a.png]
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Should it be abstract class

You should know the consequences of your design choices. Don't just do something without knowing why you're doing it.

So, let me ask you this instead: What reasons would you have to make it an abstract class? What reasons would you have to make it a concrete (opposite of abstract) class?

As for the compile-time error, look carefully at how Product is defined. It's an interface. On line 10 in the tool class, you declare the obj parameter as being a Product. Then on line 12 and line 14, you refer to obj.cost. Does Product have a cost? If you answer "Yes", are you sure? Look at your code for Product again.

Additional note: Follow naming conventions. By convention, class names are capitalized. Your tool class does not follow this convention and will make your code a little confusing to read.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, note that this is not a very realistic exercise. To have Car and Truck extend Vehicle like that is not something you should probably do in a real-world program. The exercise simply demonstrates the mechanics of inheritance but it's NOT a very good example of proper object-oriented programming and design because it does not give you a problem or context for which you would arrive at such a design.

I understand that it's a typical example because most people can relate their experience with real-world objects to the relationships that software entities defined this way would have but without a problem to solve to provide context, it's a very flawed way to teach object-oriented programming, IMO, and only leads to programmers who have a misguided "understanding" of OOP.

Keep the following in the back of your mind for later on, when you've sort of understood the mechanics of inheritance, encapsulation, and polymorphism:

Object-Oriented Programming and Design is about organizing behaviors, responsibilities, relationships, and data. Of these, the most important thing about an object is its Behavior. That is, what it's capable of doing. The next important thing about an object is its Relationship with other objects. Objects seldom exist in a vacuum. They often have to interact with other objects to get a bigger task done. It's the programmer's job to figure out how to break down a big problem into small, focused tasks and assign the responsibility of performing each small task to different objects, and then write the code to orchestrate the work of a collection of objects. That is really what OO programming and design is about. Lastly, I find that if I focus on behavior, relationships, and responsibilities first, the data/information that is involved in completing a task, while still important, usually logically falls into the class that uses it the most.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another flaw in this exercise:
5. Create class Tool that implements Product and Comparable<T>
...
c. Add a compareTo() method that compares tools based upon cost.

If you take these instructions literally, this would be what the Tool class would look like:

But this probably would not be right because then you could write code that will attempt to compare a Tool with a Vehicle. Logically, you'd only want to be able to compare a Tool with another Tool, so really, you should have this:

The instructions would have been better stated as:
5. Create class Tool that implements Product and Comparable
...
c. Use generics with the Comparable interface such that you could only compare a Tool object with another Tool object, based on the cost.


 
Jordan Richt
Greenhorn
Posts: 2
C++ Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the late reply. Thank you for your insight. So I thought about it and decided tool shouldn't be abstract and here is my new code(if its not shown I didn't change it.)

Vehicle Class



Product Class



Car Class



Truck Class



Tool Class


InventoryDemo Class



I fixed the capitalization and the Product/obj issue. None of my code is throwing errors anymore either. But now I am working on the class that tests the code called InventoryTester. I completed step six(rest of instructions are below, see first post for the first half) and the code runs successfully and prints out the name a colon and then the price. I also got 6c working. But part b in six is where I am having issues.

I guess I have to loop through the array and find each item that has the name I am looking for, and then using the index of that item calculate quantity and total cost? In a simple array I would know what to do but since its an array of instances I don't know how to access the specific data. And if for example I was passed Jigsaw and there were two jigsaw items I would have to look for both of them somehow so I could know the quantity?
b.png
[Thumbnail for b.png]
c.png
[Thumbnail for c.png]
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read instruction 6a again. Line 8 of your InventoryDemo class violates that instruction. Prefer using the interface type for the declared type rather than the concrete type.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you do know how to access the object properties. It's what you're doing on lines 21 & 22 in your InventoryDemo class. Except this time, you accumulate the values into another variable rather than display it like you're doing on lines 21 & 22.

If you don't know what an accumulator variable is, search for that term in your textbook, notes, or on the Internet. It's really just a fancy name for "total"
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!