Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

My teacher just told me that whenever I create a class to run something for a video game company that uses Eclipse, I should make a run class with the main and any outputs. He says any arithmetic should then be put in its own class. Here's the example he gave us.

//triangle

import java.util.Scanner;
import java.lang.Math.*;
import java.lang.String;
public class Lab03a  //this class is used to test Triangle
{
public static void main( String[] args )
{
    Scanner keyboard = new Scanner(System.in);

    //ask for user input
    System.out.print("Enter side A ::  ");
    int a = keyboard.nextInt();

    System.out.print("Enter side B ::  ");
    int b = keyboard.nextInt();

    System.out.print("Enter side C ::  ");
    int c = keyboard.nextInt();


    Triangle test = new Triangle(a, b, c);
    test.calcPerimeter();
    test.calcArea();
    test.toString();
    System.out.println("Area "+test.toString());

    //ask for user input
    System.out.print("Enter side A ::  ");
    a = keyboard.nextInt();

    System.out.print("Enter side B ::  ");
    b = keyboard.nextInt();

    System.out.print("Enter side C ::  ");
    c = keyboard.nextInt();


    test.setSides(a,b,c);
    test.calcPerimeter();
    test.calcArea();
    test.toString();
    System.out.printf("%.3f\n",(test.toString()));

    //add one more input section




}
}

The other class:

import java.util.Scanner;
import java.lang.Math.*;

public class Triangle
{
private int sideA, sideB, sideC;
private double perimeter;
private double theArea;
private double s;
public Triangle()
{
   setSides(0,0,0);
   perimeter=0;
   theArea=0;
   s=0;
}

public Triangle(int a, int b, int c)
{
    sideA=a;
    sideB=b;
    sideC=c;
}


public void setSides(int a, int b, int c)
{
    sideA=a;
    sideB=b;
    sideC=c;

}

public void calcPerimeter( )
{
    perimeter=sideA+sideB+sideC;

}

public void calcArea()
{
    s=perimeter/2;
    theArea=(Math.sqrt(s*(s-sideA)*(s-sideB)*(s-sideC)));
}

public String toString()
{
    String output = ""+theArea+"\n\n";
    return output;
}

}

Is this universally true for gaming companys? True for some? Or would most accept this?

share|improve this question
1  
I didn't downvote this, since my learning OO was "objects first" which meant this particular topic was learned late, so understand why you're asking. I'm assuming the downvotes are because while you've asked specifically about gaming companies using eclipse, the crux of it is a general and fundamental question that may have duplicates elsewhere on the SE. –  StuperUser Oct 26 '11 at 19:03
add comment

2 Answers

up vote 9 down vote accepted

This is universally true for any Java program.

Since a class is not a program it cannot run, think of the Main class and run method as the entry point to the program.


In response to your comment read up on SRP from the SOLID principles: http://en.wikipedia.org/wiki/Single_responsibility_principle.

For example, your triangle class will contain all of its logic and nothing else, it should be really good at that job and not be responsible for anything else. So you will need a class for the entry/execution to the application, which is the Main class.

Also, i simply have the first class with all the methods then have the main at the end, so your program would be a single object, this isn't OO and is a particular "anti-pattern", see: http://en.wikipedia.org/wiki/God_object

In learning OO, this is where you make the jump from thinking about functionality at the method level and start thinking about the object level.

share|improve this answer
    
but couldn't i simply have the first class with all the methods then have the main at the end and have it call the methods or even run the code in it without a ton of methods. –  Bored915 Oct 26 '11 at 18:29
1  
@RudyGruse - Eventually you will create programs that are too big to manage in a single class. Understanding basic patterns and practices will help you when you get to that level. Not to mention if you came into work in an enterprise and tried coding like that the rest of us are going to hate working with your code. –  Chad Oct 26 '11 at 18:40
add comment

That's a manual form of unit testing. Most people in industry nowadays feel that if you're going to go through the trouble to write extra code for it, you may as well do some sort of automated unit tests using a unit testing framework like JUnit.

share|improve this answer
    
Where's the unit testing? It looks like plain SoC. –  StuperUser Oct 26 '11 at 19:07
    
Although that might be the entire assignment in school, it's basically code that lets you manually test the triangle class. Like I said, not unit testing the way most people in industry use the term. It's the having one main per class that makes it seem more like manual unit testing rather than a normal application. –  Karl Bielefeldt Oct 26 '11 at 21:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.