Create a java class named \"Country\". This class must have the followings; o Tw
ID: 671948 • Letter: C
Question
Create a java class named "Country". This class must have the followings; o Two instance variables "name" and population, as String and int types o Parameterized constructor o Setter and getter methods for both instance variables o The toString() method that returns a String in this format; "Country: Japan Population 89857496" Create "BadDataException" class that is a subclass of Exception class. There must be two constructors; a default constructor and a parameterized constructor Create a java class named "Tester". This class must have the followings A static method named "createCountry". This method will prompt the user to enter country name and population. We consider a valid population to be between 5 and 2 billion, inclusive. If the population is less than 5, or greater than 2000000000 (two billion), then this method will throw an object of BadDataException. Also, if the user enters a value that is not an integer, the method throws another type of exception. You must not handle any exception inside of this method. If user enters valid values, then return an object of Country using these valid values o o The main method. In the main method, declare a local variable of type Country. Call the createCountry method to create a Country object and assign it to the local variable in the main method. The main method must keep calling the createCountry method until a Country object is created successfully. Just after it is created, use println method to display the information of Country object. Use the toString method of the object for thisExplanation / Answer
import java.util.InputMismatchException;
import java.util.Scanner;
public class tester {
public static void createCountry() throws BadDateException,InputMismatchException{
Scanner scn=new Scanner(System.in);
System.out.print("Enter name of the country: ");
String name=scn.nextLine();
System.out.print("Enter the population of "+name+" : ");
int population=scn.nextInt();
if(population<5||population>2000000000){
throw new BadDateException("country population is invalid");
}else{
Country c=new Country(name, population);
System.out.println(c.toString());
}
}
public static void main(String[] args) throws InputMismatchException, BadDateException {
try{
createCountry();
}catch(InputMismatchException e){
System.out.println("Enter population between 5 and 2 billion numbers only!!!!!!");
}
}
}
public class BadDateException extends Exception {
public BadDateException(){
}
public BadDateException(String n){
super(n);
}
}
public class Country
{
String name;
int population;
public Country(String n,int p){
name=n;
population=p;
}
public void setName(String n){
name=n;
}
public void setPop(int p){
population=p;
}
public String getName(){
return name;
}
public int getPop(){
return population;
}
public String toString(){
return "Country : "+name+" Population : "+population;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.