Results 1 to 16 of 16
  1. #1
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default Need help getting average

    My code basically gets teacher assistants and their hourly wages. After I enter all the info I want to get the average of their hourly wages. This is my code

    Teacher class
    Java Code:
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    public class Teacher{
    
        private String name;
        private double price;
        private static String [] names = new String [21];
        private static double [] prices = new double [21];
        private static boolean [] availability = new boolean [21];
    
    
        private static int numberOfTeachers = 0;
    
        public Teacher(String n, double p)
        {
            numberOfTeachers++;
            names[numberOfTeachers] = n;
            prices[numberOfTeachers] = p;
            availability[numberOfTeachers] = true;
            //id = numberOfTeachers;
        }
    
        public static String getWhosAvailable()
        {
            String available = "";
            for(int i = 1; i <= numberOfTeachers; i++)
            {
                if(availability[i] == true)
                    available = available+""+i+" - "+names[i]+" (is available)\n";
                if(availability[i] == false)
                    available = available+""+i+" - "+names[i]+" (is NOT available)\n";
            }
    
            return available;
    
        }
        public double getPrice(){return price;}
        public void setPrice(double p){price = p;}
    
        public static void setAvailability(int pos)
        {
            availability[pos] = false;
        }
    
        public static int getNumberOfTeachers() {return numberOfTeachers;}
    
    
    
    
    }
    TEACHER ASSISTANT CLASS (RUN CLASS)
    Java Code:
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    class TeacherAssistant
    {
    	public static void main(String[] args)
    	{
    
        String name;
    	double price;
    	String s;
    	int option;
        String opt;
    	String msg = "";
    
    
    	Teacher[] teacherAssistant = new Teacher[20];
    	option = JOptionPane.showConfirmDialog(null, "New Teacher Assistant? ");
    	while (option == JOptionPane.YES_OPTION) // if true; YES_OPTION > 0
    		{
    			name = JOptionPane.showInputDialog("Enter Teacher Assistant Name: ");
    			s = JOptionPane.showInputDialog("Enter Hourly Wage: ");
    			price = Double.parseDouble(s);
    
    			while((price < 8.00) || (price > 18.00))
    			{
    				s = JOptionPane.showInputDialog("Error: The hourly wage must be between $8.0 and $18.0");
    				price = Double.parseDouble(s);
    			}
    
    			new Teacher(name, price);
    			option = JOptionPane.showConfirmDialog(null, "New Teacher Assistant?");
    
            }
            option = 0;
            while (option == JOptionPane.YES_OPTION)
            {
                String list = Teacher.getWhosAvailable();
                opt = JOptionPane.showInputDialog("Choose number of TA to assign:\nList of TAs and availability:\n\n"+list);
                Teacher.setAvailability(Integer.parseInt(opt));
                option = JOptionPane.showConfirmDialog(null, "Another assignment?");
    
            }
    
    		String list = Teacher.getWhosAvailable();
    		JOptionPane.showMessageDialog(null, "List of TAs and availability:\n\n"+list + "\n\n NEED AVERAGE HERE!!!!");
    "
    
    
    
    	 	} //end of main
    } //end of class
    I tried this code for the average but it does not work
    Java Code:
    		double sum = 0;
    				for (int i = 0; i < Teacher.getNumberOfTeachers(); i++)
    				{
    					sum = sum + prices[i].getPrice();
    				}
    				double average = sum / Teacher.getNumberOfTeachers();
    		System.out.println(average);

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Define "does not work". What does it print out VS what it should print out?

  3. #3
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    Define "does not work". What does it print out VS what it should print out?
    well it doesnt print out the average, and it should print out the average. Sorry i dont know how to answer your question any clearer

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Well, let's say your prices[] array has prices 5, 17, 32, and 8. What it should print out is 15.5. But if your code is incorrect, (let's say it divides by 3 instead of 4), it prints out 20.7.

    So what is the exact value of the average printed, and what is the value you wish to receive instead?

  5. #5
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    Well, let's say your prices[] array has prices 5, 17, 32, and 8. What it should print out is 15.5. But if your code is incorrect, (let's say it divides by 3 instead of 4), it prints out 20.7.

    So what is the exact value of the average printed, and what is the value you wish to receive instead?
    just gives me a bunch of compiler errors, i cant even get it to run

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Please copy and paste all compiler errors here. (Do not clip them or leave anything out. Screenshot them if necessary.)

    I get the feeling that one is a problem with getPrice. Your prices[] array is a series of doubles, whereas the getPrice method is a method of Teacher (not double).

  7. #7
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default

    I did some minor changes to my code and now I only get one compiler error
    Java Code:
            double sum = 0;
                    for (int i = 0; i < Teacher.getNumberOfTeachers(); i++)
                    {
                        sum = sum + teacherAssistant[i].getPrice();
                    }
                    double average = sum / Teacher.getNumberOfteachers();
            System.out.println(average);
    Compiler:error
    C:\Users\Victor\Documents\My Received Files\TeacherAssistant.java:67: cannot find symbol
    symbol : method getNumberOfteachers()
    location: class Teacher
    double average = sum / Teacher.getNumberOfteachers();
    **************************^
    1 error

    Tool completed with exit code 1




    Also where that code goes is at the end of the class TeacherAssistant

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Case is very important in programming.

    Note that your method is named getNumberOfTeachers, not getNumberOfteachers.

  9. #9
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default

    cant believe i missed that, anyway now it still doesnt print it out

    it prints out this
    Exception in thread "main" java.lang.NullPOinterException
    at TeacherAssistant.main(TeacherAssistant.java:65)

    line 65 =************ sum = sum + teacherAssistant[i].getPrice();

  10. #10
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Your new teacher object is never added to the teacherAssistant array. You will want to do that on the line new Teacher(name, price);.

  11. #11
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default

    could you show me how to do that? or give me an example of how to do it. I really have no idea how to do what you just said

  12. #12
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Consider how you did this:
    Java Code:
    availability[numberOfTeachers] = true;

    Now, instead, you are adding the new Teacher to the array. Something like this:
    Java Code:
    teacherAssistant[i] = new Teacher(name,price);
    ...where i is the index of the first null element. So if you have this array:
    Java Code:
    { a, b, c, d, null, null, null, null, ... null }; // 21 elements long
    ...then i would be set to 4.

    Note: Though I haven't actually run your code, it appears as though numberOfTeachers could be used as the index for the new Teacher's position in the array.

  13. #13
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default

    for (int i = 0; i < Teacher.getNumberOfTeachers(); i++)
    {
    sum = sum + teacherAssistant[i].getPrice();
    }
    double average = sum / Teacher.getNumberOfTeachers();

    wouldnt that avoid the null pointer?
    Last edited by soccer_kid_6; 09-13-2010 at 12:54 AM.

  14. #14
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    No, because right now, you are not adding the Teacher elements to the array. Basically here is what you are doing, procedurally:
    Java Code:
    Teacher[] teacherAssistant = new Teacher[20];
    new Teacher("SomeName",50.0);
    new Teacher("AnotherName",60.0);
    new Teacher("AThirdName",70.0);
    // Here, teacherAssistant is still equal to {}, as it has no elements added to it.
    for (int i = 0; i < 3; i++) {
        // Your averaging code
    }

  15. #15
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default

    I am so confused right now, how do i fix this then?

  16. #16
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by Zack View Post
    Now, instead, you are adding the new Teacher to the array. Something like this:
    Java Code:
    teacherAssistant[i] = new Teacher(name,price);
    Your teacherAssistant array has to have the new Teacher objects created added to the array.

    If you look back at my example in my last reply, the correct code should look like this instead:
    Java Code:
    Teacher[] teacherAssistant = new Teacher[20];
    teacherAssistant[0] = new Teacher("SomeName",50.0);
    teacherAssistant[1] = new Teacher("AnotherName",60.0);
    teacherAssistant[2] = new Teacher("AThirdName",70.0);
    for (int i = 0; i < 3; i++) {
        // Your averaging code
    }
    PS: If you'd like, I can put together a small working example code that you can work with. Just ask and I'll see what I can do.

Similar Threads

  1. Calculating average
    By clocksaysits9 in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 06:03 AM
  2. Replies: 1
    Last Post: 03-11-2010, 05:21 AM
  3. Help compeleting four average mark.
    By lebron11 in forum New To Java
    Replies: 7
    Last Post: 03-08-2010, 07:11 PM
  4. Calculate Average
    By sthack99 in forum New To Java
    Replies: 4
    Last Post: 06-13-2008, 12:09 PM
  5. Calculate average age for women and men?
    By Legoland in forum New To Java
    Replies: 3
    Last Post: 04-18-2007, 11:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •