Question

In: Computer Science

In this question, you are asked to write a simple java program to understand natural language....

In this question, you are asked to write a simple java program to understand natural language.

The user will enter the input following the format: Name came to City, Country in Year.

For example: Robin came to Montreal, Canada in 2009. Assume a perfect user will follow the exactly above formats for the inputs.

Your program should be able to analyze the key words (Name, City, Country and Year) from the inputs and reorganize the outputs following format: Name stay in City for X year(s). City is in Country.

For example Robin stay in Montreal for 11 years. Montreal is in Canada.

* Please note X = 2020-Year.

Here is an example of the output to illustrate the expected behavior of your program.

Please enter the input sentence (press q to exit): Robin came to Vancouver, Canada in 2009.

Output: Robin stays in Montreal for 11 years. Montreal is in Canada.

Solutions

Expert Solution

Explanation:I have written the NaturalLanguage class that accepts and natural language and prints the output in the given format.I have also shown the output of the program, please find the image attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation.

//code starts

import java.util.Scanner;

public class NaturalLanguage {

   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       String sentence = "";
       while (true) {
           System.out.print("Please enter the input sentence (press q to exit):");
           sentence = input.nextLine();
           if (sentence.equals("q"))
               break;
           String words[] = sentence.split(" ");
           String name = words[0];
           String city = words[3].substring(0, words[3].length() - 1);
           int years = 2020 - Integer.parseInt(words[6].substring(0, words[6].length() - 1));
           String country = words[4];
           System.out.println("Output: " + name + " stays in " + city + " for " + years + " years. " + city + " is in "
                   + country + ".");

       }
       input.close();
   }

}

Output:


Related Solutions

Write a simple JAVA program to understand natural language. The user will enter the input following...
Write a simple JAVA program to understand natural language. The user will enter the input following the format: Name came to City, Country in Year. For example: Chris came to Bangkok, Thailand in 2009. The user will follow the exactly the same formats for the inputs. Your program should be able to analyze the key words (Name, City, Country and Year) from the inputs and reorganize the outputs following format: Name stay in City for X year(s). City is in...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to keep track of a student's current grade. You also get to design and write WeightedCourseGradeDriver class that requests input from the user and interacts with the WeightedCourseGrade class. Your WeightedCourseGrade class should store the following information: Weighted subtotal (the sum of all of the categories multiplied by the grade category weight) Total category weights (the sum of all the grade category weights) Provide the...
write the program in java. Demonstrate that you understand how to use create a class and...
write the program in java. Demonstrate that you understand how to use create a class and test it using JUnit Let’s create a new Project HoursWorked Under your src folder, create package edu.cincinnatistate.pay Now, create a new class HoursWorked in package edu.cincinnatistate.pay This class needs to do the following: Have a constructor that receives intHours which will be stored in totalHrs Have a method addHours to add hours to totalHrs Have a method subHours to subtract hours from totalHrs Have...
Language is Java Design and write a Java console program to estimate the number of syllables...
Language is Java Design and write a Java console program to estimate the number of syllables in an English word. Assume that the number of syllables is determined by vowels as follows. Each sequence of adjacent vowels (a, e, i, o, u, or y), except for a terminal e, is a syllable. However, the minimum number of syllables in an English word is one. The program should prompt for a word and respond with the estimated number of syllables in...
***Please answer the question using the JAVA programming language. Write a program that calculates mileage reimbursement...
***Please answer the question using the JAVA programming language. Write a program that calculates mileage reimbursement for a salesperson at a rate of $0.35 per mile. Your program should interact (ask the user to enter the data) with the user in this manner: MILEAGE REIMBURSEMENT CALCULATOR Enter beginning odometer reading > 13505.2 Enter ending odometer reading > 13810.6 You traveled 305.4 miles. At $0.35 per mile, your reimbursement is $106.89. ** Extra credit 6 points: Format the answer (2 points),...
I want to write this program in java. Write a simple airline ticket reservation program in...
I want to write this program in java. Write a simple airline ticket reservation program in java.The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit...
You are asked to write a simple C++ phonebook application program. Here are the requirements for...
You are asked to write a simple C++ phonebook application program. Here are the requirements for the application. read the contact information from a given input file (phonebook.txt) into a dynamically created array of Contact objects. Each line of the input line includes name and phone information of a contact. Assume that each name has a single part Allow to perform operations on array of data such as search for a person, create a new contact or delete an existing...
Python Language: The Marietta Country Club has asked you to write a program to gather, then...
Python Language: The Marietta Country Club has asked you to write a program to gather, then display the results of the golf tournament played at the end of March. The Club president Mr. Martin has asked you to write two programs. The first program will input each player's first name, last name, handicap and golf score and then save these records in a file named golf.txt (each record will have a field for the first name, last name, handicap and...
Write a program to perform the following actions: the language is Java – Open an output...
Write a program to perform the following actions: the language is Java – Open an output file named “Assign14Numbers.txt” – Using a do-while loop, prompt the user for and input a count of the number of random integers to produce, make sure the value is between 35 and 150, inclusive. – After obtaining a good count, use a for-loop to generate the random integers in the range 0 ... 99, inclusive, and output each to the file as it is...
java language NetBeans Write a program that prompts the user to enter the weight of a...
java language NetBeans Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT