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...
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...
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...
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...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
1. To understand the value of records in a programming language, write a small program in...
1. To understand the value of records in a programming language, write a small program in a C-based language that uses an array of structs that store student information, including name, age, GPA as a float, and grade level as a string (e.g., “freshmen, junior, senior” etc.). Also, write the same program in the same language without using structs. Note: Code and with output screenshots
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT