CSJA1DP_203_0_2016 - CSJA1DP Tutorial Letter Introduction to Java programming CSJA1DP Year Module Short Learning Programme Centre for Software

CSJA1DP_203_0_2016 - CSJA1DP Tutorial Letter Introduction...

This preview shows page 1 - 5 out of 20 pages.

CSJA1DP/203/0/2016 Tutorial Letter 203/0/2016 Introduction to Java programming Year Module Short Learning Programme Centre for Software Engineering School of Computing This tutorial letter contains solutions to Assignment 3. CSJA1DP
Image of page 1
2 TABLE OF CONTENTS INTRODUCTION .................................................................................................................................. 2 Question 1 (Chapter 6 Programming Projects 5 p513) ............................................................................... 3 Question 2 (Chapter 6 Programming Projects 7 p513) ............................................................................... 4 Question 3 (Chapter 6 Programming Projects 10 p514) ........................................................................... 10 Question 4 (Chapter 6 Programming Projects 15 p516) ........................................................................... 13 Question 5 (Chapter 7 Programming Projects 1 p611) ............................................................................. 14 Question 6 (Chapter 7 Programming Projects 3 p612) ............................................................................. 15 Question 7 (Chapter 7 Programming Projects 8 p613) ............................................................................. 17 Question 8 (Chapter 7 Programming Projects 10 p514) ........................................................................... 19 Introduction Dear Student This tutorial letter contains the solutions to Assignment 3. You can contact me at 011 671 9118 or the module email at myUnisa (Course Contact) if you need any help or have any questions. Good luck with your studies! Regards CSJA1DP lecturer Bar code
Image of page 2
CSJA1DP/203/0/2016 3 Question 1 (Chapter 6 Programming Projects 5 p513) public class LetterGrades { public enum LetterGrade{ APlus(true), A(true), AMinus(true), BPlus(true), B(true), BMinus(true), CPlus(true), C(true), CMinus(true), DPlus(true), D(true), DMinus(true), EPlus(false), E(false), EMinus(false), FPlus(false), F(false), FMinus(false); private boolean passed; private LetterGrade(boolean pass){ passed = pass; } public boolean isPassing(){ return passed; } public String toString(){ String s = name(); s = s.replace("Plus", "+"); s = s.replace("Minus", "-"); return s; } } public static void main(String[] args){ LetterGrade[] letterGrades = LetterGrade.values(); for(int i = 0; i < letterGrades.length; i++){ System.out.println("Grade: " + letterGrades[i].toString()); System.out.println(letterGrades[i].isPassing() == true ? "PASS" : "FAIL"); System.out.println(); } } }
Image of page 3
4 Question 2 (Chapter 6 Programming Projects 7 p513) Notes: This project’s r equirements include two accessor methods to read the temperature, one in degrees F and the other in degrees C. From this description it is not clear if the “two accessor methods to read the temperature…” should display or return the temperature in the spe cified units. Also, note the confusing terminology: methods that display values are actually “write” methods. The solution in this manual includes both types of accessor, two write methods to display (“read”) the temperature and units, and two get methods that return just the temperature in either degrees C or degrees F. An added feature of the solution is that it displays or returns temperatures rounded to one decimal place. The expression Math.round(degrees*10)/10.0 is used, where the divisor is 10.0 (rather than 10) to force the division results to be floating point instead of an integer and not lose the decimal place. Also, as described in the prologue, units is not guaranteed to be a legitimate value (c, C, f, or F). The read methods give an error message if it is not a legitimate value, but the set methods allow any character and the get methods default to a return value of the variable degrees (no conversion is performed) if units is anything other than one of the legitimate values.
Image of page 4
Image of page 5

You've reached the end of your free preview.

Want to read all 20 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture