-
November 18th, 2014, 06:12 PM
#1
**Write a class called die that will hold the number of sides of a die, sides HELP!
Hi again,
So here is another exercise I am attempting:
Write a class called die that will hold the number of sides of a die, sides , and the
current value of a roll, value .
Use the class in Exercise 5 in a program that declares two dice objects. Set values
into the side data members. Set random values into the stored roll values. (See
Listing 5.3 for help with this program.
I know how to setup a class, and I know I have to use point class this time( Listing 5.3 is an example using point class and line class), and perhaps enumerations.
Don't give me the whole answer, just a good hint as how to approach this problem.
I always have difficulty in understanding what to use to meet the requirements of the problem.
Thanks!!
-
November 18th, 2014, 07:03 PM
#2
Re: **Write a class called die that will hold the number of sides of a die, sides HE
See Listing 5.3 for help with this program.
-
November 18th, 2014, 08:08 PM
#3
Re: **Write a class called die that will hold the number of sides of a die, sides HE
Originally Posted by Ashleycsharp
Don't give me the whole answer, just a good hint as how to approach this problem.
A good hint is to start and then post the code you have (with code tags). Nobody here is going to do your homework for you, but we'll guide you if you make an effort and post the code you have.
-
November 19th, 2014, 11:27 AM
#4
Re: **Write a class called die that will hold the number of sides of a die, sides HE
No I was never expecting anyone to do my "homework", I found a the solution at another website.
I just don't know how to approach the problem when it is given.
I sort of understand the solution. but whenever I get a question I can't solve it on my own.
-
November 19th, 2014, 12:40 PM
#5
Re: **Write a class called die that will hold the number of sides of a die, sides HE
OK this is the code I found I am pasted it into VS, and got errors (it won't compile)
Code:
class Die
{
private int sides = 6;
private int myFace;
private Random rNum= new Random();
public int getFace()
{
return myFace;
}
public int Roll()
{
myFace = rNum.Next(sides)+1;
return myFace;
}
public Die(int noSides)
{
if (noSides == 0)
sides = 6;
else
sides = noSides;
myFace = Roll();
}
}
class Program
{
static void Main(string[] args)
{
Die myDie = new Die();
Console.WriteLine("Die value: {0}",myDie.Roll().ToString());
}
return 0;
Console.ReadLine();
}
These are the errors - Error 1 Invalid token 'return' in class, struct, or interface member declaration ( for "return 0 " in the Main function)
- Error 2 Invalid token '(' in class, struct, or interface member declaration ( for the first "(" of console readline )
What do these errors mean?
Actually this isn't for a course, I am learning C# as a hobby.
Thank You Arjay and have a great day.
Last edited by Arjay; November 19th, 2014 at 01:44 PM.
-
November 19th, 2014, 01:49 PM
#6
Re: **Write a class called die that will hold the number of sides of a die, sides HE
When you post code and use code tags, take the time to format it. Formatted code is not only easier to read, but it also helps find bugs.
For example, when I look at the non-formatted code, I can't see what is wrong. When I went back and edited your code to format it (as a mod, I can do that), I immediately spotted the error.
The problem is the closing brace of the static main method is in the wrong place. Instead of:
Code:
class Program
{
static void Main(string[] args)
{
Die myDie = new Die();
Console.WriteLine("Die value: {0}",myDie.Roll().ToString());
}
return 0;
Console.ReadLine();
}
It should be:
Code:
class Program
{
static void Main(string[] args)
{
Die myDie = new Die();
Console.WriteLine("Die value: {0}",myDie.Roll().ToString());
Console.ReadLine();
return 0;
}
}
Also, the return 0; should be the last line.
P.S. As a tip, in Visual Studio you can double-click on an error line and it will take to you the error (well, it won't always get you to the correct line, but it will get you close).
-
November 20th, 2014, 05:22 PM
#7
Re: **Write a class called die that will hold the number of sides of a die, sides HE
Originally Posted by Arjay
When you post code and use code tags, take the time to format it. Formatted code is not only easier to read, but it also helps find bugs.
For example, when I look at the non-formatted code, I can't see what is wrong. When I went back and edited your code to format it (as a mod, I can do that), I immediately spotted the error.
The problem is the closing brace of the static main method is in the wrong place. Instead of:
Code:
class Program
{
static void Main(string[] args)
{
Die myDie = new Die();
Console.WriteLine("Die value: {0}",myDie.Roll().ToString());
}
return 0;
Console.ReadLine();
}
It should be:
Code:
class Program
{
static void Main(string[] args)
{
Die myDie = new Die();
Console.WriteLine("Die value: {0}",myDie.Roll().ToString());
Console.ReadLine();
return 0;
}
}
Also, the return 0; should be the last line.
P.S. As a tip, in Visual Studio you can double-click on an error line and it will take to you the error (well, it won't always get you to the correct line, but it will get you close).
HEllo Arjay,
Not sure what you mean by formatting the code? (I copy and paste from the VS).
Code:
class Program
{
static void Main(string[] args)
{
Die myDie = new Die();
Console.WriteLine("Die value: {0}", myDie.Roll().ToString());
Console.ReadLine();
return 0;
}
}
}
I pasted this in as you recommended, but I still get the following errors:
1. Error 1 'samsex.Die' does not contain a constructor that takes 0 arguments
2. Error 2 Since 'samsex.Program.Main(string[])' returns void, a return keyword must not be followed by an object
Yeah this is strange...
-
November 20th, 2014, 07:47 PM
#8
Re: **Write a class called die that will hold the number of sides of a die, sides HE
Originally Posted by Ashleycsharp
HEllo Arjay,
Not sure what you mean by formatting the code? (I copy and paste from the VS).
Code:
class Program
{
static void Main(string[] args)
{
Die myDie = new Die();
Console.WriteLine("Die value: {0}", myDie.Roll().ToString());
Console.ReadLine();
return 0;
}
}
}
I pasted this in as you recommended, but I still get the following errors:
1. Error 1 'samsex.Die' does not contain a constructor that takes 0 arguments
2. Error 2 Since 'samsex.Program.Main(string[])' returns void, a return keyword must not be followed by an object
Yeah this is strange...
When you paste the code into the code tags, take the time to align the brackets, space the code, etc.
If you aren't doing this [formatting] when you write the code, you should because it makes spotting errors easier.
For the first problem, you have a single die constructor defined as public Die(int noSides). However there is no default constructor like public Die() in your Die class. This is a problem because when you new up the class, you call it as:
Die myDie = new Die();
either you need to create a parameterless constructor or call it like
Die myDie = new Die(6);
For your second problem, main needs to return an int instead of a void. To see how you should define the static main method, create a new C# console project in Visual Studio and see how it creates it for you. You should create it the same way. Take the time to see how Visual Studio does it, and always do it the same way (if you want to create it manually).
-
November 25th, 2014, 01:34 PM
#9
Re: **Write a class called die that will hold the number of sides of a die, sides HE
Originally Posted by Arjay
When you paste the code into the code tags, take the time to align the brackets, space the code, etc.
If you aren't doing this [formatting] when you write the code, you should because it makes spotting errors easier.
For the first problem, you have a single die constructor defined as public Die(int noSides). However there is no default constructor like public Die() in your Die class. This is a problem because when you new up the class, you call it as:
Die myDie = new Die();
either you need to create a parameterless constructor or call it like
Die myDie = new Die(6);
Hello Arjay,
Wouldn't this be my constructor?
Code:
public Die(int noSides)
{
if (noSides == 0)
sides = 6;
else
sides = noSides;
myFace = Roll();
}
If you take a look at the code I pasted in a previous post, its in the die class.
Main returns ' static void Main(string[] args) ' by default when I start a new project .
Thanks.
Thanks for the help once again!
-
November 25th, 2014, 02:36 PM
#10
Re: **Write a class called die that will hold the number of sides of a die, sides HE
Originally Posted by Ashleycsharp
Hello Arjay,
Wouldn't this be my constructor?
Code:
public Die(int noSides)
{
if (noSides == 0)
sides = 6;
else
sides = noSides;
myFace = Roll();
}
If you take a look at the code I pasted in a previous post, its in the die class.
I realize that. Here's the deal... if you don't define any constructors, .net will create a default constructor for you (a default constructor takes no parameters - e.g. Die()). Once you define one or more constructors, you can only invoke the class using the destructors you have defined. You have defined a constructor that takes one int param:
Code:
public Die(int noSides)
Since this is your only constructor, you need to create the class using this constructor:
Code:
Die die = new Die(6);
if you attempt to create the class using a constructor you haven't created, you will get a compile error. For example all of these will give you errors because appropriate constructors haven't been defined:
Code:
Die die = new Die(); // compile error
Die die = new Die(6, 43); // compile error
Die die = new Die(6, "hello"); // compile error
Die die = new Die("world"); // compile error
Originally Posted by Ashleycsharp
Main returns ' static void Main(string[] args) ' by default when I start a new project .
Okay the problem here is that you have a return 0; statement inside the main. Remove it. (that was kind of my fault, because when I initially formatted your code, I didn't notice that you had a return statement in the main that was declared as void).
Code:
class Program
{
static void Main(string[] args)
{
Die myDie = new Die();
Console.WriteLine("Die value: {0}", myDie.Roll().ToString());
Console.ReadLine();
// return 0; // remove this line
}
}
Last edited by Arjay; November 25th, 2014 at 02:38 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|