You are on page 1of 7

Assignment3

Exercise 1
Write a program that calculates perimeter and area of (rectangle, triangle, square and
circle).You have to use
(abstraction or interface) and overriding.
import 'dart:ffi';
import 'dart:io';
void main() {
AreaPer shape1=AreaPer('Circle',radius: 7);
var area=shape1.findArea();
var perimeter=shape1.findPerimeter();
print("the area of shape is $area");
print("the perimeter of shape is $perimeter");
}

abstract class Inputs{


double findPerimeter();
double findArea();
}
class AreaPer extends Inputs {
var shape;
double? radius;
double? baseTr ;
double? heightTr;
double? S1Tr;
double? S2Tr;
double? S3Tr;
double? sqrside;
double? lenrectangle;
double? widthrectangle;
AreaPer(String shape,{double? radius, double? baseTr ,double? heightTr,double? S1Tr,
double? S2Tr, double? S3Tr,double? sqrside,double? lenrectangle,double? widthrectangle}){
this.shape=shape;
this.radius=radius;
this.baseTr=baseTr;
this.heightTr=heightTr;
this.S1Tr=S1Tr;
this.S2Tr=S2Tr;
this.S3Tr=S3Tr;
this.sqrside=sqrside;
this.lenrectangle=lenrectangle;
this.widthrectangle=widthrectangle;
}
@override
double findPerimeter() {
final double pi=3.14;
if (this.shape =='Circle') {
return 2*pi*this.radius!;
}
else if (this.shape =='Traingle'){
return this.S1Tr!+this.S2Tr!+this.S3Tr!;
}
else if (this.shape =='Square'){
return 4*this.sqrside!;
}
else if (this.shape =='Rectangle'){
return 2*(this.lenrectangle!+this.widthrectangle!);
}
throw UnimplementedError();
}

@override
double findArea() {
// TODO: implement findArea
final double pi=3.14;
if (this.shape =='Circle') {
return pi*this.radius!*this.radius!;
}
else if (this.shape =='Traingle'){
return 0.5*this.baseTr!*this.heightTr!;
}
else if (shape =='Square'){
return this.sqrside!*this.sqrside!;
}
else if (this.shape =='Rectangle'){
return this.lenrectangle!*this.widthrectangle!;
}
throw UnimplementedError();
}

Exercise 2
Write a program that enables user to enter four points, each point should be like (X, Y), then
determine if the weather of shape that can be created from these points (rectangle or square),
then calculate area and perimeter of shape if it rectangle or square.
import 'dart:ffi';
import 'dart:io';
import 'dart:math';
import 'dart:math';
void main() {
var t=SecOrRec();

List? SecOrRec(){
List xcor=[];
List ycor=[];
for (int i=1;i<=4;i++){
print("The x coordinate of number$i");
var x1 = int.parse(stdin.readLineSync().toString());
xcor.add(x1);
print("The y coordinate of number$i");
var y1 = int.parse(stdin.readLineSync().toString());
ycor.add(y1);
}
var d1=sqrt(pow(xcor[0]-xcor[1],2)+pow(ycor[0]-ycor[1],2));
var d2=sqrt(pow(xcor[3]-xcor[2],2)+pow(ycor[3]-ycor[2],2));
var d3=sqrt(pow(xcor[0]-xcor[3],2)+pow(ycor[0]-ycor[3],2));
var d4=sqrt(pow(xcor[1]-xcor[2],2)+pow(ycor[1]-ycor[2],2));
var x;
var bool = ((d1==d2) && (d1==d3)) &&(d1==d4) ? x=true:x=false;
if(d1==d2 && d3==d4){
return ["Rectangle","perimeter is ${2*(d1+d3)}","area is${d1*d3}"];
}
else if(x){
return ["Square","perimeter is ${4*d1}","area is ${d1*d1}"];
}

Exercise 3
1- Create a class called Point allowing to manipulate a point of a plane (x,y). You will
provide:
 A constructor receiving as arguments the coordinates (double) of a point.
 A member function "move" performing a translation defined by its two
arguments (double).
 a member function "display" simply displaying the coordinates of the point.

The coordinates of the point will be private data members.

A source file constituting the declaration of the class.

A small test program (main) declaring a point, displaying it, moving it around and showing it
again.

void main() {
Point point=Point(3.5, 12);
point.display();
point.move(1,2);
}
class Point{
double? _x;
double? _y;
void setX (double? valx){_x=valx;}
void setY (double? valy){_x=valy;}
double getX (){return _x!;}
double getY (){return _y!;}
Point(double x,double y){
this._x=x;
this._y=y;
}
void move (double xtrans,double ytrans){
double xnew=this._x!+xtrans;
double ynew=this._y!+ytrans;
print("new xcoordinate is $xnew and new ycoordinate is $ynew");
}
void display(){
print("xcoordinate is ${_x}");
print("ycoordinate is ${_y}");
}

Exercise 4
What is Lambda Function and give example?

 Lambda is a short and concise manner to represent small functions.


 Lambda functions are also called Arrow functions.
 But here remember that by using Lambda function’s syntax you can only
return one expression. It must be only one line expression.

int ShowSum(int numOne, int numTwo) => numOne + numTwo;

main() {
print(ShowSum(10, 20));
}

What Is Method Overriding In Dart and give example?

Method overriding is to redefine the body of the child class being inherited from a
parent class

class Car{

void driving(){

print(" you are driving car ");

class Honda extends Car{


@override

void driving(){

print(" you are driving Honda car ");

Exercise 5
Write a program to get a number from the user and print whether it
is positive or negative.
import 'dart:ffi';
import 'dart:io';
import 'dart:math';
import 'dart:math';
void main() {
print("Enter The a number");
var num = int.parse(stdin.readLineSync().toString());
if(num.isNegative){
print("Negative number");
}
else{
print("Positive number");
}
}

Exercise 6
Write a program that accepts three numbers from the user and prints
"increasing" if the numbers are in increasing order, "decreasing" if
the numbers are in decreasing order, and "Neither increasing or
decreasing order" otherwise.
import 'dart:ffi';
import 'dart:io';
import 'dart:math';
import 'dart:math';
void main() {
print('Enter N1');
var N1 = int.parse(stdin.readLineSync()!);
print('Enter N2');
var N2 = int.parse(stdin.readLineSync()!);
print('Enter N3');
var N3 = int.parse(stdin.readLineSync()!);

if((N1>N2)&&(N2>N3)){
print("decreasing sequence");
}
else if ((N3>N2)&&(N2>N1)){
print("increasing sequence");
}
else{
print("Neither increasing nor decreasing");
}
}

Exercise 7
Write a program that prompts the user to input a positive integer. It should
then output a message indicating whether the number is a prime number.
import 'dart:ffi';
import 'dart:io';
import 'dart:math';
import 'dart:math';
void main() {
print('Enter N');
var N = int.parse(stdin.readLineSync()!);
if (isPrime(N)) {
print('$N is a prime number.');
} else {
print('$N is not a prime number.');
}
}
bool isPrime(N) {
for (var i = 2; i <= N / i; ++i) {
if (N % i == 0) {
return false;
}
}
return true;
}

You might also like