Exercises
- Exercise 1:
- Exercise 2:
- Exercise 3:
- Exercise 4:
- Exercise 5:
The class "Point" is be defined as follows:
class Point {
int x, y;
Point(this.x, this.y);
void moveForward() {
x++;
y++;
}
void goHome(int mx, int my) {
x = mx;
y = my;
}
void display() {
print("x = $x y = $y");
}
}
1) Define a new class "PointCol" to manipulate colored points. This class is a derived from Point
2) Its constructor initializes the color of the object with a given integer (the color reference).
3) Override the display method to print also the color.
4) Override the goForward method so that it adds 2 for x and y.
5) Override the goHome method so that it assigns 0 to the color.
6) Create a small test program (main).
Solution:
pointCol.dart
import 'point.dart';
class PointCol extends Point {
int color;
PointCol(int x, int y, int c) : super(x, y) {
color = c;
}
void display() {
print("x : $x y : $y color : $color");
}
void moveForward() {
x += 2;
y += 2;
}
void goHome(int mx, int my) {
super.goHome(mx, my);
color = 0;
}
}
main.dart
import 'pointCol.dart';
import 'point.dart';
void main() {
var p = Point(5, 6);
p.display();
p.moveForward();
p.display();
p.goHome(1, 1);
p.display();
print("----------------");
var pc = PointCol(2, 3, 250);
pc.display();
pc.moveForward();
pc.display();
pc.goHome(0, 0);
pc.display();
}
x = 6 y = 7
x = 1 y = 1
----------------
x : 2 y : 3 color : 250
x : 4 y : 5 color : 250
x : 0 y : 0 color : 0
Define a class for adding, subtracting -, multiplying, dividing and giving the opposite (-) of fractions (numerator and denominator) using the overload of these operators. Create a program to test this class.
Note:
We're going to overload the operator (-) twice, once as a subtraction and another time as the opposite.
Solution:
class Fraction {
int n, d;
Fraction(this.n, this.d);
Fraction operator +(Fraction v) => Fraction(n * v.d + d * v.n, d * v.d);
Fraction operator -(Fraction v) => Fraction(n * v.d - d * v.n, d * v.d);
Fraction operator *(Fraction v) => Fraction(n * v.n, d * v.d);
Fraction operator /(Fraction v) => Fraction(n * v.d, d * v.n);
Fraction operator -() => Fraction(-n, -d);
void display() {
if (d == 0)
print("Error");
else
print("$n / $d");
}
}
void main() {
var a = Fraction(3, 6), b = Fraction(4, 2);
(a + b).display();
(a - b).display();
(a * b).display();
(a / b).display();
(-a).display();
(-b).display();
}
-18 / 12
12 / 12
6 / 24
-3 / -6
-4 / -2
1) Create an abstract class called "Animal" that has 2 abstract methods :
a) printName()
b) printSound()
2) Create 3 classes "Dog", "Cat" and "Cow" that extends the Animal Class.
3) Create a small test program (main).
Solution:
abstract class Animal {
void printName();
void printSound();
}
class Dog extends Animal {
void printName() {
print("Dog");
}
void printSound() {
print("Woof");
}
}
class Cat extends Animal {
void printName() {
print("Cat");
}
void printSound() {
print("Meaw");
}
}
class Cow extends Animal {
void printName() {
print("Cow");
}
void printSound() {
print("Moo");
}
}
void main() {
var c = Cat(), d = Dog(), w = Cow();
c.printName();
c.printSound();
d.printName();
d.printSound();
w.printName();
w.printSound();
}
Meaw
Dog
Woof
Cow
Moo
Find out the error in the following code :
class Work {
void printProblem() {
print("printing Problem");
}
void printSolution() {
print("Printing Solution");
}
}
class Algorithm implements Work {
void printProblem() {
print("printing Algorithm Problem");
}
void printHint() {
print("Printing Hint");
}
}
void main() {
var q1 = Algorithm();
q1.printProblem();
q1.printHint();
}
Solution:
The class "Algorithm" that implements "Work" must implement all the methods of that interface.
"printSolution" method is not implemented in "Algorithm", so the compiler show an error.
What is the output of the following code:
mixin Swim {
void swim() => print('Swimming');
}
mixin Bite {
void bite() => print('Chomp');
}
mixin Crawl {
void crawl() => print('Crawling');
}
abstract class Reptile with Swim, Crawl, Bite {
void hunt(food) {
print('${this.runtimeType} -------');
swim();
crawl();
bite();
print('Eat $food');
}
}
class Alligator extends Reptile {
// Alligator Specific stuff...
}
class Crocodile extends Reptile {
// Crocodile Specific stuff...
}
class Fish with Swim, Bite {
void feed() {
print('Fish --------');
swim();
bite();
}
}
main() {
Crocodile().hunt('Zebra');
Alligator().hunt('Fish');
Fish().feed();
}
Solution:
Swimming
Crawling
Chomp
Eat Zebra
Alligator -------
Swimming
Crawling
Chomp
Eat Fish
Fish --------
Swimming
Chomp