Perhaps that is the wrong type of wording! But I need help to combine my work so
ID: 3576890 • Letter: P
Question
Perhaps that is the wrong type of wording! But I need help to combine my work so that it only has one main class. As it is, my work is very disjointed. I'm very very new to Java programming, so a lot of my work is very basic and I'm unsure of how to proceed.
Here is this week's assignment:
"You will create a Virtual World application as your final project. This Virtual World will have several objects including a MyClone object and another object of your choice. It would be an excellent idea to review the Final Project Guidelines at this time. For this Third Final Project Milestone, you will finish development of your MyClone class and create another class of your choice.
In Module Two, you started development of the MyClone class. You will continue to develop the MyClone class by adding accessor methods, mutator methoda, and an introduction() method. The introduction() method will introduce yourself to the virtual world by displaying a greeting, your first and last name, and anything else you would want to say.
You will also create another class of your choice. Your programmer-defined class should have at a minimum two instance variables and one method. The instance variables and method should be representative of the data and behavior that objects of this class will have. You will demonstrate your understanding of encapsulation by declaring the instance varibales as private and by creating accessors and mutatora for each instance variable. You should have at least one constructor created that initializes all instance variables. Document your code."
I have chosen to make a Puppy class, which I have included below. What I need help with so far is tying everything I need together with one main class so it all fits... I'm really lost...
Here is what I have so far:
MyClone:
public class MyClone {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String newFirstName) {
firstName = newFirstName;
}
public void setLastName(String newLastName) {
lastName = newLastName;
}
VirtualWorld.java
public class VirtualWorld {
public static void main(String[] args) {
MyClone clone = new MyClone();
clone.setFirstName("firstnamehere");
clone.setLastName("lastnamehere");
System.out.println("First Name: " + clone.getFirstName());
System.out.println("Last Name: " + clone.getLastName());
}
}
Puppy.java
package org.puppy;
public class Puppy
{
private int puppyTag;
private String puppyType;
private String puppyName;
public Puppy() {
this.puppyTag = 001; //sets puppyTag of first dog
this.puppyType= "Dachshund";
this.puppyName = "Slinky";
}
public Puppy(int puppyTag, String puppyName, String puppyType) {
super();
this.puppyTag = puppyTag;
this.puppyType = puppyType;
this.puppyName = puppyName;
}
public int getPuppyTag()
{
return puppyTag;
}
public void setPuppyTag(int puppyTag)
{
this.puppyTag = puppyTag;
}
public String getPuppyType()
{
return puppyType;
}
public void setPuppyType(String puppyType)
{
this.puppyType = puppyType;
}
public String getPuppyName()
{
return puppyName;
}
public void setPuppyName(String puppyName)
{
this.puppyName = puppyName;
}
public void displayPuppyInfo()
{
System.out.println("Puppy Tag#: " + puppyTag);
System.out.println("Puppy Name: " + puppyName);
System.out.println("Puppy Type: " + puppyType);
System.out.println(" ");
}
}
DogTypes.java
package puppy;
import org.puppy.Puppy;
public class DogTypes {
public static void main(String[] args) {
Puppy a = new Puppy(); //allows us to differentiate different puppies
a.displayPuppyInfo();
Puppy b = new Puppy();
b.setPuppyTag(002);
b.setPuppyName("Johnny Danger");
b.setPuppyType("Poodle");
b.displayPuppyInfo();
Puppy c = new Puppy();
c.setPuppyTag(003);
c.setPuppyType("Pit Bull");
c.setPuppyName("Billy");
c.displayPuppyInfo();
Puppy d = new Puppy(004, "Kaiya", "Husky");
d.displayPuppyInfo();
}
}
Explanation / Answer
The MyClone class requries a function called introduction defined. Adding the function the code looks as follows:
public class MyClone {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String newFirstName) {
firstName = newFirstName;
}
public void setLastName(String newLastName) {
lastName = newLastName;
}
// Introduction function added here. This prints an introduction of the clone object
public void introduction() {
System.out.println("Hello, I am "+firstName+" "+lastName) ;
}
}
NOTE - : Denoting the values of an integer like 004 or 001 means that you are writing them as octal literals. (00 prefix means octal) Do you intend to do the same or is it a mistake. If you are numbering the puppies, you can use only 1 or 2 or 3, and not 001, 002, 003. It would be confusing after the 7th puppy.
Comibining everything together to make it a single main class is simple, however not really required. That being said, the overall objective of combining the two can be to make everything work together. You can change a few things to make it work like cloning a puppy. Here is a sample implementation of the same:
public class TheVirtualWorld {
public static void main(String[] args) {
Puppy a = new Puppy(); // allows us to differentiate different puppies
a.displayPuppyInfo();
Puppy b = new Puppy();
b.setPuppyTag(2);
b.setPuppyName("Johnny Danger");
b.setPuppyType("Poodle");
b.displayPuppyInfo();
Puppy c = new Puppy();
c.setPuppyTag(3);
c.setPuppyType("Pit Bull");
c.setPuppyName("Billy");
c.displayPuppyInfo();
Puppy d = new Puppy(4, "Kaiya", "Husky");
d.displayPuppyInfo();
MyClone clone = new MyClone();
clone.setFirstName("firstnamehere");
clone.setLastName("lastnamehere");
clone.introduce();
// Clone a puppy (?)
MyClone puppyClone = new MyClone();
puppyClone.setFirstName(a.getPuppyName());
puppyClone.setLastName(a.getPuppyType());
puppyClone.introduce();
}
}