Results 1 to 4 of 4
Like Tree2Likes
  • 2 Post By pbrockway2

Thread: Need help with my project (Methods)

  1. #1
    Gens is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Need help with my project (Methods)

    Hello users,
    I am a beginner programer. It is my first semester of Computer Science, so I am a noobie.
    I as supposed to write a program for my class project:
    "Random Data Generator"
    1. Write a JAVA program called RandomDataGenerator, it should have 3 methods all with the same name but with different parameters:
    a) Method to return an integer between any two integers it is passed.
    b) Method to return a double between any two doubles it is passed.
    c) Method to return an upper case character, lower case character or a special character (! # $ % & ` ( ) * + - . /)
    2. The main method should ask the user what type or random number he/she wants, how many should be generated, the lowest number generated and the highest number generated, see attached file for full description.
    Here is what I did so far:
    Java Code:
    package javaapplication2;
    
    import java.io.IOException;
    import java.util.Random;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    public class JavaApplication2 {
        
        public static void main(String[] args) throws IOException {
            Scanner in = new Scanner(System.in);
            
            char i, d, c;
            int low_int = 0,high_int = 0, quant_int = 0;
            double low_double = 0, high_double = 0, quant_double = 0;
            char low_char = 0, high_char = 0, quant_char = 0;
            int result = 0;
            String input;
            
            input = JOptionPane.showInputDialog(null, " What do you want to generate? Type"
                + " 'i' for integer; 'd' for double; 'c' for character ");
            i = input.charAt(0);
            d = input.charAt(0);
            c = input.charAt(0);
            
            if (input.trim().compareToIgnoreCase("i") == 0){
                input = JOptionPane.showInputDialog(null, " How many integers do you want? ");
                quant_int = Integer.parseInt(input);
                
                input = JOptionPane.showInputDialog(null, " Choose low value for your integer: ");
                low_int = Integer.parseInt(input);
                
                input = JOptionPane.showInputDialog(null, " Choose high value for your integer: ");
                high_int = Integer.parseInt(input);
                
                int random_generator = JavaApplication2.random_generator(quant_int, low_int, high_int); 
                int s = random_generator; //Cant't get output without this 2 lines
            }
            
            else if (input.trim().compareToIgnoreCase("d") == 0){
                input = JOptionPane.showInputDialog(null, " How many doubles do you want? ");
                quant_double = Double.parseDouble(input);
                
                input = JOptionPane.showInputDialog(null, " Choose low value for your double: ");
                low_double = Double.parseDouble(input);
                
                input = JOptionPane.showInputDialog(null, " Choose high value for your double: ");
                high_double = Double.parseDouble(input);
                
                double random_generator = JavaApplication2.random_generator(quant_double, low_double, high_double); 
                double k = random_generator; //Cant't get output without this 2 lines
            }
            
            else if (input.trim().compareToIgnoreCase("c") == 0){
                input = JOptionPane.showInputDialog(null, " How many characters do you want? ");
                quant_char = input.charAt(0);
                
                input = JOptionPane.showInputDialog(null, " Choose low limit for your character: ");
                low_char = input.charAt(0);
                
                input = JOptionPane.showInputDialog(null, " Cheese high limit for your character: ");
                high_char = input.charAt(0);
                
                char random_generator = JavaApplication2.random_generator(quant_char, low_char, high_char); 
                char e = random_generator;
            }
            
            else{
                JOptionPane.showConfirmDialog(null, " Wrong Input! ");
            }
        }
        
        public static int random_generator(int quant, int low, int high){
            Random rand_num = new Random();
            int return_int = 0;
            for(int z = low; z < quant + 1; z++)
            System.out.println(rand_num.nextInt(high) + 1); // Do I need to return value if I am printing it oit here?
            return return_int;
        }
        
        public static double random_generator(double quant, double low, double high){
            Random rand_num = new Random();
            double return_double = 0;
            for(double z = low; z < quant + 1; z++)
            System.out.println(rand_num.nextDouble() + 1);
            return return_double;
        }
        
        public static char random_generator(char quant, char low, char high){
            Random rand_num = new Random();
            char return_char = 0;
            for(double z = low; z < quant + 1; z++)
            System.out.println(rand_num.equals(high)); //how do I output my chars?
            return return_char;
        }
    }
    Looks like I got my integers working but sometimes program outputs 1 less that I asked (I have + 1 function). Doubles looks like working but it generates in a weird way, not exactly right, always same int but different decimals, ex; if Im asking for 4 doubles between 2.3 and 5.6, program gives me 3 doubles all with first same number. (if I am asking for 5 doubles it gives me 4, one less, I tried adding +1, but it didn't work. However, looks like it works with integers!). And How should I handle Characters? Do I need to import ASCII table somehow? See comments for other questions plz. Help me out with this program!
    Attached Files Attached Files

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Need help with my project (Methods)

    1. Write a JAVA program called RandomDataGenerator, it should have 3 methods all with the same name but with different parameters:
    a) Method to return an integer between any two integers it is passed.
    b) Method to return a double between any two doubles it is passed.
    c) Method to return an upper case character, lower case character or a special character (! “ # $ % & ` ( ) * + ‘ - . /)
    You haven't done this. A minor nitpick is the name of the class - it should be RandomDataGenerator, not JavaApplication2.

    You have set up the methods to do a whole lot of printing, and that's not what they were supposed to do. They are supposed to return an integer, a double etc. That's all they should do: the integer version should just calculate a random integer and return it for example. (Having small methods that do one thing is a mark of manageable design.)

    Since the methods only generate and return a single value, they don't care about what you are calling quant_xxx and should not be passed that value.

    It is up to the main method to do all the other stuff. But you should get the random generating methods implemented before worrying about main(). You can write just enough code in main() to check at each step that your value generating methods are doing what you want.

    -----

    One way of implementing the random generation of a character is to declare an array with all of the possible letters in it. Then you can use the integer generating method to select an index at random and return the character at that index.
    Fubarable and Gens like this.

  3. #3
    Gens is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Re: Need help with my project (Methods)

    Thank you very much for the tips. I will work on it and respond with my progress.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Need help with my project (Methods)

    You're welcome. Post back if you get stuck.

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Java Noob, trying to call methods from another methods
    By gabrielpr12 in forum New To Java
    Replies: 8
    Last Post: 11-17-2011, 10:07 PM
  3. Replies: 1
    Last Post: 10-23-2011, 01:37 PM
  4. Creating a project in eclipse from existing project
    By Suraiya in forum New To Java
    Replies: 1
    Last Post: 10-08-2011, 10:14 AM
  5. Replies: 1
    Last Post: 10-05-2011, 01:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •