Answer to Question #195466 in Java | JSP | JSF for mechanix

Question #195466


A school called ‘Danville’ has a provision of keeping 2 kinds of Presidents: School President and Class president. (President class can be either a concrete class or an abstract class or an interface. You need to determine what it will be from the following clues.) There can be only 1 school president but there can be multiple class presidents. School and class presidents do not have any common attribute but they have a common school name which is fixed as ‘Danville’. Class presidents have a class id which school presidents do not have. Both school president and class president have a common method named exercisePower but their implementations are different. It prints “Exercising power on school” for the former and “Exercising power on class __” for the latter.


1
Expert's answer
2021-05-20T04:52:54-0400
import java.util.Scanner;


abstract class President{
	//School and class presidents do not have any common attribute but 
	//they have a common school name which is fixed as ‘Danville’.
	public static final String SCHOOL_NAME="Danville"; 
	/**
	 * Constructor
	 */
	public President() {}
	
	public void exercisePower() {}
}


class SchoolPresident extends President{ 
	/**
	 * Constructor
	 */
	public SchoolPresident() {}
	
	@Override
	public void exercisePower() {
		System.out.println("Exercising power on school");
	}
}
class ClassPresident extends President{ 
	//Class presidents have a class id which school presidents do not have. 
	private int id;
	/**
	 * Constructor
	 * @param id
	 */
	public ClassPresident(int id) {
		this.id=id;
	}
	
	@Override
	public void exercisePower() {
		System.out.println("Exercising power on class "+id);
		
	}


	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}


	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
}


public class Q195466 {
	
	public static void main(String[] args) {
		President schoolPresident =new SchoolPresident();
		President classPresident =new ClassPresident(1112354);
		System.out.println("School name: "+President.SCHOOL_NAME);
		schoolPresident.exercisePower();
		classPresident.exercisePower();
	}
}








Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS