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 trial

Java Java Data Structures - Retired Getting There Type Casting

Type Casting Objectives

I am not being able to understand the second objective of this challenge and hence cannot find out the solution as well, please help me find out the solution and please specify the reason for your proposed solution. Thanks in advance!! Here is my code for the first challenge.

if(obj instanceof String)
    {
      String object = (String) obj;
      result = object;
    }
com/example/BlogPost.java
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;
    }
}
TypeCastChecker.java
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;
  }
}

3 Answers

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

Great job with Task 1. Now Task 2 is almost the same as task 1. Let me give you sudo code with hints, hope it helps.

   // 1. we check if argument "obj" is indeed "BlogPost" using instanceof

     // if it is BlogPost 
     // 2. we cast "obj" to BlogPost instance, e.g. "blogPost" 

     // 3. we assign `getTitle()` result of method on "blogPost" to our "result" String,
     // to return title of BlogPost passed

  // in the end of the method we return result

Thank you for your help.

This will be the solution to both of the objectives.

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 = "";
  //This part of the code is for objective 1.
if(obj instanceof String)                    // We are checking if obj is a String. (or an instance of the class String.)
    {
      String object = (String) obj;        // Downcasting it to String form the class Object.
      result = object;                             // Returning the value of 'object' through 'result'.
    }
 //This part of the code is for objective 2. 
  if(obj instanceof BlogPost)                 //We are again checking if obj is a BlogPost(or an instance of the class BlogPost.)
    {
      BlogPost objective = (BlogPost) obj;   // Downcasting it to BlogPost form the class Object.
      result = objective.getTitle();                 // Returning the value of 'objective' through 'result'. 
    }
  //We are basically checking the value of obj based on what is passed through the method 'getTitleFromObject'. If it is 'String' then 'object' is printed or if it is 'BlogPost' then 'mTitle' is printed which is the value of getTitle method. We are fetching a method of 'BlogPost' through its own instance 'objective'.
 return result;
  }
}

Of course, after all, you helped me. But on a much serious note, the objective was not very clear.

Sravan Nadella
Sravan Nadella
2,380 Points

I tried the following snippet:

if(obj instanceof String)
{ result = (String) obj;
}

But this doesn't work. Please help me in debugging my error. I am unable to satisfy Hint 1.