Welcome to Dream.In.Code
Become a Java Expert!

Join 150,198 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,005 people online right now. Registration is fast and FREE... Join Now!




Static Methods.... //wtf

 
Closed TopicStart new topic

Static Methods.... //wtf

hazezoos
24 Sep, 2008 - 06:00 PM
Post #1

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 9



Thanked: 1 times
My Contributions
My question is.. what the hell is so great about using a static method?.. My code is fine w/o it.. look ---->

CODE


/**
* @(#)Mainprog.java
*
*
* @author
* @version 1.00 2008/9/18
*/
import java.util.Scanner;

    public class Mainprog{

    public Mainprog() {
        super();
    }
    
    public static void main(String[] args){
        
    Scanner input = new Scanner(System.in);
    
    System.out.println("Please enter the number of students: ");
    
    int promptsize = input.nextInt();  
        
    Student[] studentarray = new Student[promptsize];
    
    for (int i = 0; i < studentarray.length; i++){
        System.out.println("Please enter the first and last name for student" + " #" + (i+1) +":");
        String first = input.nextLine();
        String last = input.nextLine();
        System.out.println("Please enter the total credits for student" + " #" + (i+1) +":");
        int credits = input.nextInt();
        System.out.println("Please enter the GPA for" + " #" + (i+1) +":");
        double gpa = input.nextDouble();
        studentarray[i] = new Student(first, last, credits, gpa);        
    }
    
    System.out.println("  Student Name" + "       " + "Credits" + "       " + "GPA");
    
    double size = promptsize * 1.0;
    double avggpa = 0;
    
    for (int f = 0; f < studentarray.length; f++){
    avggpa += (1.0 * (studentarray[f].getgpa()));
    }
    
    System.out.println("\n" + "Average GPA = " + avggpa / size );
    }
    
}



if someone wants, show me how to turn the avggpa array into a static method, please.. (for use in output in this class)
User is offlineProfile CardPM
+Quote Post

hazezoos
RE: Static Methods.... //wtf
24 Sep, 2008 - 06:06 PM
Post #2

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 9



Thanked: 1 times
My Contributions
My question is.. what the hell is so great about using a static method?.. My code is fine w/o it.. look ---->

CODE


/**
* @(#)Mainprog.java
*
*
* @author
* @version 1.00 2008/9/18
*/
import java.util.Scanner;

    public class Mainprog{

    public Mainprog() {
        super();
    }
    
    public static void main(String[] args){
        
    Scanner input = new Scanner(System.in);
    
    System.out.println("Please enter the number of students: ");
    
    int promptsize = input.nextInt();  
        
    Student[] studentarray = new Student[promptsize];
    
    for (int i = 0; i < studentarray.length; i++){
        System.out.println("Please enter the first and last name for student" + " #" + (i+1) +":");
        String first = input.nextLine();
        String last = input.nextLine();
        System.out.println("Please enter the total credits for student" + " #" + (i+1) +":");
        int credits = input.nextInt();
        System.out.println("Please enter the GPA for" + " #" + (i+1) +":");
        double gpa = input.nextDouble();
        studentarray[i] = new Student(first, last, credits, gpa);        
    }
    
    System.out.println("  Student Name" + "       " + "Credits" + "       " + "GPA");
    
    double size = promptsize * 1.0;
    double avggpa = 0;
    
    for (int f = 0; f < studentarray.length; f++){
    avggpa += (1.0 * (studentarray[f].getgpa()));
    }
    
    System.out.println("\n" + "Average GPA = " + avggpa / size );
    }
    
}



if someone wants, show me how to turn the avggpa array into a static method, please.. (for use in output in this class)
User is offlineProfile CardPM
+Quote Post

pbl
RE: Static Methods.... //wtf
24 Sep, 2008 - 06:07 PM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Whihout that static method

CODE

public static void main(String[] args)


you wouldn't be able to run your code

And actually all your code is executed within that static method.

This post has been edited by pbl: 24 Sep, 2008 - 06:08 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Static Methods.... //wtf
24 Sep, 2008 - 06:11 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Please avoid double posting mad.gif

Topic merged
User is offlineProfile CardPM
+Quote Post

hazezoos
RE: Static Methods.... //wtf
24 Sep, 2008 - 06:19 PM
Post #5

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 9



Thanked: 1 times
My Contributions
ok ok.. but check this out..

my teacher wants the data entry/ the array print/ and the avg gpa.. in seperate methods..

so, can i have to have a main program with 4 seperate static methods?

whats the point of that?
User is offlineProfile CardPM
+Quote Post

pbl
RE: Static Methods.... //wtf
24 Sep, 2008 - 06:33 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(hazezoos @ 24 Sep, 2008 - 07:19 PM) *

ok ok.. but check this out..

my teacher wants the data entry/ the array print/ and the avg gpa.. in seperate methods..

so, can i have to have a main program with 4 seperate static methods?

whats the point of that?


Ask your teacher not me.

On the other hand, you can limit your STATIC main method to only one line that creates an instance of Mainprog and have all your code in Mainprog. Then you won't hae to write any more static method if you don't like them

CODE

import java.util.Scanner;

public class Mainprog{

    public Mainprog() {
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter the number of students: ");

        int promptsize = input.nextInt();  

        Student[] studentarray = new Student[promptsize];

        for (int i = 0; i < studentarray.length; i++){
            System.out.println("Please enter the first and last name for student" + " #" + (i+1) +":");
            String first = input.nextLine();
            String last = input.nextLine();
            System.out.println("Please enter the total credits for student" + " #" + (i+1) +":");
            int credits = input.nextInt();
            System.out.println("Please enter the GPA for" + " #" + (i+1) +":");
            double gpa = input.nextDouble();
            studentarray[i] = new Student(first, last, credits, gpa);        
        }

        System.out.println("  Student Name" + "       " + "Credits" + "       " + "GPA");

        double size = promptsize * 1.0;
        double avggpa = 0;

        for (int f = 0; f < studentarray.length; f++){
            avggpa += (1.0 * (studentarray[f].getgpa()));
        }

        System.out.println("\n" + "Average GPA = " + avggpa / size );
    }

// one static method to create a Mainprog
    public static void main(String[] args){
        new Mainprog();
    }

}

User is offlineProfile CardPM
+Quote Post

hazezoos
RE: Static Methods.... //wtf
24 Sep, 2008 - 06:38 PM
Post #7

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 9



Thanked: 1 times
My Contributions
you didnt help me at all.. no wonder your name says dick lover..
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Static Methods.... //wtf
24 Sep, 2008 - 06:39 PM
Post #8

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,166



Thanked: 78 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
Awesome attitude. I bet you'll go far.

Any other bridges that you care to burn?

** Topic Closed **
User is offlineProfile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 1/9/09 04:52AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month