JAVA ÇÁ·Î±×·¡¹Ö ¼÷Á¦Á» µµ¿ÍÁÖ¼¼¿ä!!!

   Á¶È¸ 5931   Ãßõ 0    

¹®Á¦ :

Using the rules of encapsulation, design a class called Name that stores a person's first, middle, and last names and provides the following methods:
public Name(String first, String middle, String last)?constructor. Initializes the instance data.  The name should be stored in the case given; don't convert to all upper or lower case.

public Name(String last )? overloaded version of the constructor.  Initializes the lastname instance data variable only


public String getMiddle()?returns the middle name

public String getLast()?returns the last name

public String getFirst()?returns the first name

public void setFirst()?sets the first name

public void setMiddle()?sets the middle name

public void setLast()?sets the last name

public String firstMiddleLast()?returns a string containing the person's full name in order, e.g., "Mary Jane Smith".

public String lastFirstMiddle()?returns a string containing the person's full name with the last name first followed by a comma, e.g., "Smith, Mary Jane".

public boolean equals(Name otherName)?returns true if this name is the same as otherName. Comparisons should not be case sensitive. (Hint: There is a String method equalsIgnoreCase that is just like the String method equals except it does not consider case in doing its comparison.)

public String initials()?returns the person's initials (a 3-character string). The initials should be all in upper case, regardless of what case the name was entered in. (Hint: Instead of using charAt, use the substring method of String to get a string containing only the first letter?then you can upcase this one-letter string.)

public int length()?returns the total number of characters in the full name, not including spaces.

Now write a driver program called TestNames.java that prompts for and reads in two names from the user (you'll need first, middle, and last for each name).  Create two separate Name objects using both versions of your constructor.  In one instance, you'll have to explicitly set some of the values for your instance data after the object is instantiated by calling mutator methods. 

For each Name object, display the following using appropriate labels:

    first-middle-last version

    last-first-middle version

    initials

    length

    Tell whether or not the two names are the same.



Á¦°¡ Áö±Ý±îÁö ÇÑ ÄÚµùµé

Name.java


public class Name
{
    private String first;
  private String middle;
  private String last;
 
public Name(String nameFirst, String nameMiddle, String nameLast)
  {
    first = nameFirst;
  middle = nameMiddle;
last = nameLast;      
  }

public Name(String nameLast)
{
last = nameLast;
first = " ";
middle = " ";
}

public String getMiddle()
{
return middle;
}

public String getLast()
{
return last;
}

public String getFirst()
{
return first;
}

public void setFirst (String nFirst)
{
        first = nFirst;
}

public void setMiddle(String nMiddle)
{
middle = nMiddle;
}

public void setLast(String nLast)
{
last = nLast;
}


public String firstMiddleLast()
{
return (first + "\t" + middle + "\t" + last);
}

public String lastFirstMiddle()
{
return (last + "," + first + "\t" + middle);
}
}


testname.java

public class TestNames
{

  public static void main (String[] args)
  {

Name name1 = new Name ("James", "Smith", "Mary");

System.out.println (name1);
  }
}



¹®Á¦°¡ ¿µ¾îÀε¥.. Çѱ¹¸»·Î ¹ø¿ªÇÏ´Â ÁßÀÔ´Ï´Ù..

¿À´Ã±îÁøµ¥..µµ¿ÍÁÖ¼¼¿ä!!!
ªÀº±Û Àϼö·Ï ½ÅÁßÇÏ°Ô.
ÀÌâ½Å 2009-04
http://kin.naver.com/detail/detail.php?d1id=1&dir_id=10106&eid=z50wKFXQh50c57KUI+a+0piBgiZVkViJ&qb=Zmlyc3QtbWlkZGxlLWxhc3QgdmVyc2lvbg==&enc=utf8

¿©±â ³ª¿Í Àִ°Š°°³×¿ä Âü°íÇÏ½Ã¸é ½±°Ô ÇØ°áÇϽǵíÇÕ´Ï´Ù
Á¤Èñ¼· 2009-04
Using the rules of encapsulation,
Á¶°Ç : ĸ½¶È­ (private , protected)¸¦ »ç¿ëÇÒ°Í
design a class called Name that stores a person's first, middle, and last names and provides the following methods:
»ç¶÷ÀÇ À̸§ , ¼º , Áß°£À̸§ À» ÀúÀåÇÏ¸ç  ´ÙÀ½ÀÇ ¸Þ¼Òµå¸¦ °¡Áö´Â Name À̶ó´Â Ŭ·¡½º¸¦ µðÀÚÀÎ ÇϽÿÀ.


public Name(String first, String middle, String last)?constructor.
±¸¼ºÀÚ (»ý¼ºÀÚ) ÀÌ°Ç ¾Æ½ÃÁÒ?
Initializes the instance data.
ÀνºÅϽºÀÇ µ¥ÀÌÅ͸¦ ÃʱâÈ­ ÇÑ´Ù.
The name should be stored in the case given; don't convert to all upper or lower case.
À̸§Àº ´ë¹®ÀÚ³ª ¼Ò¹®ÀÚ·Î º¯È¯ÇÏÁö ¸»°í ´ë¼Ò¹®ÀÚ°¡ ÁÖ¾îÁø´ë·Î  ÀúÀåµÇ¾î¾ß ÇÑ´Ù.


public Name(String last )? overloaded version of the constructor.
¿À¹ö·ÎµåµÈ ±¸¼ºÀÚ(»ý¼ºÀÚ)
Initializes the lastname instance data variable only
ÀνºÅϽºÀÇ ¼º(lastname) º¯¼ö¸¸ ÃʱâÈ­ ÇÑ´Ù.

public String getMiddle()?returns the middle name
String ŸÀÔÀ¸·Î Áß°£À̸§ (¿µ¾î±Ç¿¡¼­ ¾²´Â°Í...) ¹Ýȯ

public String getLast()?returns the last name
String ŸÀÔÀ¸·Î ¼ºÀ» ¹Ýȯ

public String getFirst()?returns the first name
String ŸÀÔÀ¸·Î À̸§À» ¹Ýȯ

public void setFirst()?sets the first name
À̸§ º¯¼ö¸¦ ¼³Á¤ÇÑ´Ù (= Ŭ·¡½ºÀÇ Ä¸½¶È­µÇ¾î ÀÖ´Â º¯¼ö¿¡ Á¢±ÙÇؼ­ ³»¿ëÀ» ¹Ù²Û´Ù)

public void setMiddle()?sets the middle name
Áß°£À̸§ º¯¼ö¸¦ ¼³Á¤ÇÑ´Ù.

public void setLast()?sets the last name
¼º º¯¼ö¸¦ ¼³Á¤ÇÑ´Ù.

public String firstMiddleLast()?returns a string containing the person's full name in order, e.g., "Mary Jane Smith".
À̸§ Áß°£À̸§ ¼º ÀÇ ¼ø¼­·Î µÇ¾îÀÖ´Â String µ¥ÀÌÅ͸¦ ¹ÝȯÇÑ´Ù
¿¹½Ã "¸Þ¸® Á¦ÀÎ ½º¹Ì½º"


public String lastFirstMiddle()?returns a string containing the person's full name with the last name first followed by a comma, e.g., "Smith, Mary Jane".
¼º À̸§ Áß°£À̸§ ÀÇ ¼ø¼­·Î µÇ¾îÀÖ´Â String µ¥ÀÌÅ͸¦ ¹ÝȯÇÑ´Ù
¿¹½Ã " ½º¹Ì½º, ¸Þ¸® Á¦ÀÎ'

public boolean equals(Name otherName)?returns true if this name is the same as otherName. Comparisons should not be case sensitive. (Hint: There is a String method equalsIgnoreCase that is just like the String method equals except it does not consider case in doing its comparison.)
´Ù¸¥ Name ŸÀÔ ÀνºÅϽº¸¦ ¹Þ¾Æ¼­ À̸§ÀÌ °°ÀºÁö È®ÀÎÇÑ´Ù. ´ë¼Ò¹®ÀÚ ±¸º° ¾ÈÇÑ´Ù.
¸Þ¼Òµå´Â ÈùÆ®¿¡ ÀÖ±º¿ä.

public String initials()?returns the person's initials (a 3-character string). The initials should be all in upper case, regardless of what case the name was entered in. (Hint: Instead of using charAt, use the substring method of String to get a string containing only the first letter?then you can upcase this one-letter string.)
String ŸÀÔÀ¸·Î À̴ϼÈÀ» ¹ÝȯÇÑ´Ù. (µ¥ÀÌÅÍ¿¡¼­ °¡Àå óÀ½¿¡ ÀÖ´Â ÇѱÛÀÚ¸¦ µû¼­ ¸¸µé¸é µÇ°ÚÁÒ JFK = john f kennedy)

public int length()?returns the total number of characters in the full name, not including spaces.
int Á¤¼öÇü ŸÀÔÀ¸·Î À̸§(Ç®³×ÀÓ)ÀÇ ±æÀ̸¦ ¹ÝȯÇÑ´Ù. ½ºÆäÀ̽º´Â Æ÷ÇÔÇÏÁö ¾Ê´Â´Ù.
¹Ú¿µ·Ï 2009-04
ÀÌâ½Å´Ô²²¼­ ´äº¯À» Áּ̽À´Ï´Ù.
Á¤Èñ¼·´Ô.. Á¦°¡ ¹®Á¦¸¦ ¹ø¿ªÇؼ­ ¿Ã·Á¾ßÇϴ°É.. ¸ø¿Ã¸°°Ì´Ï´Ù^^; ÀÌ ¹®Á¦¸¦ Ǫ´Â°Ô Áß¿äÇ߰ŵç¿ä..
¾Æ¹«Æ° °¨»çÇÕ´Ï´Ù ^^
¹èÁÖ±Ô 2009-04
public class Name {
   
    private String NAME_FIRST = null;
    private String NAME_MIDDLE = null;
    private String NAME_LAST = null;
    private static final String SPACE = " ";
    private static final String COMMA_SPACE = ", ";
   
    public Name(String first, String middle, String last) {
        this.NAME_FIRST = first;
        this.NAME_MIDDLE = middle;
        this.NAME_LAST = last;
    }
   
    public Name (String last) {
        this.NAME_FIRST = "";
        this.NAME_MIDDLE = "";
        this.NAME_LAST = last;
    }
   
 
    public String getFirst() {
        return NAME_FIRST;
    }

   
    public void setFirst(String name_first) {
        NAME_FIRST = name_first;
    }

   
    public String getLast() {
        return NAME_LAST;
    }

   
    public void setLast(String name_last) {
        NAME_LAST = name_last;
    }

   
    public String getMiddle() {
        return NAME_MIDDLE;
    }

   
    public void setMiddle(String name_middle) {
        NAME_MIDDLE = name_middle;
    }

   
    public String firstMiddleLast() {
       
        boolean firstOK = false;
        boolean secondOK = false;
        boolean thirdOK = false;
       
        if (this.NAME_FIRST != null && this.NAME_FIRST.length() != 0) {
            firstOK = true;
        }
       
        if (this.NAME_MIDDLE != null && this.NAME_MIDDLE.length() != 0) {
            secondOK = true;
        }
       
        if (this.NAME_LAST != null && this.NAME_LAST.length() != 0) {
            thirdOK = true;
        }
       
        StringBuffer buf = new StringBuffer();
       
        if (firstOK) {
            buf.append(this.NAME_FIRST);
        }
       
        if (firstOK && secondOK) {
            buf.append(SPACE);
            buf.append(this.NAME_MIDDLE);
        } else if (!firstOK && secondOK) {
            buf.append(this.NAME_MIDDLE);
        }
       
        if (secondOK && thirdOK) {
            buf.append(SPACE);
            buf.append(this.NAME_LAST);
        } else if (!secondOK && thirdOK) {
            buf.append(this.NAME_LAST);
        }

        return buf.toString();
    }
   
    public String lastFirstMiddle() {
       
        boolean firstOK = false;
        boolean secondOK = false;
        boolean thirdOK = false;
       
        if (this.NAME_FIRST != null && this.NAME_FIRST.length() != 0) {
            firstOK = true;
        }
       
        if (this.NAME_MIDDLE != null && this.NAME_MIDDLE.length() != 0) {
            secondOK = true;
        }
       
        if (this.NAME_LAST != null && this.NAME_LAST.length() != 0) {
            thirdOK = true;
        }
       
        StringBuffer buf = new StringBuffer();
       
        if (thirdOK) {
            buf.append(this.NAME_LAST);
        }
       
        if (thirdOK && firstOK) {
            buf.append(COMMA_SPACE);
            buf.append(this.NAME_FIRST);
        } else if (!thirdOK && secondOK) {
            buf.append(this.NAME_FIRST);
        }
       
        if (firstOK && secondOK) {
            buf.append(SPACE);
            buf.append(this.NAME_MIDDLE);
        } else if (!firstOK && secondOK) {
            buf.append(this.NAME_MIDDLE);
        }

        return buf.toString();
    }
   
    public boolean equals(String otherName) {

        if (otherName == null || otherName.length() == 0) {
            return false;
        }
       
        if ((otherName.toUpperCase()).equals(this.NAME_FIRST.toUpperCase())) {
            return true;
        } else {
            return false;
        }
    }
   
    public String initials() throws Exception {
       
        StringBuffer initial = null;
       
        try {
            initial = new StringBuffer();
            if (this.NAME_FIRST != null && this.NAME_FIRST.length() != 0) {
                initial.append(this.NAME_FIRST.substring(0, 1).toUpperCase());
            }
            if (this.NAME_MIDDLE != null && this.NAME_MIDDLE.length() != 0) {
                initial.append(this.NAME_MIDDLE.substring(0, 1).toUpperCase());
            }
            if (this.NAME_LAST != null && this.NAME_LAST.length() != 0) {
                initial.append(this.NAME_LAST.substring(0, 1).toUpperCase());
            }

        } catch (Exception e) {
            throw e;
        }
        return initial.toString();
    }
   
    public int length() {
        return this.NAME_FIRST.length() + this.NAME_MIDDLE.length() + this.NAME_LAST.length();
    }
}



import java.io.*;

public class NameTest {

    public static void main(String[] args) {
       
        try {
            String[] nameFirst = new String[3];
            String[] nameSecond = new String[3];
           
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("please write First - firstName......");
            nameFirst[0] = in.readLine();
            if (nameFirst[0] == null || nameFirst[0].length() == 0) {
                System.out.println("input item is not usable....");
                System.exit(0);
            }
            System.out.println("You entered First - firstName:" + nameFirst[0]);
            System.out.println("");
           
            System.out.println("please write First - middleName......");
            nameFirst[1] = in.readLine();
            if (nameFirst[1] == null || nameFirst[1].length() == 0) {
                System.out.println("input item is not usable....");
                System.exit(0);
            }
            System.out.print("You entered First - middleName:" + nameFirst[1]);
            System.out.println("");
            System.out.println("");
           
            System.out.println("please write First - lastName......");
            nameFirst[2] = in.readLine();
            if (nameFirst[2] == null || nameFirst[2].length() == 0) {
                System.out.println("input item is not usable....");
                System.exit(0);
            }
            System.out.print("You entered First - lastName:" + nameFirst[2]);
            System.out.println("");
            System.out.println("");
           
           
            System.out.print("please write Second - firstName......");
            nameSecond[0] = in.readLine();
            if (nameSecond[0] == null || nameSecond[0].length() == 0) {
                System.out.println("input item is not usable....");
                System.exit(0);
            }
            System.out.println("You entered Second - firstName:" + nameSecond[0]);
            System.out.println("");
           
            System.out.println("please write Second - middleName......");
            nameSecond[1] = in.readLine();
            if (nameSecond[1] == null || nameSecond[1].length() == 0) {
                System.out.println("input item is not usable....");
                System.exit(0);
            }
            System.out.print("You entered Second - middleName:" + nameSecond[1]);
            System.out.println("");
            System.out.println("");
           
            System.out.println("please write Second - lastName......");
            nameSecond[2] = in.readLine();
            if (nameSecond[2] == null || nameSecond[2].length() == 0) {
                System.out.println("input item is not usable....");
                System.exit(0);
            }
            System.out.print("You entered Second - lastName:" + nameSecond[2]);
            System.out.println("");
            System.out.println("");
            System.out.println("");
           
            Name name1 = new Name(nameFirst[0], nameFirst[1], nameFirst[2]);
            Name name2 = new Name(nameSecond[2]);
           
            System.out.println("----------------- first Name Object ------------------------");
            System.out.println("first-middle-last version: " + name1.firstMiddleLast());
            System.out.println("last-first-middle version: " + name1.lastFirstMiddle());
            System.out.println("initials: " + name1.initials());
            System.out.println("length: " + name1.length());
            System.out.println("Tell whether or not the two names are the same: " + name1.equals(nameSecond[2]));
            System.out.println("");
            System.out.println("");
            System.out.println("");           
            System.out.println("----------------- second Name Object ------------------------");
            System.out.println("first-middle-last version: " + name2.firstMiddleLast());
            System.out.println("last-first-middle version: " + name2.lastFirstMiddle());
            System.out.println("initials: " + name2.initials());
            System.out.println("length: " + name2.length());
            System.out.println("Tell whether or not the two names are the same: " + name2.equals(nameFirst[2]));
           
           
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


QnA
Á¦¸ñPage 3706/5686
2014-05   4981965   Á¤ÀºÁØ1
2015-12   1518053   ¹é¸Þ°¡
2021-04   5929   VSPress
2015-08   5929   ±èÈÆ1
2012-03   5930   ÀÌÁ¤±Ù
2011-09   5930   ³ª³Ê¿ì¸®
2014-05   5930   ½º¿ö´Ï
2019-04   5930   Win31
2015-02   5930   ¸ó½º
2005-05   5930   ±èÀå¿ø
2009-09   5930   ±èÃæȯ
2015-12   5930   ±è°Ç¿ì
2008-06   5930   ¿ø³«¿¬
2011-12   5930   Âü¼¼»ó¸Í±Û±â
2017-09   5930   ±èÁ¦¿¬
2017-08   5930   ŸÀÌÈ£
2013-09   5930   ÀÌÇö¼®1
2008-07   5930   Á¤¼ºÈÆ
2013-07   5930   ±è°Ç¿ì
2005-08   5930   Á¶¸íÁø
2012-01   5930   ¿À»óÈÆ
2017-02   5931   ÀÌ°ÇÈñ