could someone plz help.. i'm stuck in one part where u have to access the array form the main class(random) ive juss posted the two classes i have doubt in.. the question goes soemthin like this:

Q:Make a java class file for function that can do addition, then make a new java class file again for random numbers(which will allow your program to make random equations for addition).Make a new java class file for menu where the user decides what grade(from 1 to 6) they study in and if u type something wrong like a character or anything, the program shouldnt shut down but print out “invalid type again” and then the user decided how many equations they want to solve; 10, 20, 30 or 50 . Your math program should ask to answer the equations. In
Grade1,your program shoud only ask equations from numbers between 1-20.
Grade2 from numbers between 1-50.
Grade3 from numbers between 10-
50.
Grade4 from numbers between 10-
100
Grade5 from numbers between 50-
100
Grade 6 numbers from 100-500.

When the user types in the answer,your program should state wether the answer was correct or wrong.In the end, it should tell how many eqautions the user got correct and how many wrong.

import java.util.Random;

public class Main {
    public static void main(String[] args) {

         Random generator= new Random();
    int num1 = generator.nextInt(20)+1;
    int num2 = generator.nextInt(20)+1;

        addition aObject=new addition ();
        aObject.add( num1,num2);


    }
}// this is the main class
import java.util.Scanner;
public class addition {
    public void add(int num1, int num2){


Scanner input1=new Scanner(System.in);
int choice2=input1.nextInt();


switch(choice2){

    case 10:

int read=0;
int[] numbers=new int [10];

  for (int i=0;i<numbers.length; i++){
      
    numbers[i]=generator.nextInt(10)+1;// this is the area of problem, the java gives error  in recognizin 'generator.nextInt(10)+1' since random is in the other class.

System.out.println(" wat is"+ num1 +"*"+num2);

int answer=num1*num2;
int guess=input1.nextInt();

if(answer==guess){
    System.out.println("you are right, the correct answer is "+ answer);
}
      else{

    System.out.println("you are wrong, the correct answer is "+ answer);
}

 }
break;
}
}
}//this is another 'addition' class

Recommended Answers

All 5 Replies

Member Avatar for kris0r

You can pass the object you are trying to access to the method you are calling from aObject. E.g. change Main.java to

aObject.add(num1, num2, generator);

and in addition.java

public void add(int num1, int num2, Random generator)

you may need to return it back if you need to keep the same copy throughout the program.

its still showin an error (the nulltype error), sionceit doesnt recognizes Random in : public void add( int sum1,int sum2. Random generator);
moreover is 'Random' above suppose to be a built in keyword? =S

its still showin an error (the nulltype error), sionceit doesnt recognizes Random in : public void add( int sum1,int sum2. Random generator);
moreover is 'Random' above suppose to be a built in keyword? =S

I don't see the code posted for public void add(int sum1, int sum2, Random generator) but if I had to guess, your getting a null pointer because you are trying to use an object before it is initialised most likely.

I don't see the code posted for public void add(int sum1, int sum2, Random generator) but if I had to guess, your getting a null pointer because you are trying to use an object before it is initialised most likely.

heres the codes

import java.util.Random;
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

         Random generator= new Random();
        menu mObject=new menu();
             mObject.question();

    int num1 = generator.nextInt(20)+1;
    int num2 = generator.nextInt(20)+1;

        addition aObject=new addition ();
        aObject.add( num1,num2, generator);

        // TODO code application logic here
    }

}




 */
import java.util.Scanner;
public class addition {
 public void add(int num1, int num2, Random generator ){


Scanner input1=new Scanner(System.in);
int choice2=input1.nextInt();


switch(choice2){

    case 10:

int read=0;
int[] numbers=new int [10];

  for ( int i=0;i<numbers.length; i++){

    numbers[i]=generator.nextInt(10)+1;

System.out.println(" wat is"+ num1 +"*"+num2);

int answer=num1*num2;
int guess=input1.nextInt();
if(answer==guess){
    System.out.println("you are right, the correct answer is "+ answer);
}
      else{

    System.out.println("you are wrong, the correct answer is "+ answer);
}

return(Random generator);
 }
break;


}
}
}

BoomBoomF after looking over what you just posted for you addition method in both posts, I'm thinking maybe in general you should rethink about what you are doing and how that method should work. Anywho to getting it working as posted

return(Random generator);

Is not going to work. you would need to

return new Random();

This will return a new Random object

or

return generator;

This will return the Random object that you initially passed in.

Another silly problem is that we are returning a Random object but our return type is void for the add method. Is there some reason for passing this Random object back out of the method? It already exists outside of the method when you originally created it and it hasn't been modified in anyway. Your whole for loop seems to hurt my head as well. You are putting random numbers inside of an array and then you keep asking the same multiplication problem over and over as many times as the length of the array.(num1 and num2 were provided from the outside and don't change at all inside the method). Then you exit the loop after the first iteration because you have a return statement at the end of the loop. Unless your just testing stuff out, this needs to be rethought. Good luck! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.