WSQ07-Babylonian Method

--Originally published at diegotc2016

Doing this wsq was more or less difficult for me because I’m still having problems with the syntaxis of Java. First I tried to “translate” my old code in Python to Java, but my program didn’t work, but I finally got the code right.

Here’s a video that helped me for this WSQ:

I tried to do the WSQ using this guy method but the console displayed an error

Here’s my code in Eclipse using as an input number 36:

WSQ07-Babylonian Method

WSQ07-Babylonian Method

Here’s my code in Github:https://github.com/diegoalatorre/TC2016meromero/blob/master/WSQ07.java

WSQ07-Babylonian Method

 


WSQ07-Babylonian Method

#WSQ06-Greatest Common Divisor

--Originally published at diegotc2016

Doing this WSQ was more or less easy for me, the only difficult part was that I didn’t remember how to do the greatest common divisor of two numbers, so I searched in youtube for an example. The other difficult part was the java syntax, I’ve programmed in python for a year and learning java is becoming a challenge for me but I’m getting better

Here’s the code In Eclipse:

#WSQ06-Greatest Common Divisor

#WSQ06-Greatest Common Divisor

Here’s the code in GitHub:

*****

Here’s the video:


#WSQ06-Greatest Common Divisor

What are the mechanics for class description (coding) in the JavaProgramming Language?

--Originally published at diegotc2016

Classes are defined by the following way:

class MyClass {
    // field, constructor, and 
    // method declarations
}

In the space that is between the braces you can give instrucitons, you can define objects or declarations that will make your code work. 

You can also get the name subclasses and superclasses. This can be represented in the next way: 

class MyClass extends MySuperClass implements YourInterface {
    // field, constructor, and
    // method declarations
}

The following means that MyClass is a subclass of MySuperClass and that it implements the YourInterface interface. 

You can also modify the classes from the beginning. Class declarations can include these components, in order:

  1. Modifiers such as public, private, and a number of others that you will encounter later.
  2. The class name, with the initial letter capitalized by convention.
  3. The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
  4. A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
  5. The class body, surrounded by braces, {}.

I got this out of:

https://docs.oracle.com/javase/tutorial/java/javaOO/classdecl.html

and I received help from the teacher

What are the mechanics for class description (coding) in the JavaProgramming Language?


What are the mechanics for class description (coding) in the JavaProgramming Language?

What are inheritance and polymorphism? (Specification and implementation)

--Originally published at diegotc2016

Inheritance is when a class, which is called the sub class, has the main elements of another class (super class) and has another one or more. For example:

Class: Person Class: PersonTwo
Name

Age

Height

Name

Age

Height

Weight

Polymorphism means the many uses or functionalities an object ar operator or a class can have. For example:

a=4

b=5

Then you want to print a+b your output will be 9

a=”4”

b=”5”

Then you want to print a+b your output will be “45”

I got this out of object oriented course at lynda.com

What are inheritance and polymorphism? (Specification and implementation)


What are inheritance and polymorphism? (Specification and implementation)

What are visibility modifiers, their purpose and use in practice?

--Originally published at diegotc2016

Public

Public: it is visible to the world. A class, method, constructor, interface etc declared public can be accessed from any other class.

Private

Private: it is visible to the class only. Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself.

No Modifier

No Modifier: it is the default and it is visible to the package. A variable or method declared without any access control modifier is available to any other class in the same package.

Protected

Protected: visible to the package and all the subclases. Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members’ class

I got this out of:

https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

What are visibility modifiers, their purpose and use in practice?


What are visibility modifiers, their purpose and use in practice?

“Has a” is a “Relationship”

--Originally published at diegotc2016

Has a relationship is based on object composition while is a relationship is based on inheritance. For Example:

Person                                                                  is a FootballPlayer
Has an adress

Has an age

Has a name

Has a dog

Has a helmet

Has shoulder pads

Has cleats

Has gloves

This video would get it clearer, I understood this concept thanks to this video:


“Has a” is a “Relationship”

Unity Tutorial – Basic Stuff

--Originally published at Blog

In this post I’ll show the most basic usage of the Unity3d game engine, using the C# programming language, its syntax is very similar to Java.

I’ll be using public domain assets that I downloaded here.

And instead of using the default cube of Unity, I’ll use this one, because it’s made out of Quads (like a plane, you can only see it from one side), and it’s easier to add the textures to it (I don’t know how to use blender and stuff).

Ok, so now open Unity (which you can download from it’s page), and create a new 3D project, by default, it only has a camera.

So, the Editor has windows, and you can move them around, you can add new ones in the Window top menu. The ones I’ll need are Game (here you see what the cameras are projecting), the Scene (where you edit the game), the Inspector (where you see the selected object’s properties), the Hierarchy (where you see and select objects in you scene), the Project (like a file manager), and the Console (here you see errors and warnings).

In the Project and Hierarchy windows, there is a drop-down button that reads: “create”. when you click it, you will see a lot of pre-build thing that you can use in the editor, and whatever you crete, it will appear inside the window where you clicked “create”. When I say “create something in bla”, I mean that you should click “the” bla button in this drop-down, inside the “something” window.

Unity Tutorial – Basic Stuff

Extract the and drag and drop the PNG folder into the Project window. Make sure you organize your files. Create these folders (right-click > create > folder):

Unity Tutorial – Basic Stuff

In the Hierarchy window click on Create > Light > Directional light, this light is behaves like a sun, it doesn’t matter where you put it, only it’s rotation.

Unity Tutorial – Basic StuffPut the Cube.prefab file in the Prefabs folder (does it makes sense?) and then drang and drop that weird thing to the Scene window, you’ll that the cube appears magically in the Scene, and also, the cube is added to the Hierarchy window.Unity Tutorial – Basic Stuff

Open the Tiles folder in the PNG folder, now, if you drag and drop the images in the cube that’s in the scene, you can assign that texture to the side of the cube where you drop the image (and that’s the reason behind the special cube prefab). You can see that, in the Hierarchy, there’s an arrow next to the cube, if you click that arrow, you will see it’s children.

GameObjects can have children, which are also GameObjects. Children have a relative position to it’s parent, and they can be easily referenced from a script. For example, you can have the “player” object, and a “gun” object. You want that gun to be a child of you player, so it’s always, in it’s hand.

Unity Tutorial – Basic Stuff

Now add a RigidBody to the tile, this will allow the tile interact with other objects, and also, deselect the “use gravity” button.

Unity Tutorial – Basic Stuff

Once you’ve done this, go to the Prefabs folder and (in the Project window) click on Create > Prefab. Now, drag and drop the Cube (in the Hirarchy window) to this new Prefab in the Project window. As you can see, the cube is “inside” the prefab. you can now delete the cube from the scene.

A prefab is the way Unity defines custom Game Objects, so that you can instantiate them, without the need to create them again, so it’s very similar to creating a class when you are programming (but of course, it’s not the same).

create an empty game object in the Hierarchy, and name it, I don’t know, map, or terrain, or whatever, then go to the Script folder and create a new C# script, we’ll use this script to automatically generate the terrain, based on the tile I’ve just created, when the game is loaded. drag and drop this new script to the empty game object in the Hierarchy.

Every GameObject you create has different fields, if some field is set to public, it will appear in the Inspector window, you can change the values of these properties inside the Inspector.

Unity Tutorial – Basic Stuff

Everything in Unity is a GameObject, which is a special class of Unity, and every GameObject has at least a Transform. a Transform determines the position and rotation of that object in the scene.

If you double click the script you’ve just created, the code editor should be opened (Visual Studio or Monodevelop).

Unity Tutorial – Basic Stuff

The C# syntax is very similar to Java, but to implement and interface you write <ClassName> : <InterfaceName>.

Monobehaviour is a special interface of Unity, if you delete that, you will not be able to attach the script to a game object.

In Unity Scripts, there are special method names,  the Start method is called when the GameObject is instantiated and the Update is called once every frame. You can search for more special names in the Unity documentation.

add these fields, you want them to be public so you can change them from the Unity editor. the grassTile of type Transform will hold the prefab I created earlier. the tileSize is set to 1 by default, but you can change it from the editor. You need to drag and drop the tile prefab we created to the Grass Tile field that you can see in the Inspector when the Terrain object is selected.

There are Vector2 and Vector3 classes, and they are what you would expect.

Unity Tutorial – Basic Stuff

Now add a Vector2 called terrainSize and these for loops:

Unity Tutorial – Basic Stuff

I’ll create a plain terrain for now, the terrainSize represents the amount of tiles with size = tileSize. These for loops will “walk” through a “grid”, in each position a new tile will be instantiated:

Unity Tutorial – Basic Stuff

This is a static method in the GameObject class that creates the specified object in the scene (and you can instantiate almost anything, because everything is a GameObject). It returns the instantiated object, and we can tell C# to convert it to a Transform (because aTransform is a Game Object, and you know, “polymorphism” stuff).

You can change the values of the public fields that you declarated in the script:

Unity Tutorial – Basic Stuff

You should see something like this in the Scene window.

Unity Tutorial – Basic Stuff


Unity Tutorial – Basic Stuff

#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

#WWQ08-Yo Soy 196

--Originally published at hermesespinola

This thing was really weird, I mean, it’s useless but it was interesting.
So a lychrel number is a special number, if the repetitive addition of a number with the number formed with inverse of its digits form a palindrome then it’s not a lychrel number.
So, in example: 196 + 691 = 887, 887 + 788 = 1575, … and so forth.

So here I first programmed the YoSoy186 class, before programming LychrelSequence and LychrelNumber, because I wanted to define how the class would work instead of programming it in the first place.
#WWQ08-Yo Soy 196

So because I needed to flip the digits of the numbers, I converted the number to a String. And because in this lynda Java course they said I should use the StringBuilder class to work with string, because it’s expensive to do the string operations with the normal String operators.

And then, in the Lychrel number class, I wrote a lot some fields, getters and setter then I wrote the above algorithm to evaluate if the number is a candidate to be a lychrel number:

public class LychrelNumber {
  private int value;
  private boolean isPalindromeNumber;
  private boolean isLychrel;
 
  public LychrelNumber() {
  }
 
  public LychrelNumber(int number) {
    setValue(number);
  }
 
  public boolean isLychrelNumber() {
    return this.isLychrel;
  }
 
  public boolean isPalindrome() {
    return this.isPalindromeNumber;
  }
 
  public int getValue() {
    return this.value;
  }
 
  private long[] evaluateHelper(long val) {
    StringBuilder reverseStr = (new StringBuilder(Long.
    toString(val))).reverse();
    long reverse = Long.parseLong(reverseStr.toString());
    long add = val + reverse;
    return new long[] {reverse, add};
  }
 
  private void evaluate(long value) {
    long[] r = this.evaluateHelper(value);
    String addStr = Long.toString(r[1]);
    this.isPalindromeNumber = (value == r[0]);
    if (!this.isPalindrome()) {
      for (int i = 0; i < 30; i++) {
        r = evaluateHelper(r[1]);
        addStr = Long.toString(r[1]);
        if (addStr.equals(new StringBuilder(addStr).reverse().toString())) {
          this.isLychrel = false;
          break;
        }
        this.isLychrel = true;
      }
    }
  }
 
  public void setValue(int number) {
    this.value = number;
    this.evaluate((long)this.value);
  }
}

In the code I wrote a private method, which is only called when the setter of the value is called, and another method to reverse and add these numbers. I used a long number because the repetitive addition of the numbers generated huge numbers and the program would run out of memory.

So first, before checking if the number is a lychrel number, I check if it is a palindrome, and then, if it is not, run the code to check if it is a lychrel number (because if it is a palindrome then it’s not a lychrel number). If at some point the addition of the number and the inverse of its digits is a palindrome, the code breaks and sets the isLychrel number variable to true.

For the LychrelNumber class, I used the ArrayList class to be able to append elements to the list, something you can’t do to a normal array easily. I used three lists, for storing the lychrel, non-lychrel, and palindrome sequences. The link says that since the List class is an Interface, you need to declare a specific implementation of this Interface.
All this class does is iterate from the lower bound to the upper bound, and instantiating the LychrelSequence class with that value, and if the number is a palindrome, it’s added to the palindromes list and the non-lychrel list, else, if the number is a lychrel number it is added to the lychrel list, but not all non-lychrel numbers are palindromes).

import java.util.List;
import java.util.ArrayList;
 
public class LychrelSequence {
  private int lower;
  private int upper;
  private List<Integer> lyqSeq;
  private List<Integer> nonLyqSeq;
  private List<Integer> palindromeSeq;
 
  public LychrelSequence(int lowerBound, int upperBound) {
    this.lower = lowerBound;
    this.upper = upperBound;
 
    this.lyqSeq = new ArrayList<>();
    this.nonLyqSeq = new ArrayList<>();
    this.palindromeSeq = new ArrayList<>();
 
    LychrelNumber ln = new LychrelNumber();
    for (int i = this.lower; i <= this.upper; i++) {
      ln.setValue(i);
      if (ln.isPalindrome()) {
        palindromeSeq.add(ln.getValue());
        nonLyqSeq.add(ln.getValue());
      } else if (ln.isLychrelNumber()) {
        lyqSeq.add(ln.getValue());
        System.out.println(“Found a Lychrel number candidate: ” + ln.getValue());
      } else {
        nonLyqSeq.add(ln.getValue());
      }
    }
  }
 
  public int getLowerBound() {
    return this.lower;
  }
 
  public int getUpperBound() {
    return this.upper;
  }
 
  public List<Integer> getPalindromeSequence() {
    return this.palindromeSeq;
  }
 
  public List<Integer> getLychrelSequence() {
    return this.lyqSeq;
  }
 
  public List<Integer> getNonLychrelSequece() {
    return this.nonLyqSeq;
  }
 
 
  public int getNumPalindromes() {
    return this.palindromeSeq.size();
  }
 
  public int getNumLychrels() {
    return this.lyqSeq.size();
  }
 
  public int getNumNonLychrels() {
    return this.nonLyqSeq.size();
  }
}

Something weird I noticed is that you have to use the Integer class when declaring the list, instead of the primitive type int.

#WWQ08-Yo Soy 196

And the here’s the Github repository of this homework.


#WWQ08-Yo Soy 196

What is an object, attribute and method? #Object #Attribute #Method

--Originally published at diegotc2016

Object

Wikipedia defines Object as:

“In computer science, an object can be a variable, a data structure, or a function, and as such, is a location in memory having a value and possibly referenced by an identifier.”

Ken’s definition of object:

“Something that has state and behavior”

My definition of Object:

A varible or a function that has a state and behavior

Attribute

Wikipedia defines Attribute as:

“In computing, an attribute is a specification that defines a property of an object, element, or file.”

Ken’s definition of Attribute:

“A value that an object can have (variable)”

My definition of Attribute:

A value given to a variable

Method

Wikipedia defines Method as:

“A method (or message) in object-oriented programming (OOP) is a procedure associated with an object class.

Ken’s definition of Method:

“The implementation of an attribute, kind of a function, but inside a class”

My definition of Method:

Use the attributes or variables in order to perform a certain action

 

 


What is an object, attribute and method? #Object #Attribute #Method