Writing Java_program test case

Writing Java_program test case

by Shankar Jha -
Number of replies: 4

Hello everyone,


I am a little bit confused in writing a test case of java, can anyone help me with the below problem

suppose I have to write a test case for this program 


public class ArmstrongExample{  

  public static void main(String[] args)  {  

    int c=0,a,temp;  

    int n=153;//It is the number to check armstrong  

    temp=n;  

    while(n>0)  

    {  

    a=n%10;  

    n=n/10;  

    c=c+(a*a*a);  

    }  

    if(temp==c)  

    System.out.println("armstrong number");   

    else  

    System.out.println("Not armstrong number");   

   }  

}


how to write a test case for this program so that it will check for various inputs such as for n=5 and n=125.

Regards,

In reply to Shankar Jha

Re: Writing Java_program test case

by Richard Lobb -

You can't test of various values of the "input" n, because n isn't an input. It's a hard-coded constant in the program. To test with different values of n you need to change the source code, recompile and re-run. CodeRunner doesn't do that, and nor does any standard program testing environment, as far as I know.

In order to test a program you need a specification to test against. That particular program would appear to have a specification like:

Write a program to compute if the number 153 is an Armstrong number or not. The program should print "armstrong number" if it is or "Not armstrong number" otherwise.

The test for such a program is just to run it and see if it prints the appropriate one of those two outputs.

If you wish to have a program that works with different values of n you have to pose a different question. For example, you might ask:

  1. Write a Java method boolean isArmstrong(int n)  that returns true if n is an armstrong number, or false otherwise. Or
  2. Write a Java program that reads an integer from standard input and prints "armstrong number" is the number read is an armstrong number, or "Not armstrong number" otherwise.

The first of these would be a "Java method" question, the second a "Java program" question.

See the second half of the Some C and Java questions quiz for examples. The XML exports of these questions are supplied in the CodeRunner distribution file simpledemoquestions.xml.

Note that if you are catering for learners you can provide students with a skeleton outline of the required answer via the Answer preload. This might include the code that reads the number from standard input, for example.

Richard

In reply to Richard Lobb

Re: Writing Java_program test case

by Shankar Jha -

Thanks a lot @Richard 

Your answer helps me to solve a lot of problems.

Is there any possibility that I can run Hadoop or Spark program using the python template because for programming language it's fine but for a project-based course like Apache Spark or Apache Hadoop, programming assignment is complex.

Do you have any idea on how to implement such type of assignment using Coderunner?

Regards,

In reply to Shankar Jha

Re: Writing Java_program test case

by Richard Lobb -

Check out the last few postings in this forum discussion, where Trent Lewis describes a system that pulls in the student's code from a repository for grading. But you're into guru stuff here - there's no simple solution.


In reply to Richard Lobb

Re: Writing Java_program test case

by Shankar Jha -

Sure, I will check Trent Lewis post, I am also working on this, I will update you If I will come up with a robust solution.