I wish to know different between using the new and the override keyword in this program. Because both are giving same results in following program. Even though it is said that Tostring() overrides the Object class version. You can notice in this text there is an override word but in the example program new is used.
Also when return keyword is used in the method header, indicates the type of return. But in this program in the method header type is string, return is combination of string and integer. Please explain the reason.
using System;
class DemoStudents4
{
public static void Main()
{
Student payingStudent = new Student();
ScholarshipStudent freeStudent = new ScholarshipStudent();
payingStudent.SetName("Megan");
payingStudent.SetCredits(15);
freeStudent.SetName("Luke");
freeStudent.SetCredits(15);
Console.WriteLine(payingStudent.ToString());
Console.WriteLine(freeStudent.ToString());
Console.ReadKey();
}
}
class Student
{
private string name;
protected int credits;
public void SetName(string name)
{
this.name = name;
}
public void SetCredits(int creditHours)//SetCredits in the child class as well
{
credits = creditHours;
}
public new string ToString() //Tostring() overrides the Object class version.
{
string stuString = "Student " + name + " has " + credits + " credits";
return stuString;
//OR
//return String.Format("Student {0} has {1} credits", name, credits);
}
}
class ScholarshipStudent : Student
{
new public void SetCredits(int creditHours)
{
credits = creditHours;
}
}
/*
Student Megan has 15 credits
Student Luke has 15 credits
*/
Loading
Posted Jan 16, 2012, 6:42 PM
VulpesPosted Jan 16, 2012, 6:32 PM
int i = 1;
Console.WriteLine("{0}", i);
Here the ToString() method is applied behind the scenes to the int i so that it can be displayed as a string to the console.
Posted Jan 16, 2012, 6:04 PM
I couldn't understand the meaning of the above sentence please explain.
VulpesPosted Jan 16, 2012, 4:53 PM
However, the latter does have some advantages - for example, when using the Console.Write(Line) methods, the ToString() method is automatically applied to those arguments which are not strings.
Posted Jan 16, 2012, 3:54 PM
VulpesPosted Jan 16, 2012, 9:30 AM
string s = "Hello";
object o = s;
Console.WriteLine(o.GetType());
Here the compile time type of 'o' is object.
However, it's runtime type is actually string because the variable holds a reference to a string instance, not an object instance.
Calling o.GetType(), will therefore print "System.String" rather than "System.Object".
If you change the second line to:
object o = new object();
then the compile time and runtime types will be the same and the third line will print "System.Object".
Posted Jan 16, 2012, 7:50 AM
"It returns the runtime type of the variable on which it is called:"
Can you tell me please the significant of runtime word using.
VulpesPosted Jan 15, 2012, 6:37 PM
It returns the runtime type of the variable on which it is called:
http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx
Posted Jan 15, 2012, 6:21 PM
VulpesPosted Jan 15, 2012, 6:14 PM
Posted Jan 15, 2012, 6:03 PM
// Debug07-04
// class HotelRoom has child classes SingleRoom which costs less,
// and and Suite which costs more
using System;
public class HotelRoom
{
private int roomNumber;
protected double rate;
public HotelRoom(int room)
{
roomNumber = room;
rate = 79.00;
}
public int GetroomNum()
{
return this.roomNumber;
}
public int GetRate()
{
return this.rate;
}
public new string ToString()
{
string temp = GetType + " Room " + roomNumber + " Rate: " + rate.ToString("C");
return temp;
}
}
public class SingleRoom : HotelRoom
{
public SingleRoom(int room) : base(room)
{
rate -= 20.00;
}
}
public class Suite : HotelRoom
{
public Suite(int room) : base(room)
{
rate += 45.50;
}
}
public class DemoRooms
{
HotelRoom aRoom = new HotelRoom(234);
SingleRoom aSingle = new SingleRoom(135);
Suite aSuite = new Suite(453);
Console.WriteLine(aRoom.ToString());
Console.WriteLine(aSingle.ToString());
Console.WriteLine(aSuite.ToString());
}
VulpesPosted Jan 15, 2012, 10:34 AM
Posted Jan 14, 2012, 1:25 PM
This is an incomplete program. Please anyone can help to complete this program.
using System;
namespace _7e7
{
class Program
{
static void Main(string[] args)
{
RealEstateSalesperson realEsP = new RealEstateSalesperson(6000000);
Console.WriteLine("Real Estate Salesperson Name: {0} and Commission {1}", realEsP.getSalesPfullName(), realEsP.MakeSale());
GirlScout girlScout = new GirlScout();
girlScout.setBoxSold(35);
Console.WriteLine("\n{0} No Boxes Sold {1}", girlScout.SalesSpeech(), girlScout.MakeSale());
Console.ReadKey();
}
}
}
public interface ISelling
{
string SalesSpeech();
int MakeSale();
}
abstract class Salespersons
{
string fName;
string lName;
public Salespersons()
{
this.fName = "Hendry";
this.lName = "Ford";
}
public string getSalesPfullName()
{
return "Hendry Ford";
}
}
class RealEstateSalesperson : Salespersons, ISelling
{
int tSales = 0;
double tCommission = 0;
double const COMMISSION_RATE = 5;
public RealEstateSalesperson(int tsales)
{
tCommission = tSales * COMMISSION_RATE;
}
public double MakeSale()
{
return tCommission;
}
}
class GirlScout : Salespersons, ISelling
{
int boxSold = 0;
public void setBoxSold(int boxSold)
{
this.boxSold = boxSold;
}
public string SalesSpeech()
{
return "HELP THE PEOPLE INDEED IN NEED";
}
public int MakeSale()
{
return boxSold;
}
}
VulpesPosted Jan 14, 2012, 6:54 AM
However, you don't need the private recover fields in the classes so I'd get rid of them so you don't get the compiler warning that they're never used.
I'd also check your spelling of Furniture :)
Posted Jan 14, 2012, 6:32 AM
I wish to confirm whether work is correct.
And please see whether Q(5) and Q(6) are similar.
using System;
namespace _7e5
{
class Program
{
static void Main(string[] args)
{
Patient patient = new Patient();
Furtinuter furniture = new Furtinuter();
Football football = new Football();
Console.WriteLine(patient.Recover());
Console.WriteLine(furniture.Recover());
Console.WriteLine(football.Recover());
Console.ReadKey();
}
}
}
public interface IRecovering
{
string Recover();
}
class Patient : IRecovering
{
string recover;
public string Recover()
{
return "I am getting better.";
}
}
class Furtinuter : IRecovering
{
string recover;
public string Recover()
{
return "Furniture business is recovering from the recession.";
}
}
class Football : IRecovering
{
string recover;
public string Recover()
{
return "Football team is recovering from the defeat.";
}
}
VulpesPosted Jan 13, 2012, 4:19 PM
1. You could throw an exception:
The trouble with this is that it will bring down the application unless you have code to catch the exception elsewhere.
2. You could print a message and not change the price:
However, my reading of the question is that you have to override the price rather than do either of the above.
Posted Jan 13, 2012, 2:14 PM
Whether it is possible to alter the question saying that: PRICE IS OUT OF RANGE.
VulpesPosted Jan 13, 2012, 1:58 PM
Similar code (albeit different limits) will be needed for CoffeeTableBook.
Posted Jan 13, 2012, 12:49 PM
Program is as follows. If price is out of range how to implement this concept in the program.
using System;
namespace _7e3
{
class Program
{
static void Main(string[] args)
{
Book book = new Book();
book.setTitle("Global Warming");
book.setAuthor("Alex");
book.setPrice(50);
Console.WriteLine(book.ToString());
Console.WriteLine();
TextBook tb = new TextBook();
tb.setTitle("Simple Math");
tb.setAuthor("Rex");
tb.setPrice(30);
tb.setGrade(7);
Console.WriteLine(tb.ToString());
Console.WriteLine("GRADE:{0}", tb.getGrade());
Console.WriteLine();
CoffeeTableBook ctb = new CoffeeTableBook();
ctb.setTitle("Coffee Table Book");
ctb.setAuthor("John");
ctb.setPrice(80);
Console.WriteLine(ctb.ToString());
Console.ReadKey();
}
}
}
class Book
{
string title;
string author;
int price;
public string getTitle()
{
return this.title;
}
public void setTitle(string title)
{
this.title = title;
}
public string getAuthor()
{
return this.author;
}
public void setAuthor(string author)
{
this.author = author;
}
public int getPrice()
{
return this.price;
}
public void setPrice(int price)
{
this.price = price;
}
public new string ToString()
{
return String.Format("TITLE: {0} AUTHOR: {1} PRICE: ${2}", title, author, price);
}
}
class TextBook: Book
{
int grade;
public int getGrade()
{
return this.grade;
}
public void setGrade(int grade)
{
this.grade = grade;
}
new public void setPrice(int price)
{
if (price >= 20 && price <= 80)
base.setPrice(price);
//else
//price = Convert.ToInt32("Price is out of range");//?????????????
//base.setPrice(price);
}
}
class CoffeeTableBook : Book
{
new public void setPrice(int price)
{
if (price >= 35 && price <= 100)
base.setPrice(price);
//else
//price = Convert.ToInt32("Price is out of range");//????????????
//base.setPrice(price);
}
}
VulpesPosted Jan 13, 2012, 11:53 AM
Posted Jan 13, 2012, 10:52 AM
Every subclass object "is a" specific instance of both the subclass and the superclass. You can assign a subclass object to an object of any of its superclass types. When you do so, C# makes an implicit conversion from subclass to superclass.
Example also given to demonstrate this say. How can be implicit conversion explained from this example.
using System;
class DemoStudents2
{
public static void Main()
{
Student payingStudent = new Student();
ScholarshipStudent freeStudent = new ScholarshipStudent();
payingStudent.SetName("Megan");
payingStudent.SetCredits(15);
freeStudent.SetName("Luke");
freeStudent.SetCredits(15);
DisplayStudent(payingStudent); //payingStudent - object of Student class
DisplayStudent(freeStudent);//freeStudent - object of ScholarshipStudent class
}
public static void DisplayStudent(Student stu)
{
Console.WriteLine("{0}'s tuition is {1}", stu.GetName(), stu.GetTuition().ToString("C"));
}
}
class Student
{
private const double RATE = 55.75;
private string name;
protected int credits;
protected double tuition;
public string GetName()
{
return name;
}
public void SetName(string name)
{
this.name = name;
}
public int GetCredits()
{
return credits;
}
public void SetCredits(int creditHours)//SetCredits in the child class as well
{
credits = creditHours;
tuition = credits * RATE;
}
public double GetTuition()//no SetTuition() method
{
return tuition;
}
}
class ScholarshipStudent : Student
{
//SetCredits in the parent class as well
new public void SetCredits(int creditHours)
{
credits = creditHours;
tuition = 0;
}
}
/*
Megan's tuition is $836.25
Luke's tuition is $0.00
*/
VulpesPosted Jan 12, 2012, 12:55 PM
Why don't you change the setWidth method so that it takes a double rather than an int as its argument? I can't see anything in the question to prevent you doing that.
getWidth will then need to return a double rather than an int.
Posted Jan 12, 2012, 12:41 PM
vct.setWidth(Convert.ToInt32(5.4));
vct.setWidth((int)5.4);
Casting and Converting gives the same output. In both cases decimal place value is lost. That means accuracy is lost. In casting loosing accuracy is understandable but in conversion how this occurrence can be explained.
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Tape tape = new Tape();
tape.setLength(100);
tape.setWidth(10);
Console.WriteLine("Tape Length {0} meter Width {1} mm", tape.getLength(), tape.getWidth());
Console.WriteLine(tape.ToString());
Console.WriteLine();
VideoCassetteTape vct = new VideoCassetteTape();
vct.setLength(150);
vct.setWidth(Convert.ToInt32(5.4));
vct.setPlayTime(90);
Console.WriteLine("Video Cassette Tape Play Time {0} minutes", vct.getPlayTime());
Console.WriteLine(vct.ToString());
Console.WriteLine();
AdhesiveTape at = new AdhesiveTape();
at.setLength(150);
at.setWidth(5);
at.setSfactor(8);
Console.WriteLine("Adhesive Tape Stickness Factor {0}", at.getSfactor());
Console.WriteLine(at.ToString());
Console.ReadKey();
}
}
}
class Tape
{
int length;
int width;
public int getLength()
{
return this.length;
}
public void setLength(int length)
{
this.length = length;
}
public int getWidth()
{
return this.width;
}
public void setWidth(int width)
{
this.width = width;
}
public new string ToString()
{
return String.Format("Tape Length is {0} meters & Width is {1} mm", length, width);
}
}
class VideoCassetteTape : Tape
{
int pTime;
public int getPlayTime()
{
return this.pTime;
}
public void setPlayTime(int pTime)
{
this.pTime = pTime;
}
}
class AdhesiveTape : Tape
{
int sFactor;
public int getSfactor()
{
return this.sFactor;
}
public void setSfactor(int sFactor)
{
this.sFactor = sFactor;
}
}
VulpesPosted Jan 12, 2012, 11:15 AM
Posted Jan 12, 2012, 10:59 AM
Parent class of tape.setWidth(1);
& the child class of videoCassetteTape.setWidth(0.5);
Here you can see parent class type is integer and child class type is double. Therefore
videoCassetteTape.setWidth(Convert.ToInt16(0.5)); - whether this is acceptable.
VulpesPosted Jan 12, 2012, 9:49 AM
It just prevents the compiler from warning you that you're hiding a base class member of the same name.
Similarly, if you use 'new' unnecessarily then the compiler will warn you about that as well.
Posted Jan 12, 2012, 8:53 AM
In the 1st thread even without "new" keyword program is executing well. How this can be explained?
VulpesPosted Jan 12, 2012, 5:26 AM
For question 1(c) you just need to ensure you've called all the methods:
Posted Jan 11, 2012, 6:14 PM
using System;
namespace _7e1
{
class Program
{
static void Main(string[] args)
{
Games games = new Games();
games.setGameName("Cricket");
games.setNoOfPlayers(11);
GameWithTimeLimit gameWithTimeLimit = new GameWithTimeLimit();
gameWithTimeLimit.setTime(24);
Console.WriteLine(games.ToString());
Console.WriteLine(gameWithTimeLimit.ToString());
Console.ReadKey();
}
}
}
class Games
{
string gameName;
int playersNo;
public string getGameName()
{
return this.gameName;
}
public void setGameName(string gameName)
{
this.gameName = gameName;
}
public int getNoOfPlayers()
{
return this.playersNo;
}
public void setNoOfPlayers(int playersNo)
{
this.playersNo = playersNo;
}
public new string ToString() //Tostring() overrides the Object class version.
{
return String.Format("No of plyers in {0} is {1}", gameName, playersNo);
}
}
class GameWithTimeLimit : Games
{
int minutes;
public int getTime()
{
return this.minutes;
}
public void setTime(int minutes)
{
this.minutes = minutes;
}
}
VulpesPosted Jan 11, 2012, 6:03 PM
The Game class will have 5 methods and the GameWithTimeLimit class will have 7, including the 5 inherited from Game.
Posted Jan 11, 2012, 3:07 PM
This is question from chapter 7
I couldn't understand the last part of the question 1(c) specially "demonstrate all the method". I attach the chapter and my attempt to question.
VulpesPosted Jan 11, 2012, 1:41 PM
http://msdn.microsoft.com/en-us/library/system.object.aspx
Posted Jan 11, 2012, 1:10 PM
VulpesPosted Jan 11, 2012, 12:52 PM
I wouldn't have inserted that line otherwise.
Posted Jan 11, 2012, 12:15 PM
VulpesPosted Jan 11, 2012, 9:55 AM
Posted Jan 11, 2012, 9:44 AM
If it is a casting understood that means object obj = (object) payingStudent;
VulpesPosted Jan 11, 2012, 4:56 AM