954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with property in method

Hi I have this project I'm working on and I'm totally stuck.

Things is that we need to do the following:
A - Write a class, Sphere, which has one private field of the type integer which represent the radius of the sphere.
B - Write a default, empty, constructor and a constructor that takes one parameter, the sphere’s radius.
C - Write a property that allows the class user to modify the radius.
D - Write methods that calculate the volume of the sphere.

Well i have started and I think I got it right until part D, I dont know how to use the method i get from the user in a metod. How to pass it's value into the metod, run the calculation and then return it to the user.

Here's my code

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

namespace ConsoleApplication1
{
    class Sphere
    {

        
        public int sphereradius
        {
            get
            {
                return userradius;
            }
            set
            {
                userradius = value;
            }
        }


        public Sphere()
        {

        //Empty constructor

        }

        public Sphere(int sphereradius, int userradius)
        {

        //Constructor that takes the sphere radius parameter
        
        sphereradius = int.Parse(Console.ReadLine());

        }

        public override string ToString()
        {
            return "" + sphereradius;
        }
        
        static void Main(string[] args)
        {
         //Creates object   
         Sphere sphereradius = new Sphere();
         //Takes user value
         Console.Write("Enter radius: ");
         sphereradius.userradius = int.Parse(Console.ReadLine());
         // Checks userinput
         Console.WriteLine("Sphereradius details; {0}", sphereradius);
         
        //Calculate volume of sphere
        
        }
        
        static void calculatevolume(int sphereradius)
        
        {
            
        //This is were i cant get the value from the user input to be calculated...?!?
        //as you can see Iv'e tried to send it into the method.
        
        }


    }
}
kalle82
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

You have C right, the rest are wrong. None of the class methods should have Console anywhere in them.

A) private variable that store radius. You don't have one
B) You have an empty constructor, but where is the one that take ONE parameter (and stores it in the private variable, that's what the private variable is for).
C) You have this right but, usually, properties begin with an upper case character (style issue)
D) You have no method that calculates the volume of the sphere and returns that value (something like public double Volume() {

Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

You can not have two radii. A shpereradius and a userradius are confusing. A sphere just has ONE radius, which has to be a private property of you Sphere class.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 
class Sphere
    {
        /// <summary>
        /// A - Write a class, Sphere, which has one private field of the type integer which represent the radius of the sphere.
        /// </summary>
        private int radius;

        /// <summary>
        /// B - Write a default, empty, constructor and a constructor that takes one parameter, the sphere’s radius.
        /// </summary>
        public Sphere()
        {
            //set default for radius, say maybe one or zero
            radius = 1;
        }

        /// <summary>
        /// B - Write a default, empty, constructor and a constructor that takes one parameter, the sphere’s radius.
        /// </summary>
        /// <param name="radius">a radius you optionally get to initialize your object</param>
        public Sphere(int radius)
        {
            //now that you got it, what do you wanna do with it? test it f its valid?
            //or just use it? thats besides the point, what matters is when csc reaches here, you got it
            //i'm just gonna do this
            this.radius = radius;
        }

        /// <summary>
        /// C - Write a property that allows the class user to modify the radius.
        /// </summary>
        public int Radius
        {
            get { return radius; }
            set { radius = value; }
        }


        /// <summary>
        /// D - Write methods that calculate the volume of the sphere.
        /// Just wrote it wrong, just demonstrating, i don't do math so i dnt even knw a single applicable math
        /// you will change it to whateva you have to
        /// </summary>
        /// <returns>a double valume value for your Sphere</returns>
        public double CalcVolume()
        {
            return radius * Math.PI;

            //thus after initializing an object of this type
            // you can simply call this method as
            //Sphere myS = new Sphere(25);
            //Sphere mySS = new Sphere();

            //double vol1,vol2;
            //vol1 = myS.CalcVolume();
            //etc
        }


    }


This should help, and in future, try read and understand the problem domain then tickle each one by one in an OOP manner. that will better help you solve your coding problems faster and easier.Please marks this post solved if you got what you needed

mshauny
Junior Poster in Training
62 posts since Jan 2010
Reputation Points: 35
Solved Threads: 6
 

@mshauny: Nice effort:yawn: Now all the OP has to do is copy and paste and in the process of doing so will have learned nothing.
BTW maby the math in your country is quite different than in mine, but here we calculate the volume of a sphere as being :
( 4 / 3 ) * Math.PI * Math.Pow( radius, 3.0)
or
( 4 / 3 ) * Math.PI * radius * radius * radius

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

There are somethings i did wrong in the code. And yes i made a mistake showing him the full code, but he will learn from it atleast, just this once let him learn from other's work. I strongly believe people can benefit alot from open sourced codes, we should'nt be too hash on them just because we learned programing the hardway, besides its now pointless to hide code from a Windows .NET developer using tools like VS because it virtually explains everything to them. I'm sorry though, thats just how i teach my fellow students.

On the Math part, Its not about country to country application. I'm not a math person indeed. Thanks for the Volume formular, i just did a dummy calculation to demonstrate a point not to be precise.

Thanks for your input guys

mshauny
Junior Poster in Training
62 posts since Jan 2010
Reputation Points: 35
Solved Threads: 6
 

No harm done mshauny. :)
But you should better have replaced your "calculation" with something like:
//calculation of volume done here
Happy computing!

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: