Member Avatar for alliswim2010

Hello I'm having major issues, please help me with this code;

(Generating random characters) Use the methods in RandomCharacter to print one hundred uppercase letters and then one hundred single digits, and print ten per line.
Use these methods:
public static char getRandomUppercaseLetter(){
return getRandomCharacter('A', 'Z');
}
public static char getRandomDigitCharacter(){
return getRandomCharacter('0', '9');
}

I just don't know how to work it into these methods. Try and keep it simple, i'm still new to coding.
Thanks!

Recommended Answers

All 9 Replies

What are your questions about your assignment?
You mention the names of several methods but do not say if you are to write them or if they are provided to you and you are to use them.

Please explain.

Member Avatar for alliswim2010

Liang page 193, programming exercise 5.23
(Generating random characters) Use the methods in RandomCharacter in Listing 5.10 to print one hundred uppercase letters and then one hundred single digits, and print ten per line.

(that is the question)
But the methods listed in 5.10 (a different page) were this:

all start
 public static char
                             getRandomCharacter(char ch1, char ch2){
       return (char)(ch1 + Math.random() * (ch2 - ch1 + 1)); }

                             getRandomLowerCaseLetter() {
       return getRandomCharacter ('a', 'z');   }

                              getRandomUpperCaseLetter() {
       return getRandomCharacter ('a', 'z');  }

                              get RandomDigitCharacter() {
       return getRandomCharacter ('0', '9');   }

Thats what they list. Obviously I need to use the uppercase in the digit, so thats why I typed those out. I'm REQUIRED to use those methods. Does that make sense?

I'm REQUIRED to use those methods.

Who provides the code for those methods?
Where is the code? In a library like a jar file or ???

What code do you think you have to write?

Member Avatar for alliswim2010

Its a question in my text book. I have to create a code that outputs 100 random Capital Letters and 100 random numbers... and they have to be 10 to a line. And I have to use one or more of the methods I listed above. Its a homework assignment that i've given up on. If you want to be more specific its Introduction to Java Programming 8th addition Y.Daniel Liang.

Member Avatar for alliswim2010

It was simply written in my textbook.

Can you explain what you are having problems with?
If you have the methods you are to use, then all you need is to write a program that
calls the methods and formats the lines and prints them out.

Member Avatar for alliswim2010
import java.util.Random;


public class RandomCharacter {


    public static void main(String [] args){

        final int NumberOfChars = 100;
        final int CharsPerLine = 10;

        for (int i = 0; i < NumberOfChars; i++){
            char ch= RandomCharacter.getRandomUpper();
            if ((i + 1) % CharsPerLine == 0)
                System.out.println(ch);
            else
                System.out.println(ch);



        }


        public static char getRandomUpper(){
            (char)('A' + Math.random() * ('Z' - 'A' + 1));
        }

        public static char getRandomDigit(){
            (int)(Math.random() * 10);


        }

This is what I have so far. I guess I'm calling the method wrong, and the char is marked with mistakes as well.... I'm sort of just guessing at this point. I don't know if i'm even on the right track.

You left off the leading [ on the end code tag.

If you are getting errors, copy and paste the full text here.

If the output is not what you want, please post the output and explain what is wrong and show what you want it to be.

Member Avatar for alliswim2010

Got it

public class RandomCharacter {

	public static char getRandomUpperCaseLetter(){
		int ascii = (int) (Math.random()*26) + (int) 'A';
		return (char)ascii;
	}
	public static char getRandomDigitCharacter(){
		int digit = (int)(Math.random()*10) + (int) '0';
		return (char)digit;
	}

	public static void main(String [] args){
		
		for (int i = 1; i <= 100; i++)
		{
			System.out.print(getRandomUpperCaseLetter());
			if(i%10 == 0) //10 have been printed move to a new line
				System.out.print("\n"); 
		}
		
		for (int i = 1; i <= 100; i++)
		{
			System.out.print(getRandomDigitCharacter());
			if(i%10 == 0) //10 have been printed move to a new line
				System.out.print("\n"); 
		}
	}
}

Thanks everyone!

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.