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

ADVANCED JAVA PROGRAMMING 1- What is wrong with the following interface? public

ID: 3879881 • Letter: A

Question

ADVANCED JAVA PROGRAMMING

1- What is wrong with the following interface?
public interface House {

@Deprecated

void open();

void openFrontDoor();

void openBackDoor();}


2- Consider this implementation of the House interface, shown in Question 1.
public class MyHouse implements House {

public void open() {}

public void openFrontDoor() {}

public void openBackDoor() {}

}

If you compile this program, the compiler produces a warning because open was deprecated (in the interface). What can you do to get rid of that warning?

3- Will the following code compile without error? Why or why not?
public @interface Meal { ... }
@Meal("breakfast", mainDish="cereal")

@Meal("lunch", mainDish="pizza")

@Meal("dinner", mainDish="salad")public void evaluateDiet() { ... }


4- Define an annotation type for an enhancement request with elements id, synopsis, engineer, and date. Specify the default value as unassigned for engineer and unknown for date

Explanation / Answer

1.)

public interface House {

@Deprecated

void open();

void openFrontDoor();

void openBackDoor();

}

The error in the given interface is that we haven't provided a documentation about the reason for deprecating the open() method. Also, since we are deprecating the open method, we have to specify in the documentation about which other methods can be used in place of open(). To correct the given interface, we have to simply add a documentation containing the reason and the alternative for open method as follows:

public interface House {

@Deprecated

void open();

void openFrontDoor();

void openBackDoor();

}

public class MyHouse implements House {

public void open() {}

public void openFrontDoor() {}

public void openBackDoor() {}

}

On running the above code, the compiler will produce a warning because open method is deprecated in the House interface. To get rid of the warning, we can simply suppress the warning in the MyHouse class so that whenever open method is used, the warning arised due to open being deprecated will be suppressed. Following is the code to suppress the warning:

public class MyHouse implements House {

//Supress the warning produced due to use of depreceted open method

public void open() {}

public void openFrontDoor() {}

public void openBackDoor() {}

}

3.)

public @interface Meal { ... }
@Meal("breakfast", mainDish="cereal")

@Meal("lunch", mainDish="pizza")

@Meal("dinner", mainDish="salad")

public void evaluateDiet() { ... }

The above code will not compile due to 2 possible reasons.

a.) if you are using java version below 8, then those versions does not support repeated annotations and since in the above code, Meal is annonated thrice, the program will fail to compile and there is no solution for that for java versions below 8.

b) For Java 8, the program won't compile because the Meal annonation is not defined to be repeatable. To fix this, we have to define the Meal annotation type as repeatable define a container annotation type for Meal. Following is the corrected code:

//Defining Meal to be a Repeatable annotation type

@java.lang.annotation.Repeatable(MealContainer.class)

public @interface Meal { ... }

//Defining a container annotation type for Meal

public @interface MealContainer {

Meal[] value();

}

@Meal("breakfast", mainDish="cereal")

@Meal("lunch", mainDish="pizza")

@Meal("dinner", mainDish="salad")

public void evaluateDiet() { ... }

4.)

Following code defines the Enhancement request annotation type:

public @interface EnhancementRequest {

int id();

String synopsis();

String engineer() default "[unassigned]";//default set to "[unassigned]"

String date() default "[unknown]";//default set to "[unknown]"

}

Please give this solution a thumbs up if you find it helpful and comment if you have any doubts in it.

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