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 trialnazim khan
1,409 PointsNeed help to complete the challenge.
Hi Im not getting the Question here , May be my English is weak, but i need help to complete this challenge. And I want to know what is being expected here
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="";
if (obj instanceof String) {
result =(String)obj ;}
return result;
}
}
2 Answers
andren
28,558 PointsBasically the challenge states that the getTitleFromObject method can be called either with a String, or with a BlogPost object. If the object passed to it is a string you should simply cast it to a string and return it.
If the object is a BlogPost you should cast is as such and then return the title of the BlogPost object. The title can be accessed by calling getTitle
on the BlogPost object once you have cast it.
The code you have written is enough to pass the first task, you check if the object is a string and then cast it as such and return it. For the second task you have to to pretty much exactly the same thing except that you now check if the object is a BlogPost. Then after casting it to a BlogPost (in pretty much the same way you currently do for the String) you call the getTitle
method on it and return the result of that.
So basically what is excepted is that if I call the method with a string as an argument I will simply get the string returned back to me, if I call it with a BlogPost object I should get the title of the BlogPost returned to me.
nazim khan
1,409 PointsThanks Andren for your reply Can u please help me with the code I'm just stuck in this from last 2 days
andren
28,558 PointsHere is the solution:
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="";
if (obj instanceof String) {
result = (String)obj ;
} else if (obj instanceof BlogPost) { // Check if obj is BlogPost
BlogPost blogPost = (BlogPost)obj; // Cast obj as BlogPost
result = blogPost.getTitle(); // Get title from BlogPost object
}
return result; // Return result, will be either the string passed in or the title of the BlogPost.
}
}
Your code was already pretty close, since the BlogPost check and casting code is pretty similar to the String check and casting code you had already written.
nazim khan
1,409 Pointsnazim khan
1,409 Pointsand the 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.
I've included BlogPost.java for your reference only.