# JD1's Personal Development Blog

Show 40 post(s) from this thread on one page
Page 3 of 3 First 123
• January 26th, 2012, 01:50 AM
OA-OD-JD1
Math Class Methods
There are many built in methods we can use to conduct mathematical operations to numbers. Here are some of them.

Code java:

```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.
• January 26th, 2012, 02:00 AM
OA-OD-JD1
Random Number Generator
Random numbers can be used in Java. Here's a small random number generator and how it works.

Code java:

```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.
• January 26th, 2012, 03:22 AM
Mr.777
Re: JD1's Personal Development Blog
Quote:

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.
Code :

```public class Class1{ private int total; private String name; public int getTotal() { return total; } 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()); } }```
• January 26th, 2012, 03:57 AM
OA-OD-JD1
Re: JD1's Personal Development Blog
Quote:

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.
Code :

```public class Class1{ private int total; private String name; public int getTotal() { return total; } 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?
• January 26th, 2012, 04:05 AM
Mr.777
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.
• January 26th, 2012, 04:22 AM
OA-OD-JD1
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! :-*
• January 26th, 2012, 04:30 AM
OA-OD-JD1
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.

Code java:

```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.
• January 26th, 2012, 04:38 AM
Mr.777
Re: JD1's Personal Development Blog
Quote:

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.
• January 26th, 2012, 04:40 AM
Mr.777
Re: JD1's Personal Development Blog
Quote:

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;
Quote:

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.
• January 26th, 2012, 04:51 AM
OA-OD-JD1
Re: JD1's Personal Development Blog
Quote:

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?
• January 26th, 2012, 05:04 AM
Mr.777
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;
Code :

```int[] arrayTest; arrayTest = new int[SIZE];```
OR
Code :

`int[] arrayTest = new int[SIZE];`
• January 26th, 2012, 06:05 AM
OA-OD-JD1
Re: JD1's Personal Development Blog
Quote:

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;
Code :

```int[] arrayTest; arrayTest = new int[SIZE];```
OR
Code :

`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?
• January 26th, 2012, 06:15 AM
Mr.777
Re: JD1's Personal Development Blog
Quote:

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
Code :

`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.
• January 26th, 2012, 07:01 AM
OA-OD-JD1
Re: JD1's Personal Development Blog
Quote:

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
Code :

`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.
• January 26th, 2012, 07:33 AM
KevinWorkman
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
• January 26th, 2012, 03:42 PM
OA-OD-JD1
Re: JD1's Personal Development Blog
Quote:

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.
• January 27th, 2012, 04:05 AM
OA-OD-JD1
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.

Code java:

```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.
• January 28th, 2012, 11:56 PM
OA-OD-JD1
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
• January 29th, 2012, 10:13 AM
newbie
Re: JD1's Personal Development Blog
• January 29th, 2012, 03:41 PM
OA-OD-JD1
Re: JD1's Personal Development Blog
Quote:

Originally Posted by newbie