email - How to test my java program? -
i asked write part of program , test in following way:
add statements main method create 2 or 3 contact instances, add them address book, , search 1 or 2 of them. display result of each search see if retrieved correctly. remove 1 of them , show list sure removed correctly.
i wasn't entirely sure how test this. should write in main class? also, rest of code correct?
do contact class:
create class in client package. class should have 3 fields (an email address, full name, nick name). of these should strings.
provide 2 constructors: 1 full constructor (parameters 3 member variables), , 1 has parameter email address. second constructor leave other 2 fields null.
add tostring method (and use @override
annotation) works this:
contact c1 = new contact("jenny@gmail.com"); c2.tostring() should return "jenny@gmail.com". contact c2 = new contact("jenny@gmail.com", "jennifer abott", "jenny"); c2.tostring() should return "jennifer abott (jenny) jenny@gmail.com".
do addressbook class:
- create class , have single member variable of type
arraylist<contact>
. define variable, don't declare not need constructor. - write add method has contact parameter , adds contact contact list.
- write remove method has string parameter, nick name of contact remove. returns nothing. remove contact contact list. hint: use search method wrote in order find contact, remove contact contact list. see online documentation arraylist how remove method works in arraylist class. sure remove method not crash if give nick name not exist in list.
- write search method has string parameter, nick name search for. method must iterate on contact list. if nick name found (use .equals), return contact. if no contact found, return null.
write show method displays each contact instance. has no parameters , returns nothing. display 1 contact per line, , number each line. this:
- jeff meunier (jeff)
- bill gates (money)
- vladimir putin (vman)
make sure numbers shown start @ 1, not @ 0.
contact class:
package client; public class contact { private string _emailaddress; private string _fullname; private string _nickname; public contact(string emailaddress, string fullname, string nickname) { _emailaddress = emailaddress; _fullname = fullname; _nickname = nickname; } public contact(string emailaddress) { _emailaddress = emailaddress; } @override public string tostring() { if(_fullname == null) { return "<" + _emailaddress + ">"; } else { return _fullname + " " + "(" + _nickname + ")" + " " + "<" + _emailaddress + ">"; } } }
address book class:
package client; import java.util.arraylist; public class addressbook { public arraylist<contact> contactlist = new arraylist<contact>(); public void add(contact _contact) { contactlist.add(_contact); } public contact search(string nickname) { (int n=0; n < contactlist.size(); n++) { if(contactlist.get(n).equals(nickname)) { return contactlist.get(n); } } return null; } public void remove(string nickname) { if(search(nickname) != null) { contactlist.remove(search(nickname)); } } public void show() { for(int n=0; n<contactlist.size(); n++) { system.out.println(n++ + ". " + contactlist.get(n).tostring()); } } }
i don't have in main class yet, here is:
main class
import java.util.arraylist; import client.contact; public class main { public static void main(string[] args) { contact c1 = new contact("jeffm@engr.uconn.edu"); contact c2 = new contact("jeffm@engr.uconn.edu", "jeff meunier", "jeff"); } }
"add statements main method create 2 or 3 contact instances, add them address book, , search 1 or 2 of them. display result of each search see if retrieved correctly. remove 1 of them , show list sure removed correctly."
if task then:
1. created few contact instances, done, create few more available class fields set (this needed next steps)
2. must create instance of address book class.
addressbook addressbook = new addressbook();
add created contacts adress book
addressbook.add(c1);
addressbook.add(c2);
// other contacts
use show method see if correct.
addressbook.show();
try removing 1 , show available contacts again see if worked.
addressbook.remove(c1.getnickname()); // make method nickname
addressbook.show();
then search contact name, assign new contact object , print acknowledge object correct one.
here logic can break, because search not cover possibillity have more 1 contacts same name. should return list of found contacts instead of 1 contact. implement , search again (make sure search objects unique , non-unique names testing acceptable).
your search testing @ end:
list <contact> contacts = addressbook.search("john"); for(contact contact in contacts) { system.out.println(contact.tostring()); }
now since delete method depends on search, have change bit too. when that, test remove method again above.
work out every step until works fine , done.
p.s. may want print description between different testing prints, can see results in output
Comments
Post a Comment