Jump to content

What is enum, try, catch, exception


Qubit

Recommended Posts

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 by javant
Link to comment
Share on other sites

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 tongue.png

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 happy.png

Edited by Allen
  • Like 3
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...