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