I am working on a program for class and I am stuck. When I try to compile the following code I receive and variable not found error. please help!
public class RingBuffer
{
public RingBuffer(int capacity){
double[] EmptyBuffer = new double[capacity];
System.out.print(EmptyBuffer.length);
}
public int size(){
int size = EmptyBuffer.length;
return size;
}
please note that I am getting an error with the size() method not being able to find the variable EmptyBuffer.
You should probably make that a field, aka an instance variable:
public class RingBuffer
{
private double[] emptyBuffer;
public RingBuffer(int capacity){
emptyBuffer = new double[capacity];
System.out.print(EmptyBuffer.length);
}
public int size(){
int size = emptyBuffer.length;
return size;
}
}
This will make emptyBuffer available throughout your class, i.e. in any other method.
Pass the array as an argument to your size() method like you've done with capacity:
int size(double[] EmptyBuffer){
int size = EmptyBuffer.length;
return size;
}
Then:
double[] EmptyBuffer = new Double[capacity];
int size = size(EmptyBuffer); // make call to size passing the array
// as an argument
System.out.print(size);
Related
I have an assignment to create an array class where there are 2 constructors where each constructor sets a different size for the array.
The array is already an instance variable along with another instance variable to keep track of the current position in the array.
I have to create a method called add with an integer parameter that will store the parameter value in the array at the index of the position variable, then add 1 to the position variable. If the incremented position variable is outside the bounds of the array, the method calls the addspace method.
The addspace method creates a new array 25% larger than the instance variable array, copies all the values of the instance array to the new array, and assigns the new array to the instance variable.
I also need a method called size that will return the value in the position variable and a method called get that with 1 parameter(an index), the method returns the value at the parameter index.
The last thing I need is a print method that uses a for loop to print the values in the array.
So far this is what I have
public class ArrayClass
{
private int array[];
private int x=0;
public ArrayClass()
{
this.array= new int[10];
add(1);
getThat(0);
print();
}
public ArrayClass(int y)
{
this.array= new int[y];
add(2);
getThat(0);
print();
}
public void add(int a)
{
array[x]=a;
x++;
if(x>array.length)
addspace();
}
public void addspace()
{
double d=array.length+(array.length*0.25);
int v=(int)d;
int newArray[]= new int[v];
for(int i=0; i<array.length; i++)
{
newArray[i]=array[i];
System.out.println(newArray[i]);
}
}
public int size()
{
return x;
}
public int getThat(int index)
{
return array[index];
}
public void print()
{
for(int i=0; i<array.length; i++)
System.out.println(array[i]+" ");
}
public static void main(String[] args)
{
new ArrayClass();
new ArrayClass(5);
}
}
I know the title only asks for help with the first method but if someone would be kind enough to help with the other methods and the reason why my code won't run and print what I want it to that would be much appreciated.
Use the ArrayClass for only for declaring your functionality.Call add method as obj.add(number) until and unless you need to add something inside ArrayClass constructor itself.
Modified these things as per my understanding
In your add method you are assigning the value first and then adding space if the array is full, in this case, you are increasing the size even if it might not be needed (i.e not calling add method again).
Instead of this increase the size only when you require it.
In print function you are iterating through the whole array.Modified to-> it will iterate till the last index of value (i.e x)
package com.example;
public class ArrayClass
{
private int array[];
private int x=0;
private final int DEFAULT_SIZE=4;
public ArrayClass(){
this.array = new int[DEFAULT_SIZE];
}
public ArrayClass(int size){
this.array = new int[size];
}
public void add(int number){
//check whether array have space or not .if not then increase the space.
if(x > this.array.length-1){
addSpace();
}
array[x] =number;
x++;
}
private void addSpace(){
double newSize = array.length + array.length * 0.25;
int tempArray[] = new int[(int) newSize];
for(int i=0; i<array.length; i++){
tempArray[i]=array[i];
}
this.array = tempArray;
}
public int size()
{
return x;
}
public int getThat(int index)
{
return array[index];
}
public void print()
{
//instead of of printing the whole array Printed till last value index.
for(int i=0; i<x; i++)
System.out.println(array[i]+" ");
}
}
From the main method
ArrayClass ac1 = new ArrayClass();
ac1.add(5);
ac1.add(4);
ac1.add(5);
ac1.add(4);
ac1.add(7);
ac1.add(19);
ac1.print();
ArrayClass ac2 = new ArrayClass(5);
ac2.add(1);
//rest of your function call here
I have created a stack .
public class STK {
static int capacity = 0;
STK(int size) {
capacity = size;
}
int stackk[] = new int[capacity];
int top = 0;
public void push(int d) {
if(top < capacity) {
stackk[top] = d;
top++;
} else {
System.out.println("Overflow");
}
}
}
its implementation
public class BasicStackImplementation {
public static void main(String[] args) {
STK mystack = new STK(5);
mystack.push(51);
mystack.push(23);
}
}
when i try to run this code it gives an error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at STK.push(STK.java:21)
at BasicStackImplementation.main(BasicStackImplementation.java:6)
Field initializers run before the constructor. Your code is equivalent to this:
static int capacity = 0;
int stackk[]=new int[capacity];
STK(int size)
{
capacity=size;
}
So you're initializing an empty array. To fix it, just initialize stackk inside the constructor:
int[] stackk;
STK(int size)
{
capacity = size;
stackk = new int[capacity];
}
Also, capacity varies by instance and shouldn't be static.
When you made your class, you initialized your array property in your class to equal capacity which is 0. So your array is initialized with 0 elements.
When you call your constructor and set the capacity value, you need to re-initialize your class array equal to new int[value]
I am doing a school project in java and I am trying, in a method, to refer to the class.
import java.util.ArrayList;
public class NumberIndex extends ArrayList<Integer> {
private int s;
public NumberIndex (){
super();
s = 10; // would be better if it was class.size()
//but I don't know how to refer to the class
}
public NumberIndex (int x){
super(x);
s = x;
}
public void addWord(int num) {
for(i = 0; i < s; i++)
//trying to make it so that for each Integer in ArrayList,
// if there exists an Integer that has the value num, nothing would
//happen. Else creates new Integer and adds it to the List
So in order for me to finish this code, all I need is a way to reference the class object NumberIndex itself.
Since add word is a member function use this. that refers to the current NumberIndex object.
EDIT:
public class NumberIndex extends ArrayList<Integer> {
public NumberIndex() {
super(10);//setting the size of your NumberIndex object -> list size
}
public NumberIndex(int x) {
super(x);//setting the size of your NumberIndex object -> list size
}
public void addWord(int num) {
if(!this.contains(num)){//if the current NumberIndex object (list) does not contain num
this.add(num);//to the current NumberIndex object (list) add num
}
}
}
How do I make a constructor to set the length of a global array?
I have already tried several ways to do it, none successful.
Example:
public Class{
public Class(int length){
double[] array = new double[length]; <- this is not global
L = length;
}
int L;
double[] array = new double[L]; <- this does not work
}
I need an array with a length determined by Constructor.
I think it's as simple as this:
public class MyClass{
double[] array;
public MyClass(int length){
array = new double[length];
}
}
I've also made the code actually compile :) You were missing some keywords etc.
If you want to access length in your code, use array.length rather than storing it redundantly in a separate field.
Also calling your class Class is a bad choice, even as an example, because it clashes with java.lang.Class.
Declare the array as member variable. Then initialize it in the constructor.
public class A{
private double[] array;
public Class(int length){
array = new double[length];
L = length;
}
}
You could initialize it in second way. But then you need to use a fixed length
public class A{
private double[] array = new double[100]; // use fixed length
public Class(int length){
array = new double[length];
L = length;
}
}
I don't know what you are trying to achieve but why you don't simply do it this way:
public class Class{
public Class(int length){
this.array = new double[length]; // <- this is not global
}
double[] array;
}
public class aClass{
//define the variable name here, but wait to initialize it in the constructor
public double[] array;
public aClass(int length){
array = new double[length];
}
}
You can do it
public class Test {
double[] array;
public Test (int length){
array = new double[length]; <- this is not global
}
I'm trying to get a radix sort going with an array of queues to avoid long rambling switch statements but I'm having some trouble getting the array properly initialized. The constructor and an example of an implementation are given below.
I'm just getting a cannot find symbol error when I try to compile though.
public static radixj(){
IntQueue[] buckets = new IntQueue[10];
for (int i = 0; i < 10; i++)
buckets[i] = new IntQueue();
}
public static void place(int temp, int marker)
{
int pos = temp % marker;
buckets[pos].put(temp);
}
I'm pretty sure it is a really simple mistake that I'm making but I can't find it. Any help would be greatly appreciated.
In your code
IntQueue[] buckets = new IntQueue[10];
is a local variable to the function
public static radixj()
which must have a return type
public static void radixj()
So then you can't use it in another function
buckets[pos].put(temp);
You should declare a static class variable
class Foo {
static IntQueue[] buckets = new IntQueue[10];
...
and access it using: Foo.buckets
class Foo {
public static IntQueue[] buckets = new IntQueue[10];
public static void radixj() {
for (int i = 0; i < 10; i++) {
Foo.buckets[i] = new IntQueue();
}
}
public static void place(int temp, int marker) {
int pos = temp % marker;
Foo.buckets[pos].put(temp);
}
}
the return type in radixj() is missing and buckets cannot be resolved to a variable