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?
In the java programming language. How would you find if THREE (3) numbers in an array...
In the java programming language. How would you find if THREE (3) numbers in an array add up to a certain sum so for example if my array element consists of: INPUT: 200, 10, 50, 20 and my output asks if these elements add to: 270? YES (200+50+70) 260? YES (200+10+50) 30? NO What method would you suggest to look through the elements in an array (which im going to add through a text file) and determine if these sums...
In the java programming language. How would you find if numbers in an array element add...
In the java programming language. How would you find if numbers in an array element add up to a certain sum so for example if my array element consists of: INPUT: 200, 10, 50, 20 and my output asks if these elements add to: 210? YES (200+10) 260? YES (200+10+50) 30? NO What method would you suggest to look through the elements in an array (which im going to add through a text file) and determine if these sums exist...
1. Data in Java: Primitives and Objects In this section, we’ll build a class that is...
1. Data in Java: Primitives and Objects In this section, we’ll build a class that is composed of both primitives and objects. Java has classes to help in converting from primitives to objects (for example an int to an Integer) and vice-versa. Let’s start by building a small class used to represent a single concept such as a Car or Vehicle. Make a new Java project and and create a new class called “Car”. Cars should define primitives for things...
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT