Rectangle class - applying inheritance principles

justlearning

Member
Joined
Dec 11, 2008
Messages
16
Programming Experience
Beginner
Hi I am sorry to bother you guys but have a problem. My assignment is as follows:
For this project, you'll write a set of classes that use apply inheritance principles.

Write a class named Rectangle. This class should have data fields for:

length
width
Also provide the following methods:

A way to get and set each of these fields
a constructor that takes values to initialize the length and width fields
a method that will return the area of the Rectangle (length times width)
a ToString method that will return a description of the Rectangle as a String.
NOTE: Your program will not be drawing the Rectangle, only providing a way to represent it.

Once you have your Rectangle class working correctly, write a child class of Rectangle, named Box. This class should have one additional data field:

height
Also provide the following methods:

a way to get and set the value of height
a constructor that takes values for length, width and height
a method that returns the volume (length times width times height)
a ToString method that returns a String representation of the Box
Write a Console application that lets the user decide if they want to make a Rectangle or a Box. Depending on their answer, prompt them for the necessary values (length and width or length, width and height) and then create an object of that type. Once this is complete, use the ToString method to print a description of the object and then the value of either the area or volume of the object to the Console.

Although not required to look this way, your program output might look like:

Would you like to create a Rectangle or a Box (r or b): b
Please enter the length: 4
Please enter the width: 23
Please enter the height: 3

This object has length: 4 and width: 23 and height: 3
This box has volume: 276



Okay there are a few things I don't understand. To my knowledge I do not see anything saying that I should have the GUI done but how else would I know that it worked?

Second is the console application the main form or a totally seperate application?

Third I am going to post my code but I am not sure how, in a console program, to get some of this to work. I have done two other very simple console programs.

Anyway, I just watched a two part tutorial called Lesson 6: Object Oriented Programming Fundamentals in the Absolute Beginners series so I think I have a pretty good start. Please look over my code and help me out. I would be forever grateful.


The Rectangle and Box Classes Code

VB.NET:
'Parent Class Rectangle
Public Class Rectangle

    Protected m_length As Integer
    Protected m_width As Integer

    Public Sub New(ByVal make As String, ByVal model As String)
        m_length = length
        m_width = width

    End Sub
    Public Property length()
        Get
            Return m_length
        End Get
        Set(ByVal value)
            m_length = value
        End Set
    End Property
    Public Property width()
        Get
            Return m_width
        End Get
        Set(ByVal value)
            m_width = value
        End Set
    End Property
    Sub New()
        MyBase.new()
    End Sub

    Public Sub New(ByVal length As Double, ByVal width As Double)
        MyBase.New()

        m_length = length
        m_width = width
    End Sub
End Class


'Child class
Public Class Box
    Inherits Rectangle
    Public Sub New()
        m_height = height
    End Sub
    Private m_height As Integer
    Public Property height() As Integer
        Get
            Return m_height
        End Get
        Set(ByVal value As Integer)
            m_height = value
        End Set
    End Property

End Class


The Console Application Code

VB.NET:
Module Module1
    Sub Main()
        Dim MyRectangle As New Rectangle
        Dim result As String
        Dim test As Integer

        Console.WriteLine("Would you like to make a Rectangle or a Box? ")
        result = Console.ReadLine
        If result = Rectangle Then

        End If
        Console.WriteLine("Please Enter the length: ")
        result = Console.ReadLine()

        Console.WriteLine("Did this work out correctly?")
        test = Console.ReadLine()

    End Sub

End Module
 
Last edited by a moderator:
Hi JohnH, I just have a quick question. I have looked over Jynx's post and it answers a lot of questions including this one most likely but I just want to make sure I understood. Here is the question: Is the console program supposed to go below the classes that I coded? I think that it must because that is how McFly did it. Thank you so much for your help in giving me to link to Jynx's post.

Also are there any tutorials you can recommend for me to understand console applications better?

Thanks again

Added on:
How do I keep my console open after it runs through its code. It keeps closing before I can look over the whole thing. Also can you tell me where I can find information about "console.readkey"? I don't understand it so I cannot use it. Also instead of case I used If... Then statements and it worked perfectly. I am not sure I understand case enough to use it right. There is a tutorial on that though. So I will run through it.

Thanks again.
 
Last edited:
How do I keep my console open after it runs through its code.
Any Console.Read... call will make it wait for user input, the most common is the "press any key" which is Console.ReadKey(True) that wait for user to press a single key and suppresses it from displaying.
Also are there any tutorials you can recommend for me to understand console applications better?
I would at least read all the documentation for the Console class. I don't have any particular book recommandation, read everything you can find until you know what you feel you need to know.
Also can you tell me where I can find information about "console.readkey"? I don't understand it so I cannot use it.
Actually, I don't understand what you don't understand about it...? It's just a method of the Console class that does exactly what it says, it "read key" from console input.
Is the console program supposed to go below the classes that I coded?
You do best by keeping only one class in each class file. To add a new class file to project use either Project menu or context menu in Solution explorer and select "Add class".
Also instead of case I used If... Then statements
These code structure can be used with same purpose, Select-Case offer several benefits over If-Else, which is normally used and usually for simpler cases.
 
Okay thanks for the information.

I have one more problem. I understand now how the classes and the console app work together. That was my biggest problem. I missed the part about the universal string and instead coded it just in my console. Now I finally have the console output string running from the classes, but I have a problem with option b.

When r goes its output is perfect but when b goes it doesn't give me any values for length or width. It gives me a value for height but then when it runs the volume it comes out as 0 for obvious reasons. I don't think I have my universal string Function override right. Can you please look at it and give me a little bit of advice?


Console Application
VB.NET:
Option Strict On
Module Module1
    Sub Main()
        'Declare all variables
        Dim MyRectangle As New Rectangle
        Dim MyBox As New Box
        Dim test As String
        Dim result As String

        'Asks user if they would like to make a rectangle or a box
        Console.Write("Would you like to make a Rectangle or a Box (r or b)? ")
        'result stores users answer 
        result = Console.ReadLine

        'Asks user to enter length of shape
        Console.Write("Please enter the length: ")
        'length stores and converts users answer to integer
        MyRectangle.length = CInt(Console.ReadLine)

        'Asks user to enter width of shape
        Console.Write("Please enter the width: ")
        'width stores and converts users answer to integer
        MyRectangle.width = CInt(Console.ReadLine)

        'Establishes whether the shape is an r(rectangle) or b (box)
        'If shape is a rectangle...
        If result = "r" Then
            'Creates a blank line
            Console.WriteLine("")
            'Displays the objects length and width
            Console.WriteLine(MyRectangle.toUniversalString)

            'If the shape is a box...
        ElseIf result = "b" Then
            'Adds one requirement: height
            'Asks user to enter in the height of the shape
            Console.Write("Please enter the height: ")
            'height converts and stores users answer to integer
            MyBox.height = CInt(Console.ReadLine)
            'Creates blank line
            Console.WriteLine("")
            'Displays the objects length, width and height
            Console.WriteLine(MyBox.toUniversalstring)
        End If

        'These lines of code are just keeping the console open so user can see the whole page
        Console.WriteLine("")
        Console.Write("Did this application turn out alright?")
        test = Console.ReadLine


    End Sub

End Module


Classes (Both Parent and Child)

VB.NET:
'Parent Class Rectangle
Public Class Rectangle
    'Creates protected fields
    'fields for the rectangle (length and width)
    Protected m_length As Integer
    Protected m_width As Integer
    Public Sub New(ByVal length As String, ByVal width As String)
        'constructor
        m_length = length
        m_width = width
    End Sub
    'property length
    Public Property length()
        Get
            'Retrieves length value
            Return m_length
        End Get
        Set(ByVal value)
            m_length = value
        End Set
    End Property
    'property width
    Public Property width()
        Get
            'retrieves width value
            Return m_width
        End Get
        Set(ByVal value)
            m_width = value
        End Set
    End Property
    Public Sub Area()
        'Declare area 
        Dim area As Integer
        'Multiply properties to achieve area
        area = m_length * m_width
    End Sub
    Sub New()
        MyBase.new()
    End Sub
    Public Overridable Function toUniversalString() As String
        Return String.Format("The object has length: " & length & " and width " & width & ". This rectangle has area: " & length * width)
    End Function

    'Constructor
    Public Sub New(ByVal length As Double, ByVal width As Double)
        MyBase.New()
        m_length = length
        m_width = width
    End Sub
 
End Class

'Child class
Public Class Box
    Inherits Rectangle
    'constructor
    Public Sub New(ByVal height As String)
        m_height = height
    End Sub
    Private m_height As Integer
    'Property height
    Public Property height() As Integer
        Get
            'Retrieves height value
            Return m_height
        End Get
        Set(ByVal value As Integer)
            m_height = value
        End Set
    End Property
    Sub New()
        MyBase.new()
    End Sub
    Public Sub Volume()
        'Declare volume
        Dim volume As Integer
        'Multiply properties to achieve volume
        volume = length * width * height
    End Sub
    Public Overrides Function toUniversalstring() As String
        Return ("The object has length: " & MyBase.length & ", width " & MyBase.width & " and height:" & height & " . This box has volume: " & length * width * height)
    End Function
    Public Sub New(ByVal height As Double)
        MyBase.New()
        m_height = height
    End Sub
End Class
 
What you do now is this (pseudo)
VB.NET:
new rect
new box

choice = input
rect.width = input
rect.length = input

if choice = rectangle
    output rect
else
    box.height=input
    output box
end if
Can you see the logic flaw? Can you rearrange it?
 
Back
Top