1 / 18

CSc2310 tutoring session, week 8 Fall, 2012

CSc2310 tutoring session, week 8 Fall, 2012. Using Exceptions Homework 4. Haidong Xue. 5:30pm—8:30pm 10/23/2012 and 10/24/2012 . CSc2310 Tutoring Time: 5:30pm-8:30pm Tutor: Haidong Xue Website: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/index.html There are 2 sections:

lave
Download Presentation

CSc2310 tutoring session, week 8 Fall, 2012

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSc2310 tutoring session, week 8Fall, 2012 • Using Exceptions • Homework 4 HaidongXue 5:30pm—8:30pm 10/23/2012 and 10/24/2012

  2. CSc2310 Tutoring • Time: 5:30pm-8:30pm • Tutor: HaidongXue • Website: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/index.html There are 2 sections: 1. Review • Using Exceptions • Homework 4 2. Q&A • Answer your questions about java programming

  3. Using exceptions • Throw an exception • Throw a exception when needed throw [Exception Object]; • Catch an exception try{ [code may throw exceptions] } catch (Exception e){ [do something with this exception] }

  4. Using exceptions • Why we need exceptions? • How to deal with the potential errors in a program? • E.g.: a class to calculate the area for a rectangle: class RecArea{ public static calRecArea ( double width, double height){ return width*height; } }

  5. Using exceptions • A potential problem here is: • The input width may be negative • The input height may be negative • How to prevent unwanted consequences? • class RecArea{ • public static double calRecArea( double width, double height){ • return width*height; • } • }

  6. Using exceptions • Method 1 – print some error info • class RecArea{ • public static double calRecArea( double width, double height){ • if (width<0 || height<0) • System.out.println(“invalid input!”); • return width*height; • } • } • Drawback: • The user of RecArea may annoyed by the unwanted message, and has no way to remove it. • The user of RecArea may neglect it.

  7. Using exceptions • E.g.: • class RecAreaUser{ • public void main( String[] args){ • …. • // assuming we have house area width as w and height as h • // and need to calculate the tax here • double tax = 10 * RecArea.calRecArea(w, h); • } • } The author of RecArea is Haydon; the author of RecAreaUser is you. You may have designed a very good user interface; however, sometimes there could be a very unfriendly “invalid input!” on the screen; what’s worse, you have no way remove them. (height and width may obtained from the final a final user in run time). In a project, we do not like components like this RecArea. Moreover, you may still use a negative area to calculate tax. (A logic error)

  8. Using exceptions • Method 2 – return some value to indicate the problem • class RecArea{ • public static double calRecArea( double width, double height){ • if (width<0) return -1; // -1 means invalid width • if (height<0) return -2; // -2 mean invalid height • return width*height; • } • }

  9. Using exceptions Before exceptions, it was a very popular method: • class RecAreaUser{ • public void main( String[] args){ • …. • // assuming we have house area width as w and height as h • // and need to calculate the tax here • double area = RecArea.calRecArea(w, h); • double tax = 0; • if (area == -1 ) {/*do something for invalid width*/} • elseif(area == -2 ) {/*do something for invalid height*/} • else • tax = 10 *area; • } • } Drawback: the author of RecAreaUser may also neglect the problem, and use -1 or -2 as the area.

  10. Using exceptions • Method 3 – Using exceptions • class RecArea{ • public static double calRecArea ( double width, double height) throws Exception{ • if (width<0 || height <0) throw new Exception(“invlid input”); • return width*height; • } • } The user of RecArea cannot neglect this potential exception.

  11. Using exceptions publicclassRecAreaUser{ publicstaticvoid main(String[] args){ Scanner s = new Scanner(System.in); while(true) { System.out.println("=================================="); System.out.println("Type a for calculating tax;"); System.out.println("Type q to quit."); String command = s.nextLine(); if(command.equalsIgnoreCase("a") ){ System.out.print("The width of your house area: "); doublewidth = s.nextDouble(); s.nextLine(); System.out.print("The height of your house area: "); doubleheight = s.nextDouble(); s.nextLine(); doubletax; // you can try to remove the try and catch block, you will see a compile error try{ tax = RecArea.calRecArea(width, height); System.out.println("The tax you need to pay: " + tax); } catch(Exception e){ System.out.print("Sorry, when calculating your house area, we see a error: "); System.out.println(e.getMessage()); } } elseif( command.equalsIgnoreCase("q")) break; else continue; } } }

  12. HW4 - ClassifyScores • A use case: Enter a number: 230 Wrong number Enter a number: 23 Enter a number: 1 Enter a number: 23 Enter a number: 12 Enter a number: 99 Enter a number: 100 Enter a number: -1 Decile Scores ------- -------- 0-9 1 10-19 1 20-29 2 30-39 0 40-49 0 50-59 0 60-69 0 70-79 0 80-89 0 90-100 2

  13. HW4 - ClassifyScores • Where to put the scores? • Using an array int[] scores = new int[10]; for (int i = 0; i < 10; i++) scores[i] = 0;

  14. HW4 - ClassifyScores intindex = n / 10; if (index == 10) index = 9; // only for the number of 100 e.g.: When n = 0, index will be 0; When n = 11, index will be 1; When n = 23, index will be 2; When n = 34, index will be 3; … When n = 92, index will be 9; When n = 100, index will be 9;

  15. HW4 - ClassifyScores • Finished code is at my website: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/code/ClassifyScores.java

  16. HW4 – NFLTeam5, NFLGameDay5 • Create the player array in constructor: public NFLTeam5(String eName) { win = 0; loss = 0; TeamName= eName; nTotalNumPlayers= 0; sPlayerArray= new String[100]; }

  17. HW4 – NFLTeam5, NFLGameDay5 • Add “addAPlayer”method public void addAPlayer(String playerName) { sPlayerArray[nTotalNumPlayers] = playerName; nTotalNumPlayers++; } • Finish the code to construct “Steelers” Finished code are at my website: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/code/NFLTeam5.java http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/code/NFLDay5.java

  18. Please let me know your questions. I will be here till 8:30pm

More Related