Question

In: Computer Science

Consider the following class: class Person {         String name;         int age;        ...

Consider the following class:

class Person

{

        String name;

        int age;

        Person(String name, int age){

               this.name = name;

               this.age = age;

        }

}

  1. Write a java program with two classes “Teacher” and “Student” that inherit the above class “Person”. Each class has three components: extra variable, constructor, and a method to print the student or the teacher info. The output may look like the following (Hint: you may need to use “super” reserved keyword in your implementation):

My name is: Ahmed

My age is: 35

My salary is: 8000

My name is: Khalid

My age is: 12

My grade is: 6

  1. Draw a UML diagram that shows super and subclasses with the relationship between them.

Solutions

Expert Solution

Program Code Screenshot :

Sample Output :

Program Code to Copy

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

class Student extends Person{
    int grade;
    Student(String name, int age, int grade) {
        super(name, age);
        this.grade = grade;
    }

    void print(){
        System.out.println("My name is: "+super.name);
        System.out.println("My age is: "+super.age);
        System.out.println("My grade is: "+this.grade);
    }
}

class Teacher extends Person{
    int salary;
    Teacher(String name, int age, int salary) {
        super(name, age);
        this.salary = salary;
    }

    void print(){
        System.out.println("My name is: "+super.name);
        System.out.println("My age is: "+super.age);
        System.out.println("My salary is: "+this.salary);
    }
}

class Main{
    public static void main(String[] args) {
        Teacher t = new Teacher("Ahmed",35,8000);
        t.print();
        Student s = new Student("Khalid",12,6);
        s.print();
    }
}

UML Diagrams


Related Solutions

public class Person { private String name; public Person() { name = "No name yet"; }...
public class Person { private String name; public Person() { name = "No name yet"; } public Person(String initialName) { name = initialName; } public void setName(String newName) { name = newName; } public String getName() { return name; } public void writeOutput() { System.out.println("Name: " + name); } public boolean hasSameName(Person otherPerson) { return this.name.equalsIgnoreCase(otherPerson.name); } } 2- Write a Program Patient. Java Class that extends Person to include  Social security Gender  Appropriate construtors, accessors, and mutators....
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt()
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt() = 0;     virtual void fight(Monster&);     string getName() const; }; class GiantMonster : public Monster {     protected:         int height;          public:         GiantMonster(string, int, int);         virtual void trample(); }; class Dinosaur : public GiantMonster {     public:     Dinosaur(string, int, int);     void hunt();     void roar(); }; class Kraken : protected GiantMonster {     public:     Kraken(string, int, int);     virtual void hunt();     void sinkShip(); }; Indicate if the code snippets below are...
For Questions 1-3: consider the following code: public class A { private int number; protected String...
For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...
int main() {    footBallPlayerType bigGiants[MAX];    int numberOfPlayers;    int choice;    string name;   ...
int main() {    footBallPlayerType bigGiants[MAX];    int numberOfPlayers;    int choice;    string name;    int playerNum;    int numOfTouchDowns;    int numOfcatches;    int numOfPassingYards;    int numOfReceivingYards;    int numOfRushingYards;    int ret;    int num = 0;    ifstream inFile;    ofstream outFile;    ret = openFile(inFile);    if (ret)        getData(inFile, bigGiants, numberOfPlayers);    else        return 1;    /// replace with the proper call to getData    do    {       ...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
Book SerialNum : String - Name: String – Author : String PublishYear: int - Edition:int Status...
Book SerialNum : String - Name: String – Author : String PublishYear: int - Edition:int Status : boolean + Book() Book(String, String, String, int , int)+ + setName(String) : void + setSerialNum(String) : void + setAuthor(String) : void + setEdition(int) : void + setYear(int) : void + getName():String + getAuthor():String + getYear(): int + getSerialNum(): String + getEdition():int + setAvailable() : void + setUnavailable(): void checkAvailability(): boolean ----------------------------------------------------------------------------------- Library Name : String Address: String Static NUMBEROFBOOKS: int BooksAtLibrray : ArrayList...
Suppose we have a Java class called Person.java public class Person { private String name; private...
Suppose we have a Java class called Person.java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } (a) In the following main method which lines contain reflection calls? import java.lang.reflect.Field; public class TestPerson { public static void main(String args[]) throws Exception { Person person = new Person("Peter", 20); Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "Paul"); } }...
c# language Create a class “Person” which included first name, last name, age, gender, salary, and...
c# language Create a class “Person” which included first name, last name, age, gender, salary, and havekids (Boolean) variables. You have to create constructors and prosperities for the class. Create a “MatchingDemo” class. In the main function, the program reads the number of people in the database from the “PersonInfo.txt” file and creates a dynamic array of the object. It also reads the people's information from “PersonInfo.txt” file and save them into the array. Give the user the ability to...
Given this class: class Issue { public:                 string problem; int howBad; void setProblem(string problem); };...
Given this class: class Issue { public:                 string problem; int howBad; void setProblem(string problem); }; And this code in main: vector<Issue> tickets; Issue issu; a) tickets.push_back(-99); State whether this code is valid, if not, state the reason b) Properly implement the setProblem() method as it would be outside of the class. You MUST name the parameter problem as shown in the prototype like: (string problem) c) Write code that will output the problem attribute for every element in the...
class ArrayStringStack { String stack[]; int top; public arrayStringStack(int size) { stack = new String[size]; top...
class ArrayStringStack { String stack[]; int top; public arrayStringStack(int size) { stack = new String[size]; top = -1; } //Assume that your answers to 3.1-3.3 will be inserted here, ex: // full() would be here //empty() would be here... //push() //pop() //displayStack() } 1. Write two boolean methods, full( ) and empty( ). 2. Write two methods, push( ) and pop( ). Note: parameters may be expected for push and/or pop. 3. Write the method displayStack( ) that prints the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT