Question

In: Computer Science

Assume you have created the following data definition class: public abstract class Organization { private String...

Assume you have created the following data definition class:

public abstract class Organization {
private String name;
   private int numEmployees;
   public static final double TAX_RATE = 0.01;

   public String getName() { return this.name; }
   public int getNumEmployees() { return this.numEmployees; }
    
   public abstract double calculateTax() {
      return this.numEmployees * TAX_RATE;
   }
}

In your own words, briefly (1-2 sentences) explain why the data definition class will not compile. Then, write modified code that addresses the issue.

Solutions

Expert Solution

This given Organization class is abstract class. Abstract class has at least one abstractmethod and abstract method does not have body. So, the given code won't compile.

Modified Code :

public abstract class Organization {
   private String name;
   private int numEmployees;
   public static final double TAX_RATE = 0.01;

   public String getName() { return this.name; }
   public int getNumEmployees() { return this.numEmployees; }
    
   public abstract double calculateTax() {
      return this.numEmployees * TAX_RATE;
   }

}

The text in bold is the issue because this method is abstract method and has body. That's not possible. To solve this issue, you can create a subclass of the Organization and in which you can define a body of the abstract method.

public abstract class Organization {
   private String name;
   private int numEmployees;
   public static final double TAX_RATE = 0.01;

   public String getName() { return this.name; }
   public int getNumEmployees() { return this.numEmployees; }
    
   public abstract double calculateTax();
}

XYZ.java - this is a subclass of Organization class

public class XYZ extends Organization

{

  public double calculateTax()

   {

   return this.numEmployees * TAX_RATE;

   }

}

Second solution is that you can remove abstract keyword like below.

public class Organization {
private String name;
   private int numEmployees;
   public static final double TAX_RATE = 0.01;

   public String getName() { return this.name; }
   public int getNumEmployees() { return this.numEmployees; }
    
   public double calculateTax() {
      return this.numEmployees * TAX_RATE;
   }
}


Related Solutions

Assume you have created the following data definition class: public class Book {    private String title;...
Assume you have created the following data definition class: public class Book {    private String title;    private double cost;       public String getTitle() { return this.title; }    public double getCost() { return this.cost; }       public void setTitle(String title) {       this.title = title;    } // Mutator to return true/false instead of using exception handling public boolean setCost(double cost) {       if (cost >= 0 && cost < 100) {          this.cost = cost;          return true;       }       else {          return false;       }...
Here is a C++ class definition for an abstract data type LinkedList of string objects. Implement...
Here is a C++ class definition for an abstract data type LinkedList of string objects. Implement each member function in the class below. Some of the functions we may have already done in the lecture, that's fine, try to do those first without looking at your notes. You may add whatever private data members or private member functions you want to this class. #include #include typedef std::string ItemType; struct Node { ItemType value; Node *next; }; class LinkedList { private:...
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 StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
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"); } }...
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...
// Base class for game configuration public abstract class DataSource {     private Graph map;    ...
// Base class for game configuration public abstract class DataSource {     private Graph map;     private HashMap <String,Room> rooms;     private ArrayList <Entity> entities;     private Player player;     protected HashMap < String, List<String[]> > tables;     // constructor     public DataSource() {     }         // Connect to the data source. Override if source is a database     public void connect() {     };         // Load the configuration tables required to build the game world...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT