Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need some help with my Java code! I\'m quite a newbie at coding, but I\'m trying

ID: 3583831 • Letter: N

Question

Need some help with my Java code! I'm quite a newbie at coding, but I'm trying to accomplish a couple of things with my code. I want to greet the user and introduce myself. I then want to display a list of puppies and ask the user to select one by its number. I want to then display the choice, display some dog sayings, and then have the user choose one. I then want to print this out and display their message and a randomized message. The only parts I really need help with are getting the Puppy list to display, and having the one that the user selects actually display. Can you help me? Here is my code:

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;

}

}

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

package com.ct.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", "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
       }

}

-------------------------------------------------------------------------------------------------------

package com.ct.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+"my lastname is "+lastName+", and my home in "+home);
   System.out.println("What's your name?: ");
   Scanner scan = new Scanner(System.in);
   String username = scan.nextLine();
   System.out.println("Welcome,"+username);
  
   }

}

------------------------------------------------------------------------------------------------------------------------

package com.ct.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(" 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 true;
}
}
System.out.println(" No Pupper found with Tag No: "+tag_num);
return false;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
   return "Puppy [name=" + name + ", color=" + color + ", tag_num=" + tag_num
           + "]";
}

}

---------------------------------------------------------------------------------------------------

package com.ct.resources;

import java.util.Scanner;

public class VirtualWorld {
   public static void main(String[] args)
   {
   MyClone myclone=new MyClone();
   myclone.setFirstName("david");
   myclone.setLastName("martin");
   myclone.setHome("california");
   myclone.introduction();
   Puppy newPuppy=new Puppy();
   Puppy[] puppy = new Puppy[5];

   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");
  
   for(Puppy puppies:puppy)
   {
       System.out.println("the puppy is "+puppies.toString());

   }
   System.out.print("Please enter tag number to search: ");
   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("wrong choice");
   }
   }  
}

-----------------------------------------------------------------------------------

output

Hello there! My name is davidmy lastname is martin, and my home incalifornia
What's your name?:
koti
Welcome,koti
the puppy is Puppy [name=Tom, color=Red, tag_num=C1323]
the puppy is Puppy [name=Ron, color=blue, tag_num=C9457]
the puppy is Puppy [name=Harry, color=brown, tag_num=C3067]
the puppy is Puppy [name=Billy, color=black, tag_num=C7330]
the puppy is Puppy [name=Stacy, color=white, tag_num=C6211]
Please enter tag number to search: C1325

No Pupper found with Tag No: C1325
wrong choice

Hello there! My name is davidmy lastname is martin, and my home in california
What's your name?:
leena
Welcome,leena
the puppy is Puppy [name=Tom, color=Red, tag_num=C1323]
the puppy is Puppy [name=Ron, color=blue, tag_num=C9457]
the puppy is Puppy [name=Harry, color=brown, tag_num=C3067]
the puppy is Puppy [name=Billy, color=black, tag_num=C7330]
the puppy is Puppy [name=Stacy, color=white, tag_num=C6211]
Please enter tag number to search: C7330

Pupper found...C7330 C7330
Name: Billy
Color: black
Tag No: C7330
[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:

4
Your doggy-dog motto: No one will love you are strongly or purely as a dog.
Your bonus fortune cookie message is: You won't be successful with a friend today.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote