Lab 2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28

Labsheet-2 HariHaran P

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.WriteLine("Radius of c1: " + c1.getRadius() + "; Color of c1: " + c1.getColor());


Console.WriteLine("Radius of c2: " + c2.getRadius() + "; Color of c2: " + c2.getColor());
Console.WriteLine("Radius of c3: " + c3.getRadius() + "; Color of c3: " + c3.getColor());
Console.WriteLine("\n\nSetting the radius and color of c1 & c2\n"); c1.setRadius(4.0);
c1.setColor("Blue");
c2.setRadius(5.0); c2.setColor("Red");
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
Console.WriteLine("Radius of c1: " + c1.getRadius() + "; Color of c1: " + c1.getColor());
Console.WriteLine("Radius of c2: " + c2.getRadius() + "; Color of c2: " + c2.getColor());

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;
}

public void setRadius(double d)


{
this.radius = d;
}

public void setColor(String C)


{
this.color = C;
}
}}
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
Output:
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293

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.WriteLine("\n\nComparing List of Holydays :"); for (int i = 0; i < holidays.Length - 1; i++)


{
Console.WriteLine($"Is {holidays[i].name} and {holidays[i + 1].name}" +
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
$" is in the same month? {holidays[i].InSameMonth(holidays[i + 1])}");
}

Console.ReadLine();

}
}
public class Holiday
{
public string name; protected int day; protected string month;

public Holiday(string name, int day, string month)


{
this.name = name; this.day = day; this.month = month;
}

public bool InSameMonth(Holiday holiday)


{
return this.month == holiday.month;
}
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
public void printHoliday()
{
Console.WriteLine("\nDay : " + this.day + "\nMounth : " + this.month + "\nDescription : " + this.name);
}
}
}

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);

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


{
secondClock.tick();
}
Console.WriteLine($"\nSecond Clock After 10 Tick {secondClock}");
firstClock.addClock(secondClock);
Console.WriteLine($"\n\nFirst Clock after adding Second Clock: {firstClock}");
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
Console.WriteLine($"\n\nFirst Clock: {firstClock}"); Console.WriteLine($"Second Clock:
{secondClock}");

Clock thirdClock = firstClock.subtractClock(secondClock);


Console.WriteLine($"\n\nThird Clock (Difference of First Clock and Second Clock): {thirdClock}");

Console.ReadLine();
}
}

public class Clock


{
public int Hours; public int Minutes; public int Seconds;
public int getHours()
{
return Hours;
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
}

public void setHours(int hours)


{
if (hours >= 0 && hours <= 23)
{

}
else
{

}
}

Hours = hours;
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293

throw new ArgumentOutOfRangeException("Hours must be between 0 and 23");

public int getMinutes()


{
return Minutes;
}

public void setMinutes(int minutes)


{
if (minutes >= 0 && minutes <= 59)
{

}
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
else
{

}
}

Minutes = minutes;

throw new ArgumentOutOfRangeException("Minutes must be between 0 and 59");

public int getSeconds()


{
return Seconds;
}
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293

public void setSeconds(int seconds)


{
if (seconds >= 0 && seconds <= 59)
{

}
else
{

}
}

Seconds = seconds;
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293

throw new ArgumentOutOfRangeException("Seconds must be between 0 and 59");

public Clock()
{
this.Hours = 12;
this.Minutes = 0;
this.Seconds = 0;
}

public Clock(int hours, int minutes, int seconds)


{
this.setHours(hours); this.setMinutes(minutes); this.setSeconds(seconds);
}
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
public Clock(int secondsatnight)
{
setClock(secondsatnight);
}

public void setClock(int seconds)


{
this.setHours(seconds / 3600);
this.setMinutes((seconds % 3600) / 60);
this.setSeconds((seconds % 3600) % 60);
}

public void tick()


{
int totalSeconds = (Hours * 3600) + (Minutes * 60) + Seconds; setClock(totalSeconds + 1);
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
}

public void tickDown()


{
int totalSeconds = (Hours * 3600) + (Minutes * 60) + Seconds; setClock(totalSeconds - 1);
}
public void addClock(Clock clock)
{
int totalSeconds = (Hours * 3600) + (Minutes * 60) + Seconds;
int totalSeconds2 = (clock.Hours * 3600) + (clock.Minutes * 60) + clock.Seconds;
setClock(totalSeconds + totalSeconds2);
}
public Clock subtractClock(Clock clock)
{
int totalSeconds = (Hours * 3600) + (Minutes * 60) + Seconds;
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293
int totalSeconds2 = (clock.Hours * 3600) + (clock.Minutes * 60) + clock.Seconds; return new
Clock(totalSeconds - totalSeconds2);
}

public override string ToString()


{
return $"{this.Hours:D2}:{this.Minutes:D2}:{this.Seconds:D2}";
}}}

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();

Point3D point3D = new Point3D(3.4f, 4.5f, 5.9f);


Console.WriteLine($"\n\nPoint3D: (x : {point3D.getX()}, y : {point3D.getY()}, z :
{point3D.getZ()})"); point3D.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;
}

public float[] getXYZ()


{
return new float[] { getX(), getY(), getZ() };
}
public void display()
{
Console.WriteLine($"({getX()}, {getY()}, {getZ()})");
}}}

Output:
Labsheet-2 HariHaran P
AA.SC.U3CSC2107293

You might also like