Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Angjelin Malaj
Angjelin Malaj
5,983 Points

Write a class named Personalizer with 2 instance parameters*, firstName and lastName. The Personalizer class has 4 metho

The Personalizer class has 4 methods: salutation, greeting, lengthOfNames, shortLong

1-The salutation method takes in an input string parameter sirOrMadam and returns the string "Dear [Sir | Madam]" For example, if the input string value is "Sir" than it returns the string "Dear Sir" If the input string value is "Madam" than it returns the string "Dear Madam"

2-The greeting method prints out to the console "Good Day".

3- The lengthOfNames method returns the length of the firstName and lastName combined. For example, If first name is "Hendrix" and last name is "Tavarez" it returns 14 "Hendrix" is 7 characters long and "Tavarez" is also 7 characters long. Hence, 7 + 7 = 14

4-The shortLong method returns the string short if the lengthOfNames method returns an integer less than 8 and it returns the string long if the lengthOfNames method returns an integer equal or greater than 8 write a test function main() that uses the class Personalizer to print out the following:

Dear [Sir | Madam]:

Good Day, [first_name] [last_name] has a very [short | long] name. It has [# of characters] characters.

Sincerely,

[Your Name Here]

For example, if the Personalizer's instance input is "Hendrix" and "Tavarez" and the salutation method's input is "Sir"

the test function should print out

Dear Sir:

Good Day, Hendrix Tavarez has a very long name. It has 14 characters.

Sincerely,

Your Java Developer

2 Answers

Here's a Personalizer class to get you started. The instance variables really should be private, but there were no getters or setters in the list of methods, so I made them public:

public class Personalizer {
   //FIELDS
   //these really should be private
   public String firstName;
   public String lastName;

   //CONSTRUCTOR
   //they didn't ask for a constructor, but you need one
   //if you are going to create objects with names
   public Personalizer(String firstName, String lastName) {
      this.firstName = firstName;
      this.lastName = lastName;
   }

   //METHODS
   public String salutation(String s) {
      return "Dear " + s;
   }

   public void greeting() {
      System.out.print("Good Day");
   }

   public int lengthOfNames() {
      return firstName.length() + lastName.length();
   }

   public String shortLong() {
      if (lengthOfNames() < 8) {
         return "short";
      } else {
         return "long";
      }
   }
}

And a unfinished program to test it with:

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

      Personalizer m = new Personalizer("Hendrix", "Taverez");

      System.out.println(m.salutation("Sir") + ":");
      System.out.println();
      m.greeting();
      System.out.println(", " + m.firstName + " " + m.lastName + " has a very " 
         + m.shortLong() + " name.");

      //add your code here

   }

}
Angjelin Malaj
Angjelin Malaj
5,983 Points

Mr. Jcorum thank you for your help! Its very helpful.

Regards

Angjelin Malaj
Angjelin Malaj
5,983 Points

Hello JCorum

I have an assignment in Java language and I was wondering if You could help me with that , since you have been very helpful with previously and I appreciate it. The question is :

Write a programs that given an arrays of coin flips list and count the number of heads or tails as follow:

Input:

coinFlips = { "Tail", "Tail", "Head", "Tail", "Tail", "Tail", "Head", "Head", "Tail", "Head" };

Output:

Heads

  1. index 2

  2. index 6

  3. index 7

  4. index 9

Total heads: 4

Tails

  1. index 0

  2. index 1

  3. index 3

  4. index 4

  5. index 5

  6. index 8

Total tails: 6

Finally

Add a method that randomly generates and return the array coinFlips