Results 1 to 17 of 17
  1. #1
    Moltisanti is offline Member
    Join Date
    Aug 2009
    Posts
    19
    Rep Power
    0

    Default Incorporating Methods into my program

    Trying to call my methods to compute my data...I think I got the gist of it down but I can't figure out how to retrieve the user's input so I can do the calculations...any help or hints appreciated. Just looking for help here, not to compromise academic integrity...thanks for looking

    Java Code:
            /**
             * @param args
             * @param 
             */
            public static void main(String[] args) 
            {
                    // TODO Auto-generated method stub
    
                    String studentsLast = null;
                   
                   
                    double gpa = 0;
                    
                    double avgGpa = 0, finaltuition=0;
                    boolean mathGpa;
                    double avgTuition=0, avgcredits=0, count=0, largest=0, freshman=0, sophomore=0;
                    double junior=0, senior=0;
                    boolean cisgpa;
                    String mathGpaname="";
                    double highmathgpa=0;
                    double firstpart=0, totalcredits=0;
                    int number = 0, studentsMajor=0;
                    double finalgpa=0, totalCredits=0;
                    String sstudentsmajor;
                    
                    
                    String output = "1. Math " + "\n" +
                    "2. CIS: " + "\n" +
                    "0 - End Input" + "\n" +
                    "Please enter the number of your major: ";
                    String studentsMajorString = JOptionPane.showInputDialog(null,
                    output, "Input Data",
                    JOptionPane.QUESTION_MESSAGE);
                    
                  //convert to integer
                    studentsMajor = Integer.parseInt(studentsMajorString);
                  
    
        while(studentsMajor != 0)
       {
    
            count++;
            studentsLast = JOptionPane.showInputDialog(null,
        "Enter student's last name: ", "Input Data",
        JOptionPane.QUESTION_MESSAGE);
    
    
            String numberOfCreditsString = JOptionPane.showInputDialog(null,
            ("Enter number of credits, " +
            "for example, 10: "), "Input Data",
            JOptionPane.QUESTION_MESSAGE);
    
            //Convert string to double
            double numberOfCredits = Double.parseDouble(numberOfCreditsString);
            totalCredits = totalCredits + numberOfCredits;
            avgcredits = totalCredits / count;
            if (numberOfCredits <=30){ freshman++;
            } else if (numberOfCredits <=60 && numberOfCredits >30){ sophomore++;
            }else if (numberOfCredits <=90 && numberOfCredits >60){junior++;
            }else if (numberOfCredits >90){ senior++;
            }
    
    
            String gpaString = JOptionPane.showInputDialog(null,
                            ("Enter the GPA achieved: "), "Input Data",
                            JOptionPane.QUESTION_MESSAGE);
            //Convert to double
            gpa  = Double.parseDouble(gpaString);
            finalgpa = finalgpa +gpa;
            avgGpa = finalgpa / count;
            if
    (gpa > largest)largest=gpa;
    
    //Highest Math GPA
        if((studentsMajor==1)&&(highmathgpa >gpa))
        {
          mathGpaname=studentsLast;
          highmathgpa=gpa;
        }
    
                    double largest3=0;
                    if (studentsMajor==2)
                            cisgpa = (gpa > largest3);
    
    
                            String tuitionPaidString = JOptionPane.showInputDialog(null,
                            ("Enter the total tuition paid, for example, 34000: "),
                                            "Input Data",
                            JOptionPane.QUESTION_MESSAGE);
            
    
    
    
    output = "Please enter the number of your major: " + "\n" +
        "1. Math " + "\n" +
        "2. CIS: " + "\n" +
        "0 - End Input" + "\n";
        sstudentsmajor = JOptionPane.showInputDialog(null,
        output, "Input Data",
        JOptionPane.QUESTION_MESSAGE);
    
        //Convert to integer
        studentsMajor = Integer.parseInt(sstudentsmajor);
       } // while loop
        
        print_info(firstpart);
      
        System.exit(0);
        }//main method
            
    
      
    public static String students_name (String name)
    {
    	
    	String sstudentsName = JOptionPane.showInputDialog(null,
                "Enter first name: ", "Input Data",
                JOptionPane.QUESTION_MESSAGE);
    	return sstudentsName;
    }
    
    
    
    private static double number_of_each_class(double sophomore, double junior, double senior, double senior2) {
    			// TODO Auto-generated method stub
    			double numberOfCredits=0, freshman=0;
    			 if (numberOfCredits <=30){ freshman++;
                 } else if (numberOfCredits <=60 && numberOfCredits >30){ sophomore++;
                 }else if (numberOfCredits <=90 && numberOfCredits >60){junior++;
                 }else if (numberOfCredits >90){ senior++;
                 }
    			return freshman;
    		}
    
    private static double average_tuition(double avgTuition) {
    			// TODO Auto-generated method stub
    			double finalTuition=0, tuitionPaid=0, count=0;
    			finalTuition = finalTuition + tuitionPaid;
                avgTuition = finalTuition / count;
                System.out.println("The average tuition paid is: " +(avgTuition));
    return avgTuition;
    		}
    
    private static double final_tuition_paid(double finaltuition) {
    			double finalTuition=0, tuitionPaid=0;
    			return finalTuition = finalTuition + tuitionPaid;
    }
    
    private static double average_gpa(double avgGpa) {
    			// TODO Auto-generated method stub
    			 double gpa=0, finalgpa=0, count=0, largest=0;
    			
         
                 finalgpa = finalgpa +gpa;
         avgGpa = finalgpa / count;
         
         
         System.out.println("The average GPA is:  " +(avgGpa));
         
         
         if
    (gpa > largest)largest=gpa;
        
        
    	System.out.println("The highest GPA is: " + (largest));
    	return largest;
    	
         
    		}//Method
    
    private static double average_number_of_credits_taken(double avgcredits) {
    			// TODO Auto-generated method stub
    			double totalCredits=0, count=0, numberofCredits=0;
    			
                totalCredits = totalCredits + numberofCredits;
                  avgcredits = totalCredits / count;
                
                  //Show output
                  System.out.println("The average number of credits taken is: "+(avgcredits));
    			return avgcredits; 
                 
    		}//Method
    private static double total_credits(double totalcredits){
    	double totalCredits=0, numberofCredits=0;
    	totalCredits = totalCredits + numberofCredits;
    	String totalCredits1  = JOptionPane.showInputDialog(null,
                "Enter total credits: ", "Input Data",
                JOptionPane.QUESTION_MESSAGE);
    	return numberofCredits;
    }
    
    
    public static double print_info(double firstpart) {
    	double count=0, avgTuition=0, finaltuition=0, tuitionPaid=0;
    			count++;
    			double print_info = 0;
    	String avgGpa="", avgcredits="";
    	finaltuition = finaltuition + tuitionPaid;
    	avgTuition = finaltuition / count;
    	
    	// print the first part
    	String output = "The average GPA is: " + avgGpa + "\n" +
    	"The final tuition is: " + finaltuition + "\n" +
    	"The average tuition is: " + avgTuition + "\n" +
    	"The average credits taken are: " + avgcredits + "\n";
    	JOptionPane.showMessageDialog(null, output, " ",
                JOptionPane.INFORMATION_MESSAGE);
    	return print_info;
    //End first part method
    			
    		}
    
    
        //class
    }

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    What do you mean by retrieve the users input? Isn't that what your JOptionPanes are doing? You set the inputDialog equal to some variable and once the user interacts with the dialog box it should retrieve it. What errors(if any) are you getting or what isn't working as expected?
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    Moltisanti is offline Member
    Join Date
    Aug 2009
    Posts
    19
    Rep Power
    0

    Default

    private static double average_tuition(double avgTuition) {
    // TODO Auto-generated method stub
    double finalTuition=0, tuitionPaid=0, count=0;
    finalTuition = finalTuition + tuitionPaid;
    avgTuition = finalTuition / count;
    System.out.println("The average tuition paid is: " +(avgTuition));
    return avgTuition;
    }


    In that method, when I try to retrieve the user's input, which would be "tuitionPaid", I am getting either NAN or 0.

  4. #4
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    Your not even calling your average_tuition method anywhere in your main program.

    In your main you'd have to do something like
    average_tuition(myVariable);
    that will call the method and pass myVariable into the method. Since that method is declared with a double, myVariable would have to be a double.

  5. #5
    Moltisanti is offline Member
    Join Date
    Aug 2009
    Posts
    19
    Rep Power
    0

    Default

    I tried starting over from scratch because that code was going no where for me....

    Same thing on the average tuition...I am getting an operator error when I try to use the slash for division.

    Java Code:
    String sMajor, sLastName, screditsCompleted, sgpa=null, stuitionPaid = null;
     String savgGPA;
     int major, creditsCompleted=0,tuitionPaid=0;
     double gpa=0, count=0, avgGpa = 0, avgTuition=0, avgCredits=0;
            	
            	
            	
     sMajor = menu();
     //Convert to integer
     major = Integer.parseInt(sMajor);
     
     while (major !=0)
     {
    
    count++;
    	 
    
    sLastName = students_last();
    
    screditsCompleted = total_credits();
    //Convert to Integer
    creditsCompleted = Integer.parseInt(screditsCompleted);
    
    avgCredits = creditsCompleted / count;
    
    sgpa = students_GPA() + "";
    //Convert to Double
    gpa = Double.parseDouble(sgpa);
    
    avgGpa = gpa / count;
    
    stuitionPaid = students_total_tuition() + "";
    //Convert to Integer
    tuitionPaid = Integer.parseInt(stuitionPaid);
    
    average_tuition();
    
    sMajor = menu();
    //Convert to integer
    major = Integer.parseInt(sMajor);
    
     }     //While Loop Thingy
     
     String output = "The average GPA is: " + sgpa + "\n" +
     "The average credits taken: " + creditsCompleted + "\n" +
     "The average tuition paid is: " + avgTuition + "\n" +
     "The total tuition paid is: " + stuitionPaid;
     System.out.println(output);
     
            } //End Main Method!!!!!
     
    
     
          
            
    
    
    	private static double average_tuition()
    	{
    		double totalTuition=0;
    		double count =0;
    		totalTuition / count;
    		return 0;
    			// TODO Auto-generated method stub
    			
    		}
    
    
    
    
    
    
    
    	public static String students_total_tuition()
         {
        	 String stuitionPaid;
    		double tuitionPaid = 0;
        	 String totalTuition = null;
        	 String output = "Enter the total tuition paid (example - 34000): ";
        	 stuitionPaid = JOptionPane.showInputDialog(null,
        	output, "Input Data",
        	JOptionPane.QUESTION_MESSAGE);
        	 
        	 //Convert to double
        	 tuitionPaid = Double.parseDouble(stuitionPaid);
        	 
        	 totalTuition = totalTuition + stuitionPaid;
        	 return stuitionPaid;
         } //Tuition Paid Method
            
         public static String students_GPA()
         {
        	 String sgpa;
        	 String Output = "Enter the GPA earned: ";
        	 sgpa = JOptionPane.showInputDialog(null,
        	Output, "Input Data",
        	JOptionPane.QUESTION_MESSAGE);
        	 sgpa = sgpa + "";
        	 return sgpa;
        	
        	 }// Students GPA Method
         
         public static int high_math(int sgpa)
         {
        	 int largest = 0;
        	 
    		if
        	 (sgpa > largest)largest=sgpa;
        	 System.out.println("The highest GPA is: " + (largest));
        		return largest;
         }
         
    
         public static String total_credits() 
         {
    		String screditsCompleted;
    		String Output = "Enter the number of credits completed: ";
    		screditsCompleted = JOptionPane.showInputDialog(null,
    		Output, "Input Data",
    		JOptionPane.QUESTION_MESSAGE);
    		return screditsCompleted;
    		}// Credits Completed Method
         
         
    
    	public static String menu()   
         {
        	 String sMajor;
        	 String output = "1. Math " + "\n" +
             "2. CIS: " + "\n" +
             "0 - End Input" + "\n" +
             "Please enter the number of your major: ";
             sMajor = JOptionPane.showInputDialog(null,
             output, "Input Data",
             JOptionPane.QUESTION_MESSAGE);
             return sMajor;
         } // Menu Method
         
         public static String students_last()
         {
        	 String sLastName;
        	 String output = "Enter the students last name: ";
        	 sLastName = JOptionPane.showInputDialog(null,
        	 output, "Input Data",
        	 JOptionPane.QUESTION_MESSAGE);
        	 return sLastName;
         } // Student's Last Name Method
         
         public static void print_requirements()
         {
        	 
         }
         
            
    }

  6. #6
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    Probably because you can't divide by 0. You're setting totalTuition = 0 and count = 0. 0/0 is not possible.

  7. #7
    Moltisanti is offline Member
    Join Date
    Aug 2009
    Posts
    19
    Rep Power
    0

    Default

    It keeps throwing me an error unless I intialize that variable.

  8. #8
    Moltisanti is offline Member
    Join Date
    Aug 2009
    Posts
    19
    Rep Power
    0

    Default

    I know I am missing something very stupid here...so how do I initialize the variable without it throwing me an error? Right now, the only way I know how to do that is int largest = 0.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you repost your code with good indentation and spacing, probably more will be willing to read it and to help you. Just a suggestion made in your best interest. Best of luck.

  10. #10
    Moltisanti is offline Member
    Join Date
    Aug 2009
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    If you repost your code with good indentation and spacing, probably more will be willing to read it and to help you. Just a suggestion made in your best interest. Best of luck.
    You're talking to a Java newB....I really don't know the "guidelines" for proper indentation and spacing. I thought I cleaned this one up pretty well.

    Not being sarcastic or an ass hole at all, man...I just am not sure what you mean by that. Thanks for the help, though.

  11. #11
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    read my previous post #6 and try posting your error as well

  12. #12
    Moltisanti is offline Member
    Join Date
    Aug 2009
    Posts
    19
    Rep Power
    0

    Default

    private static double average_tuition()
    {
    double totalTuition=0;
    double count =0;
    totalTuition / count;
    return 0;

    My error is occurring under my division symbol...invalid assignment operator.

    I totally understand the 0 thing. But if I don't put =0 after the variable, I get a "variable not initialized" error.

    I want the program to read the user's input so when I put totalTuition, it will recognize it from the other part of the program and know what I am talking about.

    This program was written without methods and worked a lot smoother. But I need to incorporate methods into this assignment. The problem is, every time I try to retrieve information, it makes me initialize the variables again which nixes the user's input.

    Is this maybe because I have the methods in the wrong place? Should they be in the while loop?

  13. #13
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    You don't understand the division by 0. That will not work. Your not passing anything into your average_tutition() method therefore it's always going to be 0 / 0 and it's always going to give you an error.

    If you do this

    double myVar = 12;
    double counter = 1;

    average_tuition(myVar, counter); //calls the average_tuition method and passes myVar and counter in as the parameter

    private static double average_tuition(double variable, double count)
    {
    return variable / count;
    }
    this would set count to 1 and variable is a formal parameter that would equal 12 because you passed in myVar with the value of 12 and counter with the value of 1.

    You don't have to declare the variables inside the method but if you do, you're going to have to pass in variables if you want them to have a value.

  14. #14
    gcampton Guest

    Default

    indentation typically is a set number of spaces, and occurs every selection statement and method. I generally use 4 spaces per tab, some use 8, some use 2, I think 2 is a little too small, especially when dealing with switch statements indentation is generally 1/2 tab width so, either use 4 or 8 as a guideline. Secondly true Object oriented code, should not reside in main. Main is simply a way for us the programmers to test our code. Say for example I contract you to write some code that sends very sensitive data to and from my PDA, you don't know what this data is but you have a general description it is Strings and ints. This data deals with electrical sub-stations that I need to send back to main base, and recieve new data on how to configure the systems in the sub-station. Now if you simply give me a main method with a bunch of jargon in it, there's no way for me to be able to use it. As I need class files that I can implement myself. Using your javadocs that you send me as well.

    So really what you want to do is write a standard java class called Tuition or something and a Main class to drive it so "YOU!" can see it is working :)

    so your Tuition class has all you code which starts by a method in that class called runApp() or something more appropriate;

    your main class simply calls this:
    Java Code:
    public static void main(String[] args)
    {
        // declare some variables
        final int QUIT = 0;
        int choice = 1;
    
        // create an object of your class
        Tuition myTuit = new Tuition();
        while (choice != QUIT)
        {
             choice = myTuit.runApp();
        }
    }
    There's probably better ways to run your class, as this requires you driver to return an integer to show whether to quit or keep running the class.

    as for indentation:
    Java Code:
    class Something {
        if (something) {
            if (something else) {
                if (something) {
                }
            }
            else if (something) {
            }
        }
    }
    
    another way to do this is:
    
    class Something
    {
        if (something)
        {
            if (something else)
            {
                if (something)
                {
                }
            }
            else if (something)
            {
            }
        }
    }
    
    I prefer the latter as it is a lot easier to see the code blocks and matching braces which makes
    debugging easier, but either way is acceptable as when you compile all spaces
    and braces are stripped from code anyway, having varying spaces such as:
    
    if (asdasd fsd) {
    
    adsdsa();
    }
                   if (some stuff)
         {
                         doThisNow(orElse);
       }
    this is very bad practice, and I know your code was nowhere near this bad but it's just an example of what not to do.

    remember you need to be able to read other peoples code, and they need to be able to read your code. Because a: this is a forums where people seek help, B: when working in teams if you don't follow standards then people will start bringing semi-automatics to work, and C: if you are working on someone elses code, or someone is working on your old code and can't read it, they will want to smash your head repeatedly with a keyboard... and vice versa. :D :D :D
    Last edited by gcampton; 10-10-2009 at 06:39 PM. Reason: typo

  15. #15
    Moltisanti is offline Member
    Join Date
    Aug 2009
    Posts
    19
    Rep Power
    0

    Default

    So I need a new class for each method I am coding? If i read that correctly. Thanks a ton for your help so far. Much appreciated.

  16. #16
    gcampton Guest

    Default

    ok maybe I worded that wrong, you don't need a new class for each method, but you do need a new class for each object you want to create. For example. lets say we want to make a shape class, we have shape as the super class, and circle, square, triangle, as extensions of shape, shape needs a driver, while we could simply drive the program via main, and for most purposes in the classroom this is fine. But as a standard you should have as little as possible in your main method, having your "driver" the code that drives the program in a separate method is usually better practice, as this makes your code more reusable. So in this instance I would create a main that calls a TestShape class which drives the program.
    whether this driver method is below your main static, as a static function itself or in a class of it's own, doesn't matter too much in most classroom situations. But you will find that as you start coding more and more you will find you will want to store a lot of work in libraries, so you can re-use it for other programs. If you have 101 different main methods in this "personal collections" folder then reusing this code will be difficult. Another scenarios are abundant, but this is just an example.

    I didn't want you to take that part too seriously as you will most likely learn that lesson soon anyway. But want I did want to drill into you was your syntax, the indentation of your methods were quite shocking, and things need to line up, the reason being as it is easier to see blocks of code, if code and spaces / brackets are all over the place like the last example I gave in the code block, then it's really hard to read, and hard for people to help.

    Java Code:
     while(studentsMajor != 0)
       {
    this as an example, I can see that this method has no closing curly brace, while it probably does, I cannot see it because you have selection statements(if statements) in the way of the code block, as 1 example, also many other things don't line up so it makes the code very annoying to read, people want to read your logic to help you, not check every little matching brace to make sure they start and end.
    this should be written
    Java Code:
    while (studentsMajor != 0)
    {
         ...;
         ...;
         ...;
         if(true) 
         {
              .....;
              .....;
         }
         ...;
         ...;
    }
    Last edited by gcampton; 10-12-2009 at 05:33 AM.

  17. #17
    Moltisanti is offline Member
    Join Date
    Aug 2009
    Posts
    19
    Rep Power
    0

    Default

    Kinda ironic...My code was indented and pretty much followed the guidelines you just suggested, but I moved everything over before I posted it on here because I thought it was too sloppy. My fault on doing that. Thanks for the advice. =-)

    As far as the other information, I will have to read it more tomorrow...I am headed to bed now. I am going to try to fix the program so it works and see what happens. Much thanks again.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Replies: 4
    Last Post: 04-03-2009, 06:36 AM
  3. Program To Call Random Methods
    By nyamtoko in forum Advanced Java
    Replies: 5
    Last Post: 12-01-2008, 11:49 AM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM

Posting Permissions

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