Java - Object - Field and Method example - Basics [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am learning the very basics of Java and I decided to expand on an example a little further in order to understand it better.
In this instance, the object in question is a car. Let's say 'example car 1'.
The focus for my analogy is the engine temperature.
Firstly, I would view this as a field (state).
I also have a warning light for the engine temperature. Again, I would view this as a field.
Thinking about how they would interact I see it like this.
Engine temperature > 90°c ----> Warning Light -----> ON
To me, the method would be the switching on of the warning light.
Assuming I have been correct with the analogy so far, would there also be a method that would be the reference from the engine temp to the warning light?
so: If engine temp = > 90°c then refer to warning light <---- Is this a method of the engine temp?
Many thanks in advance!

Usually you would represent engine "temperature" as a field (as you said) and provide a getEngineTemperature and setEngineTemperature method. You can provide another method such as hasEngineTemperatureWarning to represent whether the temperature is in the dangerous range. Ultimately, however, these distinctions are up to you, the programmer, and there is no "always right" approach. But I think this is a good starting point.

Related

Java call performance vs search performance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Currently my program is filled with many ugly references that often make field or method access look like this: weakReference1.get().weakReference2.get().field1.getSomeCustomObject().field2. I want to move to shorter and faster strong references like field1.field2. But my program design is such that I will also have to go for an ArrayList element-by-element search (in a for-loop) instead of accessing a WeakHashMap by get() method.
Thus, I'd like to understand, can moving to simpler references compensate for rejecting HashMap performance wise. Herewith I presume that WeakHashMap.get() is much faster than a loop-search of ArrayList.
Can someone, please, give me a rough estimate? Or maybe there's even an appropriate comparison table like this one. I'd appreciate that.
Thank you.
Currently my program is filled with many ugly references that often make field or method access look like this:
weakReference1.get().weakReference2.get().field1.getSomeCustomObject().field2
Given that the objects involved are not Data Transfer Objects
this is a violation of the law of Demeter aka Don't talk to Strangers / Tell, don't ask!
Following this LoD principle you should move the operations working with the data in field2 to a new method in the class SomeCustomObject.

Java: Is creating a "System" class a bad thing? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I recently started a project in Java, that contains a class called System. This class (Luckily) contains methods for output management, so in the rare cases where I need to use the System. methods (Or the System object in general) I just reference it as java.lang.System.. I believe that this could be looked down upon, as System could be looked at as a reserved name. I currently am in the beginning stages of this program, and could change it accordingly quickly, as there are little calls to the class itself.
While it's not illegal, you don't want to do this. If I were the next person working on your code, the first thing I would do is try to remove "java.lang" from "java.lang.System" and then get miffed when it wouldn't compile.
The idea is to go toward brevity and only write what you need to write, while making sense of it all for the next person. It's more an art than a science.
You could always name it something like ProjectnamehereSystem or OutputManager or something to that effect.
I would not create something so similarly named as an important class. While everything is easy to edit, you may be able to keep up with all the changes you are making.
But when the project evolves things will get messy and complex. I would suggest naming it something else that can be easily distinguished.

How does Casting limit Algorithm Reuse in java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
We are learning about both of these things in Java class right now. I believe I understand the basic aspects of both, but not sure about how Casting ends up limiting Algorithm Reuse. Our teacher said we need to know this for the test next week. Can anyone explain this?
If you cast you are limiting your algorithm to only work with one Class (or it's children). If you were instead to use an Interface you would be able to accept a greater variety of Objects that themselves implement that Interface. Much more flexible.
Here is a related SO question: Explaining Interfaces to Students
When you use casting in your code, you must know the exact type that you cast to (during the code write phase). Hence your code can't be reused in the future with different types. Always remember to program to interface instead of to specific type.

Linked Lists - Efficient usage? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So I've been giving a project for school and was just hoping for reassurance towards the decision making I do.
My java skills are extremely basic and need to improve drastically soon! I just seem to be getting nowhere atm :P
Anyway, back on topic.
My first task is to create an interface directory that can do the following
Keep in mind this is assessed work therefore please don't provide answers. I have enough time to complete this since I've started early!
My initial approach for going about this task is using a Linked List. I don't know what you guys think about that? I may be completely wrong but based on the topics we have covered in School. LinkedList definitely seems suitable. I can add, get and remove.
Cheers for reading guys!
You are along the right lines. The java.util.Collections package will contain most of what you need.
I would actually use an ArrayList rather than a LinkedList as it is faster for random access and sorting.
However note that it says you should be able to find people efficiently and look them up by name.
That suggests using something like a TreeMap structure, mapping name to a class containing information on each person. Store the names as "Surname, Forename" and they will be sorted correctly.
That will only allow lookups based on the complete and correct name though. If you want to search for partial names the map is less useful.

what wrong with non object oriented approach to introduce object oriented approach [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
i have been reading about this topic , and the more i read the more confused i get ,
can somebody please elaborate , we were using language C which follows structural approach ,
so what was wrong with this approach , that we moved to create a object oriented language JAVA .
I have been reading so many theoretical aspects , can some body please give more of a few practical illustrations ,
WHY WE NEEDED OBJECT ORIENTED APPROACH IN THE FIRST PLACE
I am not looking for an answer to be given in any interview or tutorial
I am looking for an answer to get the better understanding/practical importance of object oriented aproach
There are many explanations regarding this. But I would like to refer this
Modularity: The source code for a class can be written and maintained independently of the source code for other classes. Once
created, an object can be easily passed around inside the system.
Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from
the outside world.
Code re-use: If a class already exists, you can use objects from that class in your program. This allows programmers to
implement/test/debug complex, task-specific objects, which you can
then use in your own code.
Easy Debugging: If a particular object turns out to be a problem, you can simply remove it from your application and plug in a
different object as its replacement. This is analogous to fixing
mechanical problems in the real world. If a bolt breaks, you replace
it, not the entire machine.

Categories

Resources