Question

In Java*

Please implement a class called "MyPet". It is designed as shown in the following class diagram.

  • Four private instance variables: name (of the type String), color (of the type String), gender (of the type char) and weight(of the type double).
  • Three overloaded constructors:
    • a default constructor with no argument
    • a constructor which takes a string argument for name, and
    • a constructor with take two strings, a char and a double for name, color, gender and weight respectively.
  • public getter and setter methods for each attribute.
  • a public toString() method that returns short description or the instances.
  • create multiple MyPet objects in the main function and call multiple methods for testing.

MyPet name: String -color: String |- gender: char weight: double + MyPet() +MyPet(name:String) +MyPet( name:String, color:Str

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

CODE:

​​​​​​

public class MyPet {

    private String name;
    private String color;
    private char gender;
    private double weight;

    public MyPet() {
        name = color = "";
        gender = ' ';
        weight = 0;

    }

    public MyPet(String name) {
        this.name = name;
        color = "";
        gender = ' ';
        weight = 0;
    }

    public MyPet(String name, String color, char gender, double weight) {
        this.name = name;
        this.color = color;
        this.gender = gender;
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "Pet name: " + name + ", Color: " + color + ", Gender: " + gender + ", Weight: " + weight+" lbs.";
    }

    public static void main(String[] args) {

        MyPet aPet = new MyPet();
        System.out.println("aPet = " + aPet);
        aPet.setName("Pit");
        aPet.setColor("Red");
        aPet.setGender('F');
        aPet.setWeight(9.8);
        System.out.println("aPet = " + aPet);
        

        MyPet bPet = new MyPet("Bolt");
        System.out.println("bPet = " + bPet);


        MyPet cPet = new MyPet("Thunder","Black",'M',12);
        System.out.println("cPet = " + cPet);

    }
}

DON'T FORGET TO LIKE...

Add a comment
Know the answer?
Add Answer to:
In Java* Please implement a class called "MyPet". It is designed as shown in the following...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A class called Author is designed as shown in the class diagram. (JAVA)

    in javaA class called Author is designed as shown in the class diagram. Author name: String email: String gender: char +Author (name :String, email: String, gender char) +getName ():String +getEmail() String +setEmail (email: String) :void +getGender(): char +toString ():String It contains  Three private instance variables: name (String), email (String), and gender (char of either 'm' or 'f),  One constructor to initialize the name, email and gender with the given values; public Author (String name, String email, char gender) f......)  public getters/setters: getName(), getEmail setEmail and getGender(); (There are no setters for name and gender,...

  • Create a NetBeans project called "Question 1". Then create a public class inside question1 package called Author according to the following information (Make sure to check the UML diagram)

    Create a NetBeans project called "Question 1". Then create a public class inside question1 package called Author according to the following information (Make sure to check the UML diagram) Author -name:String -email:String -gender:char +Author(name:String, email:String, gender:char) +getName():String +getEmail):String +setEmail (email:String):void +getGender():char +tostring ):String . Three private instance variables: name (String), email (String), and gender (char of either 'm' or 'f'): One constructor to initialize the name, email and gender with the given values . Getters and setters: get Name (), getEmail() and getGender (). There are no setters for name and...

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

  • Create a class called Flower. Add one member variables color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower

    Create a class called Flower. Add one member variables color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower named Rose. The Rose class has one member variable name.  Add a constructor which expects color and  name. Pass color to the base constructor and set name to it's member variable.Write a program that has an array of ...

  • java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the...

    java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the below classes: public class Date {    private String month;    private String day;    private String year;    public Date(String month, String day, String year) {       this.month = month;       this.day = day;       this.year = year;    }    public String getMonth() {       return month;    }    public String getDay() {       return day;    }    public String...

  • Horse Team(Java) . Horse -name:String -age:int -weight:double +Horse():void +Horse(String, int double):void +getName():String +getAge():int +getWeight():double +setName(String):void +setAge(int):void...

    Horse Team(Java) . Horse -name:String -age:int -weight:double +Horse():void +Horse(String, int double):void +getName():String +getAge():int +getWeight():double +setName(String):void +setAge(int):void +setWeight(double):void +compareName(String):boolean +compareTo(Horse):int +toString():String Team -team:ArrayList<Horse> +Team():void +addHorse(String, int, double):void +addHorse(Horse):void +averageAge():int +averageWeight():int +getHorse(int):Horse +getSize():int +findHorse(String):Horse +largestHorse():Horse +oldestHorse():Horse +removeHorse(String):void +smallestHorse():Horse +toString():String +youngestHorse():Horse HorseTeamDriver +main(String[]):void Create a class (Horse from the corresponding UML diagram) that handles names of horse, the age of the horse, and the weight of the horse. In addition to the standard accessor and mutator methods from the instance fields, the class...

  • Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a...

    Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races(of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. DemoHorses.java public class DemoHorses { public static void main(String args[])...

  • JAVA PLEASE HELP The following is a UML diagram for a class. Write the code for...

    JAVA PLEASE HELP The following is a UML diagram for a class. Write the code for this class. Cat - breed : String - color : Color - name : String + Cat( name : String) + Cat(name : String, breed : String, color : Color) + getName() : String + getColor() : Color + getBreed() : String setColor(Color) setBreed(Breed) toString()

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • Implement the following class in Java: Class name: People Constructor Summary: public People (Str...

    Implement the following class in Java: Class name: People Constructor Summary: public People (String name) Methods: public People(String name) public void setName(String name) public String getName() Class name: Truck Constructor Summary: public Truck (int licensePlate, int onBoard, People people) Initially, the truck does not contain any on board the vehicle. If the number of people on board the vehicle is a negative number, the value of onBoard should be stored as zero. Methods: public int getLicensePlate() Returns license plate of...

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