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

As part of the United Nations software development unit team, your task is to wr

ID: 3569871 • Letter: A

Question

As part of the United Nations software development unit team, your task is to write a method that is used to enter data. Write a method named enterTreaties that returns an array of Treaty objects that are created by prompting the user to enter appropriate data. In particular, your method asks the user for the number of treaties to be entered, creates the array, enters a loop that prompts for the treaty title, number of articles, number o amendments, and creates an object to be stored in the array. For example, if the user wants to enter 2 treaties, the program creates an array of size 2, then prompts for the title of the first treaty, the number of articles in the first treaty, and the number of amendments in the first treaty, then creates a Treaty object which is stored at index 0 of the array. Then it prompts for the data for the second treaty, creates another object and store it index 1 of the array. Reminders To create a Scanner: Scanner input = new Scanner (System. in); To prompt for an integer value: v = input. nextInt(); To prompt for a String: s = input. nextLine( ); You want to appropriately write and test your enterTreaties method. ? Write the statement that you would use to call the enterTreaties method from the main method. Put your method call statement in a try/catch structure and print appropriate error messages if a Numb erFormatException or an IndexOutOfBoundsException occurs ? Explain your strategy to test your method ? Name three things you can do to make it easy for other team members to understand the code in your method.

Explanation / Answer

import java.util.Scanner;

public class Treaty
{
   private String title;
   private int numberOfArticles;
   private int numberOfAmendments;

   Treaty(String title, int numberOfArticles, int numberOfAmendments)
   {
       this.title = title;
       this.numberOfAmendments = numberOfAmendments;
       this.numberOfArticles = numberOfArticles;
   }

   public String getTitle() {
       return title;
   }

   public void setTitle(String title) {
       this.title = title;
   }

   public int getNumberOfArticles() {
       return numberOfArticles;
   }

   public void setNumberOfArticles(int numberOfArticles) {
       this.numberOfArticles = numberOfArticles;
   }

   public int getNumberOfAmendments() {
return numberOfAmendments;
   }

   public void setNumberOfAmendments(int numberOfAmendments) {
       this.numberOfAmendments = numberOfAmendments;
   }

   @Override
   public String toString() {
       return "Treaty [title=" + title + ", numberOfArticles="
               + numberOfArticles + ", numberOfAmendments="
               + numberOfAmendments + "]";
   }
}

class TreatyTest
{
public static void main(String[] args)
   {      
       try
       {
           Treaty treaties[] = enterTreaties();
           for(int i = 0; i < treaties.length; i++)
               System.out.println(treaties[i]);
       }
       catch(NumberFormatException nfe)
       {
           System.out.println("You entered wrong input.");
       }
       catch(IndexOutOfBoundsException ex)
       {
           System.out.println("You cannot create more number of treaties");
       }
   }
  
   public static Treaty[] enterTreaties()
   {
       Scanner input = new Scanner(System.in);
       System.out.print("How many treaties you want to enter? ");
       int size = Integer.parseInt(input.nextLine());

       Treaty treaties[] = new Treaty[size];
       for(int i = 0; i < size; i++)
       {
           System.out.print("Enter the title of treaty " + (i+1) + " : ");
           String title = input.nextLine();
           System.out.print("Enter number of articles : ");
           int articles = Integer.parseInt(input.nextLine());
           System.out.print("Enter number of amendments : ");
           int amendments = Integer.parseInt(input.nextLine());
           treaties[0] = new Treaty(title, articles, amendments);
       }
       input.close();
       return treaties;
   }

}

To test the method we can give wrong input as in place of entering an integer for articles we can enter some non integer value..

The enterTreaties() method will do the following :

1. It prompts the user to enter the number of treaties to be stored.

2. It prompts user to enter information about the treaty

3. It will create the object for treaty with specified info and stored it in the array then it returns to main method.