Question

In: Computer Science

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 valid or invalid:

a)

GiantMonster * m = new Kraken (“Sea Horror”, 500, 100);

b)

Kraken * k = new Kraken(“Cthulu”, 500, 100);

Cout << k->getName() << endl;

c)

Dinosaur trex(“T-Rex”, 100, 70);

GiantMonster m = trex;

m.roar();

d)

Monster * m = new GiantMonster (“Godzilla”, 100, 300);

e)

Dinosaur dino ( “Triceratops”, 0, 150);

Dinosaur trex ( “T-rex”, 200, 120);

Trex.fight(dino);

f)

Dinosaur * d = new Dinosaur (“T-Rex”, 100, 50);

GiantMonster * g = new Dinosaur (“Godzilla”, 100, 500);

g->fight(*d);

g)

Dinosaur * d = newGiantMonster(“T-Rex”, 100, 50);

Solutions

Expert Solution


a)

GiantMonster * m = new Kraken (“Sea Horror”, 500, 100);

This statement is invalid.
Because here base class is GiantMonster which is inaccessible.
So the conversion from Kraken to inaccessible base class GiantMonster is not allowed.


b)

Kraken * k = new Kraken(“Cthulu”, 500, 100);
Cout << k->getName() << endl;

This code snippet is invalid.
Because getName is in base class Monster. From GiantMonster getName is accessible. But Kraken can access only protected members of GiantMonster.
so for object k getName is inaccessible.

c)

Dinosaur trex(“T-Rex”, 100, 70);
GiantMonster m = trex;
m.roar();

This code snippet is invalid.
Because object of abstract class type "GiantMonster" is not allowed in c++.
Note :
GiantMonster is abstract class because this class has not Overriden(re-defined) "hunt" function from base class Monster.
hunt is a Pure Virtual Function and it needs to be implemented in GiantMonster.

Additionally to convert this snippet into valid one we need following code.
Dinosaur trex("T - Rex", 100, 70);
Dinosaur m = trex;
m.roar();

d)

Monster * m = new GiantMonster (“Godzilla”, 100, 300);
This code snippet is invalid.
Because object of abstract class type "GiantMonster" is not allowed in c++.


e)

Dinosaur dino ( “Triceratops”, 0, 150);
Dinosaur trex ( “T-rex”, 200, 120);
Trex.fight(dino);

I am not sure if there is a spelling mistake for 3rd line.
But if the statement at 3rd line is Trex.fight(dino); then the code snippet is invalid because variable names are case sensitive.
But if the statement at 3rd line is starts with small 't' i.e. trex.fight(dino); then the statement is valid.

f)

Dinosaur * d = new Dinosaur (“T-Rex”, 100, 50);
GiantMonster * g = new Dinosaur (“Godzilla”, 100, 500);
g->fight(*d);

This code snippet is valid.

g)

Dinosaur * d = newGiantMonster(“T-Rex”, 100, 50);
This code snippet is invalid.
Because object of abstract class type "GiantMonster" is not allowed in c++.


Related Solutions

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...
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...
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....
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return count;   } } class Worker extends Thread{ Counter count;   Worker(Counter count){     this.count = count;   }   public void run(){     for (int i = 0; i < 1000;i++){       synchronized(this){         count.inc();       }}   } } public class Test {     public static void main(String args[]) throws InterruptedException   {     Counter c = new Counter();     Worker w1 = new Worker(c);     Worker w2 = new Worker(c);     w1.start();     w2.start();     w1.join();     w2.join();     System.out.println(c.get());      ...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result;...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result; String msg; final int LOW = 1; final int HIGH = 10; result = LOW + (int)(Math.random() * HIGH); guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try to guess my number between " + LOW + " and " + HIGH)); if(guess == result) msg = "\nRight!"; else if(guess < result) msg = "\nYour guess was too low"; else msg = "\nYour guess was too high"; JOptionPane.showMessageDialog(null,"The number...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT