Saturday, October 20, 2012

Queue

Assalamualaikum. I completed Lab 5 which involve about Queue. It might be the easiest lab exercise we ever have. The following is the question
Given the following Queue and Handphone ADTs answer the questions below : public class Queue { public Queue() {... } public void enqueue(Object elem){...} public Object dequeue() {...} public boolean isEmpty(){...} //definition for other methods } public class Handphone{ //declaration of data public Handphone(String sn, String brand, double price){...} public String getSN(){....} public String getBrand(){...} public double getPrice(){...} public String toString(){...} //to display output } ATOZ Telecommunications Sdn. Bhd. is a company that sells handphones and currently having promotions on 10 brand new sophisticated handphones. Assuming that all their handphones are stored in a queue, the first 8 customers purchase the handphone will be given a discount of 25% off. One customer is only limited to purchase one handphone.
a) Create a Queue object named as qHandphone.
b) Input TEN(10) handphone objects and store them into qHandphone.
c) Display the output for all handphone objects using the following example:
SERIAL NUMBER BRAND NORMAL PRICE DISCOUNTED PRICE
1234 NOKIA 1200.50 900.375
1235SAMSUNG1234.67 926.0025
1236 SONY 860.87 645.6525
1237 BLACKBERRY 1345.30 1008.975
1238 NOKIA 985.45 739.0875
1239 NOKIA 744.40 558.3
1240 BLACKBERRY 750.00 562.50
1241 NOKIA 977.70 733.275
1242 APPLE 1668.98 1668.98
1243 NOKIA 666.60 666.60

d) Calculate and display the total prices for the handphones sold by the company.
So, we can see requirement of Queue Class and Handphone Class. For this, I implement my LinkedList class to make Queue. This also require Node class.


This is my Queue class. I didn't make method isEmpty() because already have it in my LinkedList class. I make Queue the subclass of LinkedList.

Then I create Handphone class. Although, I didn't implement my toString() method in my application later for the sake of formatting.
Now, I still not so sure about "One customer is only limited to purchase one handphone.". I think it is just a policy so the company not getting bankrupt. Imagine, the promotion for first 8 customers, but if the first customers buy 10 handphone?
Now, let's tackle and solve this problem. First, Input 10 handphone objects. We need to create a Queue variable of qHandphone. Then, we get inputs from user, 10 of handphone, and temporarily store in a Handphone object(in my case, its name is hp). Insert this into qHandphone.
Then, there are display part. looking further, we can see need to count discounted price and total price. so take it in mind to do it at the same time. Also, we want to use a counter to calculate 8 of customers, because only first 8 customers get discount.
If you notice, I use printf. there are some explanation in the links I put in documentation of the source. But if you already know about printf, yeah, it is very similar to C++ printf. This allow for formatting, but, only to your output/display stream. While using String.format() will actually format the string and can be stored as such. I don't need to store, so why bother? Simpler, efficient. We are software engineer. :D

Thursday, October 11, 2012

Linked List

Assalamualaikum. Peace be upon you, sadiqi and sadiqiah (my friend).

We were given a lab assignment involving building custom linked list and apply it. I was quite furious because I took way too long compared to previous 2 lab. Ah... and a good morning start this morning where I have the whole morning free-with Allah's will- help me build it from scratch and complete it. Well, I forgot the odd & even part, but that is easy after everything else.

Lab Exercise 3 Questions:
First, building the Node class. I found a Tutorial in D.I.C. and I like it. I want to try explaining myself. LinkedList have basic unit of node. This Node consists of 2 elements of data/info and link. Data/info store the data or information of that particular node. While link is a reference variable to store the address of next node in the chain. link will be null otherwise.

Node class:
For this specific assignment, I use int to store data. The best way would be using Object data type. Then I made an application as what exercise 1 want.

Exercise 1:


Then I made LinkedList. I made it for each method, and test each exercise. First I made public variables so I can made the right one. But this one, I change to private variables class so it needed getter.

LinkedList use Node as its units. They are all connected using link reference variable. And there are head, tail and current. Head is a reference variable to always store address of first element. Tail will be a reference variable of same type to always store address of last element. Current is mainly use to iterate/move along the LinkedList when required. These are needed to perform operation to the LinkedList. counter variable store the size of the LinkedList. It reduce the time-complexity of a method, I'm sorry I don't remember which one.

isEmpty() check of empty LinkedList by two way. See if counter == 0 or head==null.
getFirst() is simply returning the address stored in head to get first element.
getNext() will involve current or temporary variable. return current.link to return the next element address.

When adding element, if it's empty, it always about head and tail point to the newNode. Because the add method in this exercise are set to be front and back only, quite easy compared to other add method.

Remove element is as easy as setting the link reference to other element and set the element link as null. RemoveAtFront is easy because can use head. But RemoveAtBack requires tranversal to almost end of the list.


LinkedList:


Exercise 2:


Exercise 4:



Exercise 8: