Object-oriented concepts in practice in Java

--Originally published at Alan TC201

Here is a sum of all that I have published, the Journey for the Java programming its just starting, I am still learning every concept.

And to entretaing you, let’s see some hype.

  • Object-oriented concepts in practice in Java

 


Object-oriented concepts in practice in Java

Masteir: mechanics for class description

--Originally published at Alan TC201

Masteir: mechanics for class description

 

 

 

 

 

 

 

 

Java Program:  a set of instructions for the computer to follow.  The source code.

Data:  data for the Java program

Java Compiler:  translates the program from Java to a language that the computer can understand.  Compilers differ by make of machine and operating systems.

Byte Code Program:  the compiler translates Java into a language called byte-code.  Byte code is the machine language for a hypothetical computer called the Java Virtual Machine.

Byte Code Interpreter:  the interpreter translates each instruction of byte code into instructions that the computer in use can execute.

Machine Language:  is the language the computer in use understands.

Info (http://gee.cs.oswego.edu/dl/cpj/mechanics.html)

 

Masteir: mechanics for class description

 

 


Masteir: mechanics for class description

Masterie ”has-a” and “is-a” .

--Originally published at Alan TC201

One of cool thing you can do in Object-Oriented programming language is to reuse a code. There are two ways we can do code reuse, by implementation of inheritance or object composition. But you need to be careful how and what method you will use depending if it IS-A relationship or HAS-A relationship.

Masterie ”has-a” and “is-a” .

This image is very clear, but if you still can get the concept I will explain using the same example. You have a class with specifications about cars, like they uses some type of combustible, they move, and have doors, then you have a class name Maruti (a type of car) well that class is a relationship between the class car, everything inside the class car is valid for the class maruti.

Masterie ”has-a” and “is-a” .
On the other hand we have another class name engine, the maruti class HAS a relationship with the class engine because there are many types of engine but the maruti only cares about its engine, at the same time the engine has a relationship with the murati because that car needs an engine but the engine class does not care about the doors or of the car moves.

Masterie ”has-a” and “is-a” .

Down here are more specifications if you still are having trouble getting this.

IS-A relationship

  • This refers to inheritance or implementation.
  • Expressed using keyword “extends”.
  • Main advantage is code reusability.

In object oriented programming, the concept of IS-A is a totally based on Inheritance, which can be of two types Class Inheritance or Interface Inheritance. It is just like saying “A is a B type of thing”. For example, Apple is a Fruit, Car is a Vehicle etc. Inheritance is uni-directional. For example House is a Building. But Building is not a House.

It is key point to note that you can easily identify the IS-A relationship. Wherever you see an extends keyword or implements keyword in a class declaration, then this class is said to have IS-A relationship.

HAS-A relationship
Composition(HAS-A) simply mean use of instance variables that are references to other objects. For example: Maruti has Engine, or House has Bathroom.

  • Has-A means an instance of one class “has a” reference to an instance of another class or another instance of same class.
  • It is also known as “composition” or “aggregation”.
  • There is no specific keyword to implement HAS-A relationship but mostly we are depended upon “new” keyword.

More info (http://learnwithharsha.com/my-first-java-lesson-is-a-and-has-a-relationships/)

And a video that explains everything with details.


Masterie ”has-a” and “is-a” .

WSQ06 – Greatest Common Divisor

--Originally published at Alan TC201

In this assignment I was rquired to write a program that calculates the greatest common denominator of two positive integers using Euclid’s algorithm.

If you do not know what Euclid’s algorithm is, here’s a page that explains it. http://www.cut-the-knot.org/blue/Euclid.shtml
WSQ06 – Greatest Common Divisor

 

This is “officially” the second homework and I am still fresh with this of program in Java, this may be easy because I did the same WSQ in the last semester in python. The trick here should be to implement this with message calls, not a simple iterative solution. This must be more oriented programing and nothing like “Python style”, now it is time for JAVA to shine:

WSQ06 – Greatest Common Divisor

This is my second language I am learning and the first object oriented, and I gotta say it is very different, python is very stylish and easy to understand the basics in less than 20 minutes. But Java is different…. very…. very different, just to do a hello world was very weird. This is a new experiences and I hope I can suprius it like in the last semester. The new thing I use in this WSQ was the “input” to give the machine the numbers to fiend the GCD. Video of how to do it. (https://www.youtube.com/watch?v=D3AYFeLjJmU)

WSQ06 – Greatest Common Divisor

And here is the github: https://github.com/alanoswalt/GCD/blob/master/redme.java


WSQ06 – Greatest Common Divisor

Masterie: UML Class Diagrams

--Originally published at Alan TC201

Class Diagrams are usful in diferent situations, for example.

Graphical way to illustrate relationships between classes in an object-oriented system.

Appearance varies between different drawing tools.

Round-trip tools are able to generate UML class diagrams from existing code, and generate template code from existing diaframs.

Masterie: UML Class Diagrams

“UML is popular for its diagrammatic notations. We all know that UML is for visualizing, specifying, constructing and documenting the components of software and non software systems. Here the Visualization is the most important part which needs to be understood and remembered by heart.

UML notations are the most important elements in modeling. Efficient and appropriate use of notations is very important for making a complete and meaningful model. The model is useless unless its purpose is depicted properly.

So learning notations should be emphasized from the very beginning. Different notations are available for things and relationships. And the UML diagrams are made using the notations of things and relationships. Extensibility is another important feature which makes UML more powerful and flexible.

The chapter describes the UML Basic Notations in more details. This is just an extension to the UML buildling block section I have discussed in previous chapter.” (http://www.tutorialspoint.com/uml/uml_basic_notations.htm)

Structural Things:
Graphical notations used in structural things are the most widely used in UML. These are considered as the nouns of UML models. Following are the list of structural things.

Classes
object
Interface
Collaboration
Use case
Active classes
Components
Nodes

Masterie: UML Class Diagrams

More info: http://www.tutorialspoint.com/uml/uml_basic_notations.htm

http://www.lynda.com/Java-tutorials/Diagramming-UML/375490/415273-4.html


Masterie: UML Class Diagrams

Masterie: CRD cards

--Originally published at Alan TC201

CRC cards are a way to se program object orientated.

Masterie: CRD cards

CRC cards are 4 by 6 index cards used in the design of object oriented
programs.  The initials CRC stand for “Class, Responsibility and Collaboration”.
Normally, a significant program written in an object oriented language like Java
is divided into several different classes.  These classes cooperatively work
together to accomplish the goals of the program.

Masterie: CRD cards
It can be difficult to understand how to divide a program into separate
classes, and how to divide a given class into separate methods. CRC cards help in this design process and organize in a very schematic way.

Masterie: CRD cards

This video explains everything in an easy way to understand.


Masterie: CRD cards

Masterie: What is delegation?

--Originally published at Alan TC201

Inheritance”
Other term that plays in Object-Oriented programming is inheritance”, this term tell us that we can “link” or transfer different methods between different classes that have similarities in them and use the same methods besides they use particularly methods. This helps to not duplicated methonds in diferent clases that have methods in commun.

If we couldn’t use Inheritance we will have to specify each method in each class we will have to explicit define each method in each class individually.
Multiple Inheritance means that a class have common things between two classes but those two classes don’t have Inheritance between them.

BUT BE CAREFUL, YOU DON’T CIRCULATE AN INHERITANCE, this means that a class cannot inheritance from itself, like a circle.

Overriding means that you could have different classes chain together and inheritance from the top one. For example, we have class, and we have a method call MON in the class V, normally all the classes below but if we have a method also call MON in the X class the the classes below X will inheritance the MON from the x class.

V – MON
W – MON FROM CLASS V
X – MON
Y – MON FORM CLASS X
Z – MON FROM CLASS X

Intarface is a set of methods that can have commun things inside different classes.

Masterie: What is delegation?

Inheritence uses the java compiler’s type system to implicitly include code from elsewhere, with delegation you explicitly implement the callout.

Inheritance is limited to a single parent class (and ancestors) but it can sometimes be non-obvious where your code is going; delegation is less elegant, but you can bring in functionality from multiple other classes and the flow is clearly visible.

As far as invoking external functions is concerned, delegation is generally preferred for just this reason, inheritance should be reserved for cases where you want to implement a class whose behaviour specifically is like the base-class, so it can essentially be treated as an instance of the baseclass by other code (ie. it is polymorphic).” (http://stackoverflow.com/questions/13245610/what-is-difference-between-inheritance-and-delegation-in-java)

 


Masterie: What is delegation?

Masterie: What is encapsulation?

--Originally published at Alan TC201

“Encapsulation”
Now after all this information we can dig more in terms of Java programing, first of all, the term “encapsulation”.
This means that, to have a better program, that is easy to read, to understand and fix the classes need to be private, so that the methods of a class only affect the data or the objects inside that class.

The encaptulation is used whent you have two packages with objects, clases and methods and you do not want that one package know the variables of the other package.

For example, here I have two diferent packages one that has the spesifications, the other one has the methods.

Masterie: What is encapsulation?

 

Masterie: What is encapsulation?

But when I change my methods to privite the encapsulated class could not recive inmormation from the Persona class.

Masterie: What is encapsulation?

Here is different info about encaptulation: http://www.lynda.com/Java-tutorials/Understanding-encapsulation/377484/421339-4.html

Masterie: What is encapsulation?


Masterie: What is encapsulation?

Masterie: What is abstraction?

--Originally published at Alan TC201

Abstraction in Java is like for example talking about animals, this is abstract because you do not know the context of that animal, it can be a cat, dog or a monkey, but you only know that it is animal.

Masterie: What is abstraction?

Abstraction has the same proces of hiding the implementation details from other people, only the functionality will be provided to the user. In other words user will have the information on what the object does instead of how it does it. Or in other words is telling the people the basic, hiding all the complex process that it requires.

In Java you can create an Abstraction using Abstract classes and Interfaces.

“Abstract Class

  • A class which contains the abstract keyword in its declaration is known as abstract class.
  • Abstract classes may or may not contain abstract methods ie., methods with out body ( public void get(); )
  • But, if a class have at least one abstract method, then the class must be declared abstract.
  • If a class is declared abstract it cannot be instantiated.
  • To use an abstract class you have to inherit it from another class, provide implementations to the abstract methods in it.
  • If you inherit an abstract class you have to provide implementations to all the abstract methods in it.” (http://www.tutorialspoint.com/java/java_abstraction.htm)

Masterie: What is abstraction?

Taking the example of the animals again, you can have a class name Animal and that class is going to be abstract, it has inside methods that all animals do, they eat and an abstract method that says that animals move.

Masterie: What is abstraction?

Then you have another class name Dog that extends from the animal class so the Dog class has in it the method eat, because all animals eat, but there is also an abstract method that the dog class need to verify, the move method, because not all animals move the same way, ones fly, otherones swim and dogs walk in four legs, so the method move this to be verify in the Dog class.

Masterie: What is abstraction?

Now that you have the dog class in is possible to create another class, that extends from the animal class, this new class is going to be a bird, birds can eat, but they move by air while using their wings.

Masterie: What is abstraction?

Finally the main class that controls the other classes and send them inputs to receive outputs.

Masterie: What is abstraction?

Links with more info:
https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
http://www.tutorialspoint.com/java/java_abstraction.htm
http://www.lynda.com/Java-tutorials/Using-abstract-classes-methods/377484/421352-4.html

 


Masterie: What is abstraction?

#WSQ09-TheMovies

--Originally published at hermesespinola

See the assignment on github.

This assignment has been the hardest so far, I had to read a lot of documentation to complete this one.
I had to learn about these things:

  • About the Scanner class, for reading files and receiving input from the console.
  • About the Set interface and HashSet class, to use it like a dictionary in Python.
  • About the Map interface and HashMap class.
  • About how the String class operates and some methods.
  • About CharSequence, just because String is an implementation of CharSequence.
  • About handling Exceptions, for the file reading and the MovieQuery class.
  • And finally, a little bit about regular expressions, for reading the query.

So when I read the the document in the link of the assignment, it says that we need to do set operations, and I found this on stack overflow.
It turns out that I didn’t need to implement any of the code for set operations, just imported the Set interface and the HashSet class.

#WSQ09-TheMovies

 

The Set interface contains the methods addAll, retainAll and removeAll, which are equivalent to union, intersection and difference set operations, respectively. But the problem said: find the set of actors that the given movies don’t have in common. (or something by the way).
And so, this is equivalent to the difference of the union and the intersection of two sets. (A ∪ B) – (A ∩ B). And this is called the symmetric difference.

So I first created the Movie class and wrote several constructors supporting different data types, because why not. It has two fields, a String; representing the name of the movie, and a Set of Strings; representing the name of the actors. I wrote the methods for the operations I described above. It doesn’t makes much sense because it’s just returning the result of the call to the set methods, but at least I gave these methods a fancier name. But it makes sense in the getActorsNotInCommon method, because the symmetric difference is not implement in the Set interface. I also added getters and setters and bla bla bla.

Before using the File class I tested it.
#WSQ09-TheMovies

But the code would not compile, and then, reading the docs of the File class, I realized I had to add “throws Exception” after the method parameters.
#WSQ09-TheMovies
And there are a lot of different implementations of the Exception interface.

In the MovieLibrary class I used a Scanner to read the file where are the actors and movies. And the constructor can throw a FileNotFoundException, and it has to, because the File class can, and so the code where it is used has to as well. The class has a field for storing the movie objects in a Map, using its name as the key, so the program has to store the name of the movie twice, and I though it was stupid, but I didn’t found a better way to do it, but now I’m wondering if it is possible to use something like a pointer to the variable inside the Movie object, to use it as the key of the HasMap, but whatever.

And the code for reading the file with the scanner:
#WSQ09-TheMovies

In the constructor of the MovieLibrary two scanners are used, one for reading the lines of the file, and another one for reading that line and separate the actor and movies, and then instantiating a Movie object -only if the name was not already in the map- and then pushing it into that map.
This class use the methods of the Movie class in all the methods.
#WSQ09-TheMovies

And I tested what I had written up to this point, before moving into implementing the user input:
#WSQ09-TheMovies

Then I was writing the main class, TheMovies, according to the description in the assignment (with the menus and option and shit). When I reached the part where says that I should ask the user for an input like: “movieName symbol moviewName”, I realized that it would be too much code, and it would be a mess with all the logic of user input, String processing and the use of the MovieLibrary class. So I decided to write another class.

In the MovieQuery class, given the description of the problem, I though I would need to use Regular Expressions somehow. I found this nice page to learn and test regular expressions.
I didn’t wanted for this class to be instantiated, and I was thinking of something like a “static class”, but it turns out there is no such thing, at least in the Java programming language, but you can simulate this behavior by adding some modifiers, like making the constructor private and some static methods and fields.
The class use a Scanner, with a regex delimiter like this: “( )|&^“. And what this is telling to the scanner is that it should identify a substring of that format, “()” means that the thing inside the parenthesis must appear in the string and the “[]” means that there must be any of the characters inside the braces. And by doing this, when I call the next() method of the scanner it returns the name of the first movie and calling next() again will return the second movie name.
When it finds the two name of the movies it searches for them in the MovieLibrary, and, if some of the movies is not there, throws an RuntimeException, because I don’t want neither keep executing the code nor return an empty Set.
It searches for which command is in the input, calls the appropriate method in the MovieLibrary instance and returns the result.
#WSQ09-TheMovies

And so the only thing the TheMovies class does is using the previous classes to ask and execute the user input.
#WSQ09-TheMovies

And here’s the output produced by the command line.

#WSQ09-TheMovies


#WSQ09-TheMovies