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


Members have full access to the forums. Advertisements are removed for registered users.

Page 3 of 3 FirstFirst 123
Results 51 to 71 of 71

Thread: JD1's Personal Development Blog

  1. #51
    Member OA-OD-JD1's Avatar
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    69
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post
    Blog Entries
    29

    Default 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. #52
    Member OA-OD-JD1's Avatar
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    69
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post
    Blog Entries
    29

    Default 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. #53
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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() {
    		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());
    	}
    }

  4. #54
    Member OA-OD-JD1's Avatar
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    69
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post
    Blog Entries
    29

    Default Re: JD1's Personal Development Blog

    Quote Originally Posted by Mr.777 View Post
    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() {
    		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?

  5. #55
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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. #56
    Member OA-OD-JD1's Avatar
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    69
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post
    Blog Entries
    29

    Default 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. #57
    Member OA-OD-JD1's Avatar
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    69
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post
    Blog Entries
    29

    Default 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.
    Last edited by OA-OD-JD1; January 28th, 2012 at 06:24 AM.

  8. #58
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: JD1's Personal Development Blog

    Quote Originally Posted by OA-OD-JD1 View Post
    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. #59
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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. #60
    Member OA-OD-JD1's Avatar
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    69
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post
    Blog Entries
    29

    Default Re: JD1's Personal Development Blog

    Quote Originally Posted by Mr.777 View Post
    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. #61
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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. #62
    Member OA-OD-JD1's Avatar
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    69
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post
    Blog Entries
    29

    Default Re: JD1's Personal Development Blog

    Quote Originally Posted by Mr.777 View Post
    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. #63
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: JD1's Personal Development Blog

    Quote Originally Posted by OA-OD-JD1 View Post
    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. #64
    Member OA-OD-JD1's Avatar
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    69
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post
    Blog Entries
    29

    Default Re: JD1's Personal Development Blog

    Quote Originally Posted by Mr.777 View Post
    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. #65
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  17. #66
    Member OA-OD-JD1's Avatar
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    69
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post
    Blog Entries
    29

    Default Re: JD1's Personal Development Blog

    Quote Originally Posted by KevinWorkman View Post
    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. #67
    Member OA-OD-JD1's Avatar
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    69
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post
    Blog Entries
    29

    Default 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. #68
    Member OA-OD-JD1's Avatar
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    69
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post
    Blog Entries
    29

    Default 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
    Last edited by OA-OD-JD1; January 29th, 2012 at 04:44 PM.

  20. #69
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: JD1's Personal Development Blog

    Please be courteous enough to also link back to javaprogrammingforums.com from your own forums too.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  21. #70
    Member OA-OD-JD1's Avatar
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    69
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post
    Blog Entries
    29

    Default Re: JD1's Personal Development Blog

    Quote Originally Posted by newbie View Post
    Please be courteous enough to also link back to javaprogrammingforums.com from your own forums too.
    I've decided to continue the blog here (now that I've received blogging permissions).

  22. #71
    Junior Member
    Join Date
    Apr 2013
    Location
    philippines
    Posts
    28
    My Mood
    Innocent
    Thanks
    6
    Thanked 6 Times in 2 Posts

    Default Re: JD1's Personal Development Blog

    i enjoy reading your blog OA-OD plus the help of super moderators
    guys keep the fire burning

Page 3 of 3 FirstFirst 123

Similar Threads

  1. My Personal Spam Test
    By person287 in forum Totally Off Topic
    Replies: 2
    Last Post: December 18th, 2011, 03:06 AM
  2. Blog idea
    By dks in forum Java Theory & Questions
    Replies: 3
    Last Post: February 17th, 2011, 02:48 PM
  3. Advice on Java personal msg system?
    By cyborgbill in forum Java Theory & Questions
    Replies: 1
    Last Post: March 30th, 2010, 03:00 AM