1 Replies - 110 Views - Last Post: 07 February 2012 - 11:30 AM Rate Topic: -----

Topic Sponsor:

#1 Iznlol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-February 12

Box creating task

Posted 07 February 2012 - 02:35 AM

Short intro: Hello everyone! I just started the second c# course of my university. It's called, object oriented programming with c#. The problem that i am having is that i am still abit unsecure about the whole object oriented idea. I've managed to code down to the "Titled box" which is supposed to give the box a title. The title should be placed right over the box in the console window.

Now to my question: Will this still count as object oriented if i add a new drawmethod to the ColoredBox and adding two arguments to that draw method? I remember my teacher had tons of complains last time i made a drawmethod too essential

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment1._3
{
    class Program
    {
        static void Main(string[] args)
        {
            int färg = 4;
            Random xR = new Random();


            for (int i = 0; i < 10; i++)
            {
                int r1 = xR.Next(39);
                int r2 = xR.Next(24);
                färg = xR.Next(10);
                Point p = new Point(r2, r1, (ConsoleColor)färg + 1);
                ColoredBox c1 = new ColoredBox(4, 5, p);
            }
            //Box b = new Box(r2, r1, p);
            Console.ReadKey();
        }
    }
    class Point
    {
        int x = 0, y = 0;
        ConsoleColor color = ConsoleColor.Yellow;
        
        public Point(int x, int y, ConsoleColor color)
        {
            this.x = x;
            this.y = y;
            this.color = color;
        }
        public int X
        {
            get { return x; }
            set { x = value; }
        }
        public int Y
        {
            get { return y; }
            set { y = value; }
        }
        public ConsoleColor Color
        {
            get { return color; }
            set { color = value; }
        }
        
        public void draw(string utskrift)
        {
            //Console.SetCursorPosition(width, height);
            Console.SetCursorPosition(x, y);
            Console.ForegroundColor = color;            
            Console.Write(utskrift);
        }
    }
    class Box
    {
        Point topCorner;
        int width;
        int height;
        string utskrift;
        public Box()
        {
        }
        public Box(int witdh, int height, Point p)
        {
            this.width = witdh;
            this.height = height;
            this.topCorner = p;
            draw("*");
        }
        public int Width
        {
            get { return width; }
            set { width = value; }
        }
        public int Height
        {
            get { return height; }
            set { height = value; }
        }
        Point TopCorner
        {
            get { return topCorner; }
            set { topCorner = value; }
        }
        public string Utskrift
        {
            get { return utskrift; }
            set { utskrift = value; }

        }
        public void draw(string utskrift)
        {
            Point drawpoint = new Point(topCorner.X, topCorner.Y, topCorner.Color);
            
            
            for (int r = 0; r < height; r++)
            {
                for (int c = 0; c < width; c++)
                {
                    drawpoint.X = topCorner.X + c;
                    drawpoint.Y = topCorner.Y + r;
                    drawpoint.draw(utskrift);
                }
            }
        }
    }
    class ColoredBox : Box
    {
        TitleBox t1;
        public ColoredBox()
        {
        }
        public ColoredBox(int height, int width, Point point)
            : base (height, width, point)
        {
            Background = ConsoleColor.Cyan;
            cDraw();                              
        }
        public ConsoleColor Background { get; set; }
       
        public void cDraw()
        {
            Console.BackgroundColor = Background;            
            draw(" ");
        }
        

    }
    class TitleBox : ColoredBox
    {
        string title;
        ConsoleColor titlecolor;
        public TitleBox(string title, int height, int width, Point point)
            :base (height, width, point)
        {
            
        }
        public string Title { get; set; }
        public ConsoleColor Titlecolor { get; set; }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Box creating task

#2 RexGrammer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 152
  • View blog
  • Posts: 664
  • Joined: 27-October 11

Re: Box creating task

Posted 07 February 2012 - 11:30 AM

Yeah, this is OO. You have objects (points and boxes) with their properties and methods.

A note: your naming isn't quite right. Methods should start with a capital letter.
Another thing: you can use auto-properties instead of full ones:

This:
public int X
        {
            get { return x; }
            set { x = value; }
        }



Could be this:
public int X { get; set; }



It reduces the amount of code that you have to write and also making your code more cleaner and readable. If you're interested how auto-properties work: they do the same as full ones just the compiler does that behind the curtains.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1