I need some help adding a section before my quotes display on MyShoutBox.java th
ID: 3583920 • Letter: I
Question
I need some help adding a section before my quotes display on MyShoutBox.java that presents something like
"Here are some Doggy-dog quotes; which is your favorite?"
and something at the very end of my code that says "I hope you and (the dog they chose) will be very happy together! Enjoy!"
but I can't quite figure out how because I'm so bad at Java. Can someone help please and thank you? Here is what I have so far:
VirtualWorld.java
import java.util.Scanner;
public class VirtualWorld {
public static void main(String[] args)
{
MyClone myclone=new MyClone();
myclone.setFirstName("Name");
myclone.setLastName("Here");
myclone.introduction();
Puppy newPuppy=new Puppy();
Puppy[] puppy = new Puppy[5];
puppy[0] = new Puppy("Slinky","Dachshund","1");
puppy[1] = new Puppy("Kaiya","Husky","2");
puppy[2] = new Puppy("Cooper","Beagle","3");
puppy[3] = new Puppy("Billy","Golden Retriever","4");
puppy[4] = new Puppy("Stacy","Poodle","5");
for(Puppy puppies:puppy)
{
System.out.println("Puppy: "+puppies.toString());
}
System.out.print("Please select the tag number of the pup you'd like: ");
Scanner sc = new Scanner(System.in);
String tag_num = sc.nextLine();
boolean flag=newPuppy.displayTag(puppy,tag_num);
if(flag==true)
{
MyShoutBox bx=new MyShoutBox();
System.out.println("Your doggy-dog motto: "
+ bx.shoutOutCannedMessage());
System.out.println("Your bonus fortune cookie message is: "
+ bx.shoutOutRandomMessage());
}
else
{
System.out.println("Try again.");
}
}
}
MyClone.java
import java.util.Scanner;
public class MyClone {
private String firstName;
private String lastName;
private String home;
/**
* @param home the home to set
*/
public void setHome(String home) {
this.home = home;
}
// our first and last names will be set to private Strings
// we will need to access them through additional means
public String getFirstName()
{ //how we get firstName
return firstName; // returns firstName
}
public String getLastName()
{ //how we get lastName
return lastName; //returns lastName to system
}
public void setFirstName(String newFirstName) { //how we will set first name
firstName = newFirstName;
}
public void setLastName(String newLastName)
{ //how we will set last name
lastName = newLastName;
}
public void introduction() { // our introduction
System.out.println("Hello there! My name is "+firstName+" "+lastName+", and this is my home.");
System.out.println("What's your name?: ");
Scanner scan = new Scanner(System.in);
String username = scan.nextLine();
System.out.println(" Welcome, "+username+"! Enjoy our selection of puppies:");
}
}
MyShoutBox.java
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Scanner;
public class MyShoutBox {
private static Map<Integer, String> getCannedMessagesMap() {
//introduces the hm of our set phrases for user selection
Map<Integer, String> cannedMessages = new LinkedHashMap<>();
cannedMessages.put(1, "Lately, my dog's the only one around that listens to my problems.");
cannedMessages.put(2,
"Who let the dogs out?");
cannedMessages.put(3, "Dogs are man's best friends.");
cannedMessages.put(4,
"No one will love you are strongly or purely as a dog.");
cannedMessages.put(5, "Love is a puppy licking your face, even after you left him alone all day.");
cannedMessages.put(6, "A puppy is just a part of your life; you are their entire life.");
cannedMessages.put(7,
"Knick-knack, paddy whack, give the dog a bone.");
cannedMessages.put(8,
"Bow-wow.");
cannedMessages.put(9, "What is it, Lassie? Is little Timmy stuck in the well?");
cannedMessages.put(10, "I didn't cry when Old Yeller died, at least not in front of my friends.");
return cannedMessages;
}
public static String shoutOutCannedMessage() {
int id;
String message = "";
// this allows for the iteration over our collection
// allows us to access each element
Iterator<Entry<Integer, String>> msgIterator = getCannedMessagesMap().entrySet().iterator(); // utilize integer to display string
while (msgIterator.hasNext())
{ // as long as there is another in the collection
//this will continue
Map.Entry msgPair = (Map.Entry) msgIterator.next();
System.out.println("[" + msgPair.getKey() + "] "
+ msgPair.getValue());
//this puts [] around each of our numbers
}// will then end
try
{ //setting up a try/catch to handle exceptions
System.out.print("Please select the number of your favorite message: ");
// prints out the prompt for input to select
Scanner sc = new Scanner(System.in); //input for selection
System.out.println(" ");
id = sc.nextInt(); //input will be an Int
message = getCannedMessagesMap().get(id);
//this directly relates our choice to our above sentences
}
catch (Exception e)
{
// if we deviate from the expected input, we get this
System.out.println("You missed the mark! Oops! Your error is: " + e.getMessage());
}
return message; // returns the selection
}
public static String shoutOutRandomMessage() {
int i;
// This sets our words for potential random selection
String[] subject = { "You", "Someone", "Your dog", "A friend", "A stranger" };
String[] verb = { " is", " was", " will be", " might be", " won't be", " can't be" };
String[] adjective = { " rich", " successful", " educated", " friendly", " handsome" };
String[] object = { " with some help", " with a wife", " with his son", " with a friend", " with money" };
String[] adverb = { " in the end. ", " today. ", " someday. ",
" in some years. ", " tomorrow." };
Random k = new Random(); // this initializes Random
int chosenMessage = k.nextInt(subject.length);
String randomMessage = "";
for (i = 1; i <= 1; i++)
// our for loops which will allow for us to iterate
//through the potential words rather than staying stagnant
{
//sets randomMessage so it will iterate through the length of each list of words
// random will allow for this selection to be random
randomMessage = subject[k.nextInt(subject.length)]
+ verb[k.nextInt(verb.length)]
+ adjective[k.nextInt(adjective.length)]
+ object[k.nextInt(object.length)]
+ adverb[k.nextInt(adverb.length)];
}
return randomMessage;
}
public static void main(String[] args) {
System.out.println("Your selected doggy saying is: "
+ shoutOutCannedMessage()); // displays our selection
System.out.println("Your random message is: "
+ shoutOutRandomMessage()); // displays each word at random
//one from each list
}
}
Puppy.java
import java.util.Scanner;
class Puppy{
private String name;
private String color;
private String tag_num;
private int count=0;
Puppy()
{
this.name=name;
this.color=color;
this.tag_num=tag_num;
}
Puppy(String n,String c,String t){
tag_num = t;
color=c;
name = n;
tag_num=t;
count++;
}
static boolean displayTag(Puppy[] list, String tag_num)
{
for(int i=0;i<list.length;i++)
{
if(list[i] == null)
break;
if(list[i].tag_num.equals(tag_num))
{
System.out.println(" You reach in and grab... "+ list[i].tag_num+" ");
System.out.println("You've selected: ");
System.out.println("Tag No: "+list[i].tag_num);
System.out.println("Name: "+list[i].name);
System.out.println("Color: "+list[i].color+" ");
return true;
}
}
System.out.println(" No Pup found with Tag No: "+tag_num);
return false;
}
@Override
public String toString() {
return "[Tag Num]: " + tag_num + " [Name]: " + name + " [Breed]: " + color
+ "";
}
}
Explanation / Answer
solution
package com.cg.resources;
import java.util.Scanner;
public class VirtualWorld {
public static void main(String[] args)
{
MyClone myclone=new MyClone();
myclone.setFirstName("Name");
myclone.setLastName("Here");
myclone.introduction();
Puppy newPuppy=new Puppy();
Puppy[] puppy = new Puppy[5];
puppy[0] = new Puppy("Slinky","Dachshund","1");
puppy[1] = new Puppy("Kaiya","Husky","2");
puppy[2] = new Puppy("Cooper","Beagle","3");
puppy[3] = new Puppy("Billy","Golden Retriever","4");
puppy[4] = new Puppy("Stacy","Poodle","5");
for(Puppy puppies:puppy)
{
System.out.println("Puppy: "+puppies.toString());
}
System.out.print("Please select the tag number of the pup you'd like: ");
Scanner sc = new Scanner(System.in);
String tag_num = sc.nextLine();
boolean flag=newPuppy.displayTag(puppy,tag_num);
if(flag==true)
{
System.out.println( "Here are some Doggy-dog quotes; which is your favorite?");
System.out.println("-------------------------------------------------------");
MyShoutBox bx=new MyShoutBox();
System.out.println("Your doggy-dog motto: "
+ bx.shoutOutCannedMessage());
System.out.println("Your bonus fortune cookie message is: "
+ bx.shoutOutRandomMessage());
System.out.println(" ");
System.out.println( "I hope you and (the dog they chose) will be very happy together! Enjoy!");
}
else
{
System.out.println("Try again.");
}
}
}
----------------------------------------------------------------------------------------------
package com.cg.resources;
import java.util.Scanner;
public class MyClone {
private String firstName;
private String lastName;
private String home;
/**
* @param home the home to set
*/
public void setHome(String home) {
this.home = home;
}
// our first and last names will be set to private Strings
// we will need to access them through additional means
public String getFirstName()
{ //how we get firstName
return firstName; // returns firstName
}
public String getLastName()
{ //how we get lastName
return lastName; //returns lastName to system
}
public void setFirstName(String newFirstName) { //how we will set first name
firstName = newFirstName;
}
public void setLastName(String newLastName)
{ //how we will set last name
lastName = newLastName;
}
public void introduction() { // our introduction
System.out.println("Hello there! My name is "+firstName+" "+lastName+", and this is my home.");
System.out.println("What's your name?: ");
Scanner scan = new Scanner(System.in);
String username = scan.nextLine();
System.out.println(" Welcome, "+username+"! Enjoy our selection of puppies:");
}
}
---------------------------------------------------------------------------------------------------------------------------
package com.cg.resources;
import java.util.Scanner;
public class MyClone {
private String firstName;
private String lastName;
private String home;
/**
* @param home the home to set
*/
public void setHome(String home) {
this.home = home;
}
// our first and last names will be set to private Strings
// we will need to access them through additional means
public String getFirstName()
{ //how we get firstName
return firstName; // returns firstName
}
public String getLastName()
{ //how we get lastName
return lastName; //returns lastName to system
}
public void setFirstName(String newFirstName) { //how we will set first name
firstName = newFirstName;
}
public void setLastName(String newLastName)
{ //how we will set last name
lastName = newLastName;
}
public void introduction() { // our introduction
System.out.println("Hello there! My name is "+firstName+" "+lastName+", and this is my home.");
System.out.println("What's your name?: ");
Scanner scan = new Scanner(System.in);
String username = scan.nextLine();
System.out.println(" Welcome, "+username+"! Enjoy our selection of puppies:");
}
}
-------------------------------------------------------------------------------------------------------------------
package com.cg.resources;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Scanner;
public class MyShoutBox {
private static Map<Integer, String> getCannedMessagesMap() {
//introduces the hm of our set phrases for user selection
Map<Integer, String> cannedMessages = new LinkedHashMap<>();
cannedMessages.put(1, "Lately, my dog's the only one around that listens to my problems.");
cannedMessages.put(2,
"Who let the dogs out?");
cannedMessages.put(3, "Dogs are man's best friends.");
cannedMessages.put(4,
"No one will love you are strongly or purely as a dog.");
cannedMessages.put(5, "Love is a puppy licking your face, even after you left him alone all day.");
cannedMessages.put(6, "A puppy is just a part of your life; you are their entire life.");
cannedMessages.put(7,
"Knick-knack, paddy whack, give the dog a bone.");
cannedMessages.put(8,
"Bow-wow.");
cannedMessages.put(9, "What is it, Lassie? Is little Timmy stuck in the well?");
cannedMessages.put(10, "I didn't cry when Old Yeller died, at least not in front of my friends.");
return cannedMessages;
}
public static String shoutOutCannedMessage() {
int id;
String message = "";
// this allows for the iteration over our collection
// allows us to access each element
Iterator<Entry<Integer, String>> msgIterator = getCannedMessagesMap().entrySet().iterator(); // utilize integer to display string
while (msgIterator.hasNext())
{ // as long as there is another in the collection
//this will continue
Map.Entry msgPair = (Map.Entry) msgIterator.next();
System.out.println("[" + msgPair.getKey() + "] "
+ msgPair.getValue());
//this puts [] around each of our numbers
}// will then end
try
{ //setting up a try/catch to handle exceptions
System.out.print("Please select the number of your favorite message: ");
// prints out the prompt for input to select
Scanner sc = new Scanner(System.in); //input for selection
System.out.println(" ");
id = sc.nextInt(); //input will be an Int
message = getCannedMessagesMap().get(id);
//this directly relates our choice to our above sentences
}
catch (Exception e)
{
// if we deviate from the expected input, we get this
System.out.println("You missed the mark! Oops! Your error is: " + e.getMessage());
}
return message; // returns the selection
}
public static String shoutOutRandomMessage() {
int i;
// This sets our words for potential random selection
String[] subject = { "You", "Someone", "Your dog", "A friend", "A stranger" };
String[] verb = { " is", " was", " will be", " might be", " won't be", " can't be" };
String[] adjective = { " rich", " successful", " educated", " friendly", " handsome" };
String[] object = { " with some help", " with a wife", " with his son", " with a friend", " with money" };
String[] adverb = { " in the end. ", " today. ", " someday. ",
" in some years. ", " tomorrow." };
Random k = new Random(); // this initializes Random
int chosenMessage = k.nextInt(subject.length);
String randomMessage = "";
for (i = 1; i <= 1; i++)
// our for loops which will allow for us to iterate
//through the potential words rather than staying stagnant
{
//sets randomMessage so it will iterate through the length of each list of words
// random will allow for this selection to be random
randomMessage = subject[k.nextInt(subject.length)]
+ verb[k.nextInt(verb.length)]
+ adjective[k.nextInt(adjective.length)]
+ object[k.nextInt(object.length)]
+ adverb[k.nextInt(adverb.length)];
}
return randomMessage;
}
public static void main(String[] args) {
System.out.println("Your selected doggy saying is: "
+ shoutOutCannedMessage()); // displays our selection
System.out.println("Your random message is: "
+ shoutOutRandomMessage()); // displays each word at random
//one from each list
}
}
---------------------------------------------------------------------------------------------------------------------
package com.cg.resources;
import java.util.Scanner;
class Puppy{
private String name;
private String color;
private String tag_num;
private int count=0;
Puppy()
{
this.name=name;
this.color=color;
this.tag_num=tag_num;
}
Puppy(String n,String c,String t){
tag_num = t;
color=c;
name = n;
tag_num=t;
count++;
}
static boolean displayTag(Puppy[] list, String tag_num)
{
for(int i=0;i<list.length;i++)
{
if(list[i] == null)
break;
if(list[i].tag_num.equals(tag_num))
{
System.out.println(" You reach in and grab... "+ list[i].tag_num+" ");
System.out.println("You've selected: ");
System.out.println("Tag No: "+list[i].tag_num);
System.out.println("Name: "+list[i].name);
System.out.println("Color: "+list[i].color+" ");
return true;
}
}
System.out.println(" No Pup found with Tag No: "+tag_num);
return false;
}
@Override
public String toString() {
return "[Tag Num]: " + tag_num + " [Name]: " + name + " [Breed]: " + color
+ "";
}
}
output
Hello there! My name is Name Here, and this is my home.
What's your name?:
David
Welcome, David! Enjoy our selection of puppies:
Puppy: [Tag Num]: 1 [Name]: Slinky [Breed]: Dachshund
Puppy: [Tag Num]: 2 [Name]: Kaiya [Breed]: Husky
Puppy: [Tag Num]: 3 [Name]: Cooper [Breed]: Beagle
Puppy: [Tag Num]: 4 [Name]: Billy [Breed]: Golden Retriever
Puppy: [Tag Num]: 5 [Name]: Stacy [Breed]: Poodle
Please select the tag number of the pup you'd like: 2
You reach in and grab... 2
You've selected:
Tag No: 2
Name: Kaiya
Color: Husky
Here are some Doggy-dog quotes; which is your favorite?
-------------------------------------------------------
[1] Lately, my dog's the only one around that listens to my problems.
[2] Who let the dogs out?
[3] Dogs are man's best friends.
[4] No one will love you are strongly or purely as a dog.
[5] Love is a puppy licking your face, even after you left him alone all day.
[6] A puppy is just a part of your life; you are their entire life.
[7] Knick-knack, paddy whack, give the dog a bone.
[8] Bow-wow.
[9] What is it, Lassie? Is little Timmy stuck in the well?
[10] I didn't cry when Old Yeller died, at least not in front of my friends.
Please select the number of your favorite message:
2
Your doggy-dog motto: Who let the dogs out?
Your bonus fortune cookie message is: A friend will be educated with money in some years.
I hope you and (the dog they chose) will be very happy together! Enjoy!