PDA

View Full Version : Anyone know any Java?



Ginofan
04-05-2010, 11:17 AM
I'm taking a Java programming class and I'm having a bit of trouble with my second project.

Project #2 (http://www.accd.edu/sac/math/faculty/yng/classes/spring2010/cosc1315/projects/project2.pdf)

I have it all written but the one thing I'm not getting is this part:
1. public Student(String first, String middle, String last) -- constructor. The name should be stored in the case given; don't convert to all upper or lower case. If a student does not have a middle name, the middle should be set as “none”. Assume no student’s middle name is “none”.

I'm guessing there has to be an if-else statement but I don't know how to write it out where the results will only be a 2 character initial if the student enters "none" as their middle name.

Any suggestions?

ElNono
04-05-2010, 12:08 PM
Here's some pseudo code:



public Student(String first, String middle, String last)
{
if ( middle.length == 0 )
middle = "none";

String initials;

if ( middle == "none" )
initials = first[0] + last[0];
else
initials = first[0] + middle[0] + last[0];
}



I haven't written stuff in Java in ages, so I don't remember the language that well anymore. Those + at the end mean concatenate characters.

TheTruth
04-05-2010, 01:32 PM
Here's some pseudo code:



public Student(String first, String middle, String last)
{
if ( middle.length == 0 )
middle = "none";

String initials;

if ( middle == "none" )
initials = first[0] + last[0];
else
initials = first[0] + middle[0] + last[0];
}



I haven't written stuff in Java in ages, so I don't remember the language that well anymore. Those + at the end mean concatenate characters.

Thanks for trying I'll give it a whirl and see what happens.

-Ginofan-

baseline bum
04-05-2010, 02:25 PM
EDIT: oops.. let me change this real quick

Nono had the right idea overall, except it should be put it in the method initials() instead of the constructor. Also, the method charAt needs to be used instead of operator []; e.g., use str.charAt(0) to get the first character of the String str instead of str[0], which only works on arrays.

Also, strings should always be compared using the method equals().

e.g., the statement

if (middle == "none")

checks if middle is the exact same string as "none" (i.e., its the exact same object in memory), while the code


if (middle.equals("none"))

checks if middle and "none" are just strings with the value "none"



public String initials()
{
if (midName.equals("none")) {
return firstName.charAt(0) + lastName.charAt(0);
} else {
return firstName.charAt(0) + midName.charAt(0) + lastName.charAt(0);
}
}



assuming firstName, midName, and lastName are the names of the private members you allocated for the first, middle, and last names.

ElNono
04-05-2010, 03:38 PM
Well, that's why I said it was pseudocode... :lol

But thanks for the 'Javaification' baseline :tu

baseline bum
04-05-2010, 04:08 PM
Well, that's why I said it was pseudocode... :lol

But thanks for the 'Javaification' baseline :tu

:lol I always want to think in terms of C++ too. I curse UCLA for using C++ for the intro CS classes. Or maybe I should curse Java for not supporting operator overloading. Or maybe I should curse them both and wish I was using LISP. :lol

Scola
04-05-2010, 06:23 PM
I'm pretty decent at Java, but it looks like the assignment was due today... probably a bit too late by now. Anyway, post again if your still having problems.

ElNono
04-05-2010, 06:44 PM
:lol I always want to think in terms of C++ too. I curse UCLA for using C++ for the intro CS classes. Or maybe I should curse Java for not supporting operator overloading. Or maybe I should curse them both and wish I was using LISP. :lol

I'm mostly doing objective C these days, and I know I have a php project down the line. I actually find ObjC a lot more comfortable than C++ these days.
Must be because of the lazy allocation and less complicated runtime.

baseline bum
04-08-2010, 09:11 PM
I have to give a plug to my two favorite Java books:

For beginning programmers using Java:
http://ecx.images-amazon.com/images/I/51Mr4pCgr6L._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA300_SH20_OU01_.jpg (http://www.amazon.com/Head-First-Java-Kathy-Sierra/dp/0596009208/ref=sr_1_1?ie=UTF8&s=books&qid=1270778961&sr=8-1)

For somewhat seasoned programmers who want to learn Java:
http://ecx.images-amazon.com/images/I/51fmVu24qaL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA300_SH20_OU01_.jpg (http://www.amazon.com/Core-Java-TM-I-Fundamentals-8th/dp/0132354764/ref=sr_1_1?ie=UTF8&s=books&qid=1270779041&sr=8-1)

They're both really solid for their target audiences.

Libri
04-17-2010, 01:51 AM
I have to give a plug to my two favorite Java books:

For beginning programmers using Java:
http://ecx.images-amazon.com/images/I/51Mr4pCgr6L._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA300_SH20_OU01_.jpg (http://www.amazon.com/Head-First-Java-Kathy-Sierra/dp/0596009208/ref=sr_1_1?ie=UTF8&s=books&qid=1270778961&sr=8-1)

For somewhat seasoned programmers who want to learn Java:
http://ecx.images-amazon.com/images/I/51fmVu24qaL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA300_SH20_OU01_.jpg (http://www.amazon.com/Core-Java-TM-I-Fundamentals-8th/dp/0132354764/ref=sr_1_1?ie=UTF8&s=books&qid=1270779041&sr=8-1)

They're both really solid for their target audiences.

I'm thinking about teaching myself some Java. I'm going to check out that first book.