0 Members and 1 Guest are viewing this topic.
Now, you might be asking: Why would I ever want to use this? There are two main reasons you would.First, while it might be easier to manage one ship in low level programming, how about managing 100 of them? To track down all 300 variables, even in a list, would be unbearable, especially if you decided later that ships also need to have health or something. However, if you create a list of Ships, you can manage each ship individually.Second, objects encapsulate things that you just don't want to manage, or want to protect somehow. For example, our Ship had a subroutine called Move on it to move the ship forward. Instead of having to remember sines and cosines every time a person wanted to move the ship, they could just set the rotation and movement amount and just say "Move". OOP also lets you hide variables that an object needs to know about, but not the rest of the game.And objects have real-life applications as well, more so than in games. For example, you might want to create an object called CALCnet that manages calculator linking. By using this object, you don't have to worry about all of the little details and issues with CALCnet. All you have to do is start it up, shut it down, and tell it to send frames to people.
I don't think I understand the purpose of OOP. Why would you use data abstraction and encapsulation for something like, to use your example, updating ships? Since you're presumably only manipulating a single ship at a time, wouldn't it be more logical to maintain an array of data about all ships. The data about that ship could be loaded into the variables you're manipulating and then reloaded into the array when you're done. It could actually work out very easily if you use "pointers" to the data in the array and a simple subroutine to manipulate the pointers and simultaneously updating the data in the array. It seems like encapsulating the ship data just pushes the mechanics of the program onto the interpreter/compiler. That certainly makes things easier to manage, but computers also have a tendency to write less efficient code than humans. Also, I genuinely don't understand why you would want local variables. Doesn't that make things confusing when the variable "ShipSpeed" is used differently by three different subroutines? Plus, the OS/program has to maintain a table of all of the local variables, look them up, retrieve them from memory, and swap the appropriate ones in when necessary. That's not a lot of overhead, but it might not be suitable for high speed applications. It would also appear to be less memory efficient than maintaining global variables and being careful not to modify them.PS: Your "Calcnet" example seems to be similar to calling another program or subroutine.
public class Square extends Quadrilateral { private int[] sideLengths; private float[] angles; public void Square(float sideLength) { sideLengths = new int[4] = { sideLength, sideLength, sideLength, sideLength,}; angles = new float[4] = { 90.0f, 90.0f, 90.0f, 90.0f}; } @override public int Area() { return sideLengths[0]^2; }}
abstract class IntegerOperation { public abstract int selfOperation(); public abstract int binaryOperation(int otherArgument); //"abstract" just means that I'm not defining these methods}//IntegerOperation is a class that names two methods, selfOperation() and binaryOperation(int), that both return an int//It is extended twice here:class IncrementAdd extends IntegerOperation { public int number; public int selfOperation() { return number+1; } public int binaryOperation(int otherArgument) { return number+otherArgument; }}//Also here:class DecrementSubtract extends IntegerOperation { public int number; public int selfOperation() { return number-1; } public int binaryOperation(int otherArgument) { return number-otherArgument; }}//And the main code here:public class Main { public static void main(String[] args) { IntegerOperation intOp = new IncrementAdd(); intOp.number = 5; System.out.println(intOp.selfOperation()); //Prints 6 intOp = new DecrementSubtract(); intOp.number = 20; System.out.println(intOp.binaryOp(13)); //Prints 7 }}
ArrayList<ContactInfo> contacts = new ArrayList<ContactInfo>();for (int i=0; i<(# of contacts); i++){ contacts.add(new ContactInfo(name,email,phoneNumber));}
List<Dog> = new ArrayList<Dog>();List<Cat> = new ArrayList<Cat>();List<Bird> = new ArrayList<Bird>();
public class Generics<T> { private T t; public void Generics() { t = new T(); }}
Generics<Lobstar> blueLobster = new blueLobster<Lobstar>();
public class Generics<Lobstar> { private Lobstar t; public void Generics() { t = new Lobstar(); }}
I see you used the Diamond operator there a Java exclusive, so methinks. I think I might as well explain that too
class GenericClass<T> { //T stands for a certain class. Could be anything. public T thing; public void display() { System.out.println(thing.toString()); } public void setThing(T thing) { this.thing = thing; //this.thing refers to the thing in this object, not the argument thing}public class Main { public static void main(String[] args) { GenericClass<String> test = new GenericClass<String>(); test.setThing("Hello World!"); test.display(); GenericClass<Object> test2 = new GenericClass<Object>(); test.setThing(new Object()); test.display(); }}