In Java: Write a program (your main method should be in class Main4) that (i) ta
ID: 3696837 • Letter: I
Question
In Java:
Write a program (your main method should be in class Main4) that (i) takes a class name as a command line argument; (ii) in that class locates all public static methods
1. that take no parameters,
2. return a bool, and
3. whose name starts with "test"
and (iii) invokes those methods.
If such a method returns true, your program should output
OK: testX succeeded
where testX is the name of the tried test method.
In the case of false the output should be
FAILED: testX failed
Write a class MyClass that has some test methods, and try your program with that class. Ensure that your submission includes this class too.
Explanation / Answer
class MyClass
{
public static void print()
{
System.out.println("Hello");
}
public static boolean check(String n)
{
if(n.equals("MyClass"))
return true;
else
return false;
}
public static void test()
{
System.out.println("Testing...");
}
}
class Main4
{
public static void main(String args[])
{
MyClass o = new MyClass();
if(args[0].equals("MyClass"))
{
System.out.println("OK: testX succeeded");
o.print();
o.check(args[0]);
o.test();
}
else
{
System.out.println("FAILED: testX failed");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.