Question

In: Computer Science

Here is class Dog -String name; -int age -String breed; -boolean adopted; //false if available …………………...

Here is class Dog

-String name; -int age

-String breed;

-boolean adopted; //false if available ………………… +constructor, getters, setters, toString() Write a lambda expression showAdoptable using a standard functional interface that will display the dog’s name age and breed if adopted is false.

Help please!!

Solutions

Expert Solution

Here is the code for the above requirements in JAVA:

@FunctionalInterface
interface PrintIfNotAdopted{
        String showAdoptable();
}

class Dog implements PrintIfNotAdopted{
    String name;
    int age;
    String breed;
    boolean adopted;

    public Dog(String name, int age, String breed, boolean adopted){
        this.name = name;
        this.age = age;
        this.breed = breed;
        this.adopted = adopted;    
    }
    
    public String getName(){
         return name;
    } 
    public void setName(String name){
         this.name = name;
    } 
    public int getAge(){
         return age;
    } 
    public void setAge(int age){
         this.age = age;
    } 
    public String getBreed(){
         return breed;
    } 
    public void setBreed(String breed){
         this.breed = breed;
    } 
    public boolean getAdopted(){
         return adopted;
    } 
    public void setAdopted(boolean adopted){
         this.adopted = adopted;
    } 
     
    public String toString(){
        return "DOG NAME: "+name+", DOG AGE: "+age+", DOG BREED: "+breed+", IS ADOPTED: "+adopted;    
    }
    
        @Override
        public String showAdoptable() {
         if(!adopted){
            return "DOG IS NOT ADOPTED: NAME: "+name+", AGE: "+age+", BREED: "+breed;    
          }       
          else{return "";} 
        }

    public static void main(String[] args){
        Dog d1 = new Dog("Bruno",12,"Wild",false);
        System.out.println(d1.toString());               
        System.out.println(d1.showAdoptable());    
    }
}

Output when Dog is adopted:

Output when Dog is not adopted:

I hope this will help you. Thanks.


Related Solutions

Consider the following class: class Person {         String name;         int age;        ...
Consider the following class: class Person {         String name;         int age;         Person(String name, int age){                this.name = name;                this.age = age;         } } Write a java program with two classes “Teacher” and “Student” that inherit the above class “Person”. Each class has three components: extra variable, constructor, and a method to print the student or the teacher info. The output may look like the following (Hint: you may need to use “super”...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
// TODO 1 class Cat(private val name: String) { var sleep: Boolean = false fun toSleep()...
// TODO 1 class Cat(private val name: String) { var sleep: Boolean = false fun toSleep() { println() } } fun main() { // TODO 2 val gippy = Cat("") gippy.toSleep() gippy.sleep = true gippy.toSleep() } TODO 1: Complete the code in the Cat class with the following conditions: Create a getter setter function for the sleep property in which there is a function to print text: The getter / setter function is called Add code to the toSleep ()...
1. Lets construct a Dog class 2.Declare name, breed and weight as their private variables 3.Add...
1. Lets construct a Dog class 2.Declare name, breed and weight as their private variables 3.Add a default constructor 4.Write a constructor with name, breed and weight as arguments 5.Add a toString method to the class 6.Add a equals method 7.Add a copy contructor 8.Add a bark method in Dog class •This method checks if the weight is greater than 25 , then print (dog.name says woof!) else (dog.name say yip!)
Create a class DogWalker member List <String>doggies method: void addDog(String name ) //add dog to the...
Create a class DogWalker member List <String>doggies method: void addDog(String name ) //add dog to the List method: void printDogList() //print all dogs in list /* You’ll need an index. Iterate over your list of dog names in a while loop. Use your index to “get” the dog at the current index When you ‘find’ your dog name in the list, print it out */ Method: void findDogUsingWhile(String dogName) /* You’ll need an index. Iterate over your list of dog...
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt()
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt() = 0;     virtual void fight(Monster&);     string getName() const; }; class GiantMonster : public Monster {     protected:         int height;          public:         GiantMonster(string, int, int);         virtual void trample(); }; class Dinosaur : public GiantMonster {     public:     Dinosaur(string, int, int);     void hunt();     void roar(); }; class Kraken : protected GiantMonster {     public:     Kraken(string, int, int);     virtual void hunt();     void sinkShip(); }; Indicate if the code snippets below are...
int main() {    footBallPlayerType bigGiants[MAX];    int numberOfPlayers;    int choice;    string name;   ...
int main() {    footBallPlayerType bigGiants[MAX];    int numberOfPlayers;    int choice;    string name;    int playerNum;    int numOfTouchDowns;    int numOfcatches;    int numOfPassingYards;    int numOfReceivingYards;    int numOfRushingYards;    int ret;    int num = 0;    ifstream inFile;    ofstream outFile;    ret = openFile(inFile);    if (ret)        getData(inFile, bigGiants, numberOfPlayers);    else        return 1;    /// replace with the proper call to getData    do    {       ...
Book SerialNum : String - Name: String – Author : String PublishYear: int - Edition:int Status...
Book SerialNum : String - Name: String – Author : String PublishYear: int - Edition:int Status : boolean + Book() Book(String, String, String, int , int)+ + setName(String) : void + setSerialNum(String) : void + setAuthor(String) : void + setEdition(int) : void + setYear(int) : void + getName():String + getAuthor():String + getYear(): int + getSerialNum(): String + getEdition():int + setAvailable() : void + setUnavailable(): void checkAvailability(): boolean ----------------------------------------------------------------------------------- Library Name : String Address: String Static NUMBEROFBOOKS: int BooksAtLibrray : ArrayList...
in java, write code that defines a class named Cat The class should have breed, name...
in java, write code that defines a class named Cat The class should have breed, name and weight as attributes. include setters and getters for the methods for each attribute. include a toString method that prints out the object created from the class, and a welcome message. And use a constructor that takes in all the attributes to create an object.
Given this class: class Issue { public:                 string problem; int howBad; void setProblem(string problem); };...
Given this class: class Issue { public:                 string problem; int howBad; void setProblem(string problem); }; And this code in main: vector<Issue> tickets; Issue issu; a) tickets.push_back(-99); State whether this code is valid, if not, state the reason b) Properly implement the setProblem() method as it would be outside of the class. You MUST name the parameter problem as shown in the prototype like: (string problem) c) Write code that will output the problem attribute for every element in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT