10 Replies - 4043 Views - Last Post: 26 August 2013 - 05:39 PM Rate Topic: -----

#1 tgcbraun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-August 13

PhoneBook App

Posted 23 August 2013 - 08:00 AM

Hi Folks,

I have an assignment to create a class named PhoneBook that will manage up to 10 names and phone numbers. This class will be used by another class named Main.

Here is the provided code for the Main class:

public class Main {

  public static void main(String[] args) {
    int x;
    PhoneBook pb = new PhoneBook();
    System.out.println(pb);
    x = pb.nextIndex();
    if(x != -1) {
      pb.setName("Bob", x);
      pb.setNumber("0987654321", x);
    }
    else {
      System.out.println("Error: No empty phone book entries.");
    }
    pb.setName("Mike", 0);
    pb.setNumber("1234567890", 0);
    pb.setName(null, 1);
    pb.setNumber(null, 1);
    System.out.println(pb);
    x = pb.nextIndex();
    if(x != -1) {
      pb.setName("Bob", x);
      pb.setNumber("0987654321", x);
    }
    else {
      System.out.println("Error: No empty phone book entries.");
    }
    System.out.println("Entry for Mike at index: " + pb.getRecord("mike"));
    System.out.println("Entry number 2 (index 1) is: ");
    String temp[] = pb.getRecord(1);
    if(temp != null) {
      System.out.println("\t" + temp[0]);
      System.out.println("\t" + temp[1]);
    }
    else {
      System.out.println("Null entry.");
    }
    System.out.println("\n" + pb);
  }
}


The provided guidelines are these:

1. No method should use hard numbers to indicate sizes. Use a constant (final) field so that the size of this data structure can be updated easily. (That is, we might want to use more than 10 names and numbers later.)
2. Two private instance variables that are String arrays. One array represents names and the other array represents phone numbers. The name at index 0 in the names array corresponds to the phone number in index 0 of the phoneNumber array. This is a logical linking of the two arrays that must be maintained by the class (you the programmer).
3. A constructor for the class. You should fill the arrays with data in this method.
4. A toString() method that returns a formatted string representation of the entire class. This method cannot use System.out.println() in keeping with the spirit of the toString() method inherited from Object. It is simply a String representation of the class. You should return all valid data in the class in some formatted way, such as name0\t\phonNumber0\nname1\tphoneNumber1\n ...
5. A setName() method that takes a String as a name and an int as an index number for parameters. Set the name array at index to equal the String parameter. Return true if the index is in bounds of the name array and the data was set. Return false if the index is out of bounds.
6. A setNumber() method that takes a String as a phone number and an int as an index number for parameters. Set the phoneNumber array at index to equal the String parameter. Return true if the index is in bounds of the phoneNumber array and the data was set. Return false if the index is out of bounds.
7. A getRecord() method that takes an index number of a record (array slot). Look up the record at that index in the name and phoneNumber arrays. Return a new String array with the record's name in element 0 and the person's phone number in element 1. If the index number is out of bounds or if the record contains no valid data, return null. This method returns either a two element String array or null.
8. A nextIndex() method returns the next index in the name array that contains null. If no element in the array is null, return -1 as a flag indicating there are no empty slots in the name array. This method can be used by another program to find the nextIndex() in the PhoneBook class where data may be stored without overwriting current data.
9. A getRecord() method that overloads your other getRecord() method. This method needs to take a String as a parameter that represents the name of a person you want to get the record of. It should return the index number of that record. Your search of the array should be case insensitive. There are methods in the String class that can help with this. Remember to check for a null value before trying to access data in the array element. Return -1 if the record is not found.


So, first things first, I got that I need two String arrays that will store the user input for the names and numbers of up to ten people. Question is, how can I write a constructor that receives data input and store it on both string arrays?

I thought about starting with something like this:

public class PhoneBook {
  
  private String [] Name = new String[10];
  private String [] Number = new String[10];
  
  public PhoneBook(String[] Name, String[] Number) {
    this.Name = Name;
    this.Number = Number;
    
  }

}


Is this any good?

What does instruction number 1 actually means? (no method should use hard numbers to indicate sizes. Use a constant (final) field so that the size of this data structure can be updated easily). I have really no idea how to implement that.

Thanks for any input.

Is This A Good Question/Topic? 0
  • +

Replies To: PhoneBook App

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: PhoneBook App

Posted 23 August 2013 - 08:13 AM

Number 1 means that a final variable, like:

final int PHONE_BOOK_SIZE = 10;

will be used throughout the program (as needed) to define the size of data structures, collections, etc. The variable will be used instead of sprinkling the number '10' throughout the application. If you decide later to double the size of the phone, you'll only have to change the value of PHONE_BOOK_SIZE.

Rather than use two String[] arrays to store the user's data, the PhoneBook class should include the fields necessary (name, streetAddress, city, zip, phoneNumber, etc.) to store the required data.
Was This Post Helpful? 2
  • +
  • -

#3 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: PhoneBook App

Posted 23 August 2013 - 08:14 AM

Firstly, your constructor should not take in an array of names and numbers. I know the instruction says that it should fill the arrays of data, but it is ambiguous. It could mean that it wants you to fill the arrays with empty strings (an empty string is data, nothing is not), this however, conflicts with the other requirements of finding the first null element, since nothing is null.

Quote

1. No method should use hard numbers to indicate sizes. Use a constant (final) field so that the size of this data structure can be updated easily. (That is, we might want to use more than 10 names and numbers later.)


This means that you should have in your class

private final int MAX_RECORDS = 10;


and now instead of using 10 else where in your code (the same 10). You should replace it with MAX_RECORDS instead.

Edit:

View PostGregBrannon, on 23 August 2013 - 11:43 AM, said:

Rather than use two String[] arrays to store the user's data, the PhoneBook class should include the fields necessary (name, streetAddress, city, zip, phoneNumber, etc.) to store the required data.


This goes against requirement #2. However, if this can be done, it is the way to go.

This post has been edited by Mylo: 23 August 2013 - 08:28 AM

Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: PhoneBook App

Posted 23 August 2013 - 07:07 PM

the main() method is there just to launch your application, it should have 2 or 3 lines
the rest should happen in your class instance
Was This Post Helpful? 1
  • +
  • -

#5 tgcbraun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-August 13

Re: PhoneBook App

Posted 26 August 2013 - 09:47 AM

Thanks guys. This is what Ive managed to come up with so far:

import java.util.Arrays;
import java.lang.*;

public class PhoneBook extends Object {
  
  private final int PHONE_BOOK_SIZE = 10;
  private String[] name;
  private String[] number;
  private int cur_entry;
  
  
        
  public PhoneBook() {
	name = new String[PHONE_BOOK_SIZE];
	number = new String[PHONE_BOOK_SIZE];
	cur_entry = 0;
   
  }
  
  public String toString(){
	  String data = "";
	  	data += name[cur_entry]+ "\t";
	  	data += number[cur_entry]+ "\n";
	return data; 
	  
  }
  
  
  public boolean setName(String Name, int index) {
	index = cur_entry;
	Name = name[index];
	if (index >= 0 && index < PHONE_BOOK_SIZE && Name != null)
		return true; else
		return false;
	
  }
   
  public boolean setNumber(String Number, int index) {
	  index = cur_entry;
	  Number = number[index];
		if (index >= 0 && index < PHONE_BOOK_SIZE && Number != null)
			return true; else
			return false;
	     
	  }
	
  public String[] getRecord(){
	  int index = cur_entry;
	  String[] getRecord = new String[2];
	  getRecord [0] = name[index];
	  getRecord[1] = name[index]; 
	  if (index >= 0 && index < PHONE_BOOK_SIZE)		   
	  return getRecord; else 
		  return null;  
			 
  }
  
  public int nextIndex (){
	  
	  for (int nextIndex=0; nextIndex < PHONE_BOOK_SIZE; nextIndex++)
		  if (name[nextIndex] == null)
			  return nextIndex; 
	  return -1;
  }

  
  public int getRecord(int index) {
	index = cur_entry;	
	String[] getRecord = new String[PHONE_BOOK_SIZE];
	if (name[index] != null)
		if (getRecord[index] == name[index])
		return index;
	return -1;
}



Of course its not working properly, as the code has numerous misconceptions. My goal now is to try to identify all these errors.

The first questions that come to my mind are:


1. Regarding the int cur_entry, is it possible to define a method that matches it to the current index number of both arrays?


2. There are currently two errors on the main class regarding both getRecord() methods. The first one at line 28 (main class is shown on the first post) says that The method getRecord(int) in the type PhoneBook is not applicable for the arguments (String) and the other at line 30 says Type mismatch: Cannot convert from int to String[]. How should I proceed trying to fix these?


Thanks again!
Was This Post Helpful? 0
  • +
  • -

#6 ChrisNt  Icon User is offline

  • cute bug

Reputation: 222
  • View blog
  • Posts: 696
  • Joined: 31-July 13

Re: PhoneBook App

Posted 26 August 2013 - 10:06 AM

Quote

There are currently two errors on the main class regarding both getRecord() methods. The first one at line 28 (main class is shown on the first post) says that The method getRecord(int) in the type PhoneBook is not applicable for the arguments (String) and the other at line 30 says [i]Type mismatch: Cannot convert from int to String[]. How should I proceed trying to fix these?


You are passing wrong parameter value,
this method accepts an int value as parameter
  public int getRecord(int index)



but here you are trying to pass a String as argument.
pb.getRecord("mike")



This method returns an int,
 public int getRecord(int index) {
		index = cur_entry;	
		String[] getRecord = new String[PHONE_BOOK_SIZE];
		if (name[index] != null)
			if (getRecord[index] == name[index])
			return index;
		return -1;



but here you are trying to pass that value to a String array(this is not how you pass a value to an array which i see you have not initialized).

 String temp[] = pb.getRecord(1);


This post has been edited by ChrisNt: 26 August 2013 - 10:21 AM

Was This Post Helpful? 2
  • +
  • -

#7 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: PhoneBook App

Posted 26 August 2013 - 10:21 AM

That code, in terms of it's structure, is significantly better than at first, it's good to see improvement. However, it still has many flaws logic wise, some are more of redundancies over problems:

1. Every class implicitly extends Object, there is no need to do it yourself.
2. Numerical variables are set to 0 by default, no need to do that in the constructor.
3. The cur_entry variable is useless, you don't need it. The guidelines ask for a method that returns the next null index. With having methods like setName(index), cur_entry makes no sense.
4. toString() doesn't meet the requirements, it has asked to print out a representation of the entire class, presumably meaning all entries in the PhoneBook.
5. SetName()/SetNumber() You set the parameter to whats in the record, which is the wrong way around. Flip y = x to x = y.
6. SetName()/SetNumber() do the assignment operations BEFORE you check if the index is valid.
7. GetRecord() + the overload, do not need to modify any variable. Hint: Any 'Getter' should not need to modify anything. It should simply return the records array. It also does not take in the required parameters.

There is some more things, but those are the main points. The guidelines you have been given are quite poorly written which doesn't help though.
Was This Post Helpful? 2
  • +
  • -

#8 tgcbraun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-August 13

Re: PhoneBook App

Posted 26 August 2013 - 01:17 PM

Thanks a lot to everyone, especially to Mylo, for such helpful tips. Im almost finished with this one.

Last question: Regarding toString() method, would this method satisfy the requirements?

  public String toString(){
for (int nextIndex=0; nextIndex < PHONE_BOOK_SIZE; nextIndex++)
	  return ("Name:" + name[nextIndex] + "\t" + "Phone Number:" + number[nextIndex] + "\n");
	return null;

Was This Post Helpful? 0
  • +
  • -

#9 ChrisNt  Icon User is offline

  • cute bug

Reputation: 222
  • View blog
  • Posts: 696
  • Joined: 31-July 13

Re: PhoneBook App

Posted 26 August 2013 - 02:19 PM

No,methods cant return more than once.

This post has been edited by ChrisNt: 26 August 2013 - 02:22 PM

Was This Post Helpful? 1
  • +
  • -

#10 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 138
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: PhoneBook App

Posted 26 August 2013 - 02:33 PM

Try something like,

    @Override
    public String toString() {
        String str = "";
        for(int i = 0; i < size; i++)
            str += ("String " + array[i] + "\tString2 " + anotherArray[i] + "\n");
        return str;
    }



Add everything into the string, and then return that string instead of trying to return each line which won't work.
Was This Post Helpful? 1
  • +
  • -

#11 tgcbraun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-August 13

Re: PhoneBook App

Posted 26 August 2013 - 05:39 PM

Thank you guys very much! This is the most friendly and helpful coding community I've stumbled upon so far.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1