Question: 1. Open the starter code program and explore the Kingdom and Fiefdom classes and the Main.java. 2. Create the following methods: getName() - returns the

1. Open the starter code program and explore the Kingdom and Fiefdom classes and the Main.java.

2. Create the following methods:

getName() - returns the name of the Kingdom

getPop() - returns the population of the Kingdom

getNumLords() - returns the number of lords in the Fiefdom

3. Attempt the following code in the main:

Fiefdom c9 = new Fiefdom(Test,1,1);

Fiefdom c10 = new Kingdom(Test,1,1);

Fiefdom c11 = new Kingdom(Test,1);

If any of the above lines cause an error comment them out.

4. Write the code that will output the name of the Kingdom with the largest population.

5. Write the code that will create a new ArrayList of only the Fiefdom objects. You must use a loop. Output the new ArrayList using a loop to verify that all elements in the list have a numLords.

Hint: Java has a keyword called instanceof that will return true if an object is an instance of a particular class. This might be useful to use for this task. Example:

if(a instanceof SomeObject)

{ //do something }

The conditional will return true if A is a SomeObject or a subclass of SomeObject

1. Open the starter code program and explore the Kingdom and Fiefdomclasses and the Main.java. 2. Create the following methods: getName() - returnsthe name of the Kingdom getPop() - returns the population of the

1 Main.java import java.util.ArrayList; 2 3 public class Main 4 { 5 public static void main(String[] args) 6 { 7 Kingdom c1 = new Fiefdom("Colombo", 648034, 3); 8 Kingdom c2 = new Kingdom("Moratuwa", 185031); 9 Kingdom c3 = new Kingdom("Kandana", 33424); 10 Kingdom C4 = new Kingdom("Pettyagoda", 30717); 11 Kingdom c5 = new Fiefdom("Devinuwara", 45000, 1); 12 Kingdom c6 = new Kingdom ("Jaffna", 169102); 13 Kingdom c7 = new Fiefdom("Negomb", 137223, 2); 14 Kingdom c8 = new Kingdom("kerunegala", 28571); 15 16 Kingdom[] dataLands = {c1, c2, C3, C4, C5, C6, 7, c8}; 17 ArrayList lands = new ArrayList(); 18 for (Kingdom C : dataLands) 19 lands.add(c); 20 21 for(Kingdom c: lands) 22 System.out.println(); 23 } 24 1 7 Fiefdom.java 1 public class Fiefdom extends Kingdom 2 { 3 private int numLords; 4 5 public fiefdom(String s, int p, int x) 6 { 7 super(s,p); 8 numLords = x; } 10 11 public String toString() 12 { return super.toString() + "\t\t Number of Lords: 14 } 15 } 13 + numLords; Kingdom.java 1 public class Kingdom { private String name; private int population; 4 5 NM N 000 6 public Kingdom) { "Default"; population = 0; } 9 10 name = 11 12 13 14 public Kingdom(String n, int p) { name = n; population = p; 15 16 17 18 19 20 21 22 public String toString() { String output if(name.length() > 8) output += "Kingdom Name: +name+"\tPop: "+population; else output += "Kingdom Name: "+name+"\t\tPop: "+population; return output; 23 24 25 26 27 28 }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!