OOPExercise
OOPExercise
Using inheritance, one class can acquire the properties of others. Consider the
following Animal class:
public class Animal
{
public void Walk()
{
Console.WriteLine("I am walking");
}
}
This class has only one method, walk. Next, we want to create a Bird class that also has
a fly method. We do this using extends keyword:
I am walking
I am flying
This means that a Bird object has all the properties that an Animal object has, as well as some
The code above is provided for you in your editor. You must add a sing method to the Bird class,
then modify the main method accordingly so that the code prints the following lines:
I am walking
I am flying
I am singing
Inheritance II
Write the following code in your editor below:
Input Format
You are not responsible for reading any input from stdin; a locked code stub will test
parameters.
Output Format
You are not responsible for printing anything to stdout. Your add method must return
Sample Output
namespace CSharpInheritanceII
{
// Write your code here
class Solution
{
static void Main(String[] args)
{
// Create a new Adder object
Adder a = new Adder();
Using inheritance, one class can acquire the properties of others. Consider the
following Animal class:
public abstract class Book
{
String title;
public abstract void SetTitle(String s);
String GetTitle()
{
return title;
}
}
If you try to create an instance of this class like the following line you will get an error:
You have to create another class that extends the abstract class. Then you can create an instance
Notice that setTitle method is abstract too and has no body. That means you must implement the
In the editor, we have provided the abstract Book class and a Main class. In the Main class, we
Sample Input
namespace CSharpAbstract
{
public abstract class Book
{
String title;
public abstract void SetTitle(String s);
String GetTitle()
{
return title;
}
}
// Write your code here
class Solution
{
static void Main(string[] args)
{
string title = Console.ReadLine();
MyBook Novel = new MyBook();
Novel.SetTitle(title);
Console.WriteLine("The title is: " + Novel.GetTitle());
}
}
}
Interface
A Java interface can only contain method signatures and fields. The interface can be used to
achieve polymorphism. In this problem, you will practice your knowledge on interfaces.
divisor_sum(int n). You need to write a class called MyCalculator which implements the interface.
divisorSum function just takes an integer as input and return the sum of all its divisors. For example
divisors of 6 are 1, 2, 3 and 6, so divisor_sum should return 12. The value of n will be at most 1000.
Read the partially completed code in the editor and complete it. You just need to write the
Sample Input
6
Sample Output
I implemented: AdvancedArithmetic
12
Explanation:
Exercise:
namespace CSharpInterface
{
public interface AdvancedArithmetic
{
int DivisorSum(int n);
}
class Solution
{
static void Main(string[] args)
{
MyCalculator MyCalculator = new MyCalculator();
Console.WriteLine("I am implemented: ");
ImplementedInterfaceNames(MyCalculator);
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(MyCalculator.DivisorSum(n) + "\n");
}
/*
* ImplementedInterfaceNames method takes an object and prints the name of the
interfaces it implemented
*/
static void ImplementedInterfaceNames(Object o)
{
Type[] theInterfaces = o.GetType().GetInterfaces();
for (int i = 0; i < theInterfaces.Length; i++)
{
string interfaceName = theInterfaces[i].Name;
Console.WriteLine(interfaceName);
}
}
}
}