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.

Results 1 to 5 of 5

Thread: Simple averaging program using two different classes

  1. #1
    Junior Member Crash's Avatar
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Simple averaging program using two different classes

    This is a simple project that i was using to test my current java knowledge (kinda like revision) and for that i used two classes to make a simple averaging program. i know i0m making it more difficult but like i said i'm testing myself but i come up with an unexpected error. Here's the code:

    - 1st Class
    import java.util.Scanner;
     
    public class Main {
     
    	public static int num;
    	public static int total = 0;
    	public static int average;
     
    	public static void main(String args[]) {
    		Scanner scanner = new Scanner(System.in);
     
    		Secondary secondaryObject = new Secondary();
    		secondaryObject.loop(num, total, scanner);
     
                    average = total/3;
     
    		System.out.println("Average: " + average);
    	}
    }

    - 2nd Class
     import java.util.Scanner;
     
    public class Secondary {
    	public void loop(int num, int total, Scanner scanner) {
    		int counter = 0;
     
    		while(counter < 3) {
    			num = scanner.nextInt();
    			total += num;
    			counter++;
    		}
    	}
    }

    Now the problem is after inputing the numbers it doesn't give me the average but the value of 0. Althought this is not that big of a deal that just made me want to know why and if you could tell me that would be much appreciated.
    Last edited by Crash; April 9th, 2014 at 10:25 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Simple averaging program using two different classes

    doesn't give me the average but the value of 0.
    How is the average computed? What variable has the total and what variable the count? What variable is assigned the average?

    Is integer math being used: 4/5 = 0 vs floating math: 4/5.0 = 0.8
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member Crash's Avatar
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Simple averaging program using two different classes

    What i did was creating a variable to hold the inputed numbers and then one to hold the total. Then this two are asigned with the while loop in the 2nd Class being num the inputed value and total the adition of num values each time the loop runs. Now the problem is with the average variable that insted of outputting total divided by three outputs 0. My thought is that the error is with the num variable assignment or with the total variable assignment in the while loop (but of course just a thought).

    Sorry but i still dont know the difference betwen integer math and floating math.

    --- Update ---

    To demonstrate a working code i made the program using only one class. Here's the code:
    import java.util.Scanner;
     
    public class MainEXAMPLE {
    	public static void main(String args[]) {
    		Scanner scanner = new Scanner(System.in);
     
    		int num;
    		int total = 0;
    		int average;
    		int counter = 0;
     
    		while(counter < 3) {
    			num = scanner.nextInt();
    			total += num;
    			counter++;
    		}
     
    		average = total/3;
     
    		System.out.println("Average: " + average);
    	}
    }

    This code works and in the end it gives the correct average. Hope it's useful.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Simple averaging program using two different classes

    the difference between integer math and floating math.
    An example: integer math: 4/5 = 0 vs floating math: 4/5.0 = 0.8
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member Crash's Avatar
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Simple averaging program using two different classes

    Got it now. I'm using integer math.

Similar Threads

  1. Beginner Averaging Grade - ALMOST COMPLETE
    By m0ranwad in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 23rd, 2014, 05:27 PM
  2. 3 classes - Simple school assignment - ArrayLists
    By TSSF44 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 15th, 2014, 01:19 AM
  3. How do improve the accuracy of this averaging program
    By Alge4 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 10th, 2013, 09:23 PM
  4. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM
  5. Grade averaging program with array and multiple methods.
    By jeremykatz in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 9th, 2009, 09:44 PM