Results 1 to 12 of 12

Thread: help please

  1. #1
    thrinity is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default help please

    hi
    they gave me this code but i cant end :s can you help me?

    The solution assumes that each contact has a sole cellphone number. You will need to extend my implementation to consider that any contact can have more than one cellphone number.
    can sombody help me :(
    thanks


    public class CellPhone {
    int size = 10;

    String[] names = new String[size];
    int[] numbers = new int[size];

    int n = 0; // number of contacts in the contact list

    public void swap(int a, int b) {
    // swapping names
    String c = names[a];
    names[a] = names[b];
    names[b] = c;

    // swapping numbers
    int d = numbers[a];
    numbers[a] = numbers[b];
    numbers[b] = d;
    }

    // inserts name and number to the contact list
    public void insert(String name, int number) {
    int i = 0;

    while(i < n) {
    if(names[i].compareTo(name) == 0) { // if the person is already in the contact list
    numbers[i] = number;
    return;
    }

    if(name.compareTo(names[i]) < 0) { // introduces the new contact in order
    int k = n;
    while(k > i) { // shifts up existing contacts
    names[k] = names[k-1];
    numbers[k] = numbers[k-1];
    k--;
    } // end of while i less than or equal to n

    names[i] = name;
    numbers[i] = number;

    n++; // increases the number of contacts in the contact list

    return;
    } // end of else

    i++;
    } // end of while i less than n

    // introduces the first time or at the end of the contact list
    if(i==n) {
    names[n] = name;
    numbers[n] = number;
    } // end of if

    n++; // increases the number of persons in the contact list

    } // end of method


    public void print() { // prints the contact list
    for(int i=0; i < n; i++) {
    System.out.println(names[i] +": " +numbers[i]);
    }
    }

    public static void main(String[] args) {
    CellPhone cp = new CellPhone();
    cp.insert("n�stor", 967245370);
    cp.insert("jos�", 9212259);
    cp.insert("valter",9111321);
    cp.insert("ana", 9999999);
    cp.insert("nuno", 93121444);
    cp.insert("jorge", 9211111);
    cp.insert("am�lia", 9177777);
    cp.print();

    } // end of method main

    } // end of class

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Implement a class Contact, that has the name and as many cellphone numbers as you like and let class CellPhone implement an array of Contacts.


    And use code tags when posting code.
    And don't expect others to present you a ready made solution, it's your job.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    thrinity is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    i just wanted a light in the end of the tunel ;)
    thanks
    ;)

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    You're welcome and good luck.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Why are you using an array to represent your phone numbers? Why not use a TreeMap?

    Your code maps each name to a number, and keeps them sorted by name. By using a TreeMap, it automatically sorts for you, plus you can have as many names as you wish. That, and the code is easier to understand.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  6. #6
    thrinity is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    this is what i did but it's not verry good IN RED tell me what you think.. I'm at work and a can't test it :(


    public class CellPhone {
    int size = 10;

    String[] names = new String[size];
    int[] numbers = new int[size];

    int n = 0; // number of contacts in the contact list

    public void swap(int a, int b) {
    // swapping names
    String c = names[a];
    names[a] = names[b];
    names[b] = c;

    // swapping numbers
    int d = numbers[a];
    numbers[a] = numbers[b];
    numbers[b] = d;
    }

    // inserts name and number to the contact list
    public void insert(String name, int number) {
    int i = 0;

    while(i < n) {
    if(names[i].compareTo(name) == 0) { // if the person is already in the contact list
    numbers[i] = number;
    return;
    }

    if(name.compareTo(names[i]) < 0) { // introduces the new contact in order
    int k = n;
    while(k > i) { // shifts up existing contacts
    names[k] = names[k-1];
    numbers[k] = numbers[k-1];
    k--;
    } // end of while i less than or equal to n

    names[i] = name;
    numbers[i] = number;

    n++; // increases the number of contacts in the contact list

    return;
    } // end of else

    i++;
    } // end of while i less than n

    // introduces the first time or at the end of the contact list
    if(i==n) {
    names[n] = name;
    numbers[n] = number;
    } // end of if

    n++; // increases the number of persons in the contact list

    } // end of method

    // inserts double number to the contact list
    public void insert( int NewNumber) {
    int i > 0;
    while(i < n) {
    if(names[i].compareTo(name) == 0) { // if the person is already in the contact list
    if number[i]>0 then
    numbers[i] = number + NewNumber;
    return;
    }


    public void print() { // prints the contact list
    for(int i=0; i < n; i++) {
    System.out.println(names[i] +": " +numbers[i]);
    }
    }

    public static void main(String[] args) {
    CellPhone cp = new CellPhone();
    cp.insert("n�stor", 967245370);
    cp.insert("jos�", 9212259);
    cp.insert("valter",9111321);
    cp.insert("ana", 9999999);
    cp.insert("nuno", 93121444);
    cp.insert("jorge", 9211111);
    cp.insert("am�lia", 9177777);
    cp.print();

    } // end of method main

    } // end of class

  7. #7
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Let me first verify that this is not a class assignment. If it is, then you are forced to follow the directions (no matter how poor of code may result).

    If this is not for a class assignment, can I ask some questions, because you're code is getting messier with each revision.

    1) What is the purpose of the class? Specifically, how many numbers can be stored for each name?

    2) Why are you storing your name/number mapping in two arrays, instead of a SortedMap?
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  8. #8
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    It's seems that you're lost, so I thought I would clean up your code. This code will allow each person to have one phone number. You can then extend it using PhHein's idea to allow each contact in the phone to have multiple numbers (e.g. Home, Work, etc.).

    It sorts the contacts by name. If the specified name already is in the list, the new number replaces it. I believe this code is functionally equivalent to yours. This is why I suggested using a TreeMap, because the insert method is a single line of code.


    Java Code:
    import java.util.SortedMap;
    import java.util.TreeMap;
    import java.util.Map.Entry;
    
    public class CellPhone
    {
    	SortedMap<String, Integer> numbers = new TreeMap<String, Integer>();
    
    	// inserts name and number to the contact list
    	public void insert(String name, int number)
    	{
    		numbers.put(name, number);
    	}
    
    	public void print()
    	{ 
    		// prints the contact list
    		for (Entry<String, Integer> number: numbers.entrySet())
    		{	
    			System.out.println(number.getKey() + ": " + number.getValue());
    		}
    	}
    
    	public static void main(String[] args)
    	{
    		CellPhone cp = new CellPhone();
    		cp.insert("nostor", 967245370);
    		cp.insert("josh", 9212259);
    		cp.insert("valter", 9111321);
    		cp.insert("ana", 9999999);
    		cp.insert("nuno", 93121444);
    		cp.insert("jorge", 9211111);
    		cp.insert("amilia", 9177777);
    		cp.print();
    
    	} // end of method main
    
    } // end of class
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  9. #9
    thrinity is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    it's data structer and algoritms in java and i'm a liltle lost :(
    he as for:

    1. You will write a program implementing a cell phone contact list application in Java.
    2. The cell phone contact list will contain peoples' names and cell phone numbers. A
    single person may be associated to several phone numbers, yet cell phone numbers
    must be saved in increasing order, and entries for people's names must also be saved
    in increasing order.
    3. You will use arrays to store the information of your cell phone contact list application.
    4. You will write a function to print the contact list of your cell phone.


    but he already gave part of the code and his code cunfuses me :s i have to finish it
    Last edited by thrinity; 10-19-2009 at 11:32 AM.

  10. #10
    RamyaSivakanth's Avatar
    RamyaSivakanth is online now Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    797
    Rep Power
    6

    Default

    Tell clearly which part of the code u got confused?

    Have u tried to run the above code given?
    Ramya:cool:

  11. #11
    thrinity is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    all of it
    he mixes erverything
    a just can't put what he want running

  12. #12
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Tell him (whoever he is) and ask him for assistance.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Posting Permissions

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