SAMARA UNIVERSITY
COLLEGE OF MEDICINE AND HEALTH SCIENCE
DEPARTMENT OF HEALTH INFORMATICS
COURSE TITLE: OBJECT ORIENTED PROGRAMMING
COURSE CODE HeIn3042
INSTRUCTOR NAME: TARIKU A.
GROUP ASSIGNMENT G-4
NAME ID NUMBER
1. ABDU YESUF………………………………………..1300763
2. TAHIR MUSSA………………………………………1301475
3. TSEDALE TESFA ……………………………………1301526
4. HALIMA MOHAMMED…………………………1302293
5. SEIDA ABDELLA…………………………………….1302302
6. EYASU KASSA……………………………………….1301069
7. SADIK ALI………………………………………….….1302405
8. GETANEH ABEJE…………………………………...1301142
1. Develop java program that calculates factorial of a number, the number is
Accepted from keyboard? (2 point)
Answer
import java.util.*;
public class Main
public static void main(String []args)
//Take input from the user
//Create an instance of the Scanner Class
Scanner sc=new Scanner(System.in);
//Declare and Initialize the variable
System.out.println("Enter the number: ");
int num=sc.nextInt();
int i=1,fact=1;
while(i<=num)
fact=fact*i;
i++;
System.out.println("Factorial of the number: "+fact);
2. Develop java program that count number of digit that the number entered
from the keyboard? (3 point)
Answer
import java.util.Scanner;
class CodesCracker
public static void main(String[] args)
int num, totalDigits=0;
Scanner s = new Scanner(System.in);
System.out.print("Enter a Number: ");
num = s.nextInt();
while(num!=0)
totalDigits++;
num = num/10;
System.out.println("\nNumber of Digits is= " +totalDigits);
General Instructions for project 3: (10 point)
3. Create a Java project called ‘MiniProject’ and create the following classes under
your project.
A. A High School application has two classes: Person superclass (with three public
fields Name, Age, Gender and static field Address) and Student subclass (with two
public fields IDNO and GPA). Using inheritance create two new classes, Teacher
and CollegeStudent. A Teacher will be like Person but will have additional
properties such as salary (the amount the teacher earns) and subject (e.g. “Computer
Science”, “Chemistry”, “English”, “Maths”). The CollegeStudent class will extend
the Student class by adding a year (current level in college) and major (e.g.
“Software Engineering”, “Information Technology”, “Undeclared”).
B. Add the same method for person class and student class to display the instance
variable values.
C. Write Teacher class that extends the parent class Person.
I. Add instance variables to the class for subject (e.g. “IT”, "Chemistry”, "English",
"Other”) and salary (the teachers’ annual salary). Subject should be of type String
and salary of type double. Choose appropriate names for the instance variables.
II. Write a constructor for the Teacher class. The constructor will use five
parameters to initialize Name, Age, Gender, subject, and salary. Use the super
reference to use the constructor in the Person superclass to initialize the inherited
values.
III. Write a method for the Teacher class that can print all of the values of the
variables.
IV. Write a method for the Teacher class. Use a super reference to do the things
already done by the superclass.
D. Write a CollegeStudent subclass that extends the Student class.
i. Add instance variables to the class for major (e.g. “Software Engineering”,
“Information Technology”, “Undeclared”) and year (e.g. I, II, III, …). Major
should be of type String and year of type int. Choose appropriate names for
the instance variables.
ii. Write a constructor for the CollegeStudent class. The constructor will use
seven parameters to initialize Name, Age, Gender, IDNO, GPA, year, and
major. Use the super reference to use the constructor in the Student superclass
to initialize the inherited values.
iii. Write a method to display the values of the variables on the screen. Use a
super reference to do the things already done by the superclass.
E. Write a Test class with a main() method that constructs all of the classes (Person,
Student, Teacher, and CollegeStudent) and calls their method.
F. In your program, write a code to show method overloading and method
overriding.
G. Multiple inheritances are not supported through class in java, but it is possible by
an interface, why? Show with example?
Answer
A.
public class Person {
public String name;
public int age;
public String gender;
public static String address;
public Person(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
public void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Gender: " + gender);
System.out.println("Address: " + address);
```
B.
public class Student extends Person {
public int idNo;
public double gpa;
public Student(String name, int age, String gender, int idNo, double gpa) {
super(name, age, gender);
this.idNo = idNo;
this.gpa = gpa;
public void displayDetails() {
super.displayDetails();
System.out.println("ID No: " + idNo);
System.out.println("GPA: " + gpa);
C.
public class Teacher extends Person {
public String subject;
public double salary;
public Teacher(String name, int age, String gender, String subject, double salary) {
super(name, age, gender);
this.subject = subject;
this.salary = salary;
public void displayDetails() {
super.displayDetails();
System.out.println("Subject: " + subject);
System.out.println("Salary: " + salary);
```
D.
public class CollegeStudent extends Student {
public String major;
public int year;
public CollegeStudent(String name, int age, String gender, int idNo, double gpa, int year, String major) {
super(name, age, gender, idNo, gpa);
this.year = year;
this.major = major;
public void displayDetails() {
super.displayDetails();
System.out.println("Year: " + year);
System.out.println("Major: " + major);
}
E.
public class Test {
public static void main(String[] args) {
Person.address = "1234 Main St";
Person p = new Person("John Doe", 30, "Male");
p.displayDetails();
Student s = new Student("Jane Doe", 20, "Female", 123456, 3.5);
s.displayDetails();
Teacher t = new Teacher("John Smith", 40, "Male", "Computer Science", 50000.0);
t.displayDetails();
CollegeStudent c = new CollegeStudent("Jane Smith", 22, "Female);
F.
class Person {
private String name;
private int age;
private String gender;
public Person(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
public void displayValues() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Gender: " + gender);
G.
Java does not support multiple inheritance through classes due to the "Diamond Problem" where it
would result in ambiguity as to which superclass's method should be called if multiple superclasses have
the same method signature.
However, multiple inheritances can be achieved through interfaces. An interface can extend multiple
interfaces, and a class can implement multiple interfaces. The class that implements multiple interfaces
inherits the methods and constants of all the interfaces, making it equivalent to multiple inheritances.
Here is an example:
interface A {
void methodA();
interface B {
void methodB();
interface C extends A, B {
void methodC();
class D implements C {
public void methodA() {
// implementation
public void methodB() {
// implementation
public void methodC() {
// implementation
In this example, the interface `C` extends both interfaces `A` and `B`. The class `D` implements the
interface `C`, so it automatically inherits the methods of both `A` and `B` as well as the method `meth