Professional Documents
Culture Documents
Lab 2
Lab 2
Lab 2
AA.SC.U3CSC2107293
1. Create a class Circle which represent a real circle object, with the data
Radius
–
double
type
Color –
string
type
Include three types of Constructors in the class. Create three different object with the help of
three constructors defined in the class
Add methods getRadius() and getColor() to return the radius and color respectively
Add methods setRadius(double d) and setColor(String c) to set the value of radius and color
respectively.
Test the class in main method.
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
Source code:
namespace Q1
{
internal class Program
{
static void Main(string[] args)
{
Circle c1 = new Circle(); Circle c2 = new Circle(2.0);
Circle c3 = new Circle(3.0, "Green");
Console.ReadLine();
}
}
class Circle
{
private double radius; private String color;
public Circle(double radius, String color)
{
this.radius = radius; this.color = color;
}
public Circle(double radius)
{
this.radius = radius; this.color = "No Color";
}
public Circle()
{
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
this.radius = 1.0; this.color = "No Color";
}
public double getRadius()
{
return radius;
}
public String getColor()
{
return color;
}
2. Define a class Holiday which representing holidays during the year. There is three instance variables
Name – String, name of the holiday
Day – int, day of the month of the holiday
Month - string, the month in which the holiday in.
Define parametrized constructor
Define method InSameMonth() – compare two instance of the class Holiday and returns the
Boolean value “true” if they have same month and “false” if they do not.
Define a method to display the
holiday details as Day: 26
Month: January
Description: today is republic day
Create Holiday instance as array to store holidays in a month and test the class
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
Solution:
namespace Q2
{
internal class Program
{
static void Main(string[] args)
{
Holiday[] holidays = new Holiday[5];
holidays[0] = new Holiday("New Year", 1, "January"); holidays[1] = new Holiday("Pongal", 14, "January");
holidays[2] = new Holiday("Republic Day", 26, "January"); holidays[3] = new Holiday("Independence
Day", 15, "August"); holidays[4] = new Holiday("Rakshabandhan", 19, "August");
Console.WriteLine("List of Holidays : "); for (int i = 0; i < holidays.Length; i++)
{
holidays[i].printHoliday();
}
Console.ReadLine();
}
}
public class Holiday
{
public string name; protected int day; protected string month;
Output:
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
3. Write a C# class Clock for dealing with the day time represented by hours, minutes,
and seconds. Your class must have the following features:
Three instance variables for the hours (range 0 - 23), minutes (range 0 - 59), and seconds (range 0 -
59).
Three constructors:
- default (with no parameters passed; is should initialize the represented time to 12:0:0)
- a constructor with three parameters: hours, minutes, and seconds.
- a constructor with one parameter: the value of time in seconds since midnight (it
should be converted into the time value in hours, minutes, and seconds) Instance methods:
a set-method method setClock() with one parameter seconds since midnight (to be converted
into the time value in hours, minutes, and seconds as above).
get-methods getHours(), getMinutes(), getSeconds() with no parameters that return the
corresponding values.
set-methods setHours(), setMinutes(), setSeconds() with one parameter each that set up the
corresponding instance variables.
method tick() with no parameters that increments the time stored in a Clock object by one second.
method addClock() accepting an object of type Clock as a parameter. The method should
add the time represented by the parameter class to the time represented in the current
class.
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
Add an instance method toString() with no parameters to your class. toString() must return a
String representation of the Clock object in the form "(hh:mm:ss)", for example "(03:02:34)".
Add an instance method tickDown() which decrements the time stored in a Clock object by one
second.
Add an instance method subtractClock() that takes one Clock parameter and returns the
difference between the time represented in the current Clock object and the one
represented by the Clock parameter. Difference of time should be returned as an clock
object.
Write a separate class ClockDemo with a main() method. The program should:
instantiate a Clock object firstClock using one integer seconds since midnight obtained from the
keyboard.
tick the clock ten times by applying its tick() method and print out the time after each tick.
Extend your code by appending to it instructions instantiating a Clock object secondClock by using
three integers (hours, minutes, seconds) read from the keyboard.
Then tick the clock ten times, printing the time after each tick.
Add the secondClock time in firstClock by calling method addClock.
Print both clock objects calling toString method
Create a reference thirdClock that should reference to object of difference of firstClock and
secondClock by calling the method subtractClock().
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
Solution:
namespace Assignment2
{
internal class Program
{
static void Main(string[] args)
{
Console.Write("Enter seconds since midnight for the first clock: "); int secondsInput =
int.Parse(Console.ReadLine());
Clock firstClock = new Clock(secondsInput);
Console.WriteLine("\nThe first clock is: " + firstClock); for (int i = 0; i < 10; i++)
{
firstClock.tick();
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
}
Console.WriteLine($"\nFirst Clock After 10 Tick {firstClock}"); Console.Write("\n\nEnter hours for
the second clock: ");
int hoursInput = int.Parse(Console.ReadLine()); Console.Write("Enter minutes for the second clock:
"); int minutesInput = int.Parse(Console.ReadLine()); Console.Write("Enter seconds for the second
clock: "); int secondsInput2 = int.Parse(Console.ReadLine());
Clock secondClock = new Clock(hoursInput, minutesInput, secondsInput2); Console.WriteLine("\
nThe second clock is: " + secondClock);
Console.ReadLine();
}
}
}
else
{
}
}
Hours = hours;
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
}
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
else
{
}
}
Minutes = minutes;
}
else
{
}
}
Seconds = seconds;
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
public Clock()
{
this.Hours = 12;
this.Minutes = 0;
this.Seconds = 0;
}
Output:
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
4. Given the structure of two classes. Implement the given inheritance and test the
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
class. Methods and its return types are specified in the diagram. toString() method
both in the class will display the value of its data members.
Solution:
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
namespace Assignment2
{
internal class Program
{
static void Main(string[] args)
{
Point2D point2D = new Point2D(1.5f, 2.6f);
Console.WriteLine($"Point2D: (x : {point2D.getX()}, y : {point2D.getY()})");
point2D.display();
Console.ReadLine();
}}
public class Point2D
{
private float x = 0.0f; private float y = 0.0f;
public Point2D() { }
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
public Point2D(float x, float y)
{
this.x = x; this.y = y;
}
public void setX(float x) { this.x = x; } public float getX() { return x; }
public void setY(float y) { this.y = y; } public float getY() { return y; }
public void setXY(float x, float y)
{
this.x = x; this.y = y;
}
public float[] getXY()
{
return new float[] { x, y };}
public void display()
{
Console.WriteLine($"({getX()}, {getY()})");
}}
public class Point3D : Point2D
{
private float z = 0.0f; public Point3D() { }
public Point3D(float x, float y, float z) : base(x, y)
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
{
this.z = z;
}
public void setZ(float z) { this.z = z; } public float getZ() { return z; }
public void setXYZ(float x, float y, float z)
{
setXY(x, y); this.z = z;
}
Output:
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293