-2

I have been trying for weeks in this project where I have to make one class that generates 500 random numbers from 1-250 and in a second class I have to inherit the first class properties and write all those numbers in a text file but when I have being having problems getting the properties and work with it and I haven't found a way to do it online.

My First class is

import java.util.Random;

public class GenKeys {

public static void random(){
for (int i = 0; i < 250; i++) {

int x = (int) (Math.random() * 100);
}
}
}

and my second code is

import java.util.Random;
import java.io.*;
import java.lang.*;
public class MainProg extends GenKeys{

public static void main(String[] args){

    public static void random(){
    try {



BufferedWriter out = new BufferedWriter(new FileWriter("file.txt"));

out.write( x + System.getProperty("line.separator"));// when i compile the x is not     found!!!

out.close();
} catch (IOException e) {
System.out.print(e);
            }
        }

How can I make the two classes work together?

gariepy
  • 3,576
  • 6
  • 21
  • 34
  • You extend GenKeys but you never actually inherit `x` to a value before writing it. Thus compiler error... – Tdorno May 12 '13 at 14:03

6 Answers6

4
What am i doing Wrong ?
  • You are using inheritance instead of just using an instance of GenKeys in MainProg
  • You keep overwriting your random values, since you only use a single variable x, when you should be using e.g. an array
  • You create 250 values in range [0..99] instead of 500 values in range [1..250]
  • You don't store or return anything from your random() method
ValarDohaeris
  • 6,064
  • 5
  • 31
  • 43
0

and i havent found a way to do it online.

I'm not sure you've looked hard enough.

How to get your code working

Firstly, you want to change the type and name of your method to an int.

public static int randomNum()

Then, remove the loop from the code, and just return the random number generated:

return (int)Math.Random() * 100; //By the way there is a Random class.

In the random method, you want the loop:

for(int x = 0; x < 250; x++)
{
    BufferedWriter out = new BufferedWriter(new FileWriter("file.txt"));
    out.write( randomNum() + System.getProperty("line.separator"));
}

out.close();

The various issues with your code

You're mis-using inheritance here. Your class is not a type of GenKey. It simply uses it, so it should be a field in your class.

Secondly, a method can only return one value, or one object. It can not return 250 numbers as is. You're assigning 250 numbers to x. This is only going to store the last number generated.

christopher
  • 26,815
  • 5
  • 55
  • 89
0

I don't think this is right approach. you need another class, for example KeyWriter to inherit from GenKeys. let it use GenKeys method random (it doesn't need to be static) also, your random method is wrong, you only generate 250 keys instead of 500, and they are not from 0 to 250.
my solution is:
1) inherit KeyWriter from GenKeys
2) modify random to return only 1 generated number using nextInt
3) use cycle inside KeyWriter to call random 500 times and write those values into a file
4) use KeyWriter class inside you main method

I don't post the actual solution, cause it looks like you're doing your homework.

Sergi0
  • 1,084
  • 14
  • 28
0

Well, somethings aren't correct here, but the weirdest of all is that you made the random() function a void.

 void random()

Where X goes to? You just create a new int, but do nothing about it.

Besides this, there are other problems, as other folks mentioned around. I'd recommend you to read about function in Java, especially about the difference between int and void.

TheEmeritus
  • 429
  • 1
  • 6
  • 18
0

Some problems (and comments) I see of the bat:

  1. x is not an instance field and is not stored anywhere thus how can it be accessible from the child class.

  2. Like others have said x is being overwritten with each iteration of your for loop.

  3. Why is the mainProg.random() method declared inside of the mainProg.main() method?

  4. I dont think inheritance is the way to go unless it is absolutely required for this project. Why not just make an instance of your random class inside the main method of the mainProg class?

  5. If you want to use inheritance I believe a call to super.random() will be necessary inside of the mainProg.random() method.(Please someone confirm this. Im not 100% sure)

If it was me I would do something along the lines of this in my GenKeys.random() method:

public int[] random() {
   int[] keys = new int[500];
   for(int i = 0; i < 500; ++i)
   {
      keys[i] = (int) (Math.random() * 100);
   }

   return keys;
}
  1. This code creates and returns an array of 500 keys. NOT in the range of 1-250. See here for that: How do I generate random integers within a specific range in Java?

Hopefully that will get you started on the right track.

Community
  • 1
  • 1
0

x is the local variable of random().
so you can't directly access local variable out side the class.
And you are trying to generate 500 random no. between 1-250 so change the for loop in first class

for (int i = 0; i < 500; i++){
.....
}
gifpif
  • 4,507
  • 4
  • 31
  • 45