0% found this document useful (0 votes)
9 views13 pages

B1

Uploaded by

monpen.nnt
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)
9 views13 pages

B1

Uploaded by

monpen.nnt
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/ 13

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace MTKPM

//Cau 1

class Car

// Fields

private int maxSpeed;

private int numberOfSeats;

private int yearOfManufacture;

// Properties

public int MaxSpeed

get { return maxSpeed; }

set { maxSpeed = value; }

public int NumberOfSeats

get { return numberOfSeats; }

set { numberOfSeats = value; }

public int YearOfManufacture

{
get { return yearOfManufacture; }

set { yearOfManufacture = value; }

// Default constructor

public Car()

maxSpeed = 0;

numberOfSeats = 0;

yearOfManufacture = 0;

// Parameter constructor

public Car(int maxSpeed, int numberOfSeats, int yearOfManufacture)

this.maxSpeed = maxSpeed;

this.numberOfSeats = numberOfSeats;

this.yearOfManufacture = yearOfManufacture;

// Input method

public void Input()

Console.Write("Enter MaxSpeed: ");

MaxSpeed = int.Parse(Console.ReadLine());

Console.Write("Enter NumberOfSeats: ");

NumberOfSeats = int.Parse(Console.ReadLine());

Console.Write("Enter YearOfManufacture: ");

YearOfManufacture = int.Parse(Console.ReadLine());
}

// Output method

public void Output()

Console.WriteLine($"MaxSpeed: {MaxSpeed}, NumberOfSeats: {NumberOfSeats},


YearOfManufacture: {YearOfManufacture}");

//Cau 2

class Ford : Car

// Field

private int warrantyYears;

// Property

public int WarrantyYears

get { return warrantyYears; }

set { warrantyYears = value; }

// Default constructor

public Ford() : base()

warrantyYears = 0;

// Parameter constructor

public Ford(int maxSpeed, int numberOfSeats, int yearOfManufacture, int warrantyYears)


: base(maxSpeed, numberOfSeats, yearOfManufacture)

this.warrantyYears = warrantyYears;

// Input method

public void FordInput()

base.Input();

Console.Write("Enter WarrantyYears: ");

WarrantyYears = int.Parse(Console.ReadLine());

// Output method

public void FordOutput()

base.Output();

Console.WriteLine($"WarrantyYears: {WarrantyYears}");

// Run method

public void Run()

Console.WriteLine("Ford is running");

public static void CompareFord(Ford ford1, Ford ford2)

int totalWarranty = ford1.WarrantyYears + ford2.WarrantyYears;

Console.WriteLine($"Total Warranty 2 Ford: {totalWarranty}");


if (ford1.NumberOfSeats > ford2.NumberOfSeats)

Console.WriteLine("Ford 1 > Ford 2.");

else if (ford1.NumberOfSeats < ford2.NumberOfSeats)

Console.WriteLine("Ford 1 < Ford 2.");

else

Console.WriteLine("Number Of Seats Ford 1 = Number Of Seats Ford 2.");

// Cau 3

class Animal

// Fields

private string name;

private int age;

private double weight;

// Properties

public string Name

get { return name; }

set { name = value; }

public int Age

{
get { return age; }

set { age = value; }

public double Weight

get { return weight; }

set { weight = value; }

// Default constructor

public Animal()

name = "Unknown";

age = 0;

weight = 0.0;

// Parameter constructor

public Animal(string name, int age, double weight)

this.name = name;

this.age = age;

this.weight = weight;

// Input method

public virtual void Input()

Console.Write("Name: ");

Name = Console.ReadLine();
Console.Write("Age: ");

Age = int.Parse(Console.ReadLine());

Console.Write("Weight(kg): ");

Weight = double.Parse(Console.ReadLine());

// Output method

public virtual void Output()

Console.WriteLine($"Name: {Name}, Age: {Age}, Weight: {Weight} kg");

// Eat method

public void Eat()

Console.WriteLine($"{Name} is eating.");

// Cau 4

class Lion : Animal

// Field

private string lionKind;

// Property

public string LionKind

get { return lionKind; }

set { lionKind = value; }


}

// Default constructor

public Lion() : base()

lionKind = "Unknown";

// Parameter constructor

public Lion(string name, int age, double weight, string lionKind)

: base(name, age, weight)

this.lionKind = lionKind;

// Override Input

public override void Input()

base.Input();

Console.Write("Lion Kind: ");

LionKind = Console.ReadLine();

// Override Output

public override void Output()

base.Output();

Console.WriteLine($"Lion Kind: {LionKind}");

// Hunt method
public void Hunt()

Console.WriteLine($"{Name} is hunting.");

class Whale : Animal

// Field

private string whaleKind;

// Property

public string WhaleKind

get { return whaleKind; }

set { whaleKind = value; }

// Default constructor

public Whale() : base()

whaleKind = "Unknown";

// Parameter constructor

public Whale(string name, int age, double weight, string whaleKind)

: base(name, age, weight)

this.whaleKind = whaleKind;

}
// Override Input

public override void Input()

base.Input();

Console.Write("Whale Kind: ");

WhaleKind = Console.ReadLine();

// Override Output

public override void Output()

base.Output();

Console.WriteLine($"Whale Kind: {WhaleKind}");

// Fish method

public void Fish()

Console.WriteLine($"{Name} is fishing.");

internal class Program

static void Main(string[] args)

//Cau 1

Car[] cars = new Car[5];

for (int i = 0; i < cars.Length; i++)


{

Console.WriteLine($"Enter Car {i + 1}:");

cars[i] = new Car();

cars[i].Input();

Console.WriteLine("\n About 5 car:");

foreach (Car car in cars)

car.Output();

// Cau 2

Ford ford1 = new Ford();

Ford ford2 = new Ford();

Console.WriteLine("Enter Ford 1:");

ford1.FordInput();

Console.WriteLine("\nEnter Ford 2:");

ford2.FordInput();

Console.WriteLine("\n Anout Ford 1:");

ford1.FordOutput();

Console.WriteLine("\n About Ford 2:");

ford2.FordOutput();

ford1.Run();

Ford.CompareFord(ford1, ford2);
//Cau 3

Animal animal = new Animal();

Console.WriteLine("Enter information:");

animal.Input();

Console.WriteLine("\n About animal:");

animal.Output();

animal.Eat();

//Cau 4

Animal[] animals = new Animal[5];

for (int i = 0; i < 2; i++)

Console.WriteLine($"\nEnter Lion {i + 1}:");

animals[i] = new Lion();

animals[i].Input();

for (int i = 2; i < 5; i++)

Console.WriteLine($"\nEnter Whale {i - 1}:");

animals[i] = new Whale();

animals[i].Input();

Animal heaviestAnimal = animals[0];


foreach (Animal animal1 in animals)

if (animal.Weight > heaviestAnimal.Weight)

heaviestAnimal = animal;

Console.WriteLine("\nThe Heaviest Animal:");

heaviestAnimal.Output();

foreach (Animal animal1 in animals)

if (animal is Lion lion)

lion.Hunt();

else if (animal is Whale whale)

whale.Fish();

You might also like