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

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...
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"); } }...
//Question1: What is the name of the following lines in the data type class? private int...
//Question1: What is the name of the following lines in the data type class? private int intVariable; private float floatVariable; private String stringVariable; //Question2: What is the name of the following lines in the data type class? public DataTypeClass_Smith() { intVariable = 0; floatVariable = 0.0; stringVariable = “aString”; } //Question3: What is the name of the following lines in the data type class? public DataTypeClass_Smith( int intVar, float floatVar, String stringVar) { intVariable = intVar; floatVariable = floatVar; stringVariable...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: name - "John Doe" address - use the default constructor of Address one constructor with all (two) parameters one input parameter for each attribute Methods public String toString() returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as...
[after §3.22 − easy] String Processing : Consider the following code fragment: 1 int a =...
[after §3.22 − easy] String Processing : Consider the following code fragment: 1 int a = 20; 2 int b; 3 double x = 3.5; 4 String s = "All"; 5 char ch; 6 7 x += a; 8 x--; 9 a /= 4 - 1; 10 b = s.length(); 11 b += 4; 12 s += "is well"; 13 ch = s.charAt(b); 14 System.out.println("a = " + a + ", b = " + b); 15 System.out.println("x = "...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's name • idNumber: an int representing the employee's ID number • rate: a double containing the employee's hourly pay rate • hours: an int representing the number of hours this employee has worked The class should also have the following methods: • Constructor: takes the employee's name and ID number as arguments • Accessors: allow access to all of the fields of the Payroll...
int bintodec(string); // converts a binary number (represented as a STRING) to decimal int hextodec(string); //...
int bintodec(string); // converts a binary number (represented as a STRING) to decimal int hextodec(string); // converts a hexadecimal number (represented as a STRING) to decimal string dectobin(int); // converts a decimal number to binary (represted as a STRING) string dectohex(int); // converts a decimal number to hexadecimal (represted as a STRING) //the addbin and addhex functions work on UNsigned numbers string addbin(string, string); // adds two binary numbers together (represented as STRINGS) string addhex(string, string); // adds two hexadecimal...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first < second) return "less than"; else if (first == second) return "equal to"; else return "greater than";       }    // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int first = input.nextInt(); System.out.print("Enter second integer: "); int second = input.nextInt(); System.out.println("The first integer is " + comparison(first, second) + " the...
// File name: Person.h // Person class declaration. Person is the base class. #pragma once #include...
// File name: Person.h // Person class declaration. Person is the base class. #pragma once #include <iostream> using namespace std; class Person { private:         string fName;         string lName;         int birthYear;         int birthMonth;         int birthDay; public:         Person();         void setName(string, string);         void setBirthDate(int, int, int);         string getFullName();         string getBirthDate(); }; // File name: Person.cpp // Person class definition. Person is the base class. #include "Person.h" Person::Person() { fName = ""; lName =...
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT