Results 1 to 10 of 10
Thread: Java(Generics ADT Structures)
Hybrid View
-
11th December 2008 09:14 #1
Java(Generics ADT Structures)
, Generics ADT Structures - :
http://java.sun.com/j2se/1.4.2/docs/...awt.Component)
.
-
11th December 2008 11:39 #2Registered User
Join Date: Apr:2004
Location: EU
Posts: 141
"Method Overloading".
Java : "Thinking in Java" Bruce Eckel http://www.mindviewinc.com/Books/ - - .
-
11th December 2008 13:07 #3
-
11th December 2008 13:53 #4Registered User
Join Date: Apr:2004
Location: EU
Posts: 141
- . http://www.soft-press.com/goto.htm?h...l&viewbook=133
.
overload ( ) / . , .
"Method Overloading"
http://en.wikipedia.org/wiki/Method_overloading
-
11th December 2008 14:22 #5-, , .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)
, ,- , .
-
11th December 2008 15:21 #6Registered User
Join Date: Apr:2004
Location: EU
Posts: 141
. ?
- .
-
11th December 2008 18:27 #7Code:
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)
-
12th December 2008 09:35 #8
@Flower
Skype:bombov1988
mail : bombov@mail.bg
@DimKo .




Reply With Quote
Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in