Unable to use super command - apex-code

I have the following class extension:
public class Compute1 extends Compute_node{
private static final Long[] P = new Long[18];
// Constructor, string key.
public Compute1( String keyStr )
{
super( 0, 8 );
setKey( keyStr );
}
public void setKey( integer key )
{
integer i, j, k;
long data;
integer N = 16;
// Initialize P and S.
for ( i = 0; i < N + 2; ++i ){
P[i] = Pinit[i];
}
// XOR the key into P.
j = 0;
for ( i = 0; i < N + 2; ++i )
{
data = 0;
for ( k = 0; k < 4; ++k )
{
data = ( data << 8 ) ;
++j;
}
P[i] ^= data;
}
}
private static final long[] Pinit = new Long[] {
604135516L, 2242044355L, 320440478L , 57401183L,
2732047618L, 698298832L, 137296536L , 3964563569L,
1163258022L, 954160567L, 3193502383L, 887688400L,
3234508543L, 3380367581L, 1065660069L, 3041631479L,
2420952273L, 2306437331L
};
}
but im getting an error:
Error: Compile Error: Method does not exist or incorrect signature: [Compute_node].(Integer, Integer) at line 11 column 5
at:
super( 0, 8 );
why cant I use the super keyword here?
I am using an extension!
And I do have a Compute_node class!
Thanks

The super() keyword borrows its parent class' constructor. You need to look at Compute_node's constructor and make sure that its parameters match the ones you are passing through super(). For instance, if your constructor in Compute_node is
public Compute_node(int a) {
//code
}
Then you will absolutely get an error saying that the method Compute_node(Integer, Integer) doesn't exist. For a better answer, edit your question with the source to Compute_node.

Related

ORA-06575: Package or function BUBBLESORT is in an invalid state

I've written a bubble sort algorithm in Java and I'm trying to get it to run on Oracle 21c and I get ORA-06575: Package or function BUBBLESORT is in an invalid state
I'll state below the steps that I took in order to get to this error.
The BubbleSort.java file has this contents
public class BubbleSort {
public static void main() {
int[] nums = new int[10000];
for (int i = 0; i < nums.length; i++) {
nums[i] = (int) (Math.random() * 10000);
}
boolean swapped;
int tmp;
do {
swapped = false;
for (int i = 1; i < nums.length; ++i) {
if (nums[i - 1] > nums[i]) {
tmp = nums[i];
nums[i] = nums[i - 1];
nums[i - 1] = tmp;
swapped = true;
}
}
} while (swapped);
}
}
I've loaded it with loadjava and got the following response
loading : class BubbleSort
resolving: class BubbleSort
Classes Loaded: 1
Resources Loaded: 0
Sources Loaded: 0
Published Interfaces: 0
Classes generated: 0
Classes skipped: 0
Synonyms Created: 0
Errors: 0
After that I made a procedure using this statement
create or replace procedure BubbleSort
as language java name 'BubbleSort.main()';
/
When I try to call it using the call BubbleSort(); statement, it throws a ORA-06575: Package or function BUBBLESORT is in an invalid state
As a first debugging step I looked in user_errors and got this
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
;
The symbol ";" was substituted for "end-of-file" to continue.
What should I do to solve this error?
Change the method name to something other than main:
CREATE AND COMPILE JAVA SOURCE NAMED BubbleSortJava AS
public class BubbleSort {
private static final int ARRAY_SIZE = 10000;
public static void sort() {
int[] nums = new int[ARRAY_SIZE];
for (int i = 0; i < ARRAY_SIZE; i++) {
nums[i] = (int) (Math.random() * 10000);
}
boolean swapped;
int tmp;
do {
swapped = false;
for (int i = 1; i < ARRAY_SIZE; ++i) {
if (nums[i - 1] > nums[i]) {
tmp = nums[i];
nums[i] = nums[i - 1];
nums[i - 1] = tmp;
swapped = true;
}
}
} while (swapped);
}
}
/
Then:
CREATE PROCEDURE BubbleSort
as language java name 'BubbleSort.sort()';
/
Then:
SELECT * FROM USER_ERRORS;
Shows no errors, and:
BEGIN
BubbleSort();
END;
/
Works
If you want to pass in an array to sort then:
CREATE TYPE number_list AS TABLE OF NUMBER;
and:
CREATE AND COMPILE JAVA SOURCE NAMED BubbleSortJava2 AS
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Array;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.driver.OracleDriver;
public class BubbleSort2 {
public static Array sort(Array p_values) throws SQLException
{
BigDecimal[] values = (BigDecimal[]) p_values.getArray();
boolean swapped;
BigDecimal tmp;
do {
swapped = false;
for (int i = 1; i < values.length; ++i) {
if (
values[i - 1] != null
&& (values[i] == null || values[i - 1].compareTo(values[i]) == 1)
)
{
tmp = values[i];
values[i] = values[i - 1];
values[i - 1] = tmp;
swapped = true;
}
}
} while (swapped);
OracleConnection conn = (OracleConnection) new OracleDriver().defaultConnection();
return conn.createOracleArray("NUMBER_LIST", values);
}
}
/
CREATE FUNCTION BubbleSort2(p_values IN number_list) RETURN number_list
AS LANGUAGE JAVA NAME 'BubbleSort2.sort(java.sql.Array) return java.sql.Array';
/
Then:
SELECT *
FROM TABLE(BubbleSort2(number_list(3,5,4,2,1)));
Outputs:
COLUMN_VALUE
1
2
3
4
5
fiddle

Binary Search Symbol Table implementation going inside infinite loop

I am trying to implement 'Binary Search in an ordered array' from the book 'Algorithms (fourth edition) by Robert Sedgewick & Kevin Wayne' (on page 381). However my code is going inside infinite loop. Please help. Below is the code:
public class BinarySearchST<Key extends Comparable<Key>, Value>{
private Key keys[];
private Value values[];
private int N;
public BinarySearchST(int capacity){
keys = (Key[]) new Comparable[capacity];
values = (Value[]) new Object[capacity];
}
public int size(){
return N;
}
public boolean isEmpty(){
return N == 0;
}
public int rank(Key key){
int lo = 0, hi = N-1;
while(lo <= hi){
int mid = (lo + (hi - lo))/2;
int comp = key.compareTo(keys[mid]);
if(comp < 0) hi = mid - 1;
else if(comp > 0) lo = mid + 1;
else return mid;
}
return lo;
}
public Value get(Key key){
if(isEmpty()) return null;
int rank = rank(key);
if(rank < N && key.compareTo(keys[rank]) == 0)
return values[rank];
else
return null;
}
public void put(Key key, Value value){
int rank = rank(key);
if(rank < N && key.compareTo(keys[rank]) == 0){//key already existing, just update value.
values[rank] = value;
return;
}
for(int i = N; i > rank; i--){
keys[i] = keys[i-1]; values[i] = values[i-1];
}
keys[rank] = key;
values[rank] = value;
N++;
}
public static void main(String[] args){
BinarySearchST<String, Integer> st = new BinarySearchST<String, Integer>(10);
st.put("A", 10);
st.put("B", 100);
st.put("C", 1000);
StdOut.println(st.get("A"));
}
}
This appears to be correct to me, but looks like some issue inside put() for loop.
use int mid = (lo + hi)/2.
You are using int mid = (lo+(hi-lo))/2 which reduces to hi/2. So, eventually your middle will be less than your lo and will not converge causing infinite loop.

unable to create second deck from same code for a queue

I have to set up a queue class that implements from a deque class. I need to use this to set up two deck cards with a random order. I have the code below, it works when the first deck is created but for some reason it does not work with the second deck, its the same code that im reusing.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at prog.pkg4.Deque.insertOnBack(Prog4.java:93)
at prog.pkg4.Queue.insert(Prog4.java:153)
at prog.pkg4.Prog4.createDeck(Prog4.java:465)
at prog.pkg4.Prog4.topTrump(Prog4.java:444)
at prog.pkg4.Prog4.main(Prog4.java:287)
initiates the two decks
Queue player = new Queue();
Queue computer = new Queue();
player = createDeck(player, cards);
computer = createDeck(computer, cards);
code to create random deck
public static Queue createDeck(Queue queue, GreekHero[] cards){
Random rand = new Random();
int temp = 0;
int r;
for(int i = 0; i < 30; i++){
r = rand.nextInt(30);
cards[temp] = cards[i];
cards[i] = cards[r];
cards[r] = cards[temp];
}
for(int i = 0; i < 29; i++){
queue.insert(cards[i]);
System.out.println(queue.insertions());
System.out.println(queue);
}
return queue;
}
class Queue{
private Deque queue;
public Queue(){
queue = new Deque();
}
public void insert(Object o){
queue.insertOnBack(o);
}
public Object delete(){
return queue.deleteFromFront();
}
public boolean isEmpty(){
return queue.isEmpty();
}
public String toString(){
return queue.toString();
}
public int insertions(){
return queue.getInsertions();
}
}
i've tested out the deque code several times i know it works, as demonstrated by the first deck that is created, im just not sure what could be causing the problem for the second deck.
EDIT: I've added the Deque class code below, the way i have this set up is that if the number of insertions equals the size of the array, it should double in size, as mentioned before it works with the first deque but on the second deque it stops at size of array - 1. I've increased the size to test out and I could make it bigger to satisfy this project but I need to create a deque with an increasing array.
class Deque{
private Object[] arrayObject;
private int beggining; //tracks first element in array
private int insertions; //counts the items in the array
private static int SIZE = 30; //size of array
public Deque(){
arrayObject = new Object[SIZE];
beggining = 0;
insertions = 0;
}
// displays position of first element in circular array
public Object getBeggining(){
int temp = beggining + 1;
if(temp == SIZE)
temp = 0;
return temp;
}
public int getInsertions(){
return insertions;
}
public Object indexOne(){
int temp = beggining + 1;
if(temp == SIZE)
temp = 0;
return arrayObject[temp];
}
public String toString(){
if(isEmpty())
return "Empty";
int temp = beggining + 1;
if( temp >= SIZE)
temp = 0;
String s = "Current Index:\n[("+arrayObject[temp]+")";
int loops = 0;
for(int i = temp + 1; loops < insertions - 1; i++){
if(i >= SIZE)
i = 0;
s += ", ("+arrayObject[i]+")";
loops++;
}
s += "]";
return s;
}
public String toStore(){
String s = "Store Index:\n[(1: "+arrayObject[1]+")";
for(int i = 1; i <= SIZE - 1; i++)
s += ", ("+(i+1)+": "+arrayObject[i]+")";
s += "]";
return s;
}
public void insertOnFront(Object o){
if(insertions == SIZE)
arrayObject = increaseArray();
arrayObject[beggining] = o;
beggining--;
if(beggining < 0)
beggining = SIZE - 1;
insertions++;
}
public Object deleteFromFront(){
if(isEmpty())
return null;
int count = beggining + 1;
if(count >= SIZE)
count = 0;
Object temp = arrayObject[count];
beggining += 1;
insertions--;
if(insertions > 0)
insertions = 0;
return temp;
}
public void insertOnBack(Object o){
int temp = beggining + insertions + 1;
if(insertions == SIZE - 1)
arrayObject = increaseArray();
if(temp >= SIZE)
temp = 0 + (temp - SIZE);
arrayObject[temp] = o;
insertions++;
}
public Object deleteFromBack(){
if(isEmpty())
return null;
int count = beggining + insertions;
Object temp = arrayObject[count];
insertions--;
if(insertions >= 0)
insertions = 0;
return temp;
}
public boolean isEmpty(){
if(insertions > 0)
return false;
else
return true;
}
public Object[] increaseArray(){
SIZE *= 2;
int loops = 0;
int j = beggining;
Object[] newArray = new Object[SIZE];
for(int i = j; loops <= SIZE/2; i++){
if(j >= SIZE/2)
j = 0;
newArray[i] = arrayObject[j];
loops++;
j++;
}
return newArray;
}
}
I solved the issue by moving the SIZE variable as an instance variable of the class and removed static from it. I don't know why the issue popped in on the second iteration rather than on the first try, ill look it up later, if anyone knows please post it here.

Finding the index of the first word starting with a given alphabet form a alphabetically sorted list

Based on the current implementation, I will get an arraylist which contains some 1000 unique names in the alphabetically sorted order(A-Z or Z-A) from some source.
I need to find the index of the first word starting with a given alphabet.
So to be more precise, when I select an alphabet, for eg. "M", it should give me the index of the first occurrence of the word starting in "M" form the sorted list.
And that way I should be able to find the index of all the first words starting in each of the 26 alphabets.
Please help me find a solution which doesn't compromise on the speed.
UPDATE:
Actually after getting the 1000 unique names, the sorting is also done by one of my logics.
If this can be done while doing the sorting itself, I can avoid the reiteration on the list after sorting to find the indices for the alphabets.
Is that possible?
Thanks,
Sen
I hope this little piece of code will help you. I guessed the question is related to Java, because you mentioned ArrayList.
String[] unsorted = {"eve", "bob", "adam", "mike", "monica", "Mia", "marta", "pete", "Sandra"};
ArrayList<String> names = new ArrayList<String>(Arrays.asList(unsorted));
String letter = "M"; // find index of this
class MyComp implements Comparator<String>{
String first = "";
String letter;
MyComp(String letter){
this.letter = letter.toUpperCase();
}
public String getFirst(){
return first;
}
#Override
public int compare(String s0, String s1) {
if(s0.toUpperCase().startsWith(letter)){
if(s0.compareTo(first) == -1 || first.equals("")){
first = s0;
}
}
return s0.toUpperCase().compareTo(s1.toUpperCase());
}
};
MyComp mc = new MyComp(letter);
Collections.sort(names, mc);
int index = names.indexOf(mc.getFirst()); // the index of first name starting with letter
I'm not sure if it's possible to also store the index of the first name in the comparator without much overhead. Anyway, if you implement your own version of sorting algorithm e.g. quicksort, you should know about the index of the elements and could calculate the index while sorting. This depends on your chosen sorting algorithm and implementation. In fact if I know how your sorting is implemented, we could insert the index calculation.
So I came up with my own solution for this.
package test.binarySearch;
import java.util.Random;
/**
*
* Binary search to find the index of the first starting in an alphabet
*
* #author Navaneeth Sen <navaneeth.sen#multichoice.co.za>
*/
class SortedWordArray
{
private final String[] a; // ref to array a
private int nElems; // number of data items
public SortedWordArray(int max) // constructor
{
a = new String[max]; // create array
nElems = 0;
}
public int size()
{
return nElems;
}
public int find(String searchKey)
{
return recFind(searchKey, 0, nElems - 1);
}
String array = null;
int arrayIndex = 0;
private int recFind(String searchKey, int lowerBound,
int upperBound)
{
int curIn;
curIn = (lowerBound + upperBound) / 2;
if (a[curIn].startsWith(searchKey))
{
array = a[curIn];
if ((curIn == 0) || !a[curIn - 1].startsWith(searchKey))
{
return curIn; // found it
}
else
{
return recFind(searchKey, lowerBound, curIn - 1);
}
}
else if (lowerBound > upperBound)
{
return -1; // can't find it
}
else // divide range
{
if (a[curIn].compareTo(searchKey) < 0)
{
return recFind(searchKey, curIn + 1, upperBound);
}
else // it's in lower half
{
return recFind(searchKey, lowerBound, curIn - 1);
}
} // end else divide range
} // end recFind()
public void insert(String value) // put element into array
{
int j;
for (j = 0; j < nElems; j++) // find where it goes
{
if (a[j].compareTo(value) > 0) // (linear search)
{
break;
}
}
for (int k = nElems; k > j; k--) // move bigger ones up
{
a[k] = a[k - 1];
}
a[j] = value; // insert it
nElems++; // increment size
} // end insert()
public void display() // displays array contents
{
for (int j = 0; j < nElems; j++) // for each element,
{
System.out.print(a[j] + " "); // display it
}
System.out.println("");
}
} // end class OrdArray
class BinarySearchWordApp
{
static final String AB = "12345aqwertyjklzxcvbnm";
static Random rnd = new Random();
public static String randomString(int len)
{
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++)
{
sb.append(AB.charAt(rnd.nextInt(AB.length())));
}
return sb.toString();
}
public static void main(String[] args)
{
int maxSize = 100000; // array size
SortedWordArray arr; // reference to array
int[] indices = new int[27];
arr = new SortedWordArray(maxSize); // create the array
for (int i = 0; i < 100000; i++)
{
arr.insert(randomString(10)); //insert it into the array
}
arr.display(); // display array
String searchKey;
for (int i = 97; i < 124; i++)
{
searchKey = (i == 123)?"1":Character.toString((char) i);
long time_1 = System.currentTimeMillis();
int result = arr.find(searchKey);
long time_2 = System.currentTimeMillis() - time_1;
if (result != -1)
{
indices[i - 97] = result;
System.out.println("Found " + result + "in "+ time_2 +" ms");
}
else
{
if (!(i == 97))
{
indices[i - 97] = indices[i - 97 - 1];
}
System.out.println("Can't find " + searchKey);
}
}
for (int i = 0; i < indices.length; i++)
{
System.out.println("Index [" + i + "][" + (char)(i+97)+"] = " + indices[i]);
}
} // end main()
}
All comments welcome.

array assignment issue

I am getting a incompatible types required int found:void on this line
--> moneyCount = countDemoninations(change, denomination[i]). trying to understand why?
public class Change {
int [] denomination = {1,2,5,10,20,50,100,200,500,1000,2000,5000};
int moneyCount = 0;
public void catagorizeChange(int change){
for (int i = 0; i < denomination.length; i++){
moneyCount = countDemoninations(change, denomination[i]);
}
}
public void countDemoninations(int change, int denomination){
int moneyCount =0;
while (change >= denomination){
moneyCount = moneyCount++;
change = change - denomination;
}
}
}
I'm a new java student, I want to know if the following edited code below in a good practice i.e have one method used in another where both belong to the same class?
public void countChangedenominations (int change){
for (int i = 0; i < moneyValuearray.length; i++){
moneyCountarray[i] = countDemonination(change, moneyValuearray[i]);
}
}
public int countDemonination(int change, int denomination){
while (change >= denomination){
moneyCount = ++moneyCount;
change = change - denomination;
}
return moneyCount;
The source of the problem with the line:
moneyCount = countDemoninations(change, denomination[i]);
is that countDemoninations is of type void, whereas moneyCount is of type int.
To solve the issue, change your countDemoninations method signature to return int instead of void, then return moneyCount as the last statement in your method:
public int countDemoninations( int change, int denomination ) {
int moneyCount = 0;
while ( change >= denomination ) {
moneyCount = moneyCount++;
change = change - denomination;
}
return moneyCount;
}

Resources