Posted by: redlotus March 14, 2009
C # help(new to c#)
Login in to Rate this Post:     0       ?        

Here's what i did and its not giving me right answer, question is below: this is my first class so having hard time. pls help

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

namespace assignement1_nepal
{

    class PhoneCall
    {
        public:
        double min;
        double totalcost;
        const double unitcost = 0.10;
        
        void NumberOfMin()
        {
            Console.WriteLine(" Enter min:");
            min = Convert.ToDouble(Console.ReadLine());
        }

        Double CalculateCost(double min)
        {
            totalcost = min * unitcost;
            return totalcost;
        }

        void Output()
        {
            Console.WriteLine(" total cost:{0}", totalcost);
        }

    }
    class threecalls
    {
        static void Main(String[] args)
    {
        
        PhoneCall callone = new PhoneCall();
        for(int i=0; i<3;i++)
        {
        callone.NumberOfMin();
        callone.Calculatecost();
        callone.Output();


        }
    }
    }

}

question:

You may look up similar programs on the web or in
your books, but be sure you use your code for this project. Commenting
blocks of code in the real world is usually sufficient. Document enough
to show that you understand what is going on.

  1. PhoneCall class with a public data member that contains the number of minutes in a phone call.
  2. Public class method to get the number of minutes.
  3. Public class method to calculate the cost based on 10 cents per minute
  4. Public class method to output the total cost of the phone call.
  5. Main( ) function that instantiates one PhoneCall object, and includes a for loop to calculate the cost of three
    phone calls. The next chapter covers looping, however, you should be
    able to build a for-loop to accomplish the three phone calls. A
    for-loop in C# is no different than a for-loop in C++ or Java.
  6. Internal Documentation

Here is an example of a class definition:

namespace ConsoleApplication1
{
/*
Introduction to Classes
*/

class Product
{
public:
int productIdNum;
string productName;
double productPrice;

 
    /*

  Note:

  • class is a keyword

  • Product is a name you make up - it should be descriptive

  • A class contains data (data members) and functions (methods)

  • You cannot initialize your data members in the class definition

  • You end the class definition with a semicolon

  Here is an example of the class methods implemented inside of the class structure:

    */

void displayProduct()
{
Console.WriteLine("Product ID Number: {0}", productIdNum);
Console.WriteLine("Product Name: {0}", productName);
Console.WriteLine("Product Price: ${0}", productPrice);
}


void getProductData() //this method gets the data and stores it in the class data members
{
Console.Writeline("Enter the Product name: ");
productName = Console.Readline();
Console.Writeline("Enter the price of the product: ");
productPrice = Convert.ToDouble(Console.Readline());
}

    }	//end of class Product						 

/*
      The C# class Product above is placed outside of the class Program below which is created with the project 
      as an object wrapper for the Main function.

Note:

  • the void is the return type for both methods(in this case we are not returning anything)

  • Console.WriteLine is used to write the string literal and variable value with a newline at the end of the write

  • Console Readline is used to assign the input of the product price into the productPrice variable

  • The product price read from the console is string and must be converted to double before assigning it to the double variable productPrice

Here is an example of how to instantiate an object and call a method:

    */

class Program
{
static void Main(string[] args)
{

Product oneProduct = new Product(); //instantiation of the Book class
oneProduct.getProductData(); //method call
oneProduct.displayProduct(); //method call

}

} //end of function main
} //end of namespace ConsoleApplication1



Read Full Discussion Thread for this article