Archive for iulie, 2011

Tutorial Java SCJP – # 19 Enumerari de tip enum

Exista situatii in care o variabila trebuie sa aiba valori limitate la o anumita multime, definita in specificatiile solutiei. Sa presupunem ca trebuie dezvoltata o aplicatie Java care gestioneaza Vehicule iar tipul de motor trebuie sa ia o valoare din multimea {BENZINA, DIESEL, HYBRID, ELECTRIC}. Pentru a implementa cerinta se poate defini atributul asociat tipului de motor ca String sau ca int si se valideaza de fiecare data valoarea de intrare. Pentru siruri de caractere se poate compara valoarea de intrare cu "BENZINA", "DIESEL", si asa mai departe. Pentru int se poate face asocierea BENZINA este 1, DIESEL este 2, … si se verifica valorile pe baza acesti abordari. Aceasta este o solutie posibila, dar nu e eficienta pentru ca se pot face cu usurinta greseli si pentru ca se complica o procedura care ar trebui sa fie simpla.

Read the rest of this entry »

1 Comment

Tutorial Java SCJP – # 18 Blocuri de initializare

Pentru a face prelucrari si pentru a obtine rezultate este nevoie de date. Si aceste valori de intrare sunt de obicei stocate in variabile statice, variabilele locale (definite în metode) sau variabile de instanta (variabile nonstatice definite în clase). Pentru a initializa o variabila o poti face la definitia sau mai târziu într-o metoda (constructor sau nu). In ciuda acestor doua solutii comune, exista o alta cale folosind blocuri de initializare.

Blocurile de initializarea sunt blocuri de cod definite intre { si }. Din acest punct de vedere, ele seamana cu blocuri/corpuri de metode, dar principala diferenta este ca blocurile de initializare nu au un nume. Ele sunt ca metodele, insa fara antet/header (tip returnat, nume metoda, lista de parametri).

Read the rest of this entry »

1 Comment

Tutorial Java SCJP – #19 Enumerated lists or Enums

There are logically situations in which a variable must have values restricted to a specific range or set that is defined in the solution specifications. Let’s imagine that you must develop a Java applications that manages Vehicles and the engine type must take one value from the {GASOLINE, DIESEL, HYBRID, ELECTRICAL} set. You can define the engine type as a String or as an int and you can validate every time the input value. For strings you can compare the input value with “gasoline”, “diesel” and so on. For integer you can assume that GASOLINE is 1, DIESEL is 2, … and check the values based on this logic. This is a possible approach but it’s not ok because you can make easily mistakes and because you complicate a really simple procedure.

Read the rest of this entry »

1 Comment

Tutorial Java SCJP – #18 Initialization blocks

In order to process something you need values. And values are usually stored in static variables, local variables (defined in methods) or instance variables (nonstatic variables defined in classes). In order to initialize a variable you can do it at definition or later in a method (constructor or not). Despite these two common solutions, there is another way using initialization blocks.

Initialization blocks are blocks of codes defined between { and }. At this point they are like methods blocks, but the main difference is that initialization blocks don’t have  a name. They are like methods but without the method header (return type, name, parameter list).

Read the rest of this entry »

1 Comment

J2ME tutorial – Cum se construiesc si se afiseaza formulare si alerte intr-un MIDlet

Aplicatiile mobile J2ME sunt solutii simple, care permit utilizatorilor sa-si gestioneze datele folosind formulare/ferestre (Displayable in J2ME) ce contin diferite tipuri de controale. In API-ul J2ME, echivalentul ferestrelor din aplicatiile de tip desktop este reprezentat de un set relativ redus de formulare care reprezinta subclase ale clasei Displayable. Intregul framework este un set redus de clase, care sunt usor de utilizat si cu un impact mai mic asupra memoriei si resurselor procesorului decât omologul pentru desktop, Java AWT (Abstract Window Toolkit) sau Swing.

Read the rest of this entry »

No Comments

J2ME tutorial – How to create and display forms and alerts

J2ME mobile applications are simple solutions that allow users to manage their data using different controls that are placed in forms, windows or Displayable. In the J2ME API, the equivalent of desktop applications windows is represented by a relative reduced set of forms which are subclasses of Displayable class. The entire framework is a limited, smaller set of classes which are easy to use and less demanding of memory and processor resources than the desktop counterpart as Java AWT (Abstract Window Toolkit) or Swing.

Read the rest of this entry »

2 Comments