Could you please help me to make this JAVA program work. I did some writing, but
ID: 3870679 • Letter: C
Question
Could you please help me to make this JAVA program work. I did some writing, but it dosn't work. Please help me. I really appreciate your help
public class NewTest {
public static void main(String[] args) {
Intarray mydata1 = new Intarray();
Intarray mydata2 = new Intarray(10);
System.out.println("the length of 1 is " + mydata1.getSize());
System.out.println("the length of 2 is " + mydata2.getSize());
mydata1.PrintIt(0);
mydata1.Randomize(10);
mydata1.PrintIt(0);
mydata1.PrintIt(1);
}
}
class Intarray {
private int[] data;
Intarray(){
data= newIntarray(5);
}
Intarray(int intsize){
data = new int[intsize];
}
void Randomsize(int vals) {
// randomly generate values for each item between 1 and vals//
For (int vals 1 = 0; i< data.length;1++) {
Data[i] = (int) (Math.random()*vals);
}
}
public void println() {
}
}
Explanation / Answer
Note: I Made many changes since java is case sensitive you need to follow the naming conventions.Declaring keyword as a method throws error.
public class NewTest {
public static void main(String args[]) {
intArray mydata1 = new intArray(); // intArray reference and object
intArray mydata2 = new intArray(); // intArray reference and object
mydata1 .Intarray(); // runs the method and object class
mydata2.Intarray(10); // runs the method and object class
mydata1.randomSize(10); // runs the method in and object class
mydata1.Println(0); // runs the method and object class
mydata1.Println(0); // runs the method and object class
mydata1.Println(1); // runs the method and object class
}
}
class intArray {
private int[] data;
//Directly we cannot get size.We can get size by the following way
public void Intarray(){ //method overloading
data= new int[5];
System.out.println(data.length);
}
//Directly we cannot get size.We can get size by the following way
public void Intarray(int size) { //method overloading
data= new int[size];
System.out.println(data.length);
}
public void randomSize(int vals) {
for (int i = 0; i < data.length; i++) {
data[i] = (int) (Math.random()*vals);
}
}
//You gave println it is a keyword so it shows error.I gave Println since it is case sensitive it accepted.
public void Println(int vals) {
System.out.println("vals "+vals);
}
}
Your Mistakes:
PrintIt but in method you written like void println() which method it calls.
for (int i = 0; i < data.length; i++) {
data[i] = (int) (Math.random()*vals);
}
loop is written wrong
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.