Results 1 to 13 of 13
Thread: Inheritance and (super)
- 10-18-2008, 05:26 PM #1
Member
- Join Date
- Apr 2008
- Posts
- 42
- Rep Power
- 0
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
- 10-18-2008, 05:47 PM #2to pick one string from a multiple of strings at random and each time it loops it chooses something else
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.
- 10-18-2008, 05:57 PM #3
Member
- Join Date
- Oct 2008
- Location
- UK
- Posts
- 65
- Rep Power
- 0
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.
- 10-18-2008, 05:59 PM #4
Member
- Join Date
- Oct 2008
- Location
- UK
- Posts
- 65
- Rep Power
- 0
- 10-18-2008, 06:01 PM #5
its super not Super.
- 10-19-2008, 02:48 PM #6
Member
- Join Date
- Apr 2008
- Posts
- 42
- Rep Power
- 0
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 ??
- 10-19-2008, 04:06 PM #7
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.
- 10-19-2008, 04:41 PM #8
Member
- Join Date
- Oct 2008
- Location
- UK
- Posts
- 65
- Rep Power
- 0
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 }
- 10-19-2008, 05:29 PM #9
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);// } }
Where are you getting your strings, can they be just anything?Last edited by Nicholas Jordan; 10-19-2008 at 05:50 PM.
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 10-19-2008, 06:23 PM #10
Member
- Join Date
- Apr 2008
- Posts
- 42
- Rep Power
- 0
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
- 10-19-2008, 06:28 PM #11I never heard of random
Using a built in function bypasses the learning and experience a student gets from writing his own code.
- 10-20-2008, 03:49 PM #12
Random Strings...
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)); } }
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 10-20-2008, 04:00 PM #13
Similar Threads
-
Super CSV 1.20
By JavaBean in forum Java SoftwareReplies: 0Last Post: 11-27-2007, 09:22 PM -
Super CSV 1.15
By JavaBean in forum Java SoftwareReplies: 0Last Post: 10-16-2007, 07:37 PM -
error with super.xxxxx
By Rgfirefly24 in forum New To JavaReplies: 1Last Post: 08-06-2007, 09:13 AM -
Use super. or this.
By Marcus in forum New To JavaReplies: 1Last Post: 07-05-2007, 07:52 AM -
difference between this and super
By mrark in forum New To JavaReplies: 1Last Post: 06-27-2007, 06:23 PM
Bookmarks