0% found this document useful (0 votes)
187 views5 pages

OOPExercise

The document discusses inheritance in C# using an Animal class and Bird class. The Bird class inherits from the Animal class, allowing it to access the Walk() method. It also defines its own Fly() method. The code prints "I am walking" and "I am flying" by creating a Bird object and calling both methods.

Uploaded by

You can't see me
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
187 views5 pages

OOPExercise

The document discusses inheritance in C# using an Animal class and Bird class. The Bird class inherits from the Animal class, allowing it to access the Walk() method. It also defines its own Fly() method. The code prints "I am walking" and "I am flying" by creating a Bird object and calling both methods.

Uploaded by

You can't see me
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Inheritance I

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:

public class Bird : Animal


{
public void Fly()
{
Console.WriteLine("I am flying");
}
}

Finally, we can create a Bird object that can both fly and walk.


class Solution
{
static void Main(string[] args)
{
Bird bird = new Bird();
bird.Walk();
bird.Fly();
}
}

The above code will print:

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

additional unique properties.

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:

1. A class named Arithmetic with a method named add that takes  integers as

parameters and returns an integer denoting their sum.

2. A class named Adder that inherits from a superclass named Arithmetic.

Input Format

You are not responsible for reading any input from stdin; a locked code stub will test

your submission by calling the add method on an Adder object and passing it  integer

parameters.

Output Format

You are not responsible for printing anything to stdout. Your add method must return

the sum of its parameters.

Sample Output

The main method in the Solution class above should print the following:

My superclass is: Arithmetic


42 13 20
Exercise:

namespace CSharpInheritanceII
{
// Write your code here

class Solution
{
static void Main(String[] args)
{
// Create a new Adder object
Adder a = new Adder();

// Print the name of the superclass on a new line


Console.WriteLine("My superclass is: " + a.GetType().BaseType.Name);

// Print the result of 3 calls to Adder's `add(int,int)` method as 3 space-


separated integers:
Console.WriteLine(a.Add(10, 32) + " " + a.Add(10, 3) + " " + a.Add(10, 10) +
"\n");
}
}
}     
Abstract Class

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:

Book Novel = new Book();

You have to create another class that extends the abstract class. Then you can create an instance

of the new class.

Notice that setTitle method is abstract too and has no body. That means you must implement the

body of that method in the child class.

In the editor, we have provided the abstract Book class and a Main class. In the Main class, we

created an instance of a class called MyBook. Your task is to write just the MyBook class.

Sample Input

A tale of two cities


Sample Output

The title is: A tale of two cities


Exercise:

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.

You are given an interface AdvancedArithmetic which contains a method signature int

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

MyCalculator class only.

Sample Input

6
Sample Output

I implemented: AdvancedArithmetic
12
Explanation:

Divisors of 6 are 1,2,3 and 6. 1+2+3+6=12.

Exercise:

namespace CSharpInterface
{
public interface AdvancedArithmetic
{
int DivisorSum(int n);
}

// Write your code here

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);
}
}
}
}

You might also like