Another Console Application

Jynx

Active member
Joined
Oct 20, 2007
Messages
44
Programming Experience
Beginner
Well I've reached another one of my practice programs at the end of my book that I'm trying to figure out and need some help again fellas. Here are my instructions.
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
 
Last edited by a moderator:
Post the code you have so far, and be more specific what exactly you have problems with.
 
Ok heres what I did so far. I just can't get it to look right. And can't figure out how to make it so it will show something like "This object has length: 4 and width: 23 and height: 3" - You'll see I commented that part out cause it wasn't working.

VB.NET:
Module Module1

    Sub Main()
        Dim r As Char
        Dim b As Char
        Dim L As Integer
        Dim W As Integer
        Dim H As Integer
        Dim volume As Integer
        Dim area As Integer

        Console.WriteLine("Would you like to create a Rectangle or a Box (r or b): ")
        If Console.ReadLine = b Then
            Console.WriteLine("Please enter the length: ")
            L = Console.ReadLine
            Console.WriteLine("Please enter the Width: ")
            W = Console.ReadLine
            Console.WriteLine("Please enter the Height: ")
            H = Console.ReadLine

            '     Console.WriteLine("This object has length:", L And "and width:", W And "and height:" And H)

            volume = L * W * H
            Console.WriteLine("The Box has volume:")
            Console.WriteLine(volume.ToString)

        Else


            Console.WriteLine("Please enter the length: ")
            L = Console.ReadLine
            Console.WriteLine("Please enter the Height: ")
            H = Console.ReadLine
            area = H * L

            Console.WriteLine("The Rectangle has Area:")
            Console.WriteLine(area.ToString)

        End If

        Console.WriteLine("Press any key to exit.")
        Console.ReadKey(True)
    End Sub

End Module
 
If you look up the Console.WriteLine method in help you will see there are many overloads for various data type parameters, but you will also see there are some that take a String and some Objects. Have a closer look at that help topic, the string is used to specify custom formatting and the objects are additional parameter values that you can use in that string, the help page give you a brief overview of how the composite formatting feature works, and links to more specific help pages that explains the string formatting in detail. Here is a basic example that you can use in place of the line you commented out:
VB.NET:
Console.WriteLine("This object has length: {0} width: {1} height: {2}", L, W, H)
This answers the problem you asked, but does not solve the exercise!
create an object of that type
This implies the book has taught about writing classes/structures, and that you must write a Box and Rectangle class or structure.
use the ToString method to print a description of the object
This means that the object class/structure you define should define a ToString method that returns a string describing the object. As a hint compared to the other reply, I will tell you String class has a shared method called Format that works exactly the same regarding the custom string formatting.
 
John can you also tell me why the formating of the questions and answers don't seem right. Mine shows :
VB.NET:
Would you like to create a Rectangle or a Box (r or b): 
r
Where as I'd like the answers to be on the same line :
VB.NET:
Would you like to create a Rectangle or a Box (r or b): r
 
First of all, you really need to have Option Strict turned On in order to avoid suble errors that will be hard to debug. Place the statement:

VB.NET:
Option Strict On
at the top of your code.

In order to keep the cursor on the same line, use Console.Write instead of Console.WriteLine.

The Console.ReadLine input should work only with string variables, not with numbers as you have it. Then convert the string to the number variable using TryParse:

VB.NET:
Dim snum As String, L As Integer
Console.Write("Please enter the length:  ")
snum = Console.ReadLine()
Integer.TryParse(snum, L)

As for the decision in your problem, both require input of length and width, so those statements can be coded before starting the If-Then block. If the user selected a box, you can include the height in that part of the If-Then block.

You do the math processing inside the block. The Console.WriteLine method will implicitly convert the number result to a string for output so you don't need to use ToString, even with Option Strict On.

There are two ways to output variables with Console.WriteLine. One way is with string concatenation using the & operator. The other way is by using placeholders. Here are both examples:

VB.NET:
Console.WriteLine("Length is " & L & "  Width is " & W)
Console.WriteLine("Length is {0}  Width is {1}", L, W)
 
I started rewriting this but I'm not sure I'm on the right path "OR" satisfying the exercise instructions so I wanted to post what I was doing before waiting too much time.

VB.NET:
Option Strict On
Public Class rectangle

    Public LengthValue As Integer
    Public WidthValue As Integer

    Public Property Length() As Integer
        Get
            Return LengthValue
        End Get
        Set(ByVal value As Integer)
            LengthValue = value
        End Set
    End Property

    Public Property Width() As Integer
        Get
            Return WidthValue
        End Get
        Set(ByVal value As Integer)
            WidthValue = value
        End Set
    End Property

    Public Sub conversion()
        Dim convert As Integer
        convert = WidthValue * LengthValue
    End Sub

    Public Sub displaymessage(ByVal convert As String)
        Console.WriteLine(convert)
    End Sub

    'Public Sub DisplayResults()
    '    Console.WriteLine(Conversion)
    'End Sub


End Class

VB.NET:
Option Strict On
Imports System
Module main
    Sub Main()
        Dim rectangle As New rectangle
        Console.WriteLine("Please enter the length: ")
        rectangle.Length = CInt((Console.ReadLine))
        Console.WriteLine("Please enter the Height: ")
        rectangle.Width = CInt((Console.ReadLine))

    End Sub


    Sub convert()

 
    End Sub

End Module


OR would this better describe the instructions?

VB.NET:
Module Module1
    Structure Rectangle
        Dim X
        Dim Y
    End Structure
    Structure Box
        Dim X
        Dim Y
        Dim Z
    End Structure

    Sub Main()

        'Bit different solution  
        Dim R As Rectangle = Nothing, B As Box = Nothing

        'INPUT  
        Console.Write("Would you like to create a Rectangle or a Box (r or b): ")
        Select Case CInt(Console.ReadLine)
            Case 1
                R = New Rectangle
                Console.Write("Please enter the Length: ")
                R.X = CInt(Console.ReadLine)
                Console.Write("Please enter the Width : ")
                R.Y = CInt(Console.ReadLine)
            Case 2
                B = New Box
                Console.Write("Please enter the Length: ")
                B.X = CInt(Console.ReadLine)
                Console.Write("Please enter the Width: ")
                B.Y = CInt(Console.ReadLine)
                Console.Write("Please enter the Height: ")
                B.Z = CInt(Console.ReadLine)
        End Select

        'OUTPUT  
        Console.ForegroundColor = ConsoleColor.Green
        Console.Write("Description of ")
        If Object.Equals(B, Nothing) Then 'Describe Rectangle  
            Console.WriteLine("Rectangle:")
            Console.WriteLine("Volume = {0:F}", R.X * R.Y)
        Else 'describe Box  
            Console.WriteLine("Box:")
            Console.WriteLine("Area = {0:F}", B.X * B.Y * B.Z)
        End If

        Console.ForegroundColor = ConsoleColor.White
        Console.WriteLine("Press escape to exit")
        Do : Loop Until Console.ReadKey().Key = ConsoleKey.Escape
    End Sub

End Module
 
Last edited:
Alright, here's the code, but please study from it. :)

VB.NET:
Prompt: 'Marker for start in case neither r or b is pressed

'Asks if user would like to create a box or a rectangle
Console.Write("Would you like to create a rectangle or box (r or b)? ")

'Reads and stores the output of the prompt
Dim key as ConsoleKey = Console.ReadKey

'Asks the width of the rectangle / box
Console.Write("Please enter the width: ")
Dim width As Integer = CInt(Console.ReadLine)

'Asks the height of the rectangle / box
Console.Write(vbCrLf & "Please enter the height: ")
Dim height As Integer = CInt(Console.ReadLine)

'Executes code depending if it is a r, b, or neither
Select Case key.ToString().ToLower()
 Case "r" 'If they said rectangle
  'Asks user for the rectangle's length
  Console.Write(vbCrLf & "Please enter the length: ")
  Dim length As Integer = CInt(Console.ReadLine)

  'Writes the length, width, and length of the rectangle
  Console.WriteLine("This rectangle has a length of " & CStr(length) & ", a width of " & CStr(width) & ", and a length of " & CStr(length))

  'Writes the volume of the rectangle
  Console.WriteLine("This rectangle has a volume of " & CStr(length * width * height))
 Case "b" 'If they said box
  'Writes the width and height of the box
    Console.WriteLine("This box has a width of " & CStr(width) & ", and a height of " & CStr(height))

  'Writes the volume of the box
  Console.WriteLine("This box has a volume of " & CStr(width * height))
 Case Else
  'This is if the user pressed neither r or b
  GoTo Prompt
End Select

Now, I haven't coded VB.NET in a while, but this was simple enough. The only part that my memory was fuzzy on was the key.ToString().ToLower() functions. The key.ToString() may not actually return the key, but if I recall it does. So test it out and see. Everything else is correct. You did not need to create a whole object for the rectangle and box.

:)
 
You did not need to create a whole object for the rectangle and box.
That was the requirement of the exercise :) Jynx is studying OOP.
 
Oh. Well in that case... :D

Above the Main() Sub
VB.NET:
Private Structure MyBox
 Public Width as Integer
 Public Height As Integer
 Public ReadOnly Property Volume As Integer
  Get
   Return Width * Height
  End Get
 End Property
End Structure

Private Structure MyRectangle
 Public Width as Integer
 Public Height As Integer
 Public Length As Integer
 Public ReadOnly Property Volume As Integer
  Get
   Return Width * Height * Length
  End Get
 End Property
End Structure

In the Main() Sub
VB.NET:
Prompt: 'Marker for start in case neither r or b is pressed

'Asks if user would like to create a box or a rectangle
Console.Write("Would you like to create a rectangle or box (r or b)? ")

'Reads and stores the output of the prompt
Dim key as ConsoleKey = Console.ReadKey

'Asks the width of the rectangle / box
Console.Write("Please enter the width: ")
Dim width As Integer = CInt(Console.ReadLine)

'Asks the height of the rectangle / box
Console.Write(vbCrLf & "Please enter the height: ")
Dim height As Integer = CInt(Console.ReadLine)

'Executes code depending if it is a r, b, or neither
Select Case key.ToString().ToLower()
 Case "r" 'If they said rectangle
  'Asks user for the rectangle's length
  Console.Write(vbCrLf & "Please enter the length: ")
  Dim length As Integer = CInt(Console.ReadLine)

  'Instantiates and sets the rectangle object
  Dim myR as New MyRectangle
  myR.Width = width
  myR.Height = height
  myR.Length = length

  'Writes the length, width, and length of the rectangle
  Console.WriteLine("This rectangle has a height of " & CStr(myR.Height) & ", a width of " & CStr(myR.Width) & ", and a length of " & CStr(myR.Length))

  'Writes the volume of the rectangle
  Console.WriteLine("This rectangle has a volume of " & CStr(myR.Volume))
 Case "b" 'If they said box

  'Instantiates and sets the box object
  Dim myB As New MyBox
  myB.Width = width
  myB.Height = height

  'Writes the width and height of the box
    Console.WriteLine("This box has a width of " & CStr(myB.Width) & ", and a height of " & CStr(myB.Height))

  'Writes the volume of the box
  Console.WriteLine("This box has a volume of " & CStr(myB.Volume))
 Case Else
  'This is if the user pressed neither r or b
  GoTo Prompt
End Select

=D
 
I think they also meant theBox.ToString method returns the description and area/volume string... OOP-style :)

Another thing, according to .Net Developers Guide instance fields should not be made public, instead use properties. It is actually not more typing to do things correctly because intellisense will help you write them, by writing "prop" and press Tab key twice the default Property structure snippet is inserted in code, and you can just Tab around to change name and type.
 
Back
Top