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 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 71

Thread: JD1's Personal Development Blog

  1. #26
    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 Private Variables

    Private variables cannot be used over multiple classes. They are specific to the class in which they were created, hence the private part. You can declare a private variable by writing private before it, as seen below.

    private String devName;

    If the variable devName was in Class2, it would not be able to be used in Class1, since it is exclusive to its home class, if you will.

  2. #27
    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 Many Methods & Instances

    This post goes over how we can use multiple methods in different classes. It's a very inefficient program we've written, but it's designed to show you exactly what each method does and how they relate to each other. Here's Class2, the guts of the application. I've included comments to show you what everything does.

    public class Class2 {
    	//New string
    	private String friendName;
    	/* This method is responsible for collecting the name
    	 * variable we sent from the main method. We assign
    	 * the variable friendName to the name collected in
    	 * the main method. */
    	public void setName(String name){
    		friendName=name;
    	}
    	//New method with a return type of String
    	public String getName(){
    		/* Basically just inserts the friendName variable
    		 * into the getName method.*/
    		return friendName;
    	}
    	//New method for finally outputting our name variable
    	public void outputName(){
    		//Outputs the variable getName
    		System.out.printf("Your friend's name is %s", getName());
    	}
    }

    The application basically outputs someone's name which we collect from Class1. Basically, we start by creating a friendName variable that we can use in Class2. It's going to be responsible for carrying our friend's name throughout the series of methods we've created here. Our first method, setName assigns the variable name (sent from Class1) to our friendName variable.

    The next method, which is pretty useless, simply assigns the variable of friendName to the method of getName. This way we can use it in our last method, outputName by simply calling the getName method (which is storing our friendName variable). Let's take a look at Class1.

    class Class1{
    	public static void main(String args[]){
    		//New object for Class2
    		Class2 class2Object = new Class2();
    		//New string
    		String name = "Tony";
    		//Send the variable 'name' to setName method in Class2
    		class2Object.setName(name);
    		//Run the outputName method in Class2
    		class2Object.outputName();
    	}
    }

    All we're doing here is creating a new object for Class2, followed by creating our name string, followed by sending this variable via our object through to the setName method in Class2.

    I know it seems stupid using four different methods to output a simple variable, but it shows us how we can use multiple methods with multiple purposes for each to get the job done. Obviously this sort of setup would be better used had we had many more operations we wished to carry out.

  3. #28
    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 Constructors

    We can use a constructor to quickly initialise variables as soon as our object is created. If you haven't read the above post, please do - you will become familiar with the code we're using for these examples.

    Here's Class1.

    class Class1{
    	public static void main(String args[]){
    		Class2 class2Object = new Class2("Tony");
    		class2Object.outputName();
    	}
    }

    You can see what we've done here is added an argument of "Tony" to our class2Object. To use this constructor, you must make a method with the exact same name as the class in which it's in. Take a look at our Class2 code.

    public class Class2 {
    	private String friendName;
     
    	public Class2(String name){
    		friendName = name;
    	}
    	public void setName(String name){
    		friendName = name;
    	}
    	public String getName(){
    		return friendName;
    	}
    	public void outputName(){
    		System.out.printf("Your friend's name is %s", getName());
    	}
    }

    You will notice this new method.

    public Class2(String name){
    		friendName = name;
    	}

    The method's name is Class2, identicle to that of our class name. Since we were able to send the name variable over to Class2 as the object was created, this now makes our setName method redundant. Its job has been taken care of.

    Constructors are useful if we wanted to use multiple objects. Each object has its own set of variables. What we could do, if we wanted, would be to create another object like this.

    Class2 class2Object2 = new Class2("Betty");

    We could now decide to output class2Object2 or class2Object1; each would produce a different output.

  4. #29
    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 Conditional Operators

    Basically, I like to look at Conditional Operators as a quick way of implementing an If Statement. That's really all they are. Take a look at this code.

    class Class1{
    	public static void main(String[] args){
    		int age =50;
    		System.out.println(age > 30 ? "You are quite old!" : "You are still young!");
    	}
    }

    You can see that we've initalised an integer with the value of 50. Our Conditional Operator then checks to see if it's greater than thirty (age > 30 ?). We use a question mark to see if the condition is true. You then write the true and false outputs separated by a colon. That's really all there is to it!

  5. #30
    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 20 Tutorials Summarised!

    Well, I've finally finished watching and summarising (in this thread) the first twenty tutorials of TheNewBoston's Java Programming Tutorial series. I've previously watched all 87 tutorials, but never summarised any of them to the point that the information stuck in my head long enough for me to retain.

    I plan on doing the same sort of thing for the next 67 tutorials and then into the Intermediate Java tutorials.

    Thank you to everyone who has helped me out along the way. I have a long way to go but that will be made much easier with your continued support.

    Thanks,
    JD1

  6. #31
    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

    Again, just to nitpick, the tertiary (conditional) operator isn't exactly like an if statement- it simply chooses between two values. You can't, for example, do this:


    boolean blue = true;
    blue ? System.out.println("blue") : System.out.println("red");

    Instead, you'd have to do something like this:

    boolean blue = true;
    String print = blue ? "blue" : "red";
    System.out.println(print);
    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!

  7. #32
    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
    Again, just to nitpick, the tertiary (conditional) operator isn't exactly like an if statement- it simply chooses between two values. You can't, for example, do this:


    boolean blue = true;
    blue ? System.out.println("blue") : System.out.println("red");

    Instead, you'd have to do something like this:

    boolean blue = true;
    String print = blue ? "blue" : "red";
    System.out.println(print);
    I suppose you're right in saying that. The extent of my knowledge at the moment has the conditional operator acting like an If Statement in my head. Hopefully that will change when I start learning more things. Thanks for pointing that out mate.

  8. #33
    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

    Quote Originally Posted by OA-OD-JD1 View Post
    I suppose you're right in saying that. The extent of my knowledge at the moment has the conditional operator acting like an If Statement in my head. Hopefully that will change when I start learning more things. Thanks for pointing that out mate.
    Well thinking about it as an if/else statement is probably pretty okay, as long as you know that they have to return values (of the same type) and not execute a block like an if statement does.
    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!

  9. #34
    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 Scanners

    I forgot to write about the scanner, so I'm going to do that now.

    Basically, a scanner is used to get information from the user. At this stage, the scanner will be used only to gather information from the keyboard. Here's how it works.

    import java.util.Scanner;
    class Class1{
    	public static void main(String args[]){
    		Scanner input = new Scanner(System.in);
    		System.out.println("Enter some text:");
    		System.out.println("The text you entered was: " + input.nextLine());		
    	}
    }

    Firstly, we must import the scanner, since it's not available in the default library. It's found in the java.util library, so we write import java.util.Scanner; at the top of our code to allow us to use the scanner. Capitalisation here is important.

    Next, we need to create a new scanner variable to let us use the scanner. Do this by typing Scanner followed by the name of the object (in this case, I called mine input). Set the object equal to new Scanner with the parameter of System.in - this allows us to read the keyboard input.

    Now whenever you type into the console (or other field), the text you enter before pressing enter will be stored in the input scanner variable. To output this, we need to figure out what data type we're working with. In this case, it's a string of text so we use input.nextLine(). That's really all there is to it!

  10. #35
    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
    Well thinking about it as an if/else statement is probably pretty okay, as long as you know that they have to return values (of the same type) and not execute a block like an if statement does.
    So, basically, I'm limited to the amount of things I can do in the Conditional Statement? Whereas in an If Statement, you can really have as many instructions as you want. I suppose I can only output a variable using a Conditional Statement, and not a series of instructions, right?

  11. #36
    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

    There are also third type of comments and they are Java Doc Comments.

  12. #37
    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 Simple Averaging Program

    What this application does is collects a number of inputs from the user via a scanner. These inputs are numbers. They are then averaged and the average is outputted. Pretty simple stuff.

    import java.util.Scanner;
    class Class1{
    	public static void main(String args[]){
    		Scanner input = new Scanner(System.in);
    		int total = 0;
    		int grade;
    		int average;
    		int counter = 0;
    		int numGrades = 10;
     
    		System.out.println("Please enter " + numGrades + " numbers to be averaged");
    		while(counter < numGrades){
    			grade = input.nextInt();
    			total = total + grade;
    			counter++;
    		}
    		average = total/numGrades;
    		System.out.println("Your average is " + average + ".");
    	}
    }

    How could I change this to make it more challenging to create? I could have used multiple classes or multiple methods to do various tasks. For example, one method could be used to loop through the inputs, and another could be used to find the average. In fact, I'm going to do this now. Check it out.

    import java.util.Scanner;
    class Class1{
    	public static void main(String args[]){
    		int numGrades = 10;
    		System.out.println("Please enter " + numGrades + " numbers to be averaged");
    		//Start loop/average method
    		Class1 class1Object = new Class1();
    		class1Object.loopAverage(numGrades);
    	}
    	public void loopAverage(int numGrades){
    		int grade;
    		int counter = 0;
     
    		int total = 0;
    		int average;
    		Scanner input = new Scanner(System.in);
     
    		while(counter < numGrades){
    			grade = input.nextInt();
    			total = total + grade;
    			counter++;		
    		}
    		average = total/numGrades;
    		System.out.println("Your average is " + average + ".");		
    	}
    }

    I managed to use the main method for the introduction and to call the second method. I tried using three methods, one for the main method, one for the loop, and one for the average, however, when I tried sending variables from the loop method to the average method, it was reading the variables from the main method. I wasn't sure how to go about fixing this, so if anyone could help out, I'd really appreciate that. I'll look into it myself now.

    Maybe I could use something like this.

    public String getTotal(){
       return total;
    }

    But then how would this know to get the total from the loop method and not the main method? And I want to send the total from the loop method and the numGrades from the main method? Not sure how to go about doing this.
    Last edited by OA-OD-JD1; January 26th, 2012 at 12:40 AM.

  13. #38
    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
    There are also third type of comments and they are Java Doc Comments.
    I forgot to mention that. I actually haven't yet learned about the functionality of these comments, so I'll make sure to edit that post when I know what I'm talking about. Thanks for pointing that out mate.

  14. #39
    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

    But then how would this know to get the total from the loop method and not the main method? And I want to send the total from the loop method and the numGrades from the main method? Not sure how to go about doing this.
    Make the total as Instance variable and use getTotal() as getters method of your class so that you could use it anywhere and mark total as private(if it's instance variable).

  15. #40
    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
    Make the total as Instance variable and use getTotal() as getters method of your class so that you could use it anywhere and mark total as private(if it's instance variable).
    Not sure what any of that means, sorry. I'm still new to this and I'm not 100% on sending variables through methods and objects yet. Any chance you could provide an example?

  16. #41
    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

    class Example{
    	private int total;
    	public int getTotal(){
    		return total;
    	}
    	public void setTotal(int total){
    		this.total = total;
    	}
     
    	public static void main(String... args){
    		Example obj = new Example();
    		obj.setTotal(5);
    		System.out.println(obj.getTotal());
    	}
    }
    Hope this will help.

  17. #42
    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

    Okay, with relevance to my last post about the averaging calculator, I've written up the application exactly as I'd like it to look. The only problem is the variables, that's the last step. I've taken a screenshot to show you where the errors are.



    Here's the code.

    /* The application will work as follows. The main method will be used to reference the 
     * other methods into action. It will also produce an introductory message telling the
     * user to  input a certain number of grades. There will be a loop method responsible for
     * looping through the grades and finding a total. The average method will take this total
     * and use the number of grades (mentioned in the main method) to find the average. It will
     * then output the average of the greades.*/
    import java.util.Scanner;
    class Class1{
    	//This method is responsible for the intro' message and for introducing all other methods.
    	public static void main(String args[]){
    		int numGrades;
    		System.out.print("Please enter the number of grades you wish to work with: ");
    		Scanner numGradesInput = new Scanner(System.in);
    		numGrades = numGradesInput.nextInt();
    		System.out.println("You've chosen to average " + numGrades + " grades. Please enter these grades now.");
    	}
    	//This method is responsible for finding the total of the grades.
    	public void loop(){
    		int grade;
    		int total;
    		int counter = 0;
    		Scanner gradeInput = new Scanner(System.in);
     
    		//This loop finds the total.
    		while(counter < numGrades){
    			grade = gradeInput.nextInt();
    			total = total + grade;
    			counter++;
    		}
    		//The total now needs to be sent to the averaging method.
    	}
    	//This method is responsible for finding the average of the total found in the loop method.
    	public void average(){
    		int average;
    		average = total/numGrades;
    		System.out.println("The average of the " + numGrades + " grades you entered is " + average + ".");
    	}
    }

    I'm going to see what I can do about these variables.

  18. #43
    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

    Yeah they are marked with red underline because they are not initialized. And also they are local variables not instance variables. Only instance variables get auto initialized. You must initialize numGrades and all other local variables before using them.

  19. #44
    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
    Yeah they are marked with red underline because they are not initialized. And also they are local variables not instance variables. Only instance variables get auto initialized. You must initialize numGrades and all other local variables before using them.
    That's what I'm trying to do. This is what I ended up with. No errors, but it won't let me input anything into the console.

    /* The application will work as follows. The main method will be used to reference the 
     * other methods into action. It will also produce an introductory message telling the
     * user to  input a certain number of grades. There will be a loop method responsible for
     * looping through the grades and finding a total. The average method will take this total
     * and use the number of grades (mentioned in the main method) to find the average. It will
     * then output the average of the greades.*/
    import java.util.Scanner;
    class Class1{
    	//This method is responsible for the intro' message and for introducing all other methods.
    	public static void main(String args[]){
    		int numGrades;
    		System.out.print("Please enter the number of grades you wish to work with: ");
    		Scanner numGradesInput = new Scanner(System.in);
    		numGrades = numGradesInput.nextInt();
    		System.out.println("You've chosen to average " + numGrades + " grades. Please enter these grades now.");
    		Class1 class1Object = new Class1();
    		class1Object.setNumGrades(numGrades);
    	}
    	//Testing
    	private int numGradesFromMainMeth;
    	public void setNumGrades(int mainMethNumGrades){
    		numGradesFromMainMeth = mainMethNumGrades;
    	}
    	public int getNumGrades(){
    		return numGradesFromMainMeth;
    	}
    	//\Testing
     
    	//This method is responsible for finding the total of the grades.
    	public void loop(){
    		int grade;
    		int total = 0;
    		int counter = 0;
    		Scanner gradeInput = new Scanner(System.in);
     
    		//This loop finds the total.
    		while(counter < numGradesFromMainMeth){
    			grade = gradeInput.nextInt();
    			total = total + grade;
    			counter++;
    		}
    		//The total now needs to be sent to the averaging method.
    	}
    	//Testing
    		private int numTotalFromLoopMeth;
    		public void setTotalFromLoopMeth(int totalFromLoopMeth){
    			numTotalFromLoopMeth = totalFromLoopMeth;
    		}
    		public int getTotal(){
    			return numTotalFromLoopMeth;
    		}
    		//\Testing
    	//This method is responsible for finding the average of the total found in the loop method.
    	public void average(){
    		int average;
    		average = numTotalFromLoopMeth/numGradesFromMainMeth;
    		System.out.println("The average of the " + numGradesFromMainMeth + " grades you entered is " + average + ".");
    	}
    }

    You probably can't understand that because I've named variables horrendously.

    After spending another hour trying to figure out how this is done, I'm going to give up for the time being. I think I need to do a little more work with methods before trying this again. Thanks for your help, Mr.777.
    Last edited by OA-OD-JD1; January 26th, 2012 at 01:58 AM.

  20. #45
    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 For Loops

    The For Loop is probably the most common loop used in any programming language. Here's how it works in Java.

    class Class1{
    	public static void main(String args[]){
    		for(int counter=1; counter<=10; counter++){
    			System.out.println(counter);
    		}
    	}
    }

    The For Loop takes three arguments: where you want it to start, where you want it to end, and how you'd like to increment the counter. Take a look at the below For Loop setup.

    for(int counter=1; counter<=10; counter++)

    What this is saying is that it's going to start at 1. It's going to keep running until it reaches 11 (that's ten iterations). And it's going to increment the counter by one each time. Pretty simple.

  21. #46
    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 errors, but it won't let me input anything into the console.
    After getting the number of grades, you are not actually calling the loop() to let your program get input from the users for grades.
    System.out.println("You've chosen to average " + numGrades + " grades. Please enter these grades now.");
    		Class1 class1Object = new Class1();
    Call loop() between these two lines.

  22. #47
    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 Compound Interest Calculator

    Compound interest can be calculated with the following formula.

    A = Amount
    P = Principal
    R = Rate
    n = Years

    A=P(1+R)^n

    We can calculate the amount in Java as shown below. We currently know the principal (10,000) and the rate (1%). The years can change depending on how much information we want. In this application, we'll just use 20.

    class Class1{
    	public static void main(String args[]){
    		double amount;
    		double principal = 10000;
    		double rate = .01;
     
    		for(int year = 1; year <= 20; year++){
    			amount = principal * Math.pow(1+ rate, year);
    			System.out.println(year + "  " + amount);
    		}
    	}
    }

    We initialise our variables as per usual. We then construct a For Loop which will give us 20 iterations. Using our formula, we calculate the amount. We used the Math.pow function because it allows us to use indices. The first parameter is what you want in the parentheses and the second parameter is what you want the power to be. Quite a simple calculator.

  23. #48
    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
    After getting the number of grades, you are not actually calling the loop() to let your program get input from the users for grades.
    System.out.println("You've chosen to average " + numGrades + " grades. Please enter these grades now.");
    		Class1 class1Object = new Class1();
    Call loop() between these two lines.
    Still not sure how to do that. I tried something just now but that didn't work either. I'm kind of more guessing where things go with this one. Can't quite get my head around the best way to do this.

  24. #49
    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

    If I go back to my last successfully working averaging calculator, Mr.777, I have two questions.

    import java.util.Scanner;
    class Class1{
    	public static void main(String args[]){
    		int numGrades = 10;
    		System.out.println("Please enter " + numGrades + " numbers to be averaged");
    		//Start loop/average method
    		Class1 class1Object = new Class1();
    		class1Object.loopAverage(numGrades);
    	}
    	public void loopAverage(int numGrades){
    		int grade;
    		int counter = 0;
    		int total = 0;
    		int average;
    		Scanner input = new Scanner(System.in);
     
    		while(counter < numGrades){
    			grade = input.nextInt();
    			total = total + grade;
    			counter++;		
    		}
    		average = total/numGrades;
    		System.out.println("Your average is " + average + ".");		
    	}
    }

    I managed to send the grades from the main method using class1Object. The only thing preventing me sending the total from the loop method to an average method is that I can't use the class1Object outside of the main method. How can I do this?

  25. #50
    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 Do While Loop

    We use the Do While Loop when we want to run a set of instructions before testing to see if a condition is true. The difference between a Do While Loop and a While Loop is that the While Loop tests the condition first and then runs the instructions.

    class Class1{
    	public static void main(String args[]){
    		int counter = 0;
    		do{
    			System.out.println(counter);
    			counter++;
    		}while(counter <= 10);
    	}
    }

    We have a counter initialised to zero. It's going to then output the counter as zero and then increment it by 1. It then checks to see if the while section will permit it to iterate again.

    If our counter had been initialised to 50, it would output 50 and then end, because the while section sees that the counter isn't actually <= 10.

    The only perameter used in the while part is when you want it to iterate. In this case it's while the counter is less than or equal to 10. Simple enough?

Page 2 of 3 FirstFirst 123 LastLast

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