CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2010
    Posts
    2

    Issues with Homework

    Struggling in my programming class and have barely been able to get by till this point.

    Assignment from the book is: Create a class named Square that contains fields for area and the length of a side and whose constructor requires a parameter for the length of one side of a Square. The constructor assigns its parameter to the length of the Square's side field and calls a private method that computes the area field. Also include read-only properties to get a Square's side area. Create a class named DemoSquares that instantiates an array of 10 Squares objects with sides that have values of 1 through 10. Display the values for each Square.

    Completely stumped as to what I should do on this or where to start. Keep trying to reread the chapter and the one before it and am to the point of complete frustration. Any code or assistance would be greatly appreciated. Also bear in mind Ive never programmed before this so I dont know much lingo or anything like that.

    Ziggy

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Issues with Homework

    Are you using Visual Studio and coding in C#?

    If so, do you know how to create a C# project and add a class to the project?

  3. #3
    Join Date
    Jan 2010
    Location
    .NET v3.5 SP1, VS 2008
    Posts
    18

    Re: Issues with Homework

    Square.cs
    Code:
    using System;
    
    namespace DemoSquares
    {
        public class Square
        {
            private double sideLength;
            private double area;
    
            public double SideLength {
                get { return this.sideLength; }
            }
    
            public double Area { 
                get { return this.area; }
            }
    
            public Square(double squareSideLength)
            {
                this.sideLength = squareSideLength;
                this.area = ComputeArea();
            }
    
            private double ComputeArea()
            {
                return Math.Pow(this.sideLength, 2);
            }
        }
    }

    DemoSquares.cs
    Code:
    using System;
    
    namespace DemoSquares
    {
        public class DemoSquares
        {
            public static void Main(string[] args)
            {
                Console.Title = "Demo Squares";
                Console.WriteLine("Area and Side Length of Squares:\n\n");
                Console.WriteLine("{0}\t{1}\t{2}", "#", "Side Length", "Area");
                
                Square[] squares = new Square[11];
    
                for (int i = 1; i < 10; i++)
                {
                    squares[i] = new Square(i);
    
                    Square currentSquare = squares[i];
    
                    Console.WriteLine("{0}\t{1,11}\t{2,4}",
                        i, currentSquare.SideLength, currentSquare.Area);
                }
    
                Console.Read();
            }
        }
    }
    http://www.sixsence.net
    using .NET v3.5 SP1 with VS 2008

    fairly new to C#, eager to learn, bear with me :P

  4. #4
    Join Date
    Mar 2010
    Posts
    2

    Re: Issues with Homework

    Thank you so much Sixth Sense and Arjay.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Issues with Homework

    It would probably be best to explain things to a student instead of posting the answer for them.

  6. #6
    Join Date
    Jan 2010
    Location
    .NET v3.5 SP1, VS 2008
    Posts
    18

    Re: Issues with Homework

    oh is that the answer?
    http://www.sixsence.net
    using .NET v3.5 SP1 with VS 2008

    fairly new to C#, eager to learn, bear with me :P

  7. #7
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Issues with Homework

    Quote Originally Posted by SixSence View Post
    oh is that the answer?
    I assume you are being modest...

    It's certainly not the only way of doing it but I would give you top marks for that! The problem was that you solved the problem the original poster was supposed to solve. He can copy that code and submit and get top marks even though he:

    1. Didn't do it.
    2. Possibly doesn't understand the code.

    You have not really helped. He stated in the first post that he didn't have a clue as to where to begin. This forum is about helping others to solve their programming problems and learn in the process. Doing other people's homework is generally frowned upon because you're not really helping the person. You can help a person with their homework but you shouldn't really do it for them...

    To the original poster:
    If you are completely stumped on this assignment I've got a few questions for you...

    1. Have you attended any classes or tutorials or are you teaching yourself?

    2. Is the programming class something you want to do or something you just have to do as part of another course?

    The reason I ask to is to clarify what your situation is and be able to give more specific advice. If for example you are attending a taught course or class they wouldn't give you any homework on topics they have not taught you. They should have taught enough to at least get started (i.e you would know what a class is, what a constructor is, etc)

    If you're self taught then I recommend to you MSDN and any C# beginners book.

    If you're planning to do software development for the long term you will have to enjoy it and in particular develop and enjoy the process of learning. Doing research and finding out how things work. Everybody has been a beginner at some point and we've all had to learn things bit by bit (some faster than others admitedly).

    SixSence has given you a very good solution. Try and understand it before submitting it as your own piece of work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured