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...
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...
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...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
Programming in C:- 1. Suppose that a variable 's' is an array of strings (i.e. a...
Programming in C:- 1. Suppose that a variable 's' is an array of strings (i.e. a pointer array where each element points to a string). Write an expression that obtains the length of the third string of 's'. You may assume that string.h has been included. 2. Consider the following function whose purpose is to return an array of 3 strings that are initialized to the strings in the character arrays s1, s2, and s3: #include <string.h> #include <stdlib.h> char**...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT