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.*;
public 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 static 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
public static void main(String args[])
      for (int dieNum=0; dieNum==2; dieNum++)
        Dice.roll();
        Dice_arr[dieNum]=rolled;
       }//end_for_
     return;
    }//end_func_mainI 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..

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

Similar Messages

  • Trying to compile my first Java program !

    Hi,
    I am a new programmer.Looking to clarify a very simple doubt while compiling my first java program.
    I have set up jdk in the following directory on my PC , E:\j2sdk1.4.1_01\bin.
    Now when I create the first AClass.java file in the AClass directory under bin and try and compile it I get the following error.
    E:\j2sdk1.4.1_01\bin>java E:\j2sdk1.4.1_01\bin\AClass\AClass
    Exception in thread main
    java.lang.NoClassDefFoundError: E:\j2sdk1/4/1_01\bin\AClass\AClass
    Kindly help me how to fix this.

    hi,
    Just do this,
    set path & classpath either in the system environment veriable or in the command prompt as shown below.
    set PATH=%PATH%;E:\j2sdk1.4.1_01\bin
    set CLASSPATH=%CLASSPATH%;.
    Be careful, there is a trailing DOT (.) at the end of the classpath, and that is what needed for
    the JVM to find the class file. with this you would be able to run your program. Infact JDK1.4.x and higher
    doesn't requires classpath to be set, your system classpath variable needs a DOT (.)
    at the end of the classpath variable defined in the environment variable section of your computer or
    in the command prompt you need it to set it once for the current command prompt window before
    executing any java program.
    Regards
    Goodieguy

  • My first java programming - Need some help please!!!

    I have to create a simple java program which i am not good at i am ok in C but my java is quite bad. The program need to define a class called Student and with a string called StudentNAme, a integer StudentID(key), An array of 8 integers called Marks that holds the students marks for 8 modules. It also says Include appropriate accessors, mutators and constructor(s) as well as a method to update the student�s marks. Anybody please help me in this question. I prefer an simple example of this program so I can learn faster, I dont even know how to declare a class.. So sorry but please help me out and thanks in advance

    I would also suggest you try using an IDE like
    eclipse (free)
    www.eclipse.org
    This will help you get a working program much faster.But please do also tell them that if they have troubles using it, they're supposed to read the IDE manual instead of posting here.

  • Error Message when running first java program

    Hi,
    I am reading "Sams Teach Yourself Java 6 in 21 Days" by Rogers Cadenhead. I am having a problem with compiling the sample program in the first part of the book, "HelloUser" which is supposed to print out "Hello (my name - Matt)" when run in the command line.
    I receive this error when trying to run it after having successfully compiled it (I enter java HelloUser while in correct folder):
    Exception in thread "main" java.lang.NoSuchMethodError: main
    I have correctly set the PATH and CLASSPATH settings for my PC (Windows XP). I am guessing that I need to define a "main" or something. I'm brand new to Java but am good with the scripting language PHP. Please tell me what I should do to correct this problem. Thanks in advance

    Here is the code for the program:
    public class HelloUser {
         public static void man(String[] arguments) {
              String username = System.getProperty("user.name");
              System.out.println("Hello " + username);
    }Here is what I enter in the Command Line:
    cd \JavaWork\j21work
    javac HelloUser.java
    java HelloUser
    There are no problems up until I enter the last line (java HelloUser) to run the program. By the way, I using JDK 6 update 1 with Windows XP.

  • First Java Programming Assignment - Grad student needs Help

    Thank-you to whomever helps me out here (I have been waiting for the Professor to respond for 2 days now, and our asignment is due by midnight 2-nite. Here is my problem. We have a value called nPer. This value is defined as a double (it needs to since, it perfoms lots of calculations). Now the assigment calls for the print line to display the field in its integer format. So I have done the following:
    nPer=Math.ceil(nPer);
    This has caused the value to be represented as 181.0. Not good enough! The value needs to be truncated and represented as 181.
    The professor has provided us with directions that use the following syntax
    (int)181.0
    Putting that exact code in my program causes JBuilder to create an error message. Ergo, its not right. I have serached all over the tutorial looking for the proper method to resolve this, however - no luck. Could some kind sole please help??? Thank-you.

    You can look at either java.math.BigDecimal, if the
    true representation of the value and rounding are a
    concern, or java.text.NumberFormat if it's simply a
    matter of display.
    Your professor must be an old C programmer. Casting a
    double to an int? Oh, my. - MODBut that's what his assignment says, display it as an int. And the cast would work; I'm assuming he just didn't use it right.

  • My first java program. Design improvements?

    Hello all,
    I'm new to this forum (just joined today) so hi! I've been learning java for the last few months by reading a few books, and finally decided to take a crack at my first program (beyond hello world). This one is windows only. At work with a co worker we found that in windows 2000 the background images could get out of sync, so this program gives the user an alternative way to set the backgrounds.
    What I'm asking is if anyone has any time to kill and enjoys reading source code to take a look at it for me. If you see any issues with how the program was implemented and could be improved please let me know.
    Here's the link to the src: http://will.willandnikki.com/projects/bgchanger/BGChanger_src.zip
    Here's a link to my jar:
    http://will.willandnikki.com/projects/bgchanger/BGChanger.jar
    Thanks,
    Will

    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

  • Programming your first java program, hello world

    go here and do it quick and fun;
    http://www.geocities.com/jon_riley/jon/java.htm

    It is not visual, the same may we learned programming in class.I disagree. The tutorial may not have as big pictures as you have but it sure is visual:
    http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html
    Besides getting the thing done, it explains the common errors you could get, and applies to winNT and 2k (ME, anyone?) as well as win9x. Can you give any reason I should from now on recommend people using 32bit windoze to go to your page rather than the tutorial page?
    By the way, who learned programming in class?

  • My first Java Program

    I will be taking Java in School in a couple of months. I thought it would be a good idea to start trying it out now. One of the books I am reading has this example problem. It basically wants you to draw a hollow square 5 by on the screen. It says to use a while loop and if statements to solve. I don't no where to start, any help/advice would be great.
    Thanks

    The Book "Thinking in JAVA" is a great way to learn the language. The Book is really usefull and explains alot of ideas and concepts.
    Plus its free so you can download it
    http://www.mindview.net/Books/TIJ/
    Good Luck!

  • My first Java program does;'t work HELP

    I received the following message in the Command Prompt:Exception in thread "main" java.land.NoClassDefFoundError: Test
    How do I fix this?
    Source Code:
    import java.awt.MediaTracker;
    class Test {
    public static void main(String[] args) {
    for (int i = 0; i < args.length; i++)
    System.out.print(i == 0 ? args[i] : " " + args);
    System.out.println();

    You might want to make your class public, although I don't think that's supposed to be necessary. It's most likely a classpath issue.
    Instead of java Test, try java -cp . Test (Note the dot between -cp and Test.)
    Javapedia: Classpath
    How Classes are Found
    Setting the class path (Windows)
    Setting the class path (Solaris/Linux)
    Understanding the Java ClassLoader
    java -cp .;<any other directories or jars> YourClassNameYou get a NoClassDefFoundError message because the JVM (Java Virtual Machine) can't find your class. The way to remedy this is to ensure that your class is included in the classpath. The example assumes that you are in the same directory as the class you're trying to run.
    javac -classpath .;<any additional jar files or directories> YourClassName.javaYou get a "cannot resolve symbol" message because the compiler can't find your class. The way to remedy this is to ensure that your class is included in the classpath. The example assumes that you are in the same directory as the class you're trying to run.

  • Trying to get my first java program compile.

    I just iinstalled j2sdk1.4.2_05 on Wind 98 and I have set the path variable in autoexec.bat and ran the .bat.
    So, now the system recognizes the javac compiler but I am still not able to run a program Welcome.java
    I created a Welcome.java file and stored it in C:\java\Welcome.java
    I also set the sourcepath to sourcepath=c:\java; in my autoexec.bat file.
    When I compile the following way, I get the following error:
    c:\javac Welcome.java
    Error: Cannot read: Welcome.java
    Can anybody please tell me what I am doing wrong. Do i need to set any thing else.
    Radhika.

    I got it to compile and run....successfully.
    Problem was- When I created the file Welcome.java in Notepad, it created with the following extension-
    Welcome.java.txt. I renamed it to Welcome.java and it compiled and ran fine.
    Thanks,
    Radhika.

  • One more time, programming your first java program

    go here;
    http://jonjon.0catch.com
    mirror site;
    http://geocities.com/jon_riley/jon/java.htm

    I must assume that your continued use of the forum as a signpost to your home page represents your lack of understanding of the "Terms of Use" agreement that you had to accept to be a member of the forum.
    4.5 You agree not to use the Services for any unlawful activities not otherwise covered above, including (without limitation) attempting to compromise the security of any networked account or site, operating an illegal lottery or gambling operation, stalking, or making direct threats of physical harm. Additionally, You agree not to use the Services to:
    (h) use Your My Sun account as storage for remote loading or as a door or signpost to another home page, whether inside or beyond Sun.com.
    Other sections also apply.

  • Almost done with program, just a little help please

    I am almost done with a program but I just need to have the planet which is _ball be painted on the screen.  I am making a planet that orbits a sun, it all works but the planet is not painted on the screen, but I used a fill method.  Can you guys help?
    public class MovingBall extends SmartEllipse
        int X0 = 100, Y0 = 100;
        double X2, Y2;
        double r = 10;
        double a = 0;
        double da = .04;
        double ecc = 1;
        double TWOPI = 2 * Math.PI;
        private javax.swing.JPanel _panel;
        public MovingBall (java.awt.Color aColor, javax.swing.JPanel aPanel)
         super(aColor);
         _panel = aPanel;
         X2 = X0 + r * Math.cos(a);
         Y2 = Y0 + r * Math.sin(a);
         this.setLocation(X2, Y2);
        public void move()
         a = a + da;
         a = a % TWOPI;
         X2 = X0 + r * ecc * Math.sin(a);
         Y2 = Y0 + r * ecc * Math.sin(a);
         this.setLocation (X2, Y2);
        public boolean inFront()
         return (a <= Math.PI);
        public void SetCircle(int x, int y)
         X0 = x;
         Y0 = y;
         //ecc = 1.0 - (double)pct/100;
        public void setAngleIncrement(double inc)
            da = inc;
        public void setCenter(double g, double s)
    public class MovingBallPanel extends javax.swing.JPanel implements Mover, Controller
        private final int INIT_X = 250;
        private final int INIT_Y = 200;
        private SmartEllipse _sun;
        private Sky _sky;
        private final int SUN_DIAMETER = 60;
        private MovingBall _ball;
        private final int PLANET_DIAMETER = 35;
        private final int ORBIT_DIAMETER = 185;
        int _da = 1;
        int _ecc = 1;
        private final int INTERVAL = 100;
        private MoveTimer _timer;
        public MovingBallPanel()
         super();
         this.setBackground(java.awt.Color.BLACK);
         _sun = new SmartEllipse(java.awt.Color.YELLOW);
         _sun.setSize(SUN_DIAMETER, SUN_DIAMETER);
         _sun.setLocation(INIT_X - SUN_DIAMETER/2, INIT_Y - SUN_DIAMETER/2);
         _sky = new Sky();
         _ball = new MovingBall(java.awt.Color.RED, this);
         _ball.setCenter((INIT_X - PLANET_DIAMETER)/2, (INIT_Y - PLANET_DIAMETER)/2);
         _ball.setAngleIncrement(_da);     
         _timer = new MoveTimer(INTERVAL, this);
         _timer.start();
        public void move()
            _ball.move();
            this.repaint();
        public void go (boolean g)
         if (g) _ball.setAngleIncrement(_da);
            else _ball.setAngleIncrement(0);
        public void setSpeed(int degrees)
         _da = 2 * degrees;
         _ball.setAngleIncrement(_da);
        public void setEccentricity(int pct)
         _ecc = pct;
         //_ball.setEccentricity(_ecc);
        public void paintComponent(java.awt.Graphics aBrush)
         super.paintComponent (aBrush);
         java.awt.Graphics2D betterBrush = (java.awt.Graphics2D) aBrush;
         _sky.paintSky(betterBrush);
         _sun.draw(betterBrush);
         _sun.fill(betterBrush);
         _ball.draw(betterBrush);
         _ball.fill(betterBrush);
    public class PlanetApp extends javax.swing.JFrame
        ControlPanel _controlPanel;
        MovingBallPanel _movingBallPanel;
        public PlanetApp (String title)
         super(title);
         this.setSize(600,500);
         this.setBackground(java.awt.Color.BLACK);
         this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
         _movingBallPanel = new MovingBallPanel();
         _controlPanel = new ControlPanel(_movingBallPanel);
         this.add(_movingBallPanel, java.awt.BorderLayout.CENTER);
         this.add(_controlPanel, java.awt.BorderLayout.SOUTH);
         this.setVisible(true);
        public static void main (String [] args)
         PlanetApp app =  new PlanetApp("Lab 5");
    public class SmartEllipse extends java.awt.geom.Ellipse2D.Double {
        private java.awt.Color _borderColor, _fillColor;  // attributes
        private int _rotation;
        private final int STROKE_WIDTH = 2;
        public SmartEllipse(java.awt.Color aColor){
         _borderColor = aColor;
         _fillColor = aColor;    // solid color to start
         _rotation = 0;         // no rotation for now
        // methods not provided by Java
        public void setBorderColor (java.awt.Color aColor) {
         _borderColor = aColor;
        public void setFillColor (java.awt.Color aColor) {
         _fillColor = aColor;
        public void setRotation (int aRotation) {
         _rotation = aRotation;
        // more readable versions of methods provided by Java
        public void setLocation (double x, double y) {
         this.setFrame (x, y, this.getWidth(),
                     this.getHeight());
        public void setSize (int aWidth, int aHeight) {
         this.setFrame(this.getX(), this.getY(),
                    aWidth, aHeight);
        public void move (int aChangeInX, int aChangeInY) {
         this.setFrame((int)this.getX()+aChangeInX,
                    (int)this.getY()+aChangeInY,
                    this.getWidth(),
                    this.getHeight());
        public void fill (java.awt.Graphics2D aBetterBrush){
         java.awt.Color savedColor = aBetterBrush.getColor();
         aBetterBrush.setColor(_fillColor);
         aBetterBrush.fill(this); // paint a solid ellipse
         aBetterBrush.setColor(savedColor);
        public void draw (java.awt.Graphics2D aBrush) {
         java.awt.Color savedColor = aBrush.getColor();
         aBrush.setColor(_borderColor);
         java.awt.Stroke savedStroke = aBrush.getStroke();
         aBrush.setStroke(new java.awt.BasicStroke(STROKE_WIDTH));
         aBrush.draw(this);
         aBrush.setStroke(savedStroke);
         aBrush.setColor(savedColor);
    }

    ?

  • How to find that a java program is running for the first time on daily basi

    it is like this
    1. i will ran a java program every day and i have to find that it is running for the first time(as i may stop that program and may run again the same day )
    Pls share ur ideas

    san.kumar wrote:
    it is like this
    1. i will ran a java program every day and i have to find that it is running for the first time(as i may stop that program and may run again the same day )
    Pls share ur ideasAs kajbj said - you need to store a token / file with a run date on the file system. Each time the application runs, have it compare with the date from this file with the current date. If different, it's the first run of the day. If not, it is not the first run of the day. Each time have it update the date on the file.

  • My java program runs fine even if i don't specify access specifier of class

    Hi,
    My java program runs fine even if i don't specify access specifier of class as public .
    Then why do they say that atleast one class should be specified as public.
    please help.

    public access specifier is the default access
    specifier
    so if you dont give the access specifier before the
    class name it is not wrong.I think that you are wrong. The default specifier is package or "package-private".
    See here:
    http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html
    Message was edited by:
    petes1234

  • I was almost done my project. I hadn't published it anywhere. Then, the next day, the clips from my timeline went all grey and there was an "out of date" icon. My project was lost. What happened? It's not the first time it's done that!

    I was almost done my project. I hadn't published it anywhere. Then, the next day, the clips from my timeline went all grey and there was an "out of date" icon. My project was lost. What happened? It's not the first time it's done that!

    You can undo your permission changes. Probably the most relevant one is cookies. Try one or both of these methods:
    (1) Page Info > Permissions tab
    While viewing a page on the site:
    * right-click and choose View Page Info > Permissions
    * Alt+t (open the classic Tools menu) > Page Info > Permissions
    (2) about:permissions
    In a new tab, type or paste '''about:permissions''' and press Enter. Allow a few moments for the list on the left to populate, as this information needs to be extracted from a database.
    Then type or paste ''rcn''' in the search box above the list to filter it to the most relevant domains. When you highlight a domain, you can adjust its permissions in the right pane.
    Any luck?

Maybe you are looking for

  • Error encountered during provisioning of user to SAP CUP in OIM 11g R1 BP07

    We recently updated our environment from 11.1.1.5 BP05 to 11.1.1.5 BP07. We are facing an issue with provisioning users to SAP CUP system. We receive an error specifying : Response: oracle.iam.connectors.sap.cup.ws.submitreq.ObjectFactory Response De

  • Changes and Logical Delete in View Maintenance

    Hi everybody. I've defined a table view maintenance V_TABLE for T_TABLE, with five fields. One of them is defined as 'R' (not editable field). I defined an event 05 and an event 02 (for create and save) where I move SY-DATUM to the CREATED_DATE or UP

  • Large .dv file plays fine in Quicktime, but audio out of sync in iMovie??

    hi, i'm trying to convert some old analog 8mm tapes to digital. to do this, i'm using a sony trv320 camcorder with firewire feeding iMovie '08. i've imported an entire 2 hour tape into iMovie, and everything is fine except that at some point, the aud

  • Syncing iPad 2 and also iPhone 4

    Hi I have just bought an iPad 2 and also have an iphone 4. Can anyone tell me if I can sync them both using the same iTunes account even though I want differnt stuff on each of them. Many thanks

  • Oracle 7.3.4.0.2 JDBC Thin Driver for NT 4.0

    I'm unable to download the Oracle 7 JDBC driver even after using the GetRight Download utility. - Oracle 7.3.4.1.0 - jdbc73402-nt.zip - 2/8/02 10am ET - IE 5.0 & Netscape 4.7.3 - Win 2000 Terminal - Error Message : " The document contains no data. "