Results 1 to 6 of 6
  1. #1
    genocist is offline Member
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default Initials (what am i doing wrong)

    public class Name {


    private String firstName;
    private String middleName;
    private String lastName;
    private String initials;

    Name() {

    firstName = null;
    middleName = null;
    lastName = null;
    }


    Name(String newFirstName, String newMiddleName, String newLastName){
    firstName = newFirstName;
    middleName = newMiddleName;
    lastName = newLastName;
    }



    public void setFirstName(String newFirstName){
    firstName = newFirstName;
    }
    public void setMiddleName(String newMiddleName){
    middleName = newMiddleName;
    }
    public void setLastName(String newLastName){
    lastName = newLastName;
    }



    public String getFirstName(){
    return firstName;
    }
    public String getMiddleName(){
    return middleName;
    }
    public String getLastName(){
    return lastName;
    }


    public void concat(){
    String fullName=firstName.concat(middleName).co…
    int lengthName=fullName.length();
    System.out.println("The length in characters of " + firstName + " " +middleName+ " " + lastName + " is: " + lengthName);
    }

    public void substring(){
    initials = firstName.substring(0) + middleName.substring(0) + lastName.substring(0);
    System.out.println(initials);

    }

    public void lastNameFirst(){
    String s = lastName.concat(", ").concat(firstName).concat(" "+middleName.substring(0));
    System.out.println(s);
    }
    public void fullNameInitials(){
    String f = firstName.concat(" " +middleName).concat(" "+lastName+" ") + initials;
    System.out.println(f);
    }
    }


    public class NewNamesList {
    public static void main(String[] args){

    Name[] namesList = new Name[2];

    namesList[0] = new Name("Superman", "bin", "batman");
    namesList[1] = new Name("Ironman", "bin", "flash");

    namesList[0].concat();
    namesList[0].substring();
    namesList[0].lastNameFirst();
    namesList[0].fullNameInitials();

    namesList[1].concat();
    namesList[1].substring();
    namesList[1].lastNameFirst();
    namesList[1].fullNameInitials();
    }
    }


    this is what the teacher said

  2. #2
    genocist is offline Member
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default

    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.)

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Well, that is kinda wierd coming from a teacher, since there is a:
    Java Code:
    Character.toUpperCase('a');
    method, so I don't see how a one character string would be any worse for wear then an acctual character from a charAt() method.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  4. #4
    genocist is offline Member
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default

    public char charAt(int index){
    String initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0);
    System.out.println(intials);
    }


    i can use that, but it's not compiling for me.

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    No, you missed the point completely. Use the charAt() function to get the first letters of all 3 Strings, convert them to upper case with the Character.toUpperCase() method and use them to create another String, and return the newly formed String. charAt() is a method from the String class, I was saying that you can use that instead of substring. And seeing the charAt() method you've written up, I'd suggest to go back and start from the beginning, what are variables, what are methods, what are method arguments, what are methods supposed to return etc.
    Java Code:
    public char charAt(int index){ //what purpouse does the index serve?
          //why return
          //a char?
    String initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0); 
    System.out.println(intials);
    //you defined the methods return type to be char. Where is the return statement?
    //and the return type should definately not be char, think about what this method is supposed to do
    }
    Ever seen a dog chase its tail? Now that's an infinite loop.

  6. #6
    genocist is offline Member
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default

    i would start over but this assignment is due in 30 mins. So only option i have is to turn in what i have.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. what's wrong here ?
    By doha786 in forum New To Java
    Replies: 6
    Last Post: 03-26-2010, 12:21 PM
  3. what am i doing wrong here?
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-21-2010, 04:04 PM
  4. What am I doing wrong??
    By NoNickName in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 11:04 PM
  5. how do i initials the word so i get the first letter only
    By mikeitalydz in forum New To Java
    Replies: 15
    Last Post: 04-19-2009, 10:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •