Question

In: Computer Science

Consider the following class definition:                   public class Parent {               private

Consider the following class definition:

                  public class Parent {

              private int varA;

              protected double varB;

              public Parent(int a, double b){

varA = a;

varB = b;

              }

              public int sum( ){

                   return varA + varB;

              }

public String toString( ){

                   return "" + varA + "   " + varB;

              }

        }

Consider that you want to extend Parent to Child. Child will have a third int instance data varC.

  1. Write the parameterized constructor definition for Child;
  2. Write the code to override the sum() method to now add all three values and return the sum;
  3. Write the code to define a method average() to calculate and return the average of varB and varC.
  4. Write the code to override the toString() method for Child.

Using the class definitions for Parent and Child from Question 2, write a Java application that will represent a family of 4 members.

  1. Declare a Parent array of size 4 called family.
  2. Instantiate two objects of type Parent using the parameterized constructor and assign each to the first two elements of family. Use values retrieved from the user as the parameter values.
  3. Instantiate two objects of type Child using the parameterized constructor and assign each to the last two elements of family. Use values retrieved from the user as the parameter values.
  4. Invoke the average() method for the two Child objects and display the result.
  5. Write a loop to display the member values of each object in family using the toString() method.

Solutions

Expert Solution

Child.java

public class Child extends Parent {

   private int varC;

   public Child(int a, double b, int c) { // parameretized construc
       super(a, b);
       varC = c;
   }

   @Override
   public int sum() { // overridden sum
       return super.sum() + varC; // returns sum of all three variables
   }

   public double average() { // average method
       return (varB + varC) / 2; // returns average
   }

   @Override
   public String toString() { // overridden toString
       return super.toString() + " " + varC;
   }

}

Application.java

import java.util.Scanner;

public class Application { // Application

   public static void main(String[] args) {

       Scanner input = new Scanner(System.in);
      
       Parent p1 = getParentfromUser(input, 1);
       Parent p2 = getParentfromUser(input, 2);
       Child c1 = getChildfromUser(input, 1);
       Child c2 = getChildfromUser(input, 2);
      
       Parent[] family = {p1, p2, c1, c2};
      
       System.out.println("average of child " + 1 + " " + c1.average());
       System.out.println("average of child " + 2 + " " + c2.average());
      
       for(Parent p : family) {
           System.out.println(p);
       }

   }

   static Parent getParentfromUser(Scanner input, int id) {
       Parent parent;
       System.out.println("input varA for parent " + id);
       int a = input.nextInt();
       System.out.println("input varB for parent " + id);
       double b = input.nextDouble();
       parent = new Parent(a, b);
       return parent;
   }

   static Child getChildfromUser(Scanner input, int id) {
       Child child;
       System.out.println("input varA for child " + id);
       int a = input.nextInt();
       System.out.println("input varB for child " + id);
       double b = input.nextDouble();
       System.out.println("input varC for child " + id);
       int c = input.nextInt();
       child = new Child(a, b, c);
       return child;
   }

}

Output:

input varA for parent 1
1
input varB for parent 1
2
input varA for parent 2
3
input varB for parent 2
4
input varA for child 1
2
input varB for child 1
1
input varC for child 1
3
input varA for child 2
5
input varB for child 2
5
input varC for child 2
5
average of child 1 2.0
average of child 2 5.0
1 2.0
3 4.0
2 1.0 3
5 5.0 5


Related Solutions

Consider the following class definition:                   public class Parent {               private
Consider the following class definition:                   public class Parent {               private int varA;               protected double varB;               public Parent(int a, double b){ varA = a; varB = b;               }               public int sum( ){                    return varA + varB;               } public String toString( ){                    return "" + varA + "   " + varB;               }         } Consider that you want to extend Parent to Child. Child will have a third int instance data varC....
A incomplete definition of a class Temperature is given below: public class Temperature { private double...
A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3}; } [6] (i) Copy and put it in a new class. Write a method toString() of the class, which does not have any parameters and returns a string containing all the values separated by newlines. When the string is printed, each value should appear on a line in the ascending order of their indexes. Copy the content of...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
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;       }...
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...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
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...
Consider the following class definition: class circle                                  &nb
Consider the following class definition: class circle                                                       class cylinder: public circle {                                                                      { public:                                                             public: void print() const;                                           void print() const; void setRadius(double);                                  void setHeight(double); void setCenter(double, double);                     double getHeight(); void getCenter(double&, double&);               double volume(); double getRadius();                                        double area(); double area(); circle();                                     cylinder(); circle(double, double, double);                       cylinder(double, double, double, double); private:                                                private: double xCoordinate;                                       double height; double yCoordinate;                         } double radius; } Suppose that you have the declaration: cylinder newCylinder;...
Given the following definition of class Aclass:             class Aclass { private char letter;                  
Given the following definition of class Aclass:             class Aclass { private char letter;                         private int value; public Aclass( ) { Letter =  ‘A’;         value = 0;                         }                         public Aclass( char ch, int num) { letter = ch; value = num;                         }                         public int getvalue( ) { return value; } public void print( ) { System.out.println( “letter =” + letter + “\n value =” + value);                         }             } A.   ( 20 pts ) Write the definition of the class named Bclass as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT