Qubit Posted November 9, 2014 Share Posted November 9, 2014 (edited) In AP Computer Science, my teacher is a bitch and ignores me when i ask about Java content that we she has not taught in class yet. So i was wondering what are these keywords,enum,try,catch,exception. If you could show me what it does and its purpose through a snippet of code with comments that would be great. I can say speaking for most Java newbs tutorials and books dont really go in depth of the true purpose and use of these phrases. All books and tutorials usually do is say.. put this keyword here because you have too. So if you wouldn't mind helping me out with there phrases i would be so grateful and happy. Edited November 10, 2014 by javant Link to comment Share on other sites More sharing options...
Dog_ Posted November 10, 2014 Share Posted November 10, 2014 https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html https://docs.oracle.com/javase/tutorial/essential/exceptions/ https://docs.oracle.com/javase/tutorial/essential/exceptions/try.html 1 Link to comment Share on other sites More sharing options...
Allen Posted November 10, 2014 Share Posted November 10, 2014 (edited) A try-catch statement is a method of catching or throwing an error in the code without disrupting other portions of the code, it also allows you to define a specific exception(the type of error thrown): try { //here you put code that will "try" to be ran and "Caught" if an error occurs. } catch (Exception e) { e.printStackTrace(); // this will print the source of the error and all previous methods called to the line of the error. } There are a few instances where a try-catch is required, such as reading/writing to a file. Example: try { String sItemId = params[0]; // a string array of parameters int itemId = Integer.valuOf(sItemId); // this is what we are catching the exception for (what if sItemId equals "345s"? 's' cannot be parsed to an integer object! //blah } catch (NumberFormatException e) {// here we catch the error with as specific exception e.printStackTrace(); } Enums are a way of holding constant values. The advantage to enums over just many final primitive fields is that it can act as an encapsulated object(the data within the enum cannot be changed or edited, but the values can be accessed and the enum can be treated like an object). For example: public enum Food { SHRIMP(123,5), LOBSTER(456,20); int itemId; int heal; Food(int itemId, int heal) { this.itemId = itemId; this.heal = heal; } public int getItemId() { return itemId; } public int getHealValue() { return heal; } public static Food getFoodForItem(int itemId) { for(Food food : Food.values()) if(food.getItemId() == itemId) return food; return null; } } You can reference the enum by the following: public void eat(Food food) { player.heal(food.getHealValue()); } or public void eat(int itemId) { Food food = Food.getFoodForItem(itemId); if(food != null) player.heal(food.getHealValue()); } or if(food == Food.LOBSTER) { //blah } Enums can be a lot more powerful than this example, but since you're just starting this is enough They can also be a lot more simple, you could just use it as a reference: public enum CombatType{ MELEE, RANGE, MAGIC; } Use: CombatType currentType = CombatType.MELEE; //blah... switch(currentType) { case CombatType.MELEE: //blah: fight with sword break; case CombatType.RANGE: //blah: fight with crossbow break; case CombatType.MAGIC: //blah: fight with staff break; } EDIT: added example for try-catch, if you have any other questions pm me or ask here ;p BTW: I made a 5 on AP comp science(assuming that's what you're in when you said "computer science ap"), and I'll tell you: Neither enums or try-catch are on the exam. You're teacher isn't responding to you because IF you use something on the exam that is not taught in the class, you will not get full credit(on the free response for example). Just a heads up Edited November 10, 2014 by Allen 3 Link to comment Share on other sites More sharing options...
Swizzbeat Posted November 10, 2014 Share Posted November 10, 2014 Computer Science 1 is a joke, I got a 4 on AP exam and didn't even take the class. Probably would have gotten a 5 if it wasn't for this: IF you use something on the exam that is not taught in the class, you will not get full credit(on the free response for example) But yeah, I personally learn better by watching someone DO while I follow along, so these tutorials will do you wonders: http://www.youtube.com/playlist?list=PLE7E8B7F4856C9B19 FYI NEVER WATCH THENEWBOSTON VIDEOS he's a complete moron that will only tell you textbook information. 1 Link to comment Share on other sites More sharing options...
Eliot Posted November 10, 2014 Share Posted November 10, 2014 Check out the docs and watch YouTube. Fun fact: I got a 2 on the AP CS exam! Link to comment Share on other sites More sharing options...
The Hero of Time Posted November 10, 2014 Share Posted November 10, 2014 try { // the stuff you want to do } catch(exception ex) { // "catches" the error(example: MessageBox.Show(ex.ToString()); } ^ this is C# tho, but that is the purpose of try/catch, prevent errors from crashing ur program Link to comment Share on other sites More sharing options...
Apaec Posted November 10, 2014 Share Posted November 10, 2014 Allen enlightened you. I'd just like to stress that enums are lovely & very juicy 1 Link to comment Share on other sites More sharing options...
Alek Posted November 11, 2014 Share Posted November 11, 2014 AP Computer Science and they haven't taught you about exceptions? What the hell are they teaching you? You need to learn about exceptions pretty early on for scanners and parsing strings into integers (NumberFormatExceptions, etc.). String number = "2"; int realNumber = 10; try{ realNumber = Integer.parseInt(number); } catch (NumberFormatException e){ System.out.println("The number you entered is invalid, using the default value of zero!"); realNumber = 0; } Link to comment Share on other sites More sharing options...