Making a very simple program with Java. menu

Shout-Out

User Tag List

Results 1 to 2 of 2
  1. #1
    Strichnine's Avatar Knight-Champion
    Reputation
    82
    Join Date
    Sep 2007
    Posts
    497
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Making a very simple program with Java.

    Hello there!

    [DISCLAIMER] I assume that you know how to make your own class in java, I.e (public class Program, etc..), and I also assume that you have a program to use the java language in, such as TextPad or BlueJava. [DISCLAIMER]

    I'd like to show new programmers how to make a VERY simple program. This is useful if you are just about to start java programming in school or so, so you can shine a bit in class!

    So this program is about a farmer.. The farmer has got some chickens who lay eggs every day of the week (7 days). Now, the farmer wants to know the average value of the laid eggs per day. This is very easy and I'll show you how it's done!

    Firstly, you will have to download a class called Keyboard.class (I'll explain why later).
    This can be done here: The Keyboard Class

    When you've download it, put it in the folder in which you are about to save your project, it could be "C:\Documents and Settings\Desktop\MYLEETPROGRAMMINGFOLDER" or whatever, just make sure that the class file is in the same folder as your "SIMPLEPROGRAM".java file.

    So now you have everything you need, a keyboard.class file and your java editing program. Now let's look at the code!

    Firstly, I'll make some variables ready.

    Code:
    //all code thats been done here is done in Main.
    
    int [] eggs = new int [7]; /* An array, containing 7 int variables, soon to be filled with the daily egg income*/
    Now we've made the core variable, this one is what I am going to use through out the whole program. A guide on how to further use arrays could be reached here: Java arrays - Java tutorial

    So now we have our array, but it's empty? How do we fill it?

    I'll show you now:

    Code:
    //filling it is done by doing a for-loop (my favourite)
    // a for-loop is a loop that has an amount of times to be looped, and //in it there's something to be done, looks like this:
    
    for(int i = 0; i<6; i++) //variable i starts at 0, until the value is under 7, add 1 to i every loop
    {
    
    System.out.println("Enter the collected amount of eggs on day "+ (i+1) +"."); 
    //This will tell the farmer to enter the amount of eggs on 
    //day 1, 2, 3, 4, 5, 6, 7 (the variable i starts on 0, therefor i+1 is 1, //which is day 1.
    
    //now we use the variable "eggs" and fill it up with his answers
    
    eggs[i] = Keyboard.readInt();
    //this command fills the position (i) in eggs with answer (i)
    
    
    
    }
    After this, we have filled the variable "eggs" with values from each day of the week. Now we can print it out, this can also be done with a for-loop:

    Code:
    for(int i = 0; i<6; i++)
    {
    System.out.print(eggs(i)+", "); //this will print for example "6, 3, 8, "
    
    }
    So now we've checked the code and seen that variable "eggs" is filled with values from each day. Now we want to check the average outcome value of eggs each day! This is very simple maths and could be done with another for-loop (SURPRISE!):

    Code:
    //firstly we have to make a new variable, called total, this could not be with decimals, which makes us use integer
    
    int total = 0; //creation of the variable total, starts at 0
    
    for(int i = 0; i<6; i++)
    {
    
    total = total + eggs(i);
    //this means that for every loop, we add the value "eggs(i)" to total
    
    }
    
    //now we have filled total with all the answers in eggs(i), lets check //the average value!
    //as you know, average values can look like this: "5.7" or something, //which means there's decimals involved, so we have to use double //for the average value
    
    double average = total/7;
    
    System.out.println("The average egg outcome is "+ average + " eggs per day!");
    Now the program is done.

    Q: But why use the keyboard class? Why not import scanner?
    A: Personally I like keyboard more because I've used it since I began programming, but importing scanner is better because it can be reached everywhere, using keyboard.class means that you have to include it in your pack when you have compiled it and such, much more complicated, but I still love keyboard.class and it's in my head to always use it.

    Q: What if I want to expand this program and add more features to it?
    A: Just PM me and I'll tell you how to do what you want to do!

    The final code should look like this:

    Code:
    public class Farmer{
          
           public static void main (String [] args){
    
            int [] eggs = new int [7];
    
               for(int i = 0; i<6; i++)
                     {
                     System.out.println("Enter the collected amount of eggs ond ay "+ (i+1) +"."); 
               
                      }
    
               for(int i = 0; i<6; i++)
                   {
                           System.out.print(eggs(i)+", "); 
    
                    }
    
                int total = 0; 
      
                          for(int i = 0; i<6; i++)
                                 {
    
                                      total = total + eggs(i);
    
    
                                  }
    
    
    
                           double average = total/7;
    
                           System.out.println("The average egg outcome is "+ average + " eggs per day!");
    
                   }
    
    }
    Just wanted to post something in the Java forum, since it's so dead in here

    Regards

    Making a very simple program with Java.
  2. #2
    krelit's Avatar Member
    Reputation
    1
    Join Date
    Mar 2009
    Posts
    4
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    nice program

    very good little program for beginners.
    though "Hello World" is easier, this very good.
    Programming in Java myself, but i am only i the lets say middle phase :P

Similar Threads

  1. [BOT]AFK in BG with simple program!
    By Allizkor in forum World of Warcraft Bots and Programs
    Replies: 17
    Last Post: 08-16-2008, 08:55 PM
  2. VERY SIMPLE GUIDE for noobs to create a private server with custom EVERYTHING
    By c0ddingt0n in forum WoW EMU Guides & Tutorials
    Replies: 21
    Last Post: 05-15-2008, 08:54 PM
  3. Make custom portals/ Warp NPC (with program)
    By controlsx2 in forum World of Warcraft Emulator Servers
    Replies: 1
    Last Post: 05-05-2008, 06:07 AM
  4. Very simple problem with Wamp - I cant get my server site up - please help
    By faxmunky in forum World of Warcraft Emulator Servers
    Replies: 2
    Last Post: 04-19-2008, 02:59 AM
  5. Make gold very fast with any "gathering" profession!
    By badmons in forum World of Warcraft Guides
    Replies: 12
    Last Post: 08-20-2007, 09:12 AM
All times are GMT -5. The time now is 06:12 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search