Overriding method to generate a random radius

So I'm working on this small homework assignment for my Computer Science course. What I'm supposed to be doing here is overriding my drawCircle to provide a radius for my circle between 10 - 100. Anyway I've figured out some of what I need, but I'm having an issue generating the random number. Any insight would be helpful.
public class MyCircle {
    public MyCircle(){
    double radius;
// == Page 365, EX 6.6
    public void drawCircle(Graphics g, int x, int y, int radius, Color color){
        int x1 = x - radius;
        int y1 = y - radius;
        int side = 2 * radius;
        g.setColor(color);
        g.drawOval(x, y, side, side);
   // == Page 365, EX 6.7
    public void drawCircle(Graphics g, int x, int y, int radius){
        //int x1 = x - radius;
        //int y1 = y - radius;
        //int side = 2 * radius;
        //g.setColor(Color.green);
        //g.drawOval(x, y, side, side);
        drawCircle(g, x, y, radius, Color.green);
    public void drawCircle(Graphics g, int x, int y){
        radius = (int)Math.random();
        int x1 = x - (int)radius;
        int y1 = y = (int)radius;
        int side = 2 * (int)radius;
        g.setColor(Color.WHITE);
        g.drawOval(x, y, side, side);
}So as you can seeim using Math.radius to try and do this but it only provides a number between 0.0 and 1.0.

Thanks for the input guys I got it to work, and it might be a little more then what I may have to do to get it to work, but its the only way I could figure it out at this point.
public void drawCircle(Graphics g, int x, int y, Color color){
        // Creates a Random number generater that Generates values from 10 - 100
        Random generator = new Random();
        double radius = generator.nextDouble() * 60 + 10;
        int x1 = x - (int)radius;
        int y1 = y = (int)radius;
        int side = 2 * (int)radius;
        g.setColor(color);
        g.drawOval(x, y, side, side);
    }

Similar Messages

  • How to generate n random dates between 2 time periods

    Hi,
    I'm trying to write a method that generates n random dates between 2
    time periods. For example, lets say I want to generate n random dates
    between Jan 1 2003 and Jan 15 2003. I'm not sure how to go about
    doing the random date generation. Can someone provide some direction
    on how to do this?
    The method signature should look like this.
    public String[] generateRandomDates(String date1,String date2,int n){
    // date1 and date2 come in the format of mmddyyyyhh24miss.
    // n is the number of random dates to generate.
    return dateArray;
    Thanks.
    Peter

    first take a look at the API concerning a source of randomness (Random might be a good guess), then take a look at stuff concerning dates (Date might be cool, maybe you will find some links from there).
    Who wrote this stupid assignment?

  • Generating a Random sequence

    Hello All,
    I want some help writing a method which generates a set of 100 numbers with values 0 and 1, but the input should be percent of number of 1's and 0's...I mean if I give 20% for 0's and 80% for 1's then in the generated sequence I should get 20, 0's and 80, 1's...randomly.....hope Iam clear with my question.....please help me
    Thanks,
    Raghu.

    Here is a utility you can use.. you just need to make the call to get the proper random number of ones and pass the size you want.. it returns an array of strings of ones and zero's but you can convert it to whatever type you need.
    --John Putnam
    import java.util.*;
    * <p>Title: ShuffleBits</p>
    * <p>Description: example of creating a string of 1's and 0's and shuffling them</p>
    * <p>Copyright: Copyright (c) 2003</p>
    * @author John Putnam
    * May be use freely with inclusion of this copyright notice
    * @version 1
    public class ShuffleBits
          * method to create an array of strings of 1's and zeros then shuffle them
          * @param numberOfOnes exatly to include (the rest are zero's)
          * @param maxNumberOfBits total number of ones and zeros.
          * @return randomized list of ones and zeros
      public String[]  randBits(int numberOfOnes, int maxNumberOfBits)
           ArrayList bits  = new ArrayList(maxNumberOfBits);
           for(int i=0;i<numberOfOnes;i++) bits.add("1");
           for(int j=0;j<(maxNumberOfBits-numberOfOnes);j++) bits.add("0");
           Collections.shuffle(bits);  //--do the shuffling of order
           Iterator ib = bits.iterator();
           String[] bitsBack = new String[maxNumberOfBits];
              //--pull out the shuffled bits
           int rb = 0;
           while(ib.hasNext()) bitsBack[rb++] = (String)ib.next();
           return bitsBack;
       } //--end randBits
    //--------------------------------unit testing main--------------------------------
       public static void main(String[] args)
             ShuffleBits test = new ShuffleBits();
             int numOnes;
             int maxNumber;
             String[] bitsBack = null;
             numOnes = 5;
             maxNumber = 15;
             bitsBack = test.randBits(numOnes,maxNumber);
             System.out.println("\n--Test for #ones: " + numOnes + " max: " + maxNumber);
             for(int i=0;i<maxNumber;i++) System.out.println("--I: " + i + " bit: " + bitsBack);
    numOnes = 12;
    maxNumber = 15;
    bitsBack = test.randBits(numOnes,maxNumber);
    System.out.println("\n--Test for #ones: " + numOnes + " max: " + maxNumber);
    for(int i=0;i<maxNumber;i++) System.out.println("--I: " + i + " bit: " + bitsBack[i]);
    numOnes = 12;
    maxNumber = 15;
    bitsBack = test.randBits(numOnes,maxNumber);
    System.out.println("\n--Test for #ones: " + numOnes + " max: " + maxNumber);
    for(int i=0;i<maxNumber;i++) System.out.println("--I: " + i + " bit: " + bitsBack[i]);
    numOnes = 12;
    maxNumber = 15;
    bitsBack = test.randBits(numOnes,maxNumber);
    System.out.println("\n--Test for #ones: " + numOnes + " max: " + maxNumber);
    for(int i=0;i<maxNumber;i++) System.out.println("--I: " + i + " bit: " + bitsBack[i]);
    } //--end main
    } //--end shuffle bits
    The output was.
    --Test for #ones: 5 max: 15
    --I: 0 bit: 0
    --I: 1 bit: 1
    --I: 2 bit: 0
    --I: 3 bit: 0
    --I: 4 bit: 1
    --I: 5 bit: 0
    --I: 6 bit: 0
    --I: 7 bit: 0
    --I: 8 bit: 0
    --I: 9 bit: 1
    --I: 10 bit: 0
    --I: 11 bit: 0
    --I: 12 bit: 1
    --I: 13 bit: 1
    --I: 14 bit: 0
    --Test for #ones: 12 max: 15
    --I: 0 bit: 1
    --I: 1 bit: 1
    --I: 2 bit: 0
    --I: 3 bit: 0
    --I: 4 bit: 1
    --I: 5 bit: 1
    --I: 6 bit: 0
    --I: 7 bit: 1
    --I: 8 bit: 1
    --I: 9 bit: 1
    --I: 10 bit: 1
    --I: 11 bit: 1
    --I: 12 bit: 1
    --I: 13 bit: 1
    --I: 14 bit: 1
    --Test for #ones: 12 max: 15
    --I: 0 bit: 1
    --I: 1 bit: 1
    --I: 2 bit: 0
    --I: 3 bit: 1
    --I: 4 bit: 1
    --I: 5 bit: 0
    --I: 6 bit: 1
    --I: 7 bit: 1
    --I: 8 bit: 0
    --I: 9 bit: 1
    --I: 10 bit: 1
    --I: 11 bit: 1
    --I: 12 bit: 1
    --I: 13 bit: 1
    --I: 14 bit: 1
    --Test for #ones: 12 max: 15
    --I: 0 bit: 1
    --I: 1 bit: 1
    --I: 2 bit: 1
    --I: 3 bit: 1
    --I: 4 bit: 1
    --I: 5 bit: 1
    --I: 6 bit: 1
    --I: 7 bit: 0
    --I: 8 bit: 0
    --I: 9 bit: 1
    --I: 10 bit: 1
    --I: 11 bit: 1
    --I: 12 bit: 1
    --I: 13 bit: 0
    --I: 14 bit: 1
    notice the last few are the same sizes but come out in different order.
    --John Putnam                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Generate the random numbers

    How Do I Generate Random Numbers in iWorks
    Excel has the "F9" key to generate random numbers numbers don't
    please see below what i'm trying to do
    I open up a blank Excel worksheet, and type the number "1" into cell A1. Type the number "2" into cell A2. Then type the number "3" into cell A3. Type the number "4" into cell A4, and then type the number "5" into cell A5.
    2
    Type the word "PB" into cell A6.
    3
    Enter the function "=RANDBETWEEN(1,59)" into cell B1.
    4
    Enter an exact copy of this function "=RANDBETWEEN(1,59)" into cells B2, B3, B4, and B5.
    5
    Enter the function "=RANDBETWEEN(1,39)" into cell B6.
    6
    Hit the "F9" key to generate the random numbers simulating game.
    But in numbers how do i do this?
    Thanks!
    Alex...

    firstly your not asking about generating random numbers, your asking how do you make a workbook recalculate new random numbers. From M$'s website:
    F9
    Calculates all worksheets in all open workbooks
    It does not produce random numbers the equations should produce random numbers as soon as you enter them in. If they dont then you have automatic reclaculations turned off and F9 is forcing a recalc.
    In numbers there is not shortcut to forcing the workbook to recalculate other than enter data into a cell. So if you made a new table and then enter data. For every data point you enter you should see new data apear.
    Was just validating the method for forcing and its not working on my ipad. I will mark this conversation and if i find it i will respond again.
    Jason
    Message was edited by: jaxjason

  • What is happening overriding method  in JVM

    1.what is happeining in JVM while i do overriding method ?
    2. why we cant create object for abstract class. what is happeining in JVM?
    HOW TO I KNOW internal process of JVM above i mentioned . is there any book for this?

    There's plenty of information in the Java Virtual Machine specification available here on this web site.
    http://java.sun.com/docs/books/jvms/second_edition/html/Compiling.doc.html#14787
    regards,
    Owen

  • Override method on an object construted by somebody

         customMenuComponent.addCustomLink(new CustomLinkComponenet("strlink","Storage Inbox",Index.class) {
                   protected BookmarkablePageLink getBookmarkablePageLink() {
                        BookmarkablePageLink bookmarkablePageLink=   new BookmarkablePageLink("link",clazz){
                             protected void onBeforeRender() {
                                  super.onBeforeRender();
                        return bookmarkablePageLink;
              customMenuComponent.addCustomLink(new CustomLinkComponenet("strlink","Storage Inbox",Index.class) {
                   protected BookmarkablePageLink getBookmarkablePageLink() {
                        BookmarkablePageLink bookmarkablePageLink=   super.getBookmarkablePageLink(){
                             protected void onBeforeRender() {
                                  super.onBeforeRender();
                        return bookmarkablePageLink;
              });the second block of code does not compile please explain me why and what is the workaround ? i can override a method on a object when I initialize with new but cannot override if I get the object from super or somebody constructs why ?

    miro_connect wrote:
    in my case is there way to copy object returned from super to object created in sub and override methods ?Perhaps. You need to know about the implementation of the original object.

  • How to generate n random dates

    Can anyone help me in generating Random dates, i want to generate 100 random dates in yyyy-mm-dd format.

    I've tried this, but will appreciate any neat solutions...
    public static void main(String arg[]) {
        int no = 0;
        while (no < 100) {
          // Year
          int yylower = 1970; // your lower integer value
          int yyupper = 2000; // the larger one of your two integers
          double rand = Math.random();
          int yyresult = yylower + (int) ( (yyupper - yylower) * rand);
          // Month
          int mmlower = 1; // your lower integer value
          int mmupper = 12; // the larger one of your two integers
          rand = Math.random();
          int mmresult = mmlower + (int) ( (mmupper - mmlower) * rand);
          // Month
          int ddlower = 1; // your lower integer value
          int ddupper = 29; // the larger one of your two integers
          rand = Math.random();
          int ddresult = ddlower + (int) ( (ddupper - ddlower) * rand);
          System.out.println(yyresult  + "-" + mmresult + "-" + ddresult);
          no++;
      }

  • How to generate pseudo random noise(PRN) binary sequence using shift registers?

    what is the block diagram to generate pseudo random noise (PRN) binary sequence of 1's and 0's using shift registers
    please help
     i need 2 submit this project in this week

    This is a fairly simple homework problem. My hints (on the LabVIEW side):
    1- look at while loops
    2- look at shift registers on while loops with multiple input elements
    3-look at the logic components, AND, OR XOR
    Look at this wiki article: http://en.wikipedia.org/wiki/Linear_feedback_shift_register
    Do the work, you won't learn if someone shows you how it is done! The palette that the previous post points to are of PRN and other "noise generators" but I suspect that is not what you are after.
    Know that creating a PRN with a reasonable number of shift register taps produces very pseudo results, as the "random" pattern begins to repeat pretty quickly, thereby making it not a truly random number. 
    When you have an attempt that is sort of working and want additional suggestions, post the code here. We won't do your work, we will help you try to do it, and learn it, yourself.
    Putnam
    Certified LabVIEW Developer
    Senior Test Engineer
    Currently using LV 6.1-LabVIEW 2012, RT8.5
    LabVIEW Champion

  • Urgent How to generate a random code such as registration code?

    I would like to ask that how to generate a random code?
    For example, when you register a E-mail account, you need to enter the code for activation. I would like to create a java Bean to do this job. Would you help me?

    In fact, I would like to set a 10 digi codes randomly (such as FH654CS081, MKO624VG9f) for user input when they register the forum account. This random code will be sent to the user's email. User have to input this code to a JSP web site. Also, the code which is inputed by user will be matched with the database. If user input success, the forum account will be activated.
    By generating this 10 digi codes, I want to handle this generation by using java bean.
    1) Any thing else i need to import ? (such as <% page import="java.......?")
    2) Any functions can handle this task?
    3) Any example can be my reference?
    Thank you very much

  • How to update payment method,once generated payment doc through F-58.

    Hi Experts,
    I want to update payment method,once generated payment doc through F-58.
    1)Maintained payment method in XK01
    2)Maintained the same in Invoice Number
    But, i am not able to generate payment method in payment doc through f-58.
    It can possble through F110,but why not in F-58.
    Please let me know,whether is it possible or not.
    Thanks
    Kishore.J

    Hi Ravi,
    Thanks for your reply.
    I gave my payment method,bank and check lot details in F-58 and generated payment document f-58.But,i am not able to find payment method in payment document.
    Thanks
    Kishore.J

  • Some generic anonymous class overriding methods compile, while others don't

    I have the following (stripped-down) code. The abstract class SessionHandler<T> is used by other code to define and run an operation that needs a session to something. This is best done in my code with anonymous classes, because of the shear number of operations defined. In the EntityOps<T> class, these work great. But, in the last class shown here, SomeClass, the anonymous class definition fails, though the semantics are almost identical. (List<T> vs.List<AnotherClass>) What am I doing wrong here? Or is this a bug in Java?
    Thanks, Tom
    public interface IEntityOps<T> {
        T get();
        List<t> getAll();
    public abstract class SessionHandler<T> {
        abstract T handle(Session session) throws Throwable;
        public final T perform() {
            ... calls handle(session) ...
    // These anonymous class definitions compile fine!
    public class EntityOps<T> implements IEntityOps<T> {
        public T get() {
            T ret = null;
            ret = new SessionHandler<T>() {
                T handle(Session s) throws Throwable {
                    T ret = (some T object calculation);
                    return ret;
            }.perform();
            return ret;
        public List<T> getAll() {
            T ret = null;
            return new SessionHandler<List<T>>() {
                List<T> handle(Session s) throws Throwable {
                    List<T> ret = (some List<T> calculation);
                    return ret;
            }.perform();
    // This anonymous class definition fails with the error:
    // "SomeClass.java": <anonymous someMethod> is not abstract and does not override abstract method handle()
    //     in SessionHandler at line XX, column XX
    public class SomeClass {
        public List<AnotherClass> someMethod() throws {
            List<AnotherClass> ret = null;
            ret = new SessionHandler<List<AnotherClass>>() {
                List<AnotherClass> handle(Session s) throws Throwable {
                    List<AnotherClass> ret = (some List<AnotherClass> calculation);
                    return ret;
            }.perform();
            return ret;
    }

    I added @Override above the abstract method override, and it provides this additional error:
    "HousingConfigImpl.java": method does not override a method from its superclass at line 382, column 17
    I have also reconstructed the code layout in a separate set of classes that have no dependancies, but there's no error coming from these!
    public class CustomThing {
    public interface ISomeInterface<T> {
        List<T> interfaceMethod();
    public abstract class SomeAbstractClass<T> {
        private Class _c = null;
        public SomeAbstractClass(Class c) {
            _c = c;
        protected Class getC() {
            return _c;
        public abstract T methodToOverride(Object neededObject) throws Throwable;
        public final T finalMethod() {
            try {
                return methodToOverride(new Object());
            } catch(Throwable e) {
                throw new RuntimeException(e);
    import java.util.List;
    import java.util.Collections;
    public class SomeInterfaceImpl<T> implements ISomeInterface<T> {
        public List<T> interfaceMethod() {
            return new SomeAbstractClass<List<T>>(CustomThing.class) {
                public List<T> methodToOverride(Object neededObject) throws Throwable {
                    return Collections.emptyList();
            }.finalMethod();
    import java.util.Collections;
    import java.util.List;
    public class SomeOtherClass {
        public List<CustomThing> someMethod() {
            return new SomeAbstractClass<List<CustomThing>>(CustomThing.class) {
                public List<CustomThing> methodToOverride(Object neededObject) throws Throwable {
                    return Collections.emptyList();
            }.finalMethod();
    }So, there's something about my code that causes it to be, somehow, different enough from the example provided above so that I get the error. The only differences in the override method definitions in my actual code are in the return type, but those are different in the example above as well. Here are the class declarations, anonymous abstract class creation statements, and abstract method declarations from the actual code.
    public abstract class SessionHandler<T> {
        abstract T handle(Session session) throws Throwable;
    public class EntityOps<T> implements IEntityOps<T> {
                return new SessionHandler<List<T>>(_mgr, _c, "getAll" + _c.getName()) {
                    List<T> handle(Session s) throws Throwable {
    public class HousingConfigImpl implements IHousingConfigOperations, ISessionFactoryManager {
                ret = new SessionHandler<List<Tenant>>((ISessionFactoryManager)this, Housing.class, "getTenantsInNeighborhood") {
                    List<Housing> handle(Session s) throws Throwable {I can't for the life of me see any syntactical difference between my example and the real code. But, one works and the other doesn't.

  • Method to Generate Report...??

    Hello, i want to know is that any method that can generate bar chart with jsp, mysql, tomcat??
    I tried to search from internet, and i found out SwissChart is one of the method to generate graph... but i tried to follow the step of installation n the setting also..... it doesn't work....
    can any one pls help me to get out of this problem?? it quite urgent for me to finish my project..... a prompt reply will be appreciated......
    thank you....
    from bscs

    may be this website will be useful for you
    http://www.jfree.org/jfreechart/index.php

  • How do I generate a random String

    Hi, there is probably a very simple answer to this question, but nevertheless....
    Is there a class I can use which will allow me to generate a random String of any length?
    Many thanks.

    depending on which chars you want to use for the String, sometimes it is more handy to use a char[], something like
    publc static final char[] CASE_SENSITIVE_ALPHNUM = {'a', ...., 'A',....'1',...};//for alphamumeric case sensitive chars
    public static final char[] CASE_INSENSITIVE_ALPH = {"a", ....,'z'};
    public String randString(int length, char[] charset) {
    StringBuffer sBuf = new StringBuffer();
    for (int i = 0; i < length; i++ ) {
    sBuf.append(charset[(int)(Math.random() * charset.length)] )
    return sBuf.toString();

  • Function to generate a random ID

    Hello all,
    Do we have any inbuilt function in SAP to generate a random alphanumeric ID?
    The requirement is such that there is an import parameter "Sales Rep ID" which is optional. So if the user is not providing the ID, then we have to randomly generate it ourselves. Hence I am looking for a FM which does that job.
    Regards,
    Abhishek

    look at the function modules in F052, rthere yiou can generate random value for any data type.
    RANDOM_AMOUNT
    RANDOM_C
    RANDOM_C_BY_SET
    RANDOM_F8
    RANDOM_I2
    RANDOM_I4
    RANDOM_INITIALIZE
    RANDOM_P
    RANDOM_TABLE_ENTRY

  • How to generate a random session variable in php

    i want to generate a random session variable and insert the variable in a mysql record to use later to validate an account set up.
    person fills out form to create account and submits; inserts form information in mysql record.
    i want the random variable to be inserted from a hidden field and the page sends an email with a link to click on to compare the variable to validate the user.
    Not sure how to generate a random session variable and get that to the hidden field value to be inserted with the other form information.
    thanks for your help,
    Jim Balthrop

    To insert the key I would personally do something like...
    $key = md5($username . $password . $salt);
    Insert that into your MySQL database, then send them a email with it, my next code shows how to activate it.
    This is to activate the account.
    <?php
    $key;
    $errors = array();
    if(isset($_GET['key']){
         $key = $_GET['key'];
         $sql = 'SELECT * FROM users WHERE key = \'' . $key '\' LIMIT 1';
         $result = mysql_query($sql) or die(mysql_error());
         if(mysql_num_rows($result)){
              $sql2 = 'UPDATE users SET active = 1 WHERE key = \'' . $key '\' LIMIT 1';
              $result2 = mysql_query($sql2) or die(mysql_error());
              if($result2){
                   //successfully activated account
              else{
                   //Something Went Wrong!
         else{
              $errors[] = 'Invaild Key, Please try again!';
    else{
         $errors[] = 'Invaild Key, Please try again!';
    ?>

Maybe you are looking for

  • Report of Changes made to Cusotmer requested and Actul Committed date

    Hi, Please guide me for the preparation of a report of Changes made to Customer requested date and Actual delivery Date. Which field should I take for the Actual delovery date and the field for Customer requested date. Finally the Tables for these Fi

  • Using variables for column headings in REUSE_ALV_GRID_DISPLAY

    Hi Everyone,            Is it possible for me to display the column heading in REUSE_ALV_GRID_DISPLAY using a variable?   CLEAR ls_fieldcat.   ls_fieldcat-col_pos         =   13.   ls_fieldcat-reptext_ddic      =  LWOP1.  " <===Here is my variable fo

  • Enabling SSL on SAP NetWeaver 7.0 ABAP Trial Version

    Hello, I installed the SAP NetWeaver 7.0 ABAP Trial Version and am trying to use the class cl_http_client to GET an xml file over ssl. The method cl_http_client_receive returns error "http_communication_failure", the get_last_error method returns "11

  • Creating Animated lines and text on HD timeline... converting to SD DVD

    Hi, we are filming with XDCAM EX1 cameras, with the end result to be output as NTSC DVD tutorials, Online, DVD-ROM, and Ipod/WMV cross platform. The project requires (at this point) an animated music score that scrolls from left to right on an off wh

  • Changing The Default Window Icons

    Is there a way to change the MDI Window Icon when the Form is run on the web. Changing the icon in client/server seems to work well with the following (attached d2kwutil.pll): declare a pls_integer; begin a := win_api_utility.get_active_window(false)