Question

You will write a single java program called MadLibs. java. This file will hold and allow access to the values needed to handle the details for a "MadLibs" game. This class will not contain a maino method. It will not ask the user for any input, nor will it display (via System.out.print/In()) information to the user. The job of this class is to manage information, not to interact with the user.

I am providing a MadLibsDriver.java e^{*} program that you can use to test your program. This class has a maino method and creates two instances of "MadLibs" objects. You do not need to change the driver program, although you are welcome to do so. Do not submit this program. Submit only the MadLibs. java program.

Your MadLibs. java program will require instance variables to store the following values:

MadLibs Purpose: This program will play a game with the user. The user will provide a series of words and numbers and the com

Your MadLibs.java program will require instance variables to store the following values: Instance Variables • personName: a S

petNames Story Once upon a time there was a student named personName, who had a pet petType. petName, the petType, ate so mu

HERE IS THE DRIVER:

import java.util.Scanner;

/**
 * MadLib driver.
 * 
 * @author nortonml
 * @version PA2
 */
public class MadLibsDriver {

    /**
     * The main method. 
     * 
     * @param args The command line arguments (unused)
     */
    public static void main( String[] args ) {
        
        double wage = 0.0;
        int hours = 0;
        
        Scanner scan = new Scanner( System.in );
        
        String person = "";
        String petName = "";
        String petType = "";        
        
        MadLibs test1 = new MadLibs();
        MadLibs test2 = new MadLibs( 21.3456, 30, "Tom", "Arial", 
                        "Mermaid" );
            
        System.out.println( test1 );   
        System.out.println();
        System.out.println( test2 );
        
        System.out.println( "Now it's your turn!!\n" );
        System.out.println( "Welcome to the CS149 MadLib Game\n" );
        
        System.out.print( "Enter a person's name: " );
        person = scan.nextLine();
        System.out.print( "Enter an unusual pet: " );
        petType = scan.nextLine();
        System.out.print( "Enter the pet's name: " );
        petName = scan.nextLine();
        System.out.print( "Enter an hourly wage: " );
        wage = scan.nextDouble();
        System.out.print( "Enter the hours worked: " );
        hours = scan.nextInt();
        
        test1.setHourlyWage( wage );
        test1.setHoursWorked( hours );
        test1.setPersonName( person );
        test1.setPetName( petName );
        test1.setPetType( petType );
        
        System.out.println();
        System.out.println( test1 );

    }

}


1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

MadLibs .java file

import java.text.NumberFormat;
import java.util.Locale;

/**
* MadLibs .
*
* @author nortonml
* @version PA2
*/
public class MadLibs {

   // instance variables
   private String personName;
   private String petType;
   private String petName;
   private int hoursWorked;
   private double hourlyWage;

   // constructor - default
   MadLibs() {
       personName = "Sam";
       petType = "Transformer";
       petName = "Maximus Prime";
       hoursWorked = 0;
       hourlyWage = 0.0;
   }

   // constructor - explicit
   public MadLibs(double hourlyWage, int hoursPerWeeke, String personName, String petName, String petType) {
       this.hourlyWage = hourlyWage;
       this.hoursWorked = hoursPerWeeke;
       this.personName = personName;
       this.petName = petName;
       this.petType = petType;
   }

   // setter methods for instance variables
   public void setHourlyWage(double wage) {
       this.hourlyWage = wage;
   }

   public void setHoursWorked(int hours) {
       this.hoursWorked = hours;
   }

   public void setPersonName(String name) {
       this.personName = name;
   }

   public void setPetName(String name) {
       this.petName = name;
   }

   public void setPetType(String type) {
       this.petType = type;
   }

   // getter methods for instance variables
   public double getHourlyWage() {
       return hourlyWage;
   }

   public int getHoursWorked() {
       return hoursWorked;
   }

   public String getPersonName() {
       return personName;
   }

   public String getPetName() {
       return petName;
   }

   public String getPetType() {
       return petType;
   }

   public String getSalary() {
       NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.US);
       return currencyFormat.format(getHourlyWage() * getHoursWorked());
   }

   // toString() method
   public String toString() {
       return getPetName() + "\'s Story" + "\n" +
           "Once upon a time there was a student named "+ getPersonName() + ","+ "\n" +
           "who had a pet " + getPetType() + ". " + getPetName() + ", the " + getPetType() + ", ate so much" + "\n" +
           "that "+getPersonName()+" had to get part-time job."+ getPersonName() + "\n" +
           "was paid "+ getHourlyWage()+" per hour and worked "+ getHoursWorked() +" hours per week"+ "\n" +
           "just to take care of "+getPetName()+"."+getPersonName()+" made "+ getSalary() +" per" + "\n" +
           "week and it was just enough money to pay for "+getPetName()+"\'s food." +"\n";
   }

}

Output:

Problems @ Javadoc Declaration Search Console X Call Hierarchy Debug <terminated> MadlibsDriver [Java Application] C:\Program

Add a comment
Know the answer?
Add Answer to:
You will write a single java program called MadLibs. java.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST...

    JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST CASES(WHAT IM TRYING TO PRODUCE) BELOW THOSE IMAGES ARE THE .JAVA FILES THAT I HAVE CREATED. THESE ARE GeometircObject.Java,Point.java, and Tester.Java. I just need help making the Rectangle.java and Rectangle2D.java classes. GeometricObject.Java: public abstract class GeometricObject { private String color = "white"; // shape color private boolean filled; // fill status protected GeometricObject() { // POST: default shape is unfilled blue this.color = "blue";...

  • How would you write the following program using switch statements instead of if-else statements (in java)...

    How would you write the following program using switch statements instead of if-else statements (in java) import java.util.*; public class ATM { public static void main(String[] args) { Scanner input = new Scanner (System.in); double deposit, withdrawal; double balance = 6985.00; //The initial balance int transaction; System.out.println ("Welcome! Enter the number of your transaction."); System.out.println ("Withdraw cash: 1"); System.out.println ("Make a Deposit: 2"); System.out.println ("Check your balance: 3"); System.out.println ("Exit: 4"); System.out.println ("***************"); System.out.print ("Enter Your Transaction Number "); transaction...

  • Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source...

    Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args [0] and the name of the file to save the formatted code in as args [1]. The original file is left untouched. The program makes no other changes the source code, including whitespace. For example, the...

  • read the code and comments, and fix the program by INSERTING the missing code in Java...

    read the code and comments, and fix the program by INSERTING the missing code in Java THank you please import java.util.Scanner; public class ExtractNames { // Extract (and print to standard output) the first and last names in "Last, First" (read from standard input). public static void main(String[] args) { // Set up a Scanner object for reading input from the user (keyboard). Scanner scan = new Scanner (System.in); // Read a full name from the user as "Last, First"....

  • Write a JAVA program that prompts the user for student grades and displays the highest and...

    Write a JAVA program that prompts the user for student grades and displays the highest and lowest grades in the class. The user should enter a character to stop providing values. Starter code: import java.util.Scanner; public class MaxMinGrades{   public static void main(String[] args){     Scanner input = new Scanner(System.in);     System.out.println("Enter as many student grades as you like. Enter a character to stop.");     double grade = input.nextDouble();     double minGrade = Double.MAX_VALUE;     double maxGrade = Double.MIN_VALUE;     while (Character.isDigit(grade)) {       if (grade == 0)...

  • write a program which include a class containing an array of words (strings).The program will search...

    write a program which include a class containing an array of words (strings).The program will search the array for a specific word. if it finds the word:it will return a true value.if the array does not contain the word. it will return a false value. enhancements: make the array off words dynamic, so that the use is prompter to enter the list of words. make the word searcher for dynamic, so that a different word can be searched for each...

  • In Java, write JUnit tests to verify various question-type objects for the following below: public interface...

    In Java, write JUnit tests to verify various question-type objects for the following below: public interface IAnswer {    public String getAnswer();    } import java.util.Scanner; YesNo class: public class YesNo implements IAnswer{            private String question;            public YesNo(String q){                this.question = q;            }                       //This function returns question text            public String getQuestionText() {                return question;...

  • Write one JUnit test in Java for the following: Essay class: import java.util.Scanner; public class Essay implements IAn...

    Write one JUnit test in Java for the following: Essay class: import java.util.Scanner; public class Essay implements IAnswer{ private String question; public Essay(String q){ this.question = q; } //This function returns question text public String getQuestionText() {    return question; } //This function takes answer from user public void answer(String userAnswer) {    // Take care of answer } @Override public String getAnswer() { System.out.println(question); Scanner scan = new Scanner(System.in); System.out.print("Answer: "); String ans =scan.nextLine(); scan.close(); if(ans.length() <=140){ return ans; }else{ return...

  • Hi. This is a prototype of Java. The following Java program was developed for prototyping a...

    Hi. This is a prototype of Java. The following Java program was developed for prototyping a mini calculator. Run the program and provide your feedback as the user/project sponsor. (Save the code as MiniCalculator.java; compile the file: javac MiniCalculator.java; and then run the program: java MiniCalculator). HINTs: May give feedback to the data type and operations handled by the program, the way the program to get numbers and operators, the way the calculation results are output, the termination of the...

  • Write a java programm that calculates the total of a retail sale should ask the user...

    Write a java programm that calculates the total of a retail sale should ask the user for the following: The retail price of the item being purchased The sales tax rate Once these items have been entered, the program should calculate and display the following: The sales tax for the purchase The total of the sale I tried doing it here. but it is not giving me the right answer. kindly help correct the errors. import java.util.Scanner; public class SalesTax...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT