Question

Java 2.After you write an object class, how do you test or use this class? a.You...

Java

2.After you write an object class, how do you test or use this class?

a.You can test it by running the object class directly.

b.You need testing class to test it or use it.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Right answer is option "a", to use the class simply we need an object of that class. With the help of the object you can test the methods available in that class. To test the class, all you need is to call the methods an pass arguments to the methods. Below is how you can test a class without a separate testing class.

Sample.java

public class Sample{

    public void m1(){
        System.out.println("Sample method which returns NO value");
    }
    public String m2(String name){
        return name + "! Good morning!!!";
    }
    public int divideOperation(int n1, int n2){ // this method accepts 2 int values, if n2 is 0 an exception will be thrown

        // returns an int value, if there there is an exception it will return 1
        int result;
        int flag = 0;
        try{
            result = n1/n2;
        }catch (ArithmeticException e){
            flag = 1;
            System.out.println("There was an exception as denominator can't be 0 " + e);
        }
        return flag;
    }

    public static void main (String [ ] args){

        String greet;
        int result = 0; // to hold the return value of divideOperation()
        Sample sample = new Sample(); // create the object of the class Sample
        sample.m1();
        greet = sample.m2("Pawel"); // You pass a string to test whether a greet message with the passed name
        System.out.println("The message from m2() :" + greet);

        // We can test divideOperation() if it returns 1 when n2 = 0
        result = sample.divideOperation(10,0);
        System.out.println("The result of divideOperation " + result);

        // n1=10, n2=5 since n2 is not 0, divideOperation should return 0
        result = sample.divideOperation(10,5);
        System.out.println("The result of divideOperation " + result);

    }
}

Thank you,

Good Luck!!!

Add a comment
Know the answer?
Add Answer to:
Java 2.After you write an object class, how do you test or use this class? a.You...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT