Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This one is not in the video. It is in the book, or you can dig for an explanati

ID: 3796373 • Letter: T

Question

This one is not in the video. It is in the book, or you can dig for an explanation online.

The ‘assert’ keyword was added to Java in version 1.2. That caused a problem. “assert” was used for many years in C++, and when Java did not provide that mechanism, many Java programmers added a public method called ‘assert’ into their code.   When Java 1.2 came out, all of the code that used ‘assert’ wouldn’t compile, because they had a method name that matched a keyword.

assert somethingTrue;    // does nothing

assert somethingThatIsFalse;    // this will throw an AssertionError

        It became necessary to add some qualifiers to the compile and run commands in Java, so that assertions might be used at compile time and/or at run time. Since we are doing all of our work within Eclipse, you will need to figure out how to turn these on within the Eclipse environment.   Eclipse is generating the commands to compile and run the java programs – that’s good, we don’t have to type the commands, but it makes it harder when we need to change what command is generated.

The compile and run switches for Assertions are quite elaborate. Assertions are only used during development. Everyone will want to turn off this feature at run-time when the product is released.

When you have figured out how to turn Assertions on / off within Eclipse, post your solution on the discussion board. If the answer is already there, but you can add something, do it. You don’t need to re-post something that is already there.

Coding exercise:

Demonstrate using the assert keyword. Create a private method that takes an int parameter.

If the value passed in is negative, the method will assert something that is false, causing the method to throw an AssertionError.

Pass the value that was passed into the method to the constructor of the AssertionError class.

All of this is done with a single assert statement.

Why did I ask you to make this method private? It works the same with public methods. Put a comment in the code if you can find an answer that question. (It isn’t something you can figure out, you will need to read about the convention and the reason for having that convention.)

Explanation / Answer

Steps to enable/disable the assertion in eclipse for a specific program. By default it is disabled
1) Go to menu, then Run, then Run Configuration
2) Got to java application , then go to Assertion
3) choose the tab Argument
4) in the VM Arguments type -ea to enable the assertion
5) click apply and then run

Program:

import java.util.Scanner;


public class AssertTest {

   /**
   * @param args
   */
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter the value");
       int val=scan.nextInt();//taking value from the user
       assertTest(val); //passing it to the assertTest method
       scan.close();
   }
  
   //private method are always safe as we know it is called only from the same class, Hence modification are safe. Also other class cannot call private method
   //for public method any one call that method, hence any parameter are added then all the classes which the methos is called from.
   private static void assertTest(int val){
       assert (val>0); //calling the assert based on the condition. If the condition is false then it will throw AssertionError.
      
   }

}


/--output-------------/
Enter the value
-5
Exception in thread "main" java.lang.AssertionError
   at AssertTest.assertTest(AssertTest.java:18)
   at AssertTest.main(AssertTest.java:13)

/---output-----------/

Note: Feel free to ask questions/doubts. God bless you!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote