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.

Solutions

Expert Solution

code:

public class Main
{
   public static void main(String[] args) {
  
   System.out.println("Parent class");
       Parent ob = new Parent(3,4.5);
       System.out.println(ob);
       System.out.println("Sum is: "+ob.sum());
      
       System.out.println("\nChild class");
       Child ob1 = new Child(3,4,5);
       System.out.println(ob1);
       System.out.println("Sum is: "+ob1.sum());
       System.out.println("Average is: "+ob1.average());
   }
}

class Parent {
private int varA;
protected double varB;
public Parent(int a, double b)
{
varA = a;
varB = b;
}
  
// If we add double and int it will always returns double not int. So i have changed this
public double sum( )
{
return varA+varB;
}

public String toString( )
{
return "" + varA + " " + varB;
}
}

// Creating child class which extends parent class
class Child extends Parent
{
private int varC; // third instance
  
// We cant access private variables of parent class in child class.
// The private variables are only accessed by that class methods
  
// parameterized constructor for child class
public Child(int a,double b,int c)
{
// calling super/parent class constructor
super(a,b);
varC = c;
}
  
// Override sum method
public double sum( )
{
return super.sum()+varC;
}
  
// Creating method average
public double average()
{
return (super.varB+varC)/2;
}
  
// Override method toString
public String toString( )
{
return super.toString()+ " " + varC;
}
}

OUTPUT:


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