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

Wan Nor Adzahari Wan Tajuddin
Wan Nor Adzahari Wan Tajuddin
2,438 Points

Is my code correct for task 1 ???

Can someone please check my code? I passed for this test but upon checking with other students' code on community, mines aren't the same with theirs.

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 = (String) obj;
    if (obj instanceof BlogPost) {
      obj = (String) obj;
    }
    return result;

  }
}

1 Answer

andren
andren
28,558 Points

No, it's not quite correct. It will produce the right result for task 1, hence why it passes that task. But it is not the intended solution, and will cause issues in task 2.

The getTitleFromObject is meant to be able to handle being passed either a String or a BlogPost object.

In your first line of code you convert obj to a String:

String result = (String) obj;

That works fine in the if the obj actually is a String, but if it isn't then your program will crash. Since your method is meant to handle the scenario where obj is a BlogPost this line of code is incorrect.

Unless you are absolutely certain of what type an object is, you should never do typecasting. That's where the if statements for testing its type comes in.

If you have an if statement that checks if obj is a String you can be certain that it actually is a String, so that's where you should place your String type casting code.

Pretty much exactly like you did in in the first line, except it has to be placed within the if statement.

So the first task code should look like this:

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 = ""; // You should not change this line from how it started out
    if (obj instanceof String) { // Check if obj really is a String
      result = (String) obj; // Make result equal to obj cast as String
    }
    return result; // Return whatever result holds (in task 1 that is simply obj cast as a string)
  }
}