3 Replies - 1477 Views - Last Post: 04 April 2012 - 12:21 AM Rate Topic: -----

#1 fatooma-92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-April 12

What is Wrong here? bluej programming

Posted 03 April 2012 - 11:58 PM

frist,
Class Contact:
1. Create a class named Contact that holds a name and a phone number.
2. Create a constructor that accepts values for the two data fields. Within the constructor, assign each argument to the appropriate field.
3. Create getter methods for both fields.

i did this but its telling me that (Cannot find symbol- method name)

[*][code]public class Contact
[*]{
[*] private String name;
[*] private int number;
[*]
[*] public Contact(String name, int number){
[*] this.name = name;
[*] this.number = number;
[*] }
[*] public String getName(){
[*] return name();
[*] }
[*] public int getNumber() {
[*] return number;
[*] }


second, Class Phonebook:
1. Create a class named PhoneBook that holds a collection of Contacts (ArrayList).
2. Create a constructor that initializes your list.
3. Create method addContact() that takes a name and a number and adds them as a Contact to the list.
4. Create method getSize() that returns the size of this phonebook. 5. Create method printContacts() that prints all the contacts stored in this
phonebook.

[il]public class PhoneBook
{
private ArrayList <String> contacts;

public PhoneBook() {

contacts = new ArrayList <String>();
}

public void addContact(int, String) {

contacts.add(contacts);
numbers.add(number);
}
public int getSize() {

return contacts.size();
}
public void printContacts() {
for (String s: contacts) {
System.out.print ("S");
}
}
}

what am i doing wrong here

Is This A Good Question/Topic? 0
  • +

Replies To: What is Wrong here? bluej programming

#2 The_Programmer-   User is offline

  • Paranormal Investigator
  • member icon

Reputation: 25
  • View blog
  • Posts: 694
  • Joined: 24-October 11

Re: What is Wrong here? bluej programming

Posted 04 April 2012 - 12:05 AM

You forgot your main method.
Was This Post Helpful? 0
  • +
  • -

#3 Mylo   User is offline

  • Knows all, except most.

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

Re: What is Wrong here? bluej programming

Posted 04 April 2012 - 12:17 AM

It is this: [*] public String getName(){
[*] return name();

Name is not a function
Was This Post Helpful? 0
  • +
  • -

#4 karabasf   User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: What is Wrong here? bluej programming

Posted 04 April 2012 - 12:21 AM

Also, considering your code:

public class PhoneBook
{
	private ArrayList <String> contacts;

	public PhoneBook() {
		contacts = new ArrayList <String>();
	}

	public void addContact(int, String) {
		contacts.add(contacts);
		numbers.add(number);
	}

	public int getSize() {
		return contacts.size();
	}
	public void printContacts() {
		for (String s: contacts) {
		System.out.print ("S");
		}
	}
}



Let's take a look. The first line of your of the second class assignment states:

"Create a class named PhoneBook that holds a collection of Contacts (ArrayList)."

Currently, you're creating a an ArrayList containing strings instead of Contact objects. Thus ArrayList<String> contacts should be ArrayList<Contact> contacts

Second problem, lies in your add method. Consider:
	public void addContact(int, String) {
		contacts.add(contacts);
		numbers.add(number);
	}



This should be:
	public void addContact(int number, String contact) {
	       //Make a new contact to hold the number and contact
               //Add the new contact to the arraylist
	}



What went wrong in your original code? First, there does not exist an ArrayList called numbers (hence your error). Second, you're supposed to add a Contact Object and not an Integer number and a String Contact. You want to combine these as you want to have the data coupled with each other.

Third, the toString() method. You need to define a toString() method in the Contact class, something like this should do:

public String toString(){
  return this.name + " - " + this.number;
}



This piece of line will override the toString method, so I can use it to create a custom String representation of my Contact class. If I do NOT do this, the current toString() method will run and will show the memory reference (as the Contact class is not a primitive datatype)

Now, consider you're printContacts method in the Phonebook

	public void printContacts() {
		for (String s: contacts) {
		System.out.print ("S");
		}
	}



This won't work as you will only print out "S". That's certainly not in your phonebook ;) (At least, I assume)

First of all, applying the change I proposed in this post, you need to loop through every Contact in the Arraylist (and not through each string). And as we made a toString() method for the Contact class, we can do this:

	public void printContacts() {
		for (Contact phonebookEntry: contacts) {
		System.out.println (phonebookEntry);
		}
	}



This will neatly print all the contacts of my phonebook in the following format:

Entry Name 1 - Number of Name 1
.
.
.
.
Entry Name N - Number of Name N



Final tip:
Welcome to the board and use code tags instead of inline tags to display code in your post. Would've saved my a lot of hassle...

Hope this helps you out ^^

Edit: changed some formatting
Edit2: corrected typos

This post has been edited by karabasf: 04 April 2012 - 12:40 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1