Welcome to Dream.In.Code
Become a Java Expert!

Join 149,609 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,878 people online right now. Registration is fast and FREE... Join Now!




Random Number Generation

 
Reply to this topicStart new topic

Random Number Generation

rbell84
20 Sep, 2007 - 09:27 AM
Post #1

New D.I.C Head
*

Joined: 18 Sep, 2007
Posts: 2


My Contributions
Hi I'm new to the java programming language and I have a scenario. So far I've got part of the code to generate a random age but I'm stuck. Below is the scenario and any help will be greatly appreciated. After generating the random age I'm supposed to be able to display an output of a certain age and its corresponding price per ticket. I'm a little overwhelmed being that this is my first attempt at java, so again any help will grand many thanks.


Design and implement an application called CinemaPrice to determine how much a person pays to go the the cinema. The program should generate an age from 1 - 100 using the Random class and prompt the user for the full ticket price. Then display the appropriate ticket price using the currency format (an example in your book ).


Decide ticket price on the following basis:
• under 5, free;
• aged 5 to 12, half price;
• aged 13 to 54, full price;
• aged 55, or over, free.



CODE

import java.util.Random;

public class CinemaPrice
{

    private Random generator;
    
    
    public static void main (String[] args)
    {
    
            int ageNumber;
            int priceNumber;
            String age = " ";
            String message;
            
            
            
            
            Random generator = new Random();
            age = generator.nextInt(100) + 1;
            
            
            if (age =< 5){
                priceNumber = "0";
            }else if (age =< 12){
                priceNumber = "6.25";
            }else if (age =< 54){
                priceNumber = "12.50";
            }else if (age >=55){
                priceNumber = "0";
            }else {
                priceNumber = "invalid";
            }

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Random Number Generation
20 Sep, 2007 - 11:02 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Nice start. You are on your way there. I had only a few minor changes to this and added a few things. Plus I left a little for you to do. Read the comments to see what I have done here.

CODE

import java.util.Random;
import java.text.DecimalFormat;

public class CinemaPrice
{

    private Random generator;


    public static void main (String[] args)
    {

        // Variables to hold age and price of person
        int age = 0;
        double priceNumber = 0.00;

        // Generate a random age (generator will
        Random generator = new Random();
        age = generator.nextInt(100) + 1;


        // Check age of person and assign pricing.
        // Notice how I combine both age 5 with 55 or over.
        // I am saying here "if age is 5 or less OR age is 55 and over, assign no price"
        if ((age <= 5) || (age >= 55)) {
            priceNumber = 0.0;
        }else if (age <= 12){
            priceNumber = 6.25;
        }else if (age <= 54){
            priceNumber = 12.50;
        }else {
            System.out.println("Sorry, but the age supplied was invalid.");
        }

        
        // Check if they are free, print message to say so.
        // Otherwise print the person's age and the price.

        if (priceNumber <= 0.0) {
            System.out.println("The person age " + age + " is free!");
        }
        else {
            System.out.println("Price for the person age " + age + " is: $" + priceNumber);
        }
    }
}


Some of the things you will notice is the change of a few of your variables here. Whenever you need to store a decimal value, you must use the appropriate data type. In this case double. An int data type would cut off the decimal portion of the number which is not what you want. Also note I got rid of your string variables here. You didn't need them to just include them in the message at the bottom. Notice how I concatenated them together. Lastly, notice that if the age didn't meet our requirements I couldn't assign the value "invalid" to our price because it is a string. You should only assign strings to a string variable, not the double we are now using or the integer you were using before.

Please note that I didn't format the price for you. But I did give you the tools to do it. Notice I included the class DecimalFormat in there for you. Use this class and its method format() to format the price into an appropriate money value. I will let you look that up and figure out how to do it. Hint: you will need to instantiate an instance of a DecimalFormat object first and pay attention to how you can instantiate it!

Enjoy! decap.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:08AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month