Cs141 2
Pg. 02 |
Question Two |
Assignment 1
Deadline: Day 08/10/2020 @ 23:59
[Total Mark for this Assignment is 5]
Computer Programming-II
CS141
College of Computing and Informatics
Question One
1 Mark
Learning Outcome(s):
Design and implement programs using object oriented programming concepts such as encapsulation, inheritance, polymorphism, abstract classes and methods.
Provide the UML class diagram for the following program.
public interface Person {
}
class Student {
}
Question Two
1.5 Marks
Learning Outcome(s):
Apply recursion concept in programming.
Write a Java program that:
· Asks the user to enter his/her first name and ID.
· Prints the user’s first name and ID in reverse order with a space between them recursively.
Important notes:
1. You should have to copy and paste the Java as your answer for this question. DON’T take screen shot for your Java Code. It must be editable.
1. Take a screen shot for your output and paste it as a part of your answer by using your own name and ID.
The following are examples of the output:Enter your first name and ID:
Maha 1400586001
Your first name and ID in reverse order:
1006850041 ahaM
Question Three
1.25 Marks
Learning Outcome(s):
LO1: Outline concepts such as inheritance, polymorphism and reusability with special emphasis on object oriented programming.
LO3: Design and implement programs using object oriented programming concepts such as encapsulation, inheritance, polymorphism, abstract classes and methods. (2.3)
Consider the following interface:
interface Something
{
boolean validate(int value);
}
Classes that implement “Something” interface can validate a specific value in different behaviors within some context. For example, this interface could be useful to program a candy dispenser machine to check whether the coin inserted is a valid coin such as a quarter with a diameter of 2.426 centimeters. Also, it could be useful to check if there is any candy available to dispense.
a) Write two classes “Coin” and “Available” that implements “Something” interface to serve the explained purposes. Hint: the variable “value”, treated as the diameter of the coin in the “Coin” class, and treated as the number of available candies in the “Available” class.
b) Explain in your own words how this exercise helped you understand polymorphism and code-reuse concepts.
Question Four
1.25 Marks
Learning Outcome(s):
LO2Outline concepts such as inheritance, polymorphism and reusability with special emphasis on object oriented programming. LO3: Design and implement programs using object oriented programming concepts such as encapsulation, inheritance, polymorphism, abstract classes and methods.
Consider the following class:
class Person
{
String name;
int age;
Person(String name, int age){
this.name = name;
this.age = age;
}
}
a) 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
b) Draw a UML diagram that shows super and subclasses with the relationship between them.