Question

In: Computer Science

Programming Questions: 1) How do you build an array of objects of a superclass with its...

Programming Questions:

1) How do you build an array of objects of a superclass with its subclass objects?

2) How do you use an enhanced for-loop to traverse through an array while calling a static method(superclass x)?

3) How do you create a static method for a class that has the parent class reference variable?

**Include java coding examples for each question if possible

Solutions

Expert Solution

Note : I provided the answers for the three questions which u have asked for..I mentioned the question numbers in comment section. Thank You

/*********************************************/

/******* Employee.java ******/

public class Employee extends Person {
   private double salary;

   public Employee(String name, int age, double salary) {
       super(name, age);
       this.salary = salary;
   }

   /**
   * @return the salary
   */
   public double getSalary() {
       return salary;
   }

   /**
   * @param salary
   * the salary to set
   */
   public void setSalary(double salary) {
       this.salary = salary;
   }
  
   public static void display(Person p)
   {
       Person.display(p);
       System.out.println("Age : "+p.getAge());
   }

}

/*********************************************/

/*********************************************/

/****** Person.java ******/

package org.students;

public class Employee extends Person {
   private double salary;

   public Employee(String name, int age, double salary) {
       super(name, age);
       this.salary = salary;
   }

   /**
   * @return the salary
   */
   public double getSalary() {
       return salary;
   }

   /**
   * @param salary
   * the salary to set
   */
   public void setSalary(double salary) {
       this.salary = salary;
   }
  
   /*
   * 3. Static method which is having super class reference variable
   */
   public static void display(Person p)
   {
       Person.display(p);
       System.out.println("Age : "+p.getAge());
   }

}

/*********************************************/

/*********************************************/

/******** Test.java ********/

public class Test {

   public static void main(String[] args) {

       /*
       * 1. Creating an array of sub class objects
       * which is holding by super class reference variable
       */
      
       Person emps[] = { new Employee("Mike", 23, 50000),
               new Employee("Williams", 29, 66000),
               new Employee("James", 28, 34000) };

       /*
       * 2. Iterating over the array using the enhanced for loop
       */
       for(Person p : emps)
       {
           Employee.display(p);
           System.out.println("__________________________");
       }
   }

}

/*********************************************/

/*********************************************/

Output:

/*********************************************/


Related Solutions

Give an example of how to build an array of objects of a super class with...
Give an example of how to build an array of objects of a super class with its subclass objects. As well as, use an enhanced for-loop to traverse through the array while calling a static method(superclass x). Finally, create a static method for the class that has the parent class reference variable.
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑...
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑ Create a class Employee. Employees have a name. Also give Employee a method paycheck() which returns a double. For basic employees, paycheck is always 0 (they just get health insurance). Give the class a parameterized constructor that takes the name; Add a method reportDeposit. This method prints a report for the employee with the original amount of the paycheck, the amount taken out for...
This is Python programming Focus 1. Classes and Objects 2. Creating objects 3. Manipulating objects This...
This is Python programming Focus 1. Classes and Objects 2. Creating objects 3. Manipulating objects This lab maps to learning the following objectives: Write a working program that creates a Class, Instances of Objects from that Class, and Functions that use Objects as parameters. For this portion of the lab, you will create a new program for your Professor. Create a class named Student that holds the following data about a student: 1. Name 2. Student ID number 3. GPA...
How do objects enhance Java? How do objects relate to classes and methods?
How do objects enhance Java? How do objects relate to classes and methods?
There are two programming questions for you to do. Please submit two.s or .asm files. You...
There are two programming questions for you to do. Please submit two.s or .asm files. You can use the usual settings as the previous homework. But if you want to use pseudo-instructions, you will need to uncheck "Bare Machine" and check "Accept Pseudo Instructions" (10 points) Declare an array of integers, something like: . data size : . word 8 array : . word 23 , -12 , 45 , -32 , 52 , -72 , 8 , 13 Write...
For this assignment you will implement a dynamic array. You are to build a class called...
For this assignment you will implement a dynamic array. You are to build a class called MyDynamicArray. Your dynamic array class should manage the storage of an array that can grow and shrink. The public methods of your class should be the following: MyDynamicArray(); Default Constructor. The array should be of size 2. MyDynamicArray(int s); For this constructor the array should be of size s. ~MyDynamicArray(); Destructor for the class. int& operator[](int i); Traditional [] operator. Should print a message...
Questions below is about how do you see yourself as a seller. 1. How do you...
Questions below is about how do you see yourself as a seller. 1. How do you market yourself?Who are you selling to and your strategies to ensure you are effective in terms of reaching your target buyers? 2. What do you see in yourself in regards to selling in the future?
1) Build models for the PH3 and PH4+ species. How do you compare the angles of...
1) Build models for the PH3 and PH4+ species. How do you compare the angles of H - P - H bonds in both species? If different, explain why. How do you compare your electronic and molecular geometries? 2 )Build the models for the PH3 and H2S species. How do you compare the binding angles in both species? If they are different, explain why. How do you compare your electronic and molecular geometries?
A. What are Objects? B. How do Objects differ from Classes? C. Where are Objects stored...
A. What are Objects? B. How do Objects differ from Classes? C. Where are Objects stored in Memory? D. Why do you not need to declare when you are finished using an Object in Java? E. Can you edits the contents of a String? View keyboard shortcuts EditViewInsertFormatToolsTable 12pt Paragraph
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105....
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105. In the boxes below, fill in what your array should have. Fill in the index of each element below it. Array Index Open up your editor and write the following program: Declare an array that has the numbers 100 to 105. (How many elements are there in the array?) Print the array. Save your file and test it. Compare your results with your table...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT