Home > Software engineering >  How to Type Cast a String as Null object?
How to Type Cast a String as Null object?

Time:10-10

UPDATE: Apologies for the confusion. All code MUST be written between the forward slashes i.e. the "//" beginning at the TO DO comment. And ends the line before the return answer.

Consequently this:

return price<20 ? "Buying new shirt" : null

is unfeasible as return answer is OUTSIDE the allowed code modification area. I also didn't know if it was possible to assign a null object to a String object (as my comment indicated. And yes, I'm aware of the difference between "null" v null. That is the entire issue I'm having with the code failing to compile.

The ConditionalStatements() method is supposed to pass an integer input and return a string variable called answer.

If the passed integer price < 20 (to include negative integers), then the return variable answer = "Buying new shirt". Otherwise the compiler's expected return value for answer must be null i.e. the OBJECT null and NOT the string "null" which answer is initialized with.

Constraints:

  • if statement MUST be a Simple Conditional Statement with no branching.
  • cannot use catch try or other statements. Limited to use of compound conditionals and variables.
  • MUST be WITHIN the area designated by // forward slashes. Because of this last constraint, I can't see a way to typecast/return the null as the test errors are indicating. Apologies again for the confusion.

I've tried short circuiting using && and the || operators. Tried getting creative with && and II operators in the if statement to make compound statements. e.g:

if ( (answer.equals(null) && (price >=20) ) etc.

to trap the incorrect test input and change answer data type. But compiler fails test cases of required answer = null for cases where price >= 20. Returning answer = "null" throws an error as a String object is returned v. the desired null.

How to type cast answer variable e.g. (null) answer = null; in my code below?

public class ConditionalStatements {
 /**
 * This method is used for problem one in the README.
 * @param price A price that will be passed in by the test.
 * @return String A string used to validate the test.
 */
public String simpleConditional(int price) {
    String answer = "null";
    **// TODO: Write Step 1 code between the forward slashes** <--start code modification area
    (answer == null ) || (price < 20)  )
    answer = "Buying new shirt";
            
      if( (price >= 20)  )  {                
           answer = null; //this assignment is legal according to post feedback. 
                          //However it STILL results in test failure message (shown in URLs below). 
                         //Where the compiler is expecting a null object and not a string object.... 
                          //compiler won't accept answer = null; How to typecast answer String variable so it will accept null object?
      }       
    // **<---this is the END of the code modification area**

    return answer;
}

When compiler tries to compile, it fails with these test run errors:

All tests. Only price < 20 passed: https://i.postimg.cc/TYrH051P/allTests.png

Test 3: price = 20, expected return for answer = null https://i.postimg.cc/W3GJCVk0/Test3-err1.png

Test 4: price = 21, expected return for answer = null https://i.postimg.cc/bNJsHk3x/Test4-err2.png

CodePudding user response:

All you have to do assign null to answer variable, rather than "null" string. Check the below code, I think it does what you want to achieve.

public class ConditionalStatements {
 /**
 * This method is used for problem one in the README.
 * @param price A price that will be passed in by the test.
 * @return String A string used to validate the test.
 */
public String simpleConditional(int price) {
    String answer = null;
    // TODO: Write Step 1 code between the forward slashes (answer == null ) || (price < 20)  )
    if(price < 20){
    answer = "Buying new shirt";
    }

    return answer;
}

public static void main(String[] args){
    ConditionalStatements var = new ConditionalStatements();
    System.out.println(var.simpleConditional(10)); // prints "Buying new shirt"
     System.out.println(var.simpleConditional(25)); // return null object
}

}

CodePudding user response:

To use null with explicit type you have to put a cast in front of null, in you example

  if( (price >= 20)  )  {                
       answer = (String) null; 
  }
  • Related