justlearning
Member
- Joined
- Dec 11, 2008
- Messages
- 16
- Programming Experience
- Beginner
Hi guys I am sorry I do not know where to post this. If this is the wrong spot please let me know where you think I should have posted it and why.
I have an assignment that goes as follows:
I have most of the code ( I think) but am having trouble with one spot. It says that I have to use a loop to go through the array and print a description of each object by calling its ToString method. I have the output using each class's ToString method but it isn't looped and I don't know how to do more than one instance of each shape. My output currently looks exactly like the above referenced example except for one key thing. It only has one instance of each shape.
My code is as follows:
Console Application
Classes ( Shape, Circle, Square, Cylinder)
Please let me know if you have any advice. Thanks so much!
I have an assignment that goes as follows:
Create a set of classes that use inheritance according to the following hierarchy:
Please note that your Shape class should contain only the following:
an x value – this is the horizontal location of the Shape
a y value – this is the vertical location of the Shape
a way to get and set each of these fields
a constructor that takes initial values for x and y
a ToString method that will return a description of the Shape as a String.
NOTE: At no time in this Project will you be writing code to draw any of the Shapes. Instead, you'll be using your classes to represent the Shape. If you have questions about this, please contact your instructor.
Once your Shape class is written, you'll write the Circle, Cylinder and Square classes, inheriting from the appropriate classes according to the diagram. Be sure that each class contains appropriate instance variables (based on the class type) and methods (a way to get and set each variable, appropriate constructors, a way to calculate area or volume and a ToString method).
When you have all of your classes complete, write a console application that creates a single array of Shape objects. Store at least two objects of type Circle, two of Cylinder and two of Square in your array. Be sure to prompt your user for the values of the variables. Once all objects have been created, use a loop to go through the array and print a description of each object by calling its ToString method.
Although not required to look this way, your program output might look like:
Please enter the x location of the first circle: 1
Please enter the y location of the first circle: 2
Please enter the radius of the first circle: 3
Please enter the x location of the second circle: 4
Please enter the y location of the second circle: 5
Please enter the radius of the second circle: 6
Please enter the x location of the first cylinder: 7
Please enter the y location of the first cylinder: 8
Please enter the radius of the first cylinder: 9
Please enter the height of the first cylinder: 10
Please enter the x location of the second cylinder: 11
Please enter the y location of the second cylinder: 12
Please enter the radius of the second cylinder: 13
Please enter the height of the second cylinder: 14
Please enter the x location of the first square: 15
Please enter the y location of the first square: 16
Please enter the length of the first square: 17
Please enter the x location of the second square: 18
Please enter the y location of the second square: 19
Please enter the length of the second square: 20
Your shapes are:
Circle at (1, 2) with radius 3 has area 28.26
Circle at (4, 5) with radius 6 has area 113.04
Cylinder at (7, 8) with radius 9, height 10 has volume 2543.4
Cylinder at (11, 12) with radius 13, height 14 has volume 7429.24
Square at (15, 16) with length 17 has area 289
Square at (18, 19) with length 20 has area 400
Press any key to continue
I have most of the code ( I think) but am having trouble with one spot. It says that I have to use a loop to go through the array and print a description of each object by calling its ToString method. I have the output using each class's ToString method but it isn't looped and I don't know how to do more than one instance of each shape. My output currently looks exactly like the above referenced example except for one key thing. It only has one instance of each shape.
My code is as follows:
Console Application
VB.NET:
Option Strict On
Module Module1
Sub Main()
Dim myshape As New Shape
Dim mycircle As New Circle
Dim mysquare As New square
Dim mycylinder As New cylinder
Dim result As String
Console.Write("Please enter the x location of the first circle: ")
mycircle.xvalue = Console.ReadLine
Console.Write("Please enter the y location of the first circle: ")
mycircle.yvalue = Console.ReadLine
Console.Write("Please enter the radius of the first circle: ")
mycircle.radius = CInt(Console.ReadLine)
Console.WriteLine("")
Console.Write("Please enter the x location of the first cylinder: ")
mycylinder.xvalue = Console.ReadLine
Console.Write("Please enter the y location of the first cylinder: ")
mycylinder.yvalue = Console.ReadLine
Console.Write("Please enter the radius of the first cylinder: ")
mycylinder.radius = CInt(Console.ReadLine)
Console.Write("Please enter the height of the first cylinder: ")
mycylinder.height = CInt(Console.ReadLine)
Console.WriteLine("")
Console.Write("Please enter the x location of the first square: ")
mysquare.xvalue = Console.ReadLine
Console.Write("Please enter the y location of the first square: ")
mysquare.yvalue = Console.ReadLine
Console.Write("Please enter the length of the second square: ")
mysquare.length = CInt(Console.ReadLine)
Console.WriteLine("")
Console.Write(myshape.mystringshape & vbCrLf & mycircle.mystringcircle & vbCrLf _
& mycylinder.mystringcylinder & vbCrLf & mysquare.mystringsquare & vbCrLf)
Console.WriteLine("Press any key to continue")
result = Console.ReadLine
End Sub
End Module
Classes ( Shape, Circle, Square, Cylinder)
VB.NET:
'Parent Class
Public Class Shape
'Creates protected fields
'Fields for shape(xvalue and yvalue)
Protected m_xvalue As Integer
Protected m_yvalue As Integer
Public Sub New(ByVal xvalue As String, ByVal yvalue As String)
m_xvalue = xvalue
m_yvalue = yvalue
End Sub
Public Property xvalue()
Get
Return m_xvalue
End Get
Set(ByVal value)
m_xvalue = value
End Set
End Property
Public Property yvalue()
Get
Return m_yvalue
End Get
Set(ByVal value)
m_yvalue = yvalue
End Set
End Property
Sub New()
MyBase.new()
End Sub
Public Sub New(ByVal xvalue As Double, ByVal yvalue As Double)
MyBase.New()
m_xvalue = xvalue
m_yvalue = yvalue
End Sub
Public Function mystringshape() As String
Return String.Format("Your shapes are: " & vbCrLf)
End Function
End Class
'Child class
Public Class Circle
Inherits Shape
Public Sub New(ByVal radius As String)
m_radius = radius
End Sub
Private m_radius As Integer
Public Property radius() As Integer
Get
Return m_radius
End Get
Set(ByVal value As Integer)
m_radius = value
End Set
End Property
Public Sub area()
Dim area As Integer
area = radius ^ 2 * 3.14
End Sub
Sub New()
MyBase.New()
End Sub
Public Function mystringcircle() As String
Return ("Circle at (" & xvalue & ", " _
& yvalue & ") with radius " & radius & " has area " _
& radius ^ 2 & 3.14)
End Function
End Class
Public Class square
Inherits Shape
Public Sub New(ByVal length As String)
m_length = length
End Sub
Private m_length As Integer
Public Property length() As Integer
Get
Return m_length
End Get
Set(ByVal value As Integer)
m_length = value
End Set
End Property
Public Sub area()
Dim area As Integer
area = length * length
End Sub
Sub New()
MyBase.New()
End Sub
Public Function mystringsquare() As String
Return ("Square at (" & xvalue & ", " & yvalue _
& ") with length " & length & " has area " & length * length)
End Function
End Class
Public Class cylinder
Inherits Circle
Public Sub New(ByVal height As String)
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
Public Sub volume()
Dim volume As Decimal
volume = radius ^ 2 * 3.14 * height
End Sub
Sub New()
MyBase.New()
End Sub
Public Function mystringcylinder() As String
Return ("Cylinder at (" & xvalue & ", " & yvalue _
& ") with radius " & radius & ", height " & height _
& " has volume " & radius ^ 2 * 3.14 * height)
End Function
End Class
Please let me know if you have any advice. Thanks so much!