Q:

Define a parent class name Person and child class Student in java

0

Define a class name Person with the following specifications:

  1. A private variable name of type String.
  2. A private variable age of type int.
  3. A zero-parameterized constructor to initialize all the variables to default.
  4. A full parameterized constructor to initialize the variables.
  5. A public method setPerson with 2 parameters String and int to set name and age instance variables.
  6. A public method toString to return the person data.

Define another class name Student which is extended form Person with the following specifications:

  1. A private variable gpa of type double.
  2. A zero-parameterized constructor which calls super class zero-parameter constructor, and also initializes gpa to zero.
  3. A full parameterized constructor which calls the super class parameterized constructor and initialize all the variables.
  4. A setter and getter method for gpa variable.
  5. A public method toString to return the student data, this method should call the toString method from the supper class.

 

Define a Java class Test that contains a main method and test the functionality of the above classes as the following:

  • Define an array S of type Student with size 2.
  • In the first element of the array S:

          - instantiate an object of type Student using zero-parameterized                           constructor.

           - change the name of that object to "Ahmed" and the age to 19 using                  setPerson method.

           - Ask the user to enter a student gpa and store it in that object.

  • In the second element of the array S, instantiate an object of type Student using full parametrized constructor with these information:
name age gpa
Omar 20 3.5
  • Print all students data.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

package test;

import java.util.*;

public class Test {

    public static void main(String[] args) {
        Student[] s = new Student[2];
        s[0] = new Student();
        s[0].setPerson("Ahmed", 19);
        System.out.println("please enter student gpa: ");
        Scanner input = new Scanner(System.in);
        s[0].setGpa(input.nextDouble());
        s[1] = new Student(3.5, "Omar", 20);
        System.out.println(s[0].toString());
        System.out.println(s[1].toString());
    }
}

class Person {

    private String name;
    private int age;

    public Person() {
        name = "";
        age = 0;
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void setPerson(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "name : " + name + " age : " + age;
    }

}

class Student extends Person {

    private double gpa;

    public Student() {
        super();
        gpa = 0;
    }

    public Student(double gpa) {
        super();
        this.gpa = 0;

    }

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

    public double getGpa() {
        return gpa;
    }

    public void setGpa(double gpa) {
        this.gpa = gpa;
    }

    @Override
    public String toString() {
        return "gpa : " + gpa + " " + super.toString();
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now
<< Define a superclass named Media and a subclass of ...