Click here to Skip to main content
15,169,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1. Create a console application.
2. Add a public class called compute_product.
3. In compute_product declare two private fields (class variables) named value1 and value2.
4. For field (class variable) value2 create a public property named Value2 and implement the get and set.
5. Add a constructor to assign a value to the value1 field by passing an integer value to the constructor when the constructor is instantiated in Main.
6. Add a method named public void display_product_calc() and use a Console.WriteLine to display the result of multiplying value1* Value2 to the console.
7. In Main declare a variable named myValue1 and assign the value 5 to it.
8. In Main instantiate a compute_product object named myproduct and pass myValue1 in the constructor parameter list.
9. In Main use the Value2 class property to set the value of class variable value2 to integer 10.
10. Call the compute_product class display_product_calc method from Main; the displayed result will be 50.

The output would look like this:

The product is 50
Press any key to continue . . .

What I have tried:

C#
class Program

{
  
  static void Main()
  
  {
     
       
 compute_product cp=new compute_product();
       
 cp.val1=5;
val2=10;

display_product_calc();
Console.WriteLine("The product of two numbers is:\n" display);
      
   
 }

}

public class compute_product
{

    private int val1;

    private int val2;

    public int val2
    {

        get
        {

            return val2;

        }

        set
        {

            val2 = 10;

        }

    }


    public void display_product_calc()
    {
        int result;
        result = (val1 * val2);
        return result;
    }
}
Posted
Updated 27-Mar-16 14:59pm
v2
Comments
CHill60 26-Mar-16 18:00pm
   
What is your question?
Member1345 26-Mar-16 18:59pm
   
What is your question?
Member 12418355 26-Mar-16 23:27pm
   
can anyone write the correct code plz

Let's see..

1. Create a console application.
Looks like you did that. Is there a namespace in your file which you just didn't post here?

2. Add a public class called compute_product.
You did that.

3. In compute_product declare two private fields (class variables) named value1 and value2.
You sort of did that - but if your teacher is picky I'd rather name the fields exactly like he wanted you to.

4. For field (class variable) value2 create a public property named Value2 and implement the get and set.

Now it get's interesting. Did you recognize the difference between the field and property name in the assignment? One is lower case, one is upper case. And now your apparent tendency to modify names bites you. In C#, lower case and upper case make a difference. Naming two "things" (fields, properties, methods, classes, ...) exactly the same is an error if they exist within the same scope.
Assigning a constant value (10) to the backing field of a property in its setter makes absolutely no sense. The setter exists to assign a value that gets passed to it from "outside".

5. Add a constructor to assign a value to the value1 field by passing an integer value to the constructor when the constructor is instantiated in Main.
You didn't do that yet.

6. Add a method named public void display_product_calc() and use a Console.WriteLine to display the result of multiplying value1* Value2 to the console.
You added the method.
After fixing point 4) you also need to fix your multiplication expression here.
You attempt to return the result instead of displaying it to the console. Apart from that, you can't return anything from that method because it's declared as void.

7. In Main declare a variable named myValue1 and assign the value 5 to it.
You didn't declare any variable other than cp in Main().
cp.val1 is a private field, you can't assign to it outside of the class compute_product.

8. In Main instantiate a compute_product object named myproduct and pass myValue1 in the constructor parameter list.
You instantiated a compute_product object (but again named it differently).
You didn't pass anything into it's constructor (see point 5).

9. In Main use the Value2 class property to set the value of class variable value2 to integer 10.
val2 is unknown in Main().
After fixing point 4) you can do this properly here.

10. Call the compute_product class display_product_calc method from Main; the displayed result will be 50.
display is an unknown identifier in Main() (but even if it was declared, it wouldn't work like that, you probably forgot a plus sign there).
The console output should not happen in Main() but in display_product_calc().
   
v2
Comments
Garth J Lancaster 26-Mar-16 20:53pm
   
nice work Sascha
+5
Sascha Lefèvre 26-Mar-16 21:14pm
   
Thank you, Garth!
   
::facepalm::
—SA
Sascha Lefèvre 27-Mar-16 3:51am
   
No, I won't do that. Because it would not be helpful for you. You may think it would be helpful but the truth is: If you don't start trying to do this yourself you will never be able to.
   
5ed. Hm... you got patience for that. :-)
Please see the comment above and my reply comment (click) :-)
—SA
   
Then join some non-nonsense league. :-)
—SA
Sascha Lefèvre 27-Mar-16 3:52am
   
Thank you, Sergey :)
BillWoodruff 27-Mar-16 11:10am
   
+5 a great example of guiding, rather than spoon-feeding. some day in the future the OP may appreciate this deeply. cheers, Bill
Sascha Lefèvre 27-Mar-16 11:36am
   
Thank you, Bill! Let's hope so :)
cheers, Sascha
Member 10580998 27-Mar-16 18:06pm
   
I totally agree! :)
Here I done it thank for your support guys,
and thanks for being so mean to me, it helped me alot



public class compute_product
{
private int value1;
private int value2;
public int Value2
{
get
{
return value2;
}
set
{
value2 = value;
}

}
public compute_product(int value)
{
value1 = value;
}

public void display_product_calc()
{

Console.WriteLine("The product is" + value1 * value2);
}
static void Main(string[] args)
{
int myValue1 = 5;
compute_product myproduct = new compute_product(myValue1);
myproduct.value1 = myValue1;
myproduct.Value2 = 10;
myproduct.display_product_calc();
}
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900