My first java program!! ALMOST done..I hope
I've just written my first java program (part of a class I'm taking).. It's feeling kinda awkward since I'm a C++ programmer.. I've written most of the code but I'm still having problems and I thought I should show it to you to get some help..
import java.lang.*;
import java.util.*;
publicclass Dice
{
privatestatic Random generator =new Random();
int Die1, Die2, rolled, pairNum, pairSum;
public Dice()
{
int[] Dice_arr={Die1, Die2};
new Random(System.currentTimeMillis());
return;
}//end_func_Dice
publicstatic roll()
{
rolled = generator.nextInt(6) + 1;
return rolled;
}//end_func_roll
public String toString()
{
StringBuffer bfr =new StringBuffer("\n");
System.out.println("Rolling Dice...");
for(pairNum=0; pairNum==10; pairNum++)
{
System.out.println("Pair"+pairNum+": "+Die1+","+Die2+" Sum= "+pairSum);
}//end_for_
}//end_func_
}//end_class_Dice
publicstaticvoid main(String args[])
{
for (int dieNum=0; dieNum==2; dieNum++)
{
Dice.roll();
Dice_arr[dieNum]=rolled;
}//end_for_
return;
}//end_func_main
I need main to instatiate the Dice class and then use the toString to print the dice numbers and their sum 10 times..
I hope this doesn't need much effort,
Thanks a bunch in advance..
[3029 byte] By [
Nectrona] at [2007-11-26 19:53:56]

The roll method does not have a return type. Don't make it static.Create an instance of the Dice class and call its methods.Dice_arr is in your Dice class and is not static (good) so you can't access it from main.
the toString() method is suposed to return a string representing the object Dice as you would like. You are not returning anything there
The main method public static void main(String args[])
must be inside a class, so you should create another class with nothing more than the main method, or put the main method inside the Dice class.
When you declare a method static, you can't use non static variables, so if you will work with members of the class, you shouldn't use the static keyword on the methods...
Like flounder said, instantiate the class with the new
keyword so you can call it's NON static methods.
I'm totally lost.. and my brain stopped functioning..
Right now, my opinion in java is that it's too complicated for no reason compared to C++..
Could someone please fix the problems and post it for me? I don't know what to do after this..
IVE CHANGED THE PROGRAM A LITTLE
import java.lang.*;
import java.util.*;
public static class Dice
{
private static Random generator = new Random();
int Die1, Die2, rolled, pairNum, pairSum;
public Dice()
{
int[] Dice_arr={Die1, Die2};
new Random(System.currentTimeMillis());
return;
}//end_func_Dice
public roll()
{
rolled = generator.nextInt(6) + 1;
return rolled;
}//end_func_roll
public String toString()
{
StringBuffer bfr = new StringBuffer("\n");
for(int dieNum=0; dieNum==2; dieNum++)
{
Dice.roll();
Dice_arr[dieNum]=rolled;
}//end_for_
System.out.println("Pair"+pairNum+": "+Die1+","+Die2+" Sum= "+pairSum);
}//end_func_
}//end_class_Dice
public static main(String args[])
{
int pairNum;
for(pairNum=0; pairNum==10; pairNum++)
{
//Call toString
}//end_for_
return;
}//end_func_main
> Could someone please fix the problems and post it for me?
NO! It is your job to do that. We have given you some pointers but you didn't fix some of them.
The roll method is no longer static but it still doesn't have a return type.
The toString method is supposed to return a String but returns nothing
You still have created an instance of the Dice class. When you have done that you can call its methods.
> Right now, my opinion in java is that it's too
> complicated for no reason compared to C++..
It's not too complicated, it's just that you are new at this.
Here's a little example of a simple class with a simple main method, the rest figure it out yourself, and with time read some java books, you won't learn java just by knowing C++.
public class Human {
// See this as C constants (although they're not their Equivalent)
public static boolean MALE = true;
public static boolean FEMALE = false;
//member variables.
private String name;
private boolean sex;
/** Creates a new instance of Human */
public Human(String n, boolean s) {
name = n;
sex = s;
}//End of CONSTRUCTOR.
public String getName() {
return name;
}//End of getName
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[Name: " + name);
if(sex)
sb.append(", sex: Male]");
else
sb.append(",sex: Female]");
String returnValue = new String(sb);
return returnValue;
}//End of toString()
//Just the main program, it could be at another class.
public static void main(String[] args) {
Human h = new Human("Pablo", Human.MALE);
String theHumanAsAString = h.toString();
System.out.println(theHumanAsAString);
}//End of main method
}//End of class
Are you in a Java class? Do you have a Jave textbook?You lack some basic understanding in regards to Objects and manipulating them. You need to go back and read your course notes/textbook or search the web for some basic tutorials.
can you post the assignment question and how the output should look like? your program have alot of errors. i am fixing please post the question.
i dont see any pairNum method?. i dont see any total sum method? you can always calculate totalsum for the 2 pair of dice which would betotalSum = die1 + die2;anyways post your question before i go to bed.
hope this is what you want. analyze it and learn it.
import java.lang.*;
import java.util.*;
class DiceTester
{
private int die1;
private int die2;
public DiceTester() {
roll();
}
public DiceTester(int val1, int val2) {
die1 = val1;
die2 = val2;
}
public void roll() {
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
}
public String toString()
{
StringBuffer bfr = new StringBuffer("\n");
String value = new String(bfr);
int total=0;
total += die1 + die2;
System.out.println(bfr.append("die1 " +die1+" die2 "+die2+" Sum= " + total));
return value;
}
}
class Dice{
public static void main(String[] args)
{
DiceTester dice1 = new DiceTester();
DiceTester dice2 = new DiceTester();
final int TIRES = 10;
for(int i=0; i<= TIRES; i++)
{
dice1.roll();
dice2.roll();
System.out.print(dice1.toString());
}
}
}
dont forget them dukes...Message was edited by: fastmike
@mike: what's the purpose of making 2 DiceTester instance?
thanks for clearifying i dont know what i was thinking.
import java.lang.*;
import java.util.*;
class DiceTester
{
private int die1;
private int die2;
public DiceTester() {
roll();
}
public DiceTester(int val1, int val2) {
die1 = val1;
die2 = val2;
}
public void roll() {
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
}
public String toString()
{
StringBuffer bfr = new StringBuffer("\n");
String value = new String(bfr);
int total=0;
total += die1 + die2;
System.out.println(bfr.append("die1 " +die1+" die2 "+die2+" Sum= " + total));
return value;
}
}
class Dice{
public static void main(String[] args)
{
DiceTester dice = new DiceTester();
//DiceTester dice = new DiceTester();
final int TIRES = 10;
for(int i=0; i<= TIRES; i++)
{
dice.roll();
System.out.print(dice.toString());
}
}
}
fastmike, thanks a lot for the help.. I really appreciate it..
Even though the version you wrote lacks some of the assignment requirements, I will learn from it what mistakes I was making, and see how the code design runs, and I'll hopefully be able to write one that meets the needs.
I thought java is close to C/C++, I'm enjoying it but it's kinda difficult and confusing when learning a new language.. I just compare everything with what I have in mind from C/C++ and this is what's confusing me..
I was never satisfied with the books computer science teachers choose to teach programming languages.. Please inform me about a good java book (not the teach yourself stuff), I need something comprehensive and presents the language in a clear manner without any extra show off from the authors.
Good luck bro. you will get it for sure. trust me Java is way too much easier than c++. i came from a background of C++ but i started learning java and these forums have helped me alot so always post your question if you have any doubts. Take care.p.s: dont forget them dukes. :)
Personally, I've had fun (and I'm no expert in Java) with Bruce Eckel's Thinking in Java as an intro to the different concepts.
O'Rielly's "Learning Java", though, is an excellent reference, as is "Java in a Nutshell".
Of course, I think I'm the only one out there with love for the O'Riellys...