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 trialNeelesh Tewani
1,239 Pointsplease help me to solve this challenge
// i am not able to understand the challenge
package com.example;
import java.util.Date;
public class BlogPost {
private String mAuthor;
private String mTitle;
private String mBody;
private String mCategory;
private Date mCreationDate;
public BlogPost(String author, String title, String body, String category, Date creationDate) {
mAuthor = author;
mTitle = title;
mBody = body;
mCategory = category;
mCreationDate = creationDate;
}
public String getAuthor() {
return mAuthor;
}
public String getTitle() {
return mTitle;
}
public String getBody() {
return mBody;
}
public String getCategory() {
return mCategory;
}
public Date getCreationDate() {
return mCreationDate;
}
}
import com.example.BlogPost;
public class TypeCastChecker {
/***************
I have provided 2 hints for this challenge.
Change `false` to `true` in one line below, then click the "Check work" button to see the hint.
NOTE: You must set all the hints to false to complete the exercise.
****************/
public static boolean HINT_1_ENABLED = false;
public static boolean HINT_2_ENABLED = false;
public static String getTitleFromObject(Object obj) {
// Fix this result variable to be the correct string.
String result = "";
return result;
}
}
1 Answer
Steve Hunter
57,712 PointsHi Neelesh,
The first question is: The method getTitleFromObject
will be called and passed a String
and/or a com.example.BlogPost
. For this first task, return the object obj
type casted as a String
if it is in fact a String
.
There are two hints that you can see in the code. Start by setting the first one to true
and clicking Check Work. You'll get a hint that says: HINT 1: You can check the type of an object using the instanceof keyword. What if you tried if (obj instanceof String) {...?
OK - set the true
marker back to false
. SO, if the obj
is a String
return it as a String
- else ... well, we'll worry about that in a minute! The first part clearly needs an if
test. We're testing if obj
is a String
:
if(obj instanceof String){
return (String) obj;
}
That code does what the question asks - using the HINT as guidance - if the obj
is a String
return it cast as a String
. Make sense?
Right, now change the other false
to true
nd see what the question and hint are:
Q: Now make sure that if a com.example.BlogPost
is passed in for obj
that you then cast it to a BlogPost
. Return the results of the getTitle
method on the newly type-casted BlogPost
instance.
HINT: (I can't get that to work ... sorry!
So the question wants to act on a BlogPost - that means we're in the else
part of the if
statement as there are only two possibilities of the type of obj
, String
or BlogPost
. So, we want to cast obj
to a BlogPost
then call the getTitle()
method on it. OK? here's the cast:
(BlogPost) obj
So that is a BlogPost
. We want to call a method on it:
((BlogPost) obj).getTitle();
That's what needs returning out of the else
clause.
The completed code, for the whole if
statement, looks like:
if(obj instanceof String){
return (String) obj;
} else {
return ((BlogPost) obj).getTitle();
}
Delete the last two lines regarding results
and make sure both your hints are set to false
.
That should see you through it! A tough challenge.
I hope that made sense.
Steve.
Gonzalo Torres del Fierro
Courses Plus Student 16,751 PointsGonzalo Torres del Fierro
Courses Plus Student 16,751 PointsSteve, thx for the answer, very clear. when i was looking for some help, i saw many solutions many times, and each of them had alot of sense, but the second hint, was not clear, because everybody gived the solution, and i was just solving the first part of the task. You let me very clear that if i want to complete the task, i have to change the second false to true...and keep going,.. well for a person like me, not having english as a mother tongue, sometimes get the whole idea, g more complicated...thx again
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsNo problem, Gonzalo; glad to have helped!