Results 1 to 13 of 13
  1. #1
    pheonix is offline Member
    Join Date
    Apr 2008
    Posts
    42
    Rep Power
    0

    Default Inheritance and (super)

    Hey guys. I wanted to ask about after I derive a class from another class ( super class from base class) and when writing the constructor of the new derived class sometimes its like this

    public derivedClass(String name, int number , double something){

    Super(name)

    -----//rest of programing
    }
    What does it mean ? I never knew that ?

    Also
    ------------------------- Q# 2 different- - - -- ------------------------
    What if I wanted java to pick one string from a multiple of strings at random and each time it loops it chooses something else ?
    please give examples >> this is NOT an assignment or anything I just want to know

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    to pick one string from a multiple of strings at random and each time it loops it chooses something else
    Two parts to this:
    One is to pick a String at random.
    Second is to check that this choice is not the same as a previous choice until all the strings have been used.

    Put the Strings in an array
    Use Random to generate an index into the array.
    Have a second, parallel array to keep track of what String has been chosen previously.

    I'll leave the coding to you.

  3. #3
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    You can put the strings into a List, e.g.:

    LinkedList<String> list;

    and use:

    Collections.shuffle(list);

    then you could obtain an iterator over the list to get the strings.

  4. #4
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    Quote Originally Posted by pheonix View Post
    Hey guys. I wanted to ask about after I derive a class from another class ( super class from base class) and when writing the constructor of the new derived class sometimes its like this

    public derivedClass(String name, int number , double something){

    Super(name)

    -----//rest of programing
    }
    What does it mean ?
    The Super calls the constructor of the superclass. In this case, it would use the constructor that takes the one string argument.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    its super not Super.

  6. #6
    pheonix is offline Member
    Join Date
    Apr 2008
    Posts
    42
    Rep Power
    0

    Default

    Norm : Your idea is great can I get an example please by using random?

    Paul Richards : I really didn't understand your idea. is it a file i/o ??

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    The idea here is for us to help YOU learn java. You write the code and when you have problems, bring them here and we'll help you get them working.
    I've given you the steps. Write the code for each step and come back when you have problems.

  8. #8
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    Quote Originally Posted by pheonix View Post
    Paul Richards : I really didn't understand your idea. is it a file i/o ??
    No, it is not!

    Java Code:
    String[] strings = {"apple", "banana", "clementine", "damson"}; //create an array
    List<String> list = Arrays.asList(strings); //copy this into a list
    Collections.shuffle(list); //shuffle the list into a random order
    for(String s: list) { //work through the list one by one
        System.out.println(s); //for demonstration purposes, print out each string
    }
    If your data is in a list already (e.g. LinkedList or ArrayList), you can skip the first two lines.

  9. #9
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post super

    Java Code:
    public BaseClass
    {
       BaseClass(String name, int number , double something){
          super();// if extends Object, uses default constructor probably
          System.out.println("//rest of programing");
          System.out.println(number);
          }
    }
    // default access, aka file scope.
    DerivedClass{
       DerivedClass(String name, int number , double something){
           super(name, number, something);
          System.out.println("Restless programmers.");
          System.out.println(something);
       }
       public static void main(String[] args)
       {
           DerivedClass dc = new DerivedClass("Hello, World", 4752 ,5.6705670567056705670567056705671);//
       }
    }
    Quote Originally Posted by pheonix View Post
    Hey guys. I wanted to ask about after I derive a class from another class ( super class from base class) and when writing the constructor of the new derived class sometimes its like this .... What does it mean ? I never knew that?
    It is that super must be the first word in the constructor, if you do not place it there - the compiler will place a call to the base class constructor, I think it is the default constructor. That makes problems for first time coders as the compiler will not make a default constructor if you have defined a constructor taking types.
    Quote Originally Posted by pheonix View Post
    What if I wanted java to pick one string from a multiple of strings at random and each time it loops it chooses something else ?
    please give examples >> this is NOT an assignment or anything I just want to know
    Where are you getting your strings, can they be just anything?
    Last edited by Nicholas Jordan; 10-19-2008 at 04:50 PM.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  10. #10
    pheonix is offline Member
    Join Date
    Apr 2008
    Posts
    42
    Rep Power
    0

    Default

    Norm : :P I your idea was great and u r right , but I really didn't understand when u said >> Use Random to generate an index into the array << I never heard of random before :P

    Paul Richards : THANK YOU so much :) I'm going to try your code with my own strings and my own coding to c how it works

    Nicholas Jordan : Yes it can be any string .. Its not an assignment I just want to know how to do it :) thanx a lot

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    I never heard of random
    Random is one of the classes in Suns' java API. If you don't have a copy of the API doc you must get one.

    Using a built in function bypasses the learning and experience a student gets from writing his own code.

  12. #12
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Exclamation Random Strings...

    Quote Originally Posted by pheonix View Post
    Nicholas Jordan : Yes it can be any string .. Its not an assignment I just want to know how to do it :) thanx a lot
    Java Code:
    import java.security.SecureRandom;// Better randomizer....
    // This code is a discussion in a technical forum and is not complete.
    public class Flight_of_the_Phoenix 
    {
        // A cryptographically strong random number generator (RNG).
        private static final String randomizerBase = "SHA1PRNG":
        //
        SecureRandom random;
        // Something handy, I was working on this yesterday.
        private static final Integer a = new Integer(java.lang.Character.getNumericValue('a')); //
        private static final Integer z = new Integer(java.lang.Character.getNumericValue('z')); //
    
        // default constructor:
        public Flight_of_the_Phoenix 
        {
            random = SecureRandom.getInstance(randomizerBase);
        }
        String getString(int length)
        {
            String aString = "Gentlemen, I have been examining this aeroplane.";//
            //
            if(length > 0)
            {
                StringBuffer Survivors_Wait_For_Rescue = new StringBuffer(length);
                Integer range = new Integer(z.intValue() - a.intValue());
                while(length-- > 0)
                {
                    char nextCharacter = (char) a.intValue() + random.nextInt(range.intValue());//
                    Survivors_Wait_For_Rescue.append(nextCharacter);
                }
                return Survivors_Wait_For_Rescue.toString;
            }
            else
            {
                return aString;
            }
        }
        public static void main(String[] args)
        {
            Flight_of_the_Phoenix flight_of_the_phoenix = new Flight_of_the_Phoenix();//
            System.out.println(flight_of_the_phoenix.getString(80));
        }
    }
    Or at least it is something like that. You will hear of a speculative notion that if enough monkeys type at enough keyboards long enough, they will issue sensible works. The theory is flawed, combinatorics will show with sufficient mathematical rigor that for even modest sized works the possible combinations exceeds the particle count for the observable universe.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

Similar Threads

  1. Super CSV 1.20
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-27-2007, 08:22 PM
  2. Super CSV 1.15
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 10-16-2007, 06:37 PM
  3. error with super.xxxxx
    By Rgfirefly24 in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:13 AM
  4. Use super. or this.
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 06:52 AM
  5. difference between this and super
    By mrark in forum New To Java
    Replies: 1
    Last Post: 06-27-2007, 05:23 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
  •