I need help putting this Java code all together in a way that makes this run smo
ID: 3583784 • Letter: I
Question
I need help putting this Java code all together in a way that makes this run smoothly. I need the four classes I have, and I would like for my code to introduce me/greet the user, display a list of available puppies, ask them to select a puppy by the number of the puppy, and then print their selection. From there, I want it to display a list of sayings, have them select a saying, and return that and a bonus randomized saying. The part I'm having trouble with is actually getting it to first display the list of puppies and then prompt the user to select a puppy and returning what they selected before carrying on with the rest of the code (and only having one Main-- I need it to be in VirtualWorld.java but have one in Puppy.java as well). Can someone please help? Here is what I have so far:
VirtualWorld.java
public class VirtualWorld {
public static void main(String[] args)
{
MyClone myclone=new MyClone();
myclone.setFirstName("Name");
myclone.setLastName("Here");
myclone.introduction();
MyShoutBox bx=new MyShoutBox();
System.out.println("Your doggy-dog motto: "
+ bx.shoutOutCannedMessage());
System.out.println("Your bonus fortune cookie message is: "
+ bx.shoutOutRandomMessage());
}
}
MyClone.java
import java.util.Scanner;
public class MyClone {
private String firstName;
private String lastName;
// 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+"! Please select a puppy.");
System.out.println("Select puppy number: ");
Scanner scan2 = new Scanner(System.in);
int index = scan.nextInt();
System.out.println("The puppy selected by the user is: ");
}
}
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", "A relative", "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(String n,String c,String t){
tag_num = t;
color=c;
name = n;
tag_num=t;
count++;
}
static int 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(" Pupper found..."+ list[i].tag_num+" "+tag_num);
System.out.println("Name: "+list[i].name);
System.out.println("Color: "+list[i].color);
System.out.println("Tag No: "+list[i].tag_num);
return 0;
}
}
System.out.println(" No Pupper found with Tag No: "+tag_num);
return 0;
}
public static void main(String args[]){
Puppy[] puppy = new Puppy[10];
puppy[0] = new Puppy("Tom","Red","C1323");
puppy[1] = new Puppy("Ron","blue","C9457");
puppy[2] = new Puppy("Harry","brown","C3067");
puppy[3] = new Puppy("Billy","black","C7330");
puppy[4] = new Puppy("Stacy","white","C6211");
System.out.print("Please enter tag number to search: ");
Scanner sc = new Scanner(System.in);
String tag_num = sc.nextLine();
displayTag(puppy,tag_num);
}
}
Explanation / Answer
So the below given program will return the selected puppy and then further this id is used for next programs
VirtualWorld.java
public class VirtualWorld {
public static void main(String[] args)
{
MyClone myclone=new MyClone();
myclone.setFirstName("Name");
myclone.setLastName("Here");
int puppy_id= myclone.introduction();
MyShoutBox bx=new MyShoutBox();
System.out.println("Your doggy-dog motto: "
+ bx.shoutOutCannedMessage());
System.out.println("Your bonus fortune cookie message is: "
+ bx.shoutOutRandomMessage());
}
}
MyClone.java
import java.util.Scanner;
public class MyClone {
private String firstName;
private String lastName;
// 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 int 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+"! Please select a puppy.");
System.out.println("Select puppy number: ");
Scanner scan2 = new Scanner(System.in);
int index = scan.nextInt();
System.out.println("The puppy selected by the user is: "+ index);
return index;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.