Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Creating a new class and dont know how to generate a random number math code

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating a new class and dont know how to generate a random number math code


    I am creating a new class the calculates random integers in simplistic mathematical operations for school. Bellow I have copy/paste my code from JGRASP, I have commented on the two portions that are not performing correctly. The first error message in correlation to the first portion that I commented on as being a mistake states the following:

    PrintingIntCalculator.java:4: error: illegal start of type
    import java.until.Random;
    ^
    PrintingIntCalculator.java:4: error: ';' expected
    import java.until.Random;
    ^
    PrintingIntCalculator.java:4: error: illegal start of type
    import java.until.Random;
    ^
    PrintingIntCalculator.java:4: error: ';' expected
    import java.until.Random;
    ^
    PrintingIntCalculator.java:4: error: <identifier> expected
    import java.until.Random;
    ^
    The second portion in which I commented on (at the end of my code) states the following error message:

    PrintingIntCalculator.java:34: error: ';' expected
    total = ran.nextInt(b-a+1)+a)
    ^
    Both of these are suppose to tell the class that it needs to generate a random number. Could someone please help me correct these problems. It is greatly appreciated.
    Thank you so much in advance




    public class PrintingIntCalculator {

    //This part is wrong:
    import java.until.Random;

    public PrintingIntCalculator(){}


    private int total = 0;
    private int a = 0;
    private int b = 0;

    public int add(int a, int b){
    total = a + b;
    return (total);
    }
    public int subtract(int a, int b){
    total = a - b;
    return (total);
    }
    public int multiply(int a, int b){
    total = a * b;
    return (total);
    }
    public int divide(int a, int b){
    total = a / b;
    return (total);
    }
    public int remainder(int a, int b){
    total = a % b;
    return (total);
    }

    //This part is wrong:

    public int random(int a, int b){
    Random ran = new Random ();
    total = ran.nextInt(b-a+1)+a)
    return (total);
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Creating a new class and dont know how to generate a random number math code

    Missing semicolon?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Creating a new class and dont know how to generate a random number math code

    2 Suggestions. First, suggest the following link for reading up on how to use import statement, especially the part that discusses where to place them
    Using Package Members (The Java™ Tutorials > Learning the Java Language > Packages)
    Second, make sure your lines/code statements end in semicolons - the compiler error point you to the exact line where this happens.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Creating a new class and dont know how to generate a random number math code

    Extra parenthesis?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating a new class and dont know how to generate a random number math code

    What are on the first three lines of your source?
    The error message refers to an import statement on line 4.

Similar Threads

  1. generate a random number from 100 to 150, inclusive in applet
    By chonch in forum Java Theory & Questions
    Replies: 12
    Last Post: February 14th, 2014, 05:44 AM
  2. how to use nextInt() to generate random integers?
    By rph in forum Object Oriented Programming
    Replies: 6
    Last Post: January 3rd, 2013, 02:32 AM
  3. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  4. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM