• 0

[Java] I'm new to it, and I need prompt help with it :( Simple code.


Question

Kia Ora guys,

I'm just a bit phased out today, and there's a Java test thing I'm trying to understand. I'm most of the way through, but I'm stuck on the last part. The program we're using is BlueJ, if that's any help at all. Here are my instructions and code for the part I'm stuck on:

SECTION H: The Name class

=========================

Now you'll use what you've learnt to create a class called Name. An object of

the class Name will store a full name, and allows you to call methods to

retrieve the name in different formats.

A name stored by a Name object will always consist of a first name, middle name,

and last name, such as "Richard John Lobb". You can assume that a name will always

have those three parts to it.

1. Open up the code for the Name class.

2. Complete the code for the constructor.

3. Complete the code for getFullName and getFirstName. Test your method

with the name "Richard John Lobb". Make sure it returns the correct full name

and correct first name.

4. Complete the code for getLastName.

When you code this method you might find there is something you don't know

how to do. Look in the Java API to see if there is a method available to you

that may solve your problem.

Test your method.

5. Complete the code for getMiddleName.

Test your method.

6. Try your methods on different names to ensure your program works in all cases.

Make sure you use names which have a first name, middle name, and last name.

SECTION I: Calling other methods

================================

1. Learn what the getRegularName method does. Run it and look at its code

to find out.

2. Add a method called getNameLastFirst. This method should return the name

in 'last first' format. So if the full name is "Richard John Lobb" the

method should return "Lobb, Richard John". Make sure you call the

other methods you've already coded instead of writing them again.

3. Add a method called getInitials. This method should return the initials of the

person. So if the full name is "Richard John Lobb" the method should return

"R.J.L."

4. Add a method called changeLastName. This method should take a new last name

as input, and change the last name stored by the object to this new last name.

Make sure you call the other methods you've already coded instead of writing

them again.

SECTION J: A Question of Design

===============================

1. The Name class has a single field, 'fullName'. Calls to methods like

getFirstName, getLastName and getNameLastFirst require that we perform

string operations on that fullName field, computing the new properties

of the object "on the fly".

Your task in this question is to create a new class called 'Name2' which

is an alternative *implementation* of the Name class. This new class should

have not one but THREE fields, called firstName, middleName and lastName.

The *signatures* of its methods and its constructors should be exactly as

for the Name class (except that its constructors are of course called 'Name2'

rather than 'Name').

However, most of the *bodies* of the methods and constructors will need

to be changed to work with a different representation of the object

state (i.e., different fields). Some of the methods of this new class,

like getFirstName and getLastName, will be easier because the first

and last names are now stored separately. Others, like getFullName,

will be a little harder (because the full name now has to be computed

"on the fly").

Here's the code I have to work with:

/**
 * A Name is a class that holds a person's name and performs various operations on it.
 */
public class Name
{
	private String fullName;

	/**
	 * Constructor for objects of class Name
	 * 
	 * @param newFullName The name of the person to be represented by this name object
	 */
	public Name(String newFullName)
	{
		// IMPLEMENT ME!
	}

	/**
	 *  Returns the full name of the person.
	 */
	public String getFullName()
	{
		return ""; // *** IMPLEMENT ME PROPERLY! ***
	}

	/**
	 *  Returns the first name of the person.
	 *  Example: if the full name is "Richard John Lobb" then returns "Richard"
	 */
	public String getFirstName()
	{
		return ""; // *** IMPLEMENT ME PROPERLY! ***
	}

	/**
	 *  Returns the last name of the person.
	 *  Example: if the full name is "Richard John Lobb" then returns "Lobb"
	 */
	public String getLastName()
	{
		return ""; // *** IMPLEMENT ME PROPERLY! ***
	}

	/**
	 *  Returns the middle name of the person.
	 *  Example: if the full name is "Richard John Lobb" then returns "John"
	 */
	public String getMiddleName() 
	{
		return ""; // *** IMPLEMENT ME PROPERLY! ***
	}

	/**
	 *  Returns the normal name for the person. That is just their first name 
	 *  and last name.
	 *  Example: if the full name is "Richard John Lobb" then returns "Richard Lobb"
	 */
	public String getRegularName() {
		 // the following line calls your methods getFirstName and getLastName,
		 // and uses their return values as part of another expression
		return getFirstName() + " " + getLastName();
	}

 }

I really want to get this done in the next 3 hours, so any help at all would be marvellous. I'll check this thread again in an hour and a half from the time of posting.

Thanks in advance. I hate being new to Java :|

(additionally, this is just a quiz, not work any marks; I'm not a cheater :))

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

This calls for approximately 20 lines of code. I'm not sure how I can help you without giving you the answer directly. The assignment is extremely clear. The only thing you have to "figure out" is how to retrieve part of the name from the full name. As we all know, they are divided by whitespaces.

To test the class, just create a little program that includes the class, creates a Name and Name2 object, calls their methods and checks if the return values are what is expected (for instance, output them to the console).

Link to comment
Share on other sites

  • 0
This calls for approximately 20 lines of code. I'm not sure how I can help you without giving you the answer directly. The assignment is extremely clear. The only thing you have to "figure out" is how to retrieve part of the name from the full name. As we all know, they are divided by whitespaces.

To test the class, just create a little program that includes the class, creates a Name and Name2 object, calls their methods and checks if the return values are what is expected (for instance, output them to the console).

Would it require at all using .indexOf or .subString? It probably is really easy, but my grandfther just passed away an I've had my mind on other things :(

Link to comment
Share on other sites

  • 0
I'm sorry for your loss.

String myString = "Hello World";
String[] words = myString.split(" ");

words should now contain the strings "Hello" and "World". I can't guarantee the syntax is correct but that's the method you should use. See http://www.java-examples.com/java-string-split-example

Thanks :) I'm gunna knuckle down into it now, see how I go.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.