Constructor for array of variable size - java

I want to code a constructor for an array of size x with x being a parameter speciified in main().
My class:
public class CharA
{
private char[] stack;
private int n = 0;
public void CharA (int max)
{
this.stack = new char[max];
this.n = max;
}
My main():
public class CharTest
{
public static void main (String args)
{
CharA stack1 = new CharA(100);
}
}
The error:
CharTest.java:5: cannot find symbol
symbol : constructor CharA(int)
location: class CharA
CharA stack1 = new CharA(100);
^
There are several examples here where the same thing is done with an int array. Why doesn't it work for this char array?

remove void in your "constructor":
public CharA (int max) {
// ...
}

Replace public void CharA (int max) with public CharA (int max), because constructors don't have a return type.

The constructor method should not have a return type in its definition:
public CharA(int max) {...}

Related

How to pass my object into another objects field?

I am trying to fill UCFCourse courseOne in my constructor with a courses[] object in fillWithCourses().UCFCourse courseOne does populate outside of the constructor but will not go into it.
public class UCFSemester<courses> {
private static UCFCourse courseOne;
private static double totalSemesters;
private static double completionTime;
static boolean fillSemester = true;
public UCFSemester(UCFCourse courseOne, UCFCourse[] coursetwo) {
this.courseOne = courseOne;
}
public static UCFCourse getcourseOne() {
return courseOne;
}
public static void setCoursesone(UCFCourse courses) {
courseOne = courses;
}
public static void fillWithCourses(UCFCourse courses[], int l) {
int x = 0;
while (fillSemester) {
for (int n = 0; n < 5; n++) {
if (x != n && courses[x].getCourseLevel() < courses[n].getCourseLevel()) {
setCoursesone(courses[x]);
}
}
fillSemester = false;
}
}
}
Side question.How can I access this all in a non-static way?I need the entire thing to be non-static but no matter what I do I can't get it.Thanks!
You can simply do it by creating a List like this:
public class UCFSemester {
private List<UCFCourse> courseList = new ArrayList<>();
public UCFCourse getCourse(int index) {
return courseList.get(index);
}
public void addCourses(UCFCourse[] courses) {
for(int x = 0; x < courses.length; x++) {
courseList.add(courses[x]);
}
}
}
Here, I'm assuming that you are passing the UCFCourse[] array with all the course details that are there in that particular semester.
addCourses() function will take this array and then add all the corresponding courses to the List.
getCourse() function will return you any particular course from the List (Using Index). You can also modify the search in any way you want.

How to access an array from another class

I want to operate on the array called "players" that is declared in the main method. I want to use "players" in my class called "Glucksspielthread"
I know that I can't access "players" because it is declared in the main method and is not visible for other classes.
How can I solve this problem? Here is my code:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Glucksspieltest {
public static void main(String[] args) {
int numPlayers = Integer.parseInt(args[0]);
int threadSize = Integer.parseInt(args[1]);
ExecutorService es = Executors.newFixedThreadPool(threadSize);
Glucksspielthread[] players = new Glucksspielthread[numPlayers];
for (int i = 0; i < numPlayers; i++) {
players[i] = new Glucksspielthread(i);
es.execute(players[i]);
}
}
}
class Thinker {
public static void think(int Millisekunden) {
try {
Thread.sleep(Millisekunden);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void randomThink(int minMillisekunden, int maxMillisekunden) {
System.out.println("test");
}
}
class Glucksspielthread implements Runnable {
public int playerNumber;
Glucksspielthread(int number) {
playerNumber = number;
}
#Override
public void run() {
for (int i = 0; i <= playerNumber; i++) {
// here, I want to operate on array called "players" that is declared in the main method
}
}
}
Just for your test purpose make your players variable static and public in the Glucksspieltest class, like this:
public class Glucksspieltest {
public static Glucksspielthread[] players;
Then acces it in the Glucksspielthread class like this:
for (int i = 0; i <= playerNumber; i++) {
// here, I want to operate on array called "players" that is declared in the main method
Glucksspieltest.players
}
Add a method to class Glucksspieltest, and make the players array global:
public class Glucksspieltest {
private static Glucksspielthread[] players;
public static Glucksspielthread[] getPlayers(){
return players;
}
public static void main(String[] args) {
int numPlayers = Integer.parseInt(args[0]);
int threadSize = Integer.parseInt(args[1]);
ExecutorService es = Executors.newFixedThreadPool(threadSize);
players = new Glucksspielthread[numPlayers];
for (int i = 0; i < numPlayers; i++) {
players[i] = new Glucksspielthread(i);
es.execute(players[i]);
}
}
}
This way you can get the array by calling the getPlayers() method.
(Note that, it would be adviced to add a constructor to initialize and fill the players array, and separate the player management from the main method as well.)
Make players as private global referance variable
public class Glucksspieltest {
//Make a Global reference variable players
private static Glucksspielthread[] players;
// Make a getter Method to get players
public static Glucksspielthread[] getPlayers(){
return players;
}
public static void main(String[] args) {
int numPlayers = Integer.parseInt(args[0]);
int threadSize = Integer.parseInt(args[1]);
ExecutorService es = Executors.newFixedThreadPool(threadSize);
players = new Glucksspielthread[numPlayers];
for (int i = 0; i < numPlayers; i++) {
players[i] = new Glucksspielthread(i);
es.execute(players[i]);
}
}
}
And access it by Glucksspieltest.getPlayers();
class Glucksspielthread implements Runnable {
public int playerNumber;
private static Glucksspielthread[] players;
Glucksspielthread(int number) {
playerNumber = number;
}
#Override
public void run() {
for (int i = 0; i <= playerNumber; i++) {
// here, I want to operate on array called "players" that is declared in the main method
players= Glucksspieltest.getPlayers(); // play with players
}
}
}

Setting ArrayList items in one Class from another Class

There is a foo class with an ArrayList of double msg called msgstoboo as well as a method setMsg(int index, double input) to alter individual messages in msgstoboo.
There is a networkoffoos class with an ArrayList of foo objects called listoffoos. There is an updatefoomsg method:
public void updatefoomsg (ArrayList<ArrayList<Foo>> Foonetwork)
{
for(int foolayer = 0; foolayer< foonetwork.size(); foolayer++)
for(int fooinlayer = 0; fooinlayer< foonetwork.get(foolayer).size(); fooinlayer++)
for(int msginfoo = 0; msginfoo < foonetwork.get(foolayer).get(fooinlayer).msgstoboo.size(); msginfoo++)
Foonetwork.get(foolayer).get(fooinlayer).setMsg(msginfoo,somerandomvalue)
}
The goal of updatefoomsg is to change the values of individual msgs in msgstooboo. However, no values in the foo class ArrayList `msgstoboo' are altered. Why is this and how do I fix it? Thank you in advance.
UPDATE: Here are the whole foo and networkoffoos classes
public class foo
{
ArrayList<Double> msgstoboo = new ArrayList<Double>(Double);
public foo(int numofmessages)
{
for (int i = 0; i < numofmessages; i++)
{
msgstoboo.add(1);
}
}
public void setMsg(int index, double input)
{
msgstoboo.set(index,input);
}
&&
public class networkoffoos
{
ArrayList<ArrayList<foo>> foonetwork = new ArrayList<ArrayList<foo>>();
public void networkoffoos(int numoffoos)
{
for(int i = 0; i < numoffoos; i++)
foonetwork.add(new foo(somenumberofmsgs))
}
//**AND THE "updatefoomsg" method included in this post**
}
The following works for me. Made some small changes because your code wouldn't compile. Hope it helps.
import java.util.ArrayList;
public class test {
private static ArrayList<ArrayList<Foo>> foonetwork = new ArrayList<ArrayList<Foo>>();
public static void main(String[] args){
networkoffoos(5);
updatefoomsg(foonetwork);
}
public static void networkoffoos(int numoffoos) {
for(int i = 0; i < numoffoos; i++) {
ArrayList<Foo> fooArrayList = new ArrayList<Foo>();
fooArrayList.add(new Foo(10));
foonetwork.add(fooArrayList);
}
}
public static void updatefoomsg (ArrayList<ArrayList<Foo>> foonetwork) {
for(int foolayer = 0; foolayer< foonetwork.size(); foolayer++)
for(int fooinlayer = 0; fooinlayer< foonetwork.get(foolayer).size(); fooinlayer++)
for(int msginfoo = 0; msginfoo < foonetwork.get(foolayer).get(fooinlayer).msgstoboo.size(); msginfoo++)
foonetwork.get(foolayer).get(fooinlayer).setMsg(msginfoo,3);
}
}
and your Foo class
import java.util.ArrayList;
public class Foo {
ArrayList<Double> msgstoboo = new ArrayList<Double>();
public Foo(int numofmessages) {
for (int i = 0; i < numofmessages; i++) {
msgstoboo.add(Double.valueOf(1));
}
}
public void setMsg(int index, double input) {
msgstoboo.set(index, input);
}
}

Creating an Array with random numbers with a specific object reference

For each of the 100 elements of the calc, instantiate a Claclal object with
randomly generated numbers. Use the Random class along with the .nextDouble() method
for this.
I have already created the 100 elements but i cant add any numbers to the array. When i try and add any random numbers i get an error saying that it is requires a claclal but its found an int.
public class Claclal {
private static Claclal[] calc;
public static void main(String[] args) {
Random r = new Random();
calc = new Claclal[100];
for (int i = 0; i <calc.length; i++) {
calc[i] = new Claclal();
calc[i] = (int)(Math.random());
}
the error that i am getting is on the last line.
I cant change the reference to int because its supposed to be a claclal reference. What do i need to change to get the code to work?
You have an array of Clacal type, not int type. You have 2 options
1st) Create a property in Clacal that is the double.
Example:
public class Claclal {
private static Claclal[] calc;
private final double number;
public Clacal(double number){
this.number=number;
}
public static void main(String[] args) {
Random r = new Random();
calc = new Claclal[100];
for (int i = 0; i <calc.length; i++) {
calc[i] = new Claclal(r.nextDouble());
}
}
2nd) Make a double array
public class Claclal {
private static double[] calc;
public static void main(String[] args) {
Random r = new Random();
calc = new Claclal[100];
for (int i = 0; i <calc.length; i++) {
calc[i] = r.nextDouble();
}
}
calc[i] = (int)(Math.random());
That is not allowed since array type is Claclal, and you are trying to insert integer.
You need to take a field in Claclal class and add to that.
Claclal c= new Claclal();
c.setRandomNumber((int)(r.nextDouble()));
calc[i] = c;
Then your code turns
public class Claclal {
private static Claclal[] calc;
private int randomNumber;
public static void main(String[] args) {
Random r = new Random();
calc = new Claclal[100];
for (int i = 0; i < calc.length; i++) {
Claclal c = new Claclal();
c.setRandomNumber((int) (r.nextDouble()));
calc[i] = c;
}
}
public int getRandomNumber() {
return randomNumber;
}
public void setRandomNumber(int randomNumber) {
this.randomNumber = randomNumber;
}
}
More over you are using Math.random() and you want to use r.nextDouble()
You are trying to assign an int value as an element of the Claclal array.
You should add a field to the Claclal class to store a double value in each object. In your main method, use the Random object that you have instantiated to pass a new random double value to the constructor of each new instance of Claclal in the array.
import java.util.Random;
public class Claclal {
// the double field to store a value
private double value;
public Claclal(double value) {
this.value = value;
}
// getter
public double getValue() {
return this.value;
}
// setter
public void setValue(double value) {
this.value = value;
}
public static void main(String[] args) {
Random r = new Random();
Claclal[] calc = new Clalcal[100];
for (int i = 0; i < calc.length; i++) {
// add new Claclal object with a random double value
calc = new Claclal(r.nextDouble());
}
}
}
You are getting the error because the claclal class doesnot have any attribute.
Add an attribute in claclal as shown below:
import java.util.Random;
public class Claclal {
private static Claclal[] calc;
int value;
public Claclal( int value) {
this.value = value;
}
public static void main(String[] args) {
Random r = new Random();
calc = new Claclal[100];
for (int i = 0; i < calc.length; i++) {
calc[i] = new Claclal(r.nextInt());
System.out.println(""+calc[i].value);
}
}
}

Trouble with Sort program using objects and constructors

Really new to java wanted to make my separate sorting methods (they all work hopefully i did them right)
Also very new to objects and constructors hopefully what im talking about is an object
so here are the constructors
public class sort{
public int[] selectsort(int[] num)
{
int j,i,key,min;
for(j = 0; j<num.length; j++)
{
key = num[j];
min = j;
for(i=j+1; i<num.length; i++)
{
if(num[i]<num[min])
{
min = i;
}
}
num[j] = num[min];
num[min] = key;
}
return num;
}
public int[] insertsort(int[] num)
{
int j,i,key;
for(j = 1; j<num.length; j++)
{
key = num[j];
for(i=j-1; i>=0 && num[i]>key; i--)
{
num[i+1]=num[i];
}
num[i+1]=key;
}
return num;
}
public static int[] bubblesort(int[] num)
{
int i,j,ini;
for(i = num.length-1; i>1; i--)
{
for(j=0;j<i; j++)
{
if(num[j]>num[j+1])
{
ini = num[j];
num[j]=num[j+1];
num[j+1]=ini;
}
}
}
return num;
}
}
and the program/test
import java.util.Arrays;
public class sorttest{
public static void main(String[] args)
{
int[] num = new int[]{9,1,4,5,6,2,3,7,8};
System.out.println(Arrays.toString(selectsort(num)));
}
}
javac sort.java compiles but javac sorttest.java doesnt
error:
sorttest.java:9: cannot find symbol
symbol : method selectsort(int[])
location: class sorttest
System.out.println(Arrays.toString(selectsort(num)));
^
1 error
The method selectsort is not a part of the sorttest class - it's a public static method in the sort class. This means you need to qualify it by its class name:
import java.util.Arrays;
public class sorttest{
public static void main(String[] args) {
int[] num = new int[]{9,1,4,5,6,2,3,7,8};
System.out.println(Arrays.toString(sort.selectsort(num)));
}
}
Alternatively, you could use a static import:
import static sort.selectsort;
import java.util.Arrays;
public class sorttest{
public static void main(String[] args) {
int[] num = new int[]{9,1,4,5,6,2,3,7,8};
System.out.println(Arrays.toString(selectsort(num)));
}
}

Categories