4 a. Create a class named Book that includes auto implemented properties for the
ID: 3685868 • Letter: 4
Question
4 a. Create a class named Book that includes auto implemented properties for the International Standard Book Number (ISBN), title, author, and price. Include properties for each field. create a child class named TextBook that includes a greade level and a CoffeeTableBook child class that contains no additional fields. in the child classes, override the accessor that sets a Book's price so that TextBooks must be priced between $20 and $80, inclusive and CoffeeTableBooks must be priced between $35 and $100, inclusive.write a program that creates a few objects of each type and demonstrate that all of the methods and properties work correctly. be sure to use valid and invalid values when testing the child class properties. save the file as BookDemo.cs.
b. in the Book class you created in excercise 4a, overload the object class equals() method to conside two books equal if they have the same isbn. create a program that declares three books; two should have the same isbn and one should have different one. Demonstrate that equals() method works correctly to compare the Books. same the program as BookDemo2.cs.
Explanation / Answer
4a)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BookDemo
{
class Book
{
private int isbn; // ISBN no
private string title; // Title of the book
private string author; // Author of the book
protected double price; // Price of the book
public int ISBN
{
get
{
return isbn;
}
set
{
isbn = value;
}
}
public string Title
{
get
{
return this.title;
}
set
{
this.title = value;
}
}
public string Author
{
get
{
return this.author;
}
set
{
this.author = value;
}
}
public virtual double Price
{
get
{
return price;
}
set
{
price = value;
}
}
}
class TextBook : Book
{
private int grade;
public int Grade
{
get
{
return grade;
}
set
{
grade = value;
}
}
override public double Price
{
set
{
double Minimum = 20.00;
double Maximum = 80.00;
if (value < Minimum)
price = Minimum;
else
if (value > Maximum)
price = Maximum;
else
price = value;
}
}
}
class CoffeeTableBook : Book
{
override public double Price
{
set
{
double Minimum = 35.00;
double Maximum = 100.00;
if (value < Minimum)
price = Minimum;
else
if (value > Maximum)
price = Maximum;
else
price = value;
}
}
}
public class BookDemo
{
public static void Main()
{
Book bk1 = new Book();
Book bk2 = new Book();
TextBook textbk1 = new TextBook();
TextBook textbk2 = new TextBook();
CoffeeTableBook coftbk1 = new CoffeeTableBook();
CoffeeTableBook coftbk2 = new CoffeeTableBook();
bk1.ISBN = 446382111; bk1.Title = "Life of Blues"; bk1.Author = "Author one"; bk1.Price = 23;
bk2.ISBN = 547362222; bk2.Title = "Down the street"; bk2.Author = "Author two"; bk2.Price = 111;
textbk1.ISBN = 148361234; textbk1.Title = "English"; textbk1.Author = "Guy Author"; textbk1.Price = 5;
textbk2.ISBN = 232744567; textbk2.Title = "Science of flying"; textbk2.Author = "James"; textbk2.Price = 124;
coftbk1.ISBN = 323850010; coftbk1.Title = "Lessons over coffee"; coftbk1.Author = "Peter Parker"; coftbk1.Price = 28;
coftbk2.ISBN = 434934720; coftbk2.Title = "101 trik of life"; coftbk2.Author = "James B"; coftbk2.Price = 101;
Console.WriteLine();
Console.WriteLine(" {0} - '{1}' by : {2} will cost {3}", bk1.ISBN, bk1.Title, bk1.Author, bk1.Price.ToString("C"));
Console.WriteLine();
Console.WriteLine(" {0} - '{1}' by : {2} will cost {3}", bk2.ISBN, bk2.Title, bk2.Author, bk2.Price.ToString("C"));
Console.WriteLine();
Console.WriteLine(" {0} - '{1}' by : {2} will cost {3}", textbk1.ISBN, textbk1.Title, textbk1.Author, textbk1.Price.ToString("C"));
Console.WriteLine();
Console.WriteLine(" {0} - '{1}' by : {2} will cost {3}", textbk2.ISBN, textbk2.Title, textbk2.Author, textbk2.Price.ToString("C"));
Console.WriteLine();
Console.WriteLine(" {0} - '{1}' by : {2} will cost {3}", coftbk1.ISBN, coftbk1.Title, coftbk1.Author, coftbk1.Price.ToString("C"));
Console.WriteLine();
Console.WriteLine(" {0} - '{1}' by : {2} will cost {3}", coftbk2.ISBN, coftbk2.Title, coftbk2.Author, coftbk2.Price.ToString("C"));
Console.ReadLine();
}
}
}
4b)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BookDemo2
{
class Book
{
private int isbn;
private string title;
private string author;
protected double price;
public int ISBN
{
get
{
return isbn;
}
set
{
isbn = value;
}
}
public string Title
{
get
{
return this.title;
}
set
{
this.title = value;
}
}
public string Author
{
get
{
return this.author;
}
set
{
this.author = value;
}
}
public virtual double Price
{
get
{
return price;
}
set
{
price = value;
}
}
public override bool Equals(Object e)
{
bool equal;
Book book = (Book)e;
if (isbn == book.isbn)
equal = true;
else
equal = false;
return equal;
}
}
class TextBook : Book
{
private int grade;
public int Grade
{
get
{
return grade;
}
set
{
grade = value;
}
}
override public double Price
{
set
{
double Minimum = 20.00;
double Maximum = 80.00;
if (value < Minimum)
price = Minimum;
else
if (value > Maximum)
price = Maximum;
else
price = value;
}
}
}
class CoffeeTableBook : Book
{
override public double Price
{
set
{
double Minimum = 35.00;
double Maximum = 100.00;
if (value < Minimum)
price = Minimum;
else
if (value > Maximum)
price = Maximum;
else
price = value;
}
}
}
public class BookDemo2
{
public static void Main()
{
Book bk1 = new Book();
Book bk2 = new Book();
TextBook textbk1 = new TextBook();
bk1.ISBN = 446382111; bk1.Title = "Life of Blues"; bk1.Author = "Author one"; bk1.Price = 23;
bk2.ISBN = 446382111; bk2.Title = "Life of Blues"; bk2.Author = "Author one"; bk2.Price = 23;
textbk1.ISBN = 547362222; textbk1.Title = "English"; textbk1.Author = "Guy Author"; textbk1.Price = 5;
if (bk1.Equals(bk2) == true)
Console.WriteLine("Same Book");
else
Console.WriteLine("Different Book");
if (bk2.Equals(bk1) == true)
Console.WriteLine("Same Book");
else
Console.WriteLine("Different Book");
if (bk1.Equals(textbk1) == true)
Console.WriteLine("Same Book");
else
Console.WriteLine("Different Book");
Console.ReadKey();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.