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 9230263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:46:54+00:00 2026-06-18T05:46:54+00:00

Our teacher gave us an assignment to write a very simple program in conjunction

  • 0

Our teacher gave us an assignment to write a very simple program in conjunction with his own junit test class. I’ve done that, but I don’t quite understand whether or not i’m getting the right results or if the whole thing is broken.

Here is the class we are testing (the one I wrote):

package person;

public class Person {
    private String name;
    private char sex;

    public Person(String name, char sex) {
        if(name == null || name.equals(""))
            throw new IllegalArgumentException();
        if(sex != 'M' || sex != 'F')
            throw new IllegalArgumentException();

        this.name = name;
        this.sex = sex;
    }

    public void setName(String name) {
        if(name == null || name.equals(""))
            throw new IllegalArgumentException();
        this.name = name;
    }

    public void setSex(char sex) {
        if(sex != 'M' || sex != 'F')
            throw new IllegalArgumentException();
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public char getSex() {
        return sex;
    }
}

And here is the JUnit test class:

package parameterchecking;

import person.Person;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author andersb
 */
public class PersonTest2 {

  @Test
  public void constructor() {
    try {
      new Person(null, 'F');
      fail("Should throw exception");
    } catch (IllegalArgumentException ex) { /* expected */ }

    try {
      new Person("", 'F');
      fail("Should throw exception");
    } catch (IllegalArgumentException ex) { /* expected */ }

    try {
      new Person("Anders", 'W');
      fail("Should throw exception");
    } catch (IllegalArgumentException ex) { /* expected */ }
  }

  @Test
  public void setSex() {
    final Person person = new Person("SomeName", 'M');
    try {
      person.setSex('L');
            fail("Should throw exception");
    } catch (IllegalArgumentException ex) { /* expected */ }

    person.setSex('F');
    assertSame('F', person.getSex());
  }

  @Test
  public void setName() {
    final Person person = new Person("Anders", 'M');
    try {
      person.setName(null);
            fail("Should throw exception");
    } catch (IllegalArgumentException ex) { /* expected */ }

    try {
      person.setName("");
            fail("Should throw exception");
    } catch (IllegalArgumentException ex) { /* expected */ }

    person.setName("Henrik");
    assertEquals("Henrik", person.getName());
  }

}

Now, the constructor test passes, but setName and setSex does not. They throw an error “java.lang.IllegalArgumentException”. I don’t see why they won’t pass the test.

JUnit hints that the problem is the “valid” object creation at the start of each test (final Person person), and says that the problem is that I throw an illegal argument exception, which really shouldn’t happen.

  • 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-18T05:46:55+00:00Added an answer on June 18, 2026 at 5:46 am
    if(sex != 'M' || sex != 'F')
    

    should be

    if(sex != 'M' && sex != 'F')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am in a web scripting class and our teacher is very strict on
i am new to program,and our teacher asked us to write a small program
just had a walkthrough in class and the example code our teacher gave us
In our discrete mathematics course in my university, the teacher shows his students the
I'm trying to understand the syntax used in STL for a class. Our teacher
I had a computer science class at school and our teacher was talking about
Our teacher gave us the pseudocode for Merge Sort as below I want to
I have a web design class and our teacher wish's for us to force
So for our first assignment we made a basic ftp program using TCP, now
Our PHP application makes use of json_encode($myObject) a lot, in conjunction with the mustache

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.