07-25-2017, 02:29 PM
(07-24-2017, 05:29 PM)firenice03 link Wrote: Giving it a quick look... The part listed...
Although you have spacing between " + there isn't any coded spaces..Code:new Name("Cookie" + "Monster" + "Jr")
Maybe try, see if it results properly
Code:new Name("Cookie " + "Monster " + "Jr")
Hello firenice03, thank you for your suggestion. It did work, but there are still extra empty spaces that appeared in front of the name due to the toString method.
newName is supposed to appear as indicated by the toString method which has a space in between first, middle, and last name. The idea of the exercise is to to input each name alone without any spaces just like anyone filling up a field on a form etc., that's why toString method is used.
There are two Java files and they both worked together, and here's the first set of code (called Name.java) that sets the foundation (if that's how programmer(s) put it?):
Code:
public class Name
{
private String first;
private String middle;
private String last;
// constructor methods allow us to declare class objects and provide those objects with some data.
public Name(String f, String m, String l)
{
first = f;
middle = m;
last = l;
}
public Name(String f, String l)
{
first = f;
middle = "";
last = l;
}
public Name(String l)
{
first = "";
middle = "";
last = l;
}
//default constructor - it's a good idea to add one that doesn't have data in them
public Name()
{
first = "";
middle = "";
last = "";
}
public String toString()
{
return first + " " + middle + " " + last;
}
}
And here's the other file that has the data called NameTest.java:
Code:
public class NameTest
{
public static void main(String[] args)
{
// instantiation - creating an instance of the Name class
Name myName = new Name("Cookie" + "Monster" + "Jr");
Name yourName = new Name("Great" + "Cookie");
Name aName = new Name("Durr");
System.out.println("myName: " + myName.toString());
System.out.println("yourName: " + yourName.toString());
}
}
NameTest.java would not work without Name.java. These are the two files in their entirety. Hope this would give clarity to what the codes are supposed to do.