Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: JD1's Personal Development Blog

1. ## Math Class Methods

There are many built in methods we can use to conduct mathematical operations to numbers. Here are some of them.

```class Class1{
public static void main(String args[]){
//How far number is from zero.
System.out.println(Math.abs(-26.7));
//Rounds number up.
System.out.println(Math.ceil(7.4));
//Rounds number down.
System.out.println(Math.floor(7.8));
//Greater of the two numbers.
System.out.println(Math.max(8.6, 5.2));
//Smaller of the two numbers.
System.out.println(Math.min(8.6, 5.2));
//First number to the power of the second number.
System.out.println(Math.pow(5, 3));
//Square root of the number.
System.out.println(Math.sqrt(9));
}
}```

All pretty self explanatory.

2. ## Random Number Generator

Random numbers can be used in Java. Here's a small random number generator and how it works.

```import java.util.Random;
class Class1{
public static void main(String args[]){
Random dice = new Random();
int number;

for(int counter = 1; counter <= 10; counter++){
number = dice.nextInt(6) + 1;
System.out.println(number);
}
}
}```

First of all, we need to import the random class. Type import java.util.Random; at the top of your code. We then created an object to use this random numer and we named it dice. We made an integer named number to store this value. We then created a loop iterating ten times and each time it did we assigned number a new random number using our dice object. We did this by typing dice.nextInt(6) + 1;. The reason we've added one to the output is to prevent us from receiving numbers 0-5. We want numbers 1-6, so we simply add one to the output.

3. ## Re: JD1's Personal Development Blog

I can't use the class1Object outside of the main method.
You are in Class1 and class1Object is actually the object of Class1 so, outside main() why do you need to use this object?
You can call or use any of instance variable or method of Class1 as far as you are working in Class1.
Well, if you still want to use object, you can make a method to accept object and pass the object into the method call.
```public class Class1{
private int total;
private String name;
public int getTotal() {
}
public void setTotal(int total) {
this.total = total;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String... args){
Class1 obj = new Class1();
obj.setTotal(1);
obj.setName("Mr.777");
usingObject(obj);
}
public static void usingObject(Class1 obj){
System.out.println(obj.getTotal());
System.out.println(obj.getName());
}
}```

4. ## Re: JD1's Personal Development Blog

Originally Posted by Mr.777
You are in Class1 and class1Object is actually the object of Class1 so, outside main() why do you need to use this object?
You can call or use any of instance variable or method of Class1 as far as you are working in Class1.
Well, if you still want to use object, you can make a method to accept object and pass the object into the method call.
```public class Class1{
private int total;
private String name;
public int getTotal() {
}
public void setTotal(int total) {
this.total = total;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String... args){
Class1 obj = new Class1();
obj.setTotal(1);
obj.setName("Mr.777");
usingObject(obj);
}
public static void usingObject(Class1 obj){
System.out.println(obj.getTotal());
System.out.println(obj.getName());
}
}```
Hi,

Some of that is still new to me. I still can't get my head around any of what you've written (since you're using alternative code). It's difficult for me to relate that back to my problem, let alone understand what it does. Also, how can I call other methods within the same class without using that class1Object. As you said, there's no point using an object to get into the same class, but how else do I access the other methods in that class?

5. ## Re: JD1's Personal Development Blog

1. You must know the difference between static and non static methods.
2. I will recommend you to write main() as a separate class instead of mixing it in the already written class. Because your program must be highly cohesive (independent) and loosely coupled (how a class knows about the other class)
I know you are a beginner so, just focus on your track and keep going. Good Luck.

6. ## Re: JD1's Personal Development Blog

Alright, I'll keep that in mind. Once again, I'd like to thank you for all your help today. I really appreciate it!

7. ## Arrays

Arrays are used to store many values under the one variable, so to speak. You can initialise an array the long and drawn out way by manually assigning each index a value or you can implement an array initialiser to do the job for you.

```class Class1{
public static void main(String args[]){
int array1[] = new int[10];
array1[0] = 87;
array1[1] = 543;
array1[2] = 65;
System.out.println(array1[2]);

int array2[] = {1,2,3,4,5,6,7,8};
System.out.println(array2[4]);

}
}```

In this example, array1 is the slow way, and array2 makes use of an array initialiser. To build an array, you must first create the variable. You do this by typing the data type (int) followed by the array name (array1) and then add two square brackets ([]) to show that we're working with an array. Set this equal to a new int (if that is the chosen data type) followed by the number of indices in square brackets. That's the slow way of course.

You can use an array initialiser as follows. Data Type arrayName[] = {index1, index2, index3, etc};. That's a much easier was of going about building an array.

8. ## Re: JD1's Personal Development Blog

Originally Posted by OA-OD-JD1
Alright, I'll keep that in mind. Once again, I'd like to thank you for all your help today. I really appreciate it!
No need to say thanks. That's why, we are here at forums, to help everyone.

9. ## The Following User Says Thank You to Mr.777 For This Useful Post:

OA-OD-JD1 (January 26th, 2012)

10. ## Re: JD1's Personal Development Blog

You can use an array initialiser as follows. Data Type arrayName[] = {index1, index2, index3, etc};. That's a much easier was of going about building an array.
Though this method of declaring and creating an array is not wrong but the right convention is to use the syntax like;
DataType[] arrayName;
These minor suggestions are just to improve your programming style as you are passing on with your concepts and topics. So, keep these in mind too.

11. ## Re: JD1's Personal Development Blog

Originally Posted by Mr.777
Though this method of declaring and creating an array is not wrong but the right convention is to use the syntax like;

These minor suggestions are just to improve your programming style as you are passing on with your concepts and topics. So, keep these in mind too.
And I'm glad you're providing these tips. This is the sort of thing I need. Also, with your example there, are you referring to an array initialiser with no indexes?

12. ## Re: JD1's Personal Development Blog

No, just declaring an array.
So, if you want to have a reference to an array you will most likely be;
```int[] arrayTest;
arrayTest = new int[SIZE];```
OR
`int[] arrayTest = new int[SIZE];`

13. ## Re: JD1's Personal Development Blog

Originally Posted by Mr.777
No, just declaring an array.
So, if you want to have a reference to an array you will most likely be;
```int[] arrayTest;
arrayTest = new int[SIZE];```
OR
`int[] arrayTest = new int[SIZE];`
Understood. Can you tell me why you put the square brackets after the data type instead of the array name?

14. ## Re: JD1's Personal Development Blog

Originally Posted by OA-OD-JD1
Understood. Can you tell me why you put the square brackets after the data type instead of the array name?
Well it's the convention from the Sun. Also, it is used to let others understand that we are going to have an array of that datatype named this. Like
`int[] arrayName;`
The code will let others know easily that we are going to have an array of integers naming arrayName. Just a convention to follow by everyone so that everyone could code in a known standards and understand them easily though int arrayName[] is not wrong to write. It works similar to the latter.

15. ## Re: JD1's Personal Development Blog

Originally Posted by Mr.777
Well it's the convention from the Sun. Also, it is used to let others understand that we are going to have an array of that datatype named this. Like
`int[] arrayName;`
The code will let others know easily that we are going to have an array of integers naming arrayName. Just a convention to follow by everyone so that everyone could code in a known standards and understand them easily though int arrayName[] is not wrong to write. It works similar to the latter.
Okay. In other languages, I've been used to doing it the way I orginally did, but since what you mentioned is standard Sun convention, I'll be sure to stick to that in the future.

16. ## Re: JD1's Personal Development Blog

You might want to check out the standard code conventions and the JLS, both of which are where we're getting most of these rules.
Code Conventions for the Java(TM) Programming Language: Contents
The Java Language Specification, Third Edition - TOC

17. ## Re: JD1's Personal Development Blog

Originally Posted by KevinWorkman
You might want to check out the standard code conventions and the JLS, both of which are where we're getting most of these rules.
Code Conventions for the Java(TM) Programming Language: Contents
The Java Language Specification, Third Edition - TOC
Thanks mate. I'll be sure to take a look some time.

18. ## Summing The Elements of An Array

Say we have an array with ten indices. Below is how we can calculate the sum of these indices.

```class Class1{
public static void main(String args[]){
int[] array1 = {1,2,3,4,5,6,7,8,9,10};
int total = 0;

for(int counter = 0; counter < array1.length; counter++){
total += array1[counter];
}

System.out.println("Total\t" + total);

}
}```

Firstly, initialise your array. Then, create a variable to store the total. A simple For Loop set to iterate to the length of our array will do the trick. All you need to do is set the total to the total plus the array with the index if the current counter. A very simple concept.

In an unrelated topic, I'm now up to my 30th Java tutorial. I'm making some nice leadway here, though I've still got a heck of a lot to learn.

19. ## Re: JD1's Personal Development Blog

This blog has been continued over at Java Development Forums and in my JPF Blog.

Harvey's Java Blog
JD1's Personal Development Blog