Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9103867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:43:55+00:00 2026-06-17T01:43:55+00:00

How come the assert statement in the constructor doesn’t work in the following code?

  • 0

How come the assert statement in the constructor doesn’t work in the following code?
(I wanted to use it instead of if statement, which I commented out)

import java.util.HashMap;
import java.util.Map;


enum Suite{
    HEARTS, DIAMONDS, SPADES,CLUBS;
}

public class Card {
    private Suite suite;
    private int number;
    private String name;

    public Card(int number, Suite suite) {
//      if(number < 0 || number > 13)
//          throw new IllegalArgumentException(); 
        assert(!(number < 0 || number >13));

        this.number=number;
        this.suite=suite;
        initialize();

    }
    private void initialize(){
        switch(number){
        case 1: name="Ace"; break;
        case 2: name="Two"; break;
        case 3: name="Three"; break;
        case 4: name="Four"; break;
        case 5: name="Five"; break;
        case 6: name="Six"; break;
        case 7: name ="Seven"; break;
        case 8: name ="Eight"; break;
        case 9: name="Nine"; break;
        case 10: name="Ten"; break;
        case 11: name="Jack"; break;
        case 12: name="Queen"; break;
        case 13: name="King"; break;
        default: break;
        }
    }
    public int getNumber() {
        return number;
    }

    public int getBJNumber(){
        if(number == 11 || number == 12 || number == 13 )
            return 10;
        else
            return number;
    }

    public Suite getSuite() {
        return suite;
    }

    public String toString(){
        return name + " of " + suite.toString();
    }

    public static void main(String[] args) {
        Card testCard = new Card(1, Suite.CLUBS);
        System.out.println(testCard);
        Card testCard2 = new Card(5, Suite.CLUBS);
        System.out.println(testCard2);
        Card testCard3 = new Card(1, Suite.CLUBS);
        System.out.println(testCard3);
        Card testCard4 = new Card(15, Suite.CLUBS);
        System.out.println(testCard4);
    }
}

output:

Ace of CLUBS
Five of CLUBS
Ace of CLUBS
null of CLUBS <---this should have not been shown.

Maybe my understanding of assert is not correct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:43:56+00:00Added an answer on June 17, 2026 at 1:43 am

    By default assert is disabled. You need to explicitly enable using -ea flag. Here is java tutorial on how to enable/disable assertions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Occasionally I come accross a unit test that doesn't Assert anything. The particular example
In this thread some one commented that the following code should only be used
I come from ruby/C# and am new to Python. I'm looking at the following
How come when I do the following: var query = from a in OBJ
I come across this code in my web.xml but I don't understand what it
I've come across odd behavior when comparing strings. First assert passes, but I don't
I come up against this all the time, and I'm never sure which way
StackOverflow. My first post here. I've come to D from C++ and MFC, which
I hope this doesn't come across as a stupid question but its something I
If I have code like the following: public const string UNSPECIFIED_DATATYPE = 11; private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.