Question

In: Computer Science

// 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 () function to print text:

[name], sleep!

if sleep is true and text:

name, let's play!

if sleep is false.

TODO 2:

Complete initialization with Cat class.

If run the console will display text like the following:

The getter function is called

Gippy, let's play!

The setter function is called

The getter function i

Solutions

Expert Solution

#include <iostream>

using namespace std;

// TODO 1

class Cat

{

private:

    string name;//private variable name

public:

  //constructor called when object created and set name

   Cat(string name){

    this->name = name;

       }

   //boolean sleep

    bool sleep = false;

    //toSleep function to print the status of sleep

    void toSleep()

    {

        if(getSleep()){   //when sleep is true

            cout<<this->name<<",sleep!"<<endl;

        }

        else{

            cout<<this->name<<",let's play!"<<endl;

        }       

    }

    //set sleep

    void setSleep(bool sleep){

        this->sleep = sleep;

    }

    //get sleep

    bool getSleep(){

        return this->sleep;

    }

};

//main driver

int main()

{

    Cat gippy = Cat("gippy");  

    gippy.setSleep(true);  

    gippy.toSleep();

    gippy.setSleep(false);

     gippy.toSleep();

    return 0;

}


Related Solutions

fun main() {     val stringResult = getResult("Kotlin")     val intResult = getResult(100)     // TODO...
fun main() {     val stringResult = getResult("Kotlin")     val intResult = getResult(100)     // TODO 2     println() } // TODO 1 fun <T> getResult(args: T): Int {     return 0 } TODO 1: Create a new generics function with the following conditions: The function name is getResult. Has one type of parameter. If the attached argument is of type Int, then the value returned is the value of the argument multiplied by 5. If the attached argument is...
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!!
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...
public class Person { private String name; public Person() { name = "No name yet"; }...
public class Person { private String name; public Person() { name = "No name yet"; } public Person(String initialName) { name = initialName; } public void setName(String newName) { name = newName; } public String getName() { return name; } public void writeOutput() { System.out.println("Name: " + name); } public boolean hasSameName(Person otherPerson) { return this.name.equalsIgnoreCase(otherPerson.name); } } 2- Write a Program Patient. Java Class that extends Person to include  Social security Gender  Appropriate construtors, accessors, and mutators....
Create a java class with name Cat. Instructions for Cat class: This class is modeled after...
Create a java class with name Cat. Instructions for Cat class: This class is modeled after a Cat. You should have instance variables as follows: The Cat’s name The number of mice caught by the Cat. Whether or not the Cat is secretly plotting to kill you Note that you will need to choose both good types and meaningful identifiers for each of these instance variables. You may also assume that the Cat is not automatically always secretly plotting to...
Suppose we have a Java class called Person.java public class Person { private String name; private...
Suppose we have a Java class called Person.java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } (a) In the following main method which lines contain reflection calls? import java.lang.reflect.Field; public class TestPerson { public static void main(String args[]) throws Exception { Person person = new Person("Peter", 20); Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "Paul"); } }...
In Java, design and implement a class called Cat. Each Cat class will contain three private...
In Java, design and implement a class called Cat. Each Cat class will contain three private variables - an integer representing its speed, a double representing its meowing loudness, and a String representing its name. Using the Random class, the constructor should set the speed to a random integer from 0 to 9, the meowing loudness to a random double, and the name to anything you want; the constructor should take no parameters. Write “get” and “set” methods for each...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: name - "John Doe" address - use the default constructor of Address one constructor with all (two) parameters one input parameter for each attribute Methods public String toString() returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /**...
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /** * Constructor for objects of class Classroom */ public Classroom() { this.capacity = 0; }    /** * Constructor for objects of class Classroom * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Classroom(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT