Results 1 to 10 of 10

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User bombov's Avatar
    Join Date: Jan:2007
    Location:
    Posts: 260

    Java(Generics ADT Structures)


  2. #2
    Registered User
    Join Date: Apr:2004
    Location: EU
    Posts: 141
    "Method Overloading".


    Java : "Thinking in Java" Bruce Eckel http://www.mindviewinc.com/Books/ - - .

  3. #3
    Registered User bombov's Avatar
    Join Date: Jan:2007
    Location:
    Posts: 260
    , , , , , , .

  4. #4
    Registered User
    Join Date: Apr:2004
    Location: EU
    Posts: 141

  5. #5
    Registered User bombov's Avatar
    Join Date: Jan:2007
    Location:
    Posts: 260
    Write an immutable class named Contact containing name, phone number and
    comment. The values of the properties should be passed as constructor
    arguments. Write a class PhoneBook which contains Contact objects, with the following operations:
    boolean add(Contact) - should return false if the book already contains item with this phone or this name.
    Contact getByName(String name)
    Contact getByPhone(String phone)
    Implement the following additional methods:
    Iterable<Contact> sortedByName(PhoneEntry)
    Iterable<Contact> sortedByPhone(PhoneEntry)
    String toString()
    Add and delete operations (directly and by iterator)
    -, , .
    , ,- , .

  6. #6
    Registered User
    Join Date: Apr:2004
    Location: EU
    Posts: 141
    . ?
    - .

  7. #7
    Registered User bombov's Avatar
    Join Date: Jan:2007
    Location:
    Posts: 260
    Code:
    public final class ImmutableContact implements Comparable<ImmutableContact>  {
    
    	private final String name;
    	private final String phone;
    	private final String comments;
    	
    	public ImmutableContact(String paramName,String paramPhone,String paramComments){
    		
    		this.name = paramName;
    		this.phone  = paramPhone;
    		this.comments = paramComments;
    	}
    	
    	@Override
    	public String toString() {
    		return this.name+" "+this.phone+" "+this.comments+".";
    	}
    	
    	public String getName() {
    		return name;
    		}
    	
    	public String getPhone() {
    		return phone;
    		}
    		
    	public String getComments() {
    		return comments;
    		}
    		
    	public int compareTo(ImmutableContact contact) {
    		return this.name.compareTo(contact.name);
    	}
    }
    Code:
    import java.util.*;
    
    public class Phonebook{
    	
    	List<ImmutableContact> phonebook;
    	private int sizeList;
    	
    	public Phonebook()
    	{
    		phonebook = new LinkedList<ImmutableContact>();
    		sizeList = 0;
    	}
    	
    	public void add(ImmutableContact contact) {
    			phonebook.add(contact);
    			sizeList++;
    	}
    	
    	public boolean checkElementExist(ImmutableContact contact) {
    		if(phonebook.isEmpty()){
    			return false;
    		}
    		else {
    			for(int counter=0;counter<sizeList;sizeList++){
    				if((phonebook.get(counter).getName().compareToIgnoreCase(contact.getName()) !=0) ||
    				  (phonebook.get(counter).getPhone().compareToIgnoreCase(contact.getPhone()) != 0)) 
    				{
    					return true;
    				}
    				else {
    					return false;
    				}
    			}
    		}
    		return false;
    	}
    	
    	
    	public ImmutableContact getByName(String name) {
    		
    		for(int counter=0;counter<sizeList;counter++) {
    			if(phonebook.get(counter).getName().compareTo(name) == 0)	
    			{
    				return phonebook.get(counter);
    			}
    		}
    		return null;
    	}
    	
    	public ImmutableContact getByPhone(String phone) {
    		
    		for(int counter=0;counter<sizeList;counter++) {
    			 
    			if (phonebook.get(counter).getPhone().compareToIgnoreCase(phone) ==0)
    			{
    				return phonebook.get(counter);
    			}
    		}
    		return null;
    	}
    	
    	public boolean isEmpty() {
    		return sizeList == 0;
    	}
    	
    	public void printPhonebook(){
    		for(int counter=0;counter<sizeList;counter++) {
    			if(phonebook.get(counter) != null){
    				System.out.println(phonebook.get(counter).toString());
    			}
    		}
    	}
    	
    	public Iterable<ImmutableContact> sortedByName(){
    		List<ImmutableContact> sortedElByName = phonebook;
    		Collections.sort(sortedElByName);
    		return sortedElByName;
    	}
    	
    	public Iterable<ImmutableContact> sortedByPhone() {
    		List<ImmutableContact> sortedelByPhone = phonebook;
    		Collections.sort(sortedelByPhone);
    		return sortedelByPhone;
    	}
    	
    	public void delete() {
    		if(!phonebook.isEmpty()) {
    			ImmutableContact value = phonebook.remove(0);
    			System.out.println(sizeList);
    			sizeList--;
    			System.out.println(value);
    			}
    		else {
    			System.out.println("Done");
    		}
    	}
    }
    Code:
    public class TestImmutableClass {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		//      
    		
    		ImmutableContact contact1 = new ImmutableContact("koko","555555555","account manager");
    		ImmutableContact contact2 = new ImmutableContact("koko","444444444","sales manager");
    		ImmutableContact contact3 = new ImmutableContact("joro","555555555","ship manager");
    		
    		Phonebook phonebook = new Phonebook();
    		
    		//add Elements in phonebook
    		phonebook.add(contact1);
    		phonebook.add(contact2);
    		phonebook.add(contact3);	
    		System.out.println("-----------------------");
    
    		
    		//Check exist elements in phonebook
    		System.out.println("Check exist elements :");
    		System.out.println(phonebook.checkElementExist(contact1));
    		System.out.println(phonebook.checkElementExist(contact2));
    		System.out.println(phonebook.checkElementExist(contact3));
    		System.out.println("-----------------------");
    		
    		//GetByName
    		String name = "koko";
    		System.out.println(phonebook.getByName(name));
    		System.out.println("-----------------------");
    		
    		//GetByPhone
    		String phone="444444444";
    		String wrongphone = "111";
    		System.out.println(phonebook.getByPhone(phone));
    		System.out.println(phonebook.getByPhone(wrongphone));
    		System.out.println("-----------------------");
    		
    		//Print Elements
    		System.out.println("Print Elements : ");
    		phonebook.printPhonebook();
    		System.out.println("-----------------------");
    		
    		//Print SortedByName
    		phonebook.sortedByName();
    		System.out.println("Elements sorted by name :");
    		phonebook.printPhonebook();
    		System.out.println("-----------------------");
    		
    		//Print SortedByPhone
    		phonebook.sortedByPhone();
    		System.out.println("Elements sorted by name :");
    		phonebook.printPhonebook();
    		System.out.println("-----------------------");
    		
    		//Delete Elements
    		for(int i=0;i<3;i++) {
    		phonebook.delete();
    		}
    	}
    }
    :
    Add and delete operations (by iterator)

  8. #8
    Registered User bombov's Avatar
    Join Date: Jan:2007
    Location:
    Posts: 260
    @Flower
    Skype:bombov1988
    mail : bombov@mail.bg

    @DimKo .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |