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

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...
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;...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
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 +...
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...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT