Page 1 of 1

bug in enthuware.ocpjp.v8.2.1826?

Posted: Fri Jul 08, 2016 4:38 am
by codemonkey
Hello,
I've compiled and run the code given in the question and got a result other then in the answer. Can you please tell me what I did wrong?

Answer applied by enthuware is:
UNKNOWN
PASS

Real result:
FAIL
PASS

My code:

Code: Select all

package tests;

import java.util.Optional;

public class Test2 {

	public static void main(String[] args) {
		Optional<String> g1 = getGrade(50);
		Optional<String> g2 = getGrade(55);
		System.out.println(g1.orElse("UNKNOWN"));
		if (g2.isPresent()) {
			g2.ifPresent(x -> System.out.println(x));
		} else {
			System.out.println(g2.orElse("EMPTY"));
		}
	}

	static Optional<String> getGrade(int marks) {
		Optional<String> grade = Optional.empty();
		if (marks > 50) {
			grade = Optional.of("PASS");
		} else {
			grade = Optional.of("FAIL");
		}
		return grade;
	}
}
Sorry if I post this question in any incorrect way and do moderate it.

Re: bug in enthuware.ocpjp.v8.2.1826?

Posted: Fri Jul 08, 2016 9:05 pm
by admin
The given answer is correct. Please try exactly the same code as given in the question.

Re: bug in enthuware.ocpjp.v8.2.1826?

Posted: Sat Jul 09, 2016 2:27 am
by codemonkey
Admin, you are right. I found my mistake. Typed

Code: Select all

grade = Optional.of("FAIL");
instead of

Code: Select all

grade.of("FAIL"); 
wich changed an output. I apologize.

Re: bug in enthuware.ocpjp.v8.2.1826?

Posted: Wed May 19, 2021 1:25 pm
by Sieusc

Code: Select all

 grade.of("FAIL");
How come this does not result in a compilation error?

Re: bug in enthuware.ocpjp.v8.2.1826?

Posted: Wed May 19, 2021 11:34 pm
by admin
Why do you expect it to result in a compilation error?
of(T ) is a static method in Optional class and grade is a variable of type Optional. Although not a preferred way to invoke a static method, you can invoke a static method using a reference.