/** * class Person here. * * @author Fr Chris * @version Jan 13 2008 */ public class Person { // instance variables private String phone;//no punctuation private String first; private String last; private String mi; //middle initial /** * Constructor for objects of class Person */ public Person() { // initialize instance variables phone="1234567"; first="John"; last="Doe"; mi="Q"; } /** * Constructor that initializes with the Given Name * @param nm * Precondition: there is a space between the first name * and middle initial and another space between the * middle initial and the last name */ // put your code here for Part 2 /** * Constructor that initializes with the Given Name and number * @param nm * Precondition: there is a space between the first name * and middle initial and another space between the * middle initial and the last name, * the number is a String that is either * 7 or 10 digits */ // put your code here for Part 2 /** * setFirst * * @param nm becomes the persons First Name * */ public void setFirst(String nm) { // put your code here } /** * setLast * * @param nm becomes the persons Last Name * */ public void setLast(String nm) { // put your code here } /** * setMI * * @param i becomes the persons Middle Inital * */ public void setMI(String i) { // put your code here } /** * setPhone * * @param nm becomes the persons number * Precondition: the String must be a String * with either 7 or 10 digits, and nothing else * For example "8185551212" (no spaces) * */ public void setPhone(String nm) { phone=nm; } /** * getName returns the Complete name * * for example "Alice A. Anderson" * this method adds spaces and the "." after the * middle initial * * @return the Name */ public String getName() { // put your code here } /** * getPhone returns the Name * * @return the Name * * Precondition: the string is either 7 or 10 digits * is it is 7 digits, then the out put is xxx-xxxx * otherwise is it (xxx) xxx-xxxx * */ public String getPhone() { if (phone.length() >6) // put your code here else // put your code here } /** * if you dont overload the default toString method * it will return a memory address * * add code so it will return the Complete name and phone number * (separated by a tab) * for example: "Alice A. Anderson 123-4567" * */ public String toString() { // put your code here } }
/** * PersonTest tests the People Class * in order to get a Phone Book Class going * * @author Fr Chris Thiel * @version 13 Jan 2008 */ public class PersonTest { public static void main (String [] args) { Person p1 = new Person(); p1.setFirst("Alice"); p1.setLast("Anderson"); p1.setMI("A"); Person p2 = new Person(); p2.setFirst("Betty"); p2.setLast("Butters"); p2.setMI("B"); p2.setPhone("8185551234"); System.out.println(p1.getName() ); System.out.println(p1); System.out.println(p2.getName() ); System.out.println(p2); //Tests Part 2 (the overloaded constructors) //Person p3 = new Person("Dorothy D Dolan"); //System.out.println(p3); //Person p4 = new Person("Edith E Edwards", "2139876543"); //System.out.println(p4); } }
/** * PersonTest tests the People Class * in order to get a Phone Book Class going * * @author Fr Chris Thiel * @version 13 Jan 2008 */ public class PersonArrayTest { public static void main (String [] args) { Person [] array = new Person[3]; array[0] = new Person(); array[0].setFirst("Alice"); array[0].setLast("Anderson"); array[0].setMI("A"); array[1] = new Person(); array[1].setFirst("Babette"); array[1].setLast("Barker"); array[1].setMI("B"); array[2] = new Person(); array[2].setFirst("Charline"); array[2].setLast("Chase"); array[2].setMI("C"); //Tests the New Constructors //array[2]= new Person("Fanny F Flanders", "2131119999"); for (Person entry:array) System.out.println(entry); } }
import java.util.ArrayList; /** * PersonTest tests the People Class * in order to get a Phone Book Class going * * @author Fr Chris Thiel * @version 13 Jan 2008 */ public class PersonArrayListTest { public static void main (String [] args) { ArrayList<Person> list = new ArrayList<Person>(); list.add(new Person()); list.get(0).setFirst("Alice"); list.get(0).setLast("Anderson"); list.get(0).setMI("A"); list.add(new Person() ); list.get(1).setFirst("Babette"); list.get(1).setLast("Barker"); list.get(1).setMI("B"); list.add (new Person()); list.get(2).setFirst("Charline"); list.get(2).setLast("Chase"); list.get(2).setMI("C"); //Tests the New Constructors //list.add(new Person("Ginny G Grapefruit")); //list.add(new Person("Hilda H Hasthopper", "9871234")); for (Person entry:list) System.out.println(entry); } }
import java.util.ArrayList; import java.util.Scanner; /** * InputPersonTest tests the People Class * in order to get a Phone Book Class going * * @author Fr Chris Thiel * @version 13 Jan 2008 */ public class InputPersonTest { public static void main (String [] args) { System.out.println("Input Person Test Started\n(Type an empty line to end)"); Scanner in=new Scanner(System.in); ArrayList<Person> list = new ArrayList<Person>(); list.add(new Person("Hilda H Hasthopper", "9871234")); String s=" "; while (s.length()>0) { for (Person entry:list) System.out.println(entry); System.out.print("Full Name: "); s=in.nextLine(); if(s.length() > 0) { System.out.print("Number: "); String n=in.nextLine(); if (n.length()>0) { list.add(new Person(s,n) ); } else { list.add(new Person(s)); } } } System.out.println("Input Person Test Ended Normally"); } }