31.3k views
4 votes
#Create a class called Name. Name should have two attributes

#(instance variables): first_name and last_name. Make sure

#the variable names match those words. Both will be strings.

#

#Name should have a constructor with two required parameters,

#one for each of those attributes (first_name and last_name,

#in that order).

#

#Name should also have two methods. The first should be

#called find_printed_name, and should return the first and

#last name with a space in between, e.g. "David Joyner". The

#second method should be called find_sortable_name, and

#should return the last name, then a comma and space, and

#then only the first initial, e.g. "Joyner, D".

#

#Neither sortable_name nor printed_name should be attributes:

#both should be created and returned when those methods are

#called. Neither method will have any parameters besides self.


#Write your class here!



#Below are some lines of code that will test your function.

#You can change the value of the variable(s) to test your

#function with different inputs.

#

#If your function works correctly, this will originally

#print

#David

#Joyner

#David Joyner

#Joyner, D

test_name = Name("David", "Joyner")

print(test_name.first_name)

print(test_name.last_name)

print(test_name.find_printed_name())

print(test_name.find_sortable_name())

User Yjsoon
by
8.6k points

1 Answer

4 votes

Answer:

//class Name

class Name {

// attributes gave you 3

String first_name;

String last_name;

int length = 0;

//constructor with 2 parameters

Name(String first_name, String last_name){

}

//methods

public String find_printed_name(){

return first_name + " " + last_name;

}

public void find_sortable_name(){

return last_name + ", " first_name.charAt(0);

}

public static void main(String[] args){

// instantiate the class

Name test_name = new Name("David", "Joyner");

System.out.println(test_name.first_name);

System.out.println(test_name.last_name);

System.out.println(test_name.find_printed_name());

System.out.println(test_name.find_sortable_name());

}

Step-by-step explanation:

You didn't specify a language so I did it in Java.

You should know how to declare methods in Python.

User Ctietze
by
8.8k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.