Question

In: Computer Science

This is to be done using JAVA Create a Madlib bean. The bean should have several...

This is to be done using JAVA

Create a Madlib bean. The bean should have several string properties and the text of the Madlib can be hardcoded. The bean should have a toString() method that outputs the text of the Madlib with the values of the properties inserted. For an additional challenge, make the text of the Madlib a property and pass an ArrayList of String arguments.

MadLib: My ______ (animal) lives in _______(place).

Solutions

Expert Solution

Following is the code for the Madlib bean. Alongside is the demo class for testing the madlib bean. Please follow the comments for understanding the code.

Output:

E:\Folder\Java>java MadlibDemo
My lion lives in den.
My sparrow lives in nest.
My snake lives in hole.

Explanation of the output:

We have provided the three animal-home pairs as this: lion-den, sparrow-nest, and snake-hole. We have set these arguments to the test of each of the madlib object. Then we have printed the output of invoking toString method on each of the madlib instances to the console.

Code:

import java.util.ArrayList;

class Madlib {

    // Three properties of the madlib bean

    private String animalName;

    private String animalHome;

    private String madlibText;

    /* Method that generates the madlib text

     * using an ArrayList of arguments, as

     * required in the challenge.

     */

    public void createMadlibText(ArrayList<String> args) {

        this.animalName = args.get(0);

        this.animalHome = args.get(1);

        this.madlibText = "My " + this.animalName +

                " lives in " + this.animalHome + ".";

    }

    /* The toString method that returns the

     * madlib text. This has been used in

     * the main method to print text to console.

     */

    @Override

    public String toString() {

        return madlibText;

    }

    

}

public class MadlibDemo {

    public static void main(String[] args) {

        // Three instances of madlib for demo

        Madlib madlib1 = new Madlib();

        Madlib madlib2 = new Madlib();

        Madlib madlib3 = new Madlib();

        // Three ArrayLists of arguments

        ArrayList<String> args1 = new ArrayList<>();

        args1.add("lion");

        args1.add("den");

        ArrayList<String> args2 = new ArrayList<>();

        args2.add("sparrow");

        args2.add("nest");

        ArrayList<String> args3 = new ArrayList<>();

        args3.add("snake");

        args3.add("hole");

        // Generating text for the three madlibs

        madlib1.createMadlibText(args1);

        madlib2.createMadlibText(args2);

        madlib3.createMadlibText(args3);

        // Printing text of all madlibs to console

        System.out.println(madlib1.toString());

        System.out.println(madlib2.toString());

        System.out.println(madlib3.toString());

    }

}


Related Solutions

Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that...
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that will be part of an overall class named InstrumentDisplay. The classes are FuelGage, Odometer, MilesSinceLastGas, and CruisingRange. The FuelGage should assume a 15 gallon tank of gasoline and an average consumption of 1 gallon every 28 miles. It should increment in 1 gallon steps when you "add gas to the tank". It should decrement by 1 every 28 miles. It should display its current...
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file,...
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file, read them back in, and format a report. Project Description You’ll read and write files containing objects of the Loan class. Here are the details of that class: Instance Variables: customer name (String) annual interest percentage (double) number of years (int) loan amount (double) loan date (String) monthly payment (double) total payments (double) Methods: getters for all instance variables setters for all instance variables...
Using java you have to create a simple program that will allow for the storage of...
Using java you have to create a simple program that will allow for the storage of requests for money. Each request will consist of a person's name and dollar amount (US dollars). The business rules are straight forward. Each name should be a first and last name. The first letter of each element in the name should be capitalized. The dollar amount should be between 1 and 1000 dollars and include cents (2 decimals). You should include validations, but it...
Create Kernel of a Strongly Connected Components using Kosaraju's Algorithm in Java. I have to create...
Create Kernel of a Strongly Connected Components using Kosaraju's Algorithm in Java. I have to create a Kernel DAG using the Strongly Connected Components found using Kosaraju's Algorithm. The input to the code is in the format. 5 \n 5 \n 1 0 \n 0 2 \n 2 1 \n 0 3 \n 3 4 The code will find the SCCs and print them to the screen, however, I need to find a way to construct a kernel based on...
This is to done in Java: create the infrastructure for building a word cloud application. We...
This is to done in Java: create the infrastructure for building a word cloud application. We will do so by 1) Reading the content of a text file and creating a binary tree of words in that file. When a duplicate word is encountered. we simply increase the frequency count of that word in its corresponding node. In other words, the nodes in the tree have two parts. One part maintains the word, and the other maintains the frequency count....
1. Create a1. Create a new java file named Name.java that should have Name class based...
1. Create a1. Create a new java file named Name.java that should have Name class based on new java file named Name.java that should have Name class based on the following UML Name - first: String - last: String + Name () + Name (String firstName, String lastName) + getName () : String + setName (String firstName, String lastName) : void + getFirst () : String + setFirst (String firstName) : void + getLast () : String + setLast (String...
All code is done using Java. The steps to the question are as follows (There is...
All code is done using Java. The steps to the question are as follows (There is more information in the code comments posted below as well): Begin by deciding how many fields are required and what their types should be. Add these fields to your class (making sure that they each have a private access modifier) giving them a sensible name when you do so. Once you have added the fields to your class, implement the methods getLevel and getWidth....
Using Java, write code as follows: Create an interface called Items. It should define at least...
Using Java, write code as follows: Create an interface called Items. It should define at least the following methods: public void add( Object item ) - adds a single object to the collection. public Object get( int index ) - returns an item at a specific index. public int size() - returns the number of items stored in the collection. public void addAll( Object[] items ) - adds all of the elements in the array to the collection. Write a...
Create a java program that allows people to buy tickets to a concert. Using java create...
Create a java program that allows people to buy tickets to a concert. Using java create a program that asks for the users name, and if they want an adult or teen ticket. As long as the user wants to purchase a ticket the program with "yes" the program will continue. When the user inputs "no" the program will output the customer name, total amount of tickets, and the total price. The adult ticket is $60 and the child ticket...
Create a Java Program to calculate compound interest. This can all be done in the main()...
Create a Java Program to calculate compound interest. This can all be done in the main() method. Create a double variable named currentBalance. Create a double variable named newBalance. Create a final double variable named INTEREST_RATE and assign 1.05 to it. Create a int variable named yearCount. Prompt for the "Number of years to invest" and store the input into the yearCount variable. Prompt for the "Amount to Invest" and store this in the currentBalance variable. Assign currentBalance to newBalance....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT