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 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....
In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java...
In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java application. Given a set of events, choose the resulting programming actions. Understand the principles behind Java. Understand the basic principles of object-oriented programming including classes and inheritance. Deliverables .java files as requested below. Requirements Create all the panels. Create the navigation between them. Start navigation via the intro screen. The user makes a confirmation to enter the main panel. The user goes to the...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape,...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape, Two Dimensional Shape, Three Dimensional Shape, Square, Circle, Cube, Rectangular Prism, and Sphere. Cube should inherit from Rectangular Prism. The two dimensional shapes should include methods to calculate Area. The three dimensional shapes should include methods to calculate surface area and volume. Use as little methods as possible (total, across all classes) to accomplish this, think about what logic should be written at which...
Using Java create an interactive (the user should be able to input the values) Java Application to process your utility bill for at-least 6 months.
Java ProgrammingAssignment 7.1 (25 points)Using Java create an interactive (the user should be able to input the values) Java Application to process your utility bill for at-least 6 months. Use Class-Object methodology and use single dimensional arrays in your application. Write detailed comments in your program. You should have a two classes (for example, MontlyBill.java & MontlyBillTest.java). You can also be creative and create your “own problem scenario” and come up with your “solution”.Assignment 7.2 (25 points)Use the same program...
*****Using Java 1.         There is a class called Wages. It should have one method: calculateWages. This...
*****Using Java 1.         There is a class called Wages. It should have one method: calculateWages. This method accepts from a user (1) an integer hourly rate and (2) an integer total number of hours worked in a week. It calculates and displays the total weekly wage of an employee. The company pays straight time for the first 40 hours worked by an employee and times and a half for all the hours worked in excess of 40. This class should...
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
Use JAVA language. Using the switch method concept create a program in which you have an...
Use JAVA language. Using the switch method concept create a program in which you have an artist (singer) and the list of his or her songs. Add 18 songs. Then alter the script to achieve the following in each test run: a) Print all the songs. b) Print only song 15. c) Print only song 19.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT