1 / 19

CS1020 Data Structures and Algorithms I Tutorial 1

CS1020 Data Structures and Algorithms I Tutorial 1. Java and Simple OOP. Introduction. Zhao Jin – please call me Jin for short Email: zhaojin@nus.edu.sg Office: COM-02-50 Consultation hours: by appointment. The big picture. Q1 Java naming convention and syntax/logic errors.

stew
Télécharger la présentation

CS1020 Data Structures and Algorithms I Tutorial 1

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. CS1020 Data Structures and Algorithms ITutorial 1 Java and Simple OOP

  2. Introduction Zhao Jin – please call me Jin for short Email: zhaojin@nus.edu.sg Office: COM-02-50 Consultation hours: by appointment [CS1020 Tutorial 1 AY2013/4 S2]

  3. The big picture [CS1020 Tutorial 1 AY2013/4 S2]

  4. Q1 Java naming convention and syntax/logic errors Every word in class names should be initial capitalized, e.g., Tut1Q1 importjava.util.*; public class tut1Q1 { public static void main(String[] args) { Scanner Input = new Scanner(System.in); System.out.print("Enter length of box: "); intBOX_LENGTH = Input.nextInt(); System.out.print("Enter width of box : "); intBOX_WIDTH = Input.nextInt(); System.out.print("Enter volume of box: "); intBOX_VOL = Input.nextInt(); All words except for the first in variable names should be initial capitalized, e.g., input and boxLength. In addition, names in all caps and with underscore between words are for constants, e.g., BOX_LENGTH. [CS1020 Tutorial 1 AY2013/4 S2]

  5. Q1 Java naming convention and syntax/logic errors Corrected version importjava.util.*; publicclass Tut1Q1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter length of box: "); intboxLength = input.nextInt(); System.out.print("Enter width of box : "); intboxWidth = input.nextInt(); System.out.print("Enter volume of box: "); intboxVol = input.nextInt(); [CS1020 Tutorial 1 AY2013/4 S2]

  6. Q1 Java naming convention and syntax/logic errors double BOX_HEIGHT = ComputeHeight(BOX_LENGTH, BOX_WIDTH, BOX_VOL); System.out.println("Height of box = " + BOX_HEIGHT); } public double ComputeHeight(intaa, int bb, int cc) { return cc / (aa * bb); } } Compilation error. The modifier "static" should be added here so that this method can be called without an object. The naming convention for methods is the same as the one for variables, e.g., computeHeight All names (except for loop variables) should be descriptive. Logic error. The result would be truncated due to integer division and hence not accurate. [CS1020 Tutorial 1 AY2013/4 S2]

  7. Q1 Java naming convention and syntax/logic errors Corrected version double boxHeight = computeHeight(boxLength, boxWidth, boxVol); System.out.println("Height of box = " + boxHeight); } public static double computeHeight(intlen, intwid, intvol){ return(double) vol / (len * wid); } } [CS1020 Tutorial 1 AY2013/4 S2]

  8. Q2 Math class, writing method where s is half the triangle’s perimeter: The area of a triangle can be computed given the lengths of its sides a, b and c with Heron’s Formula: Write a program TriangleArea.java that reads 3 positive values of type double representing the lengths of the sides of a triangle, and computes the area of the triangle. The program should include a method area() to compute the triangle’s area. You are to determine what parameters the method should have. Print the area in 2 decimal places. [CS1020 Tutorial 1 AY2013/4 S2]

  9. Q2 Math class, writing method Writing a method in Java is similar to writing a function in C except that the correct modifiers should be used in Java. importjava.util.*; public classTriangleArea { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter lengths of 3 sides: "); doubleside1 = sc.nextDouble(); double side2 = sc.nextDouble(); double side3 = sc.nextDouble(); System.out.printf("Area of triangle = %.2f\n", area(side1, side2, side3)); } public static double area(double a, double b, double c) { doubles = (a + b + c)/2; returnMath.sqrt(s * (s-a) * (s-b) * (s-c)); } } printf() in System.out works in the same way as printf() in C. Methods in Math class are static and hence called in this way Math.methodName(…). Do check out other useful methods in this class. [CS1020 Tutorial 1 AY2013/4 S2]

  10. Q3 String Class (a) String str = 1 + 2 + " buckle my shoe"; System.out.println(str); 3 buckle my shoe • + is left associative so • First +: Addition, since both operands are integers. • Second +: String concatenation, since one of the operands is a string. String str = "" + 1+ 2 + " buckle my shoe"; System.out.println(str); 12 buckle my shoe • All + are string concatenation, since at least one of the operands is a string. [CS1020 Tutorial 1 AY2013/4 S2]

  11. Q3 String Class (a) String str = '1' + 2 + " buckle my shoe"; System.out.println(str); 51 buckle my shoe • '1' is in fact the integer 49 according to ASCII table so • First +: Addition, Second +: String concatenation String str = 1 + "-2" + " buckle my shoe"; System.out.println(str); 1-2 buckle my shoe • All + are string concatenation, since at least one of the operands is a string. [CS1020 Tutorial 1 AY2013/4 S2]

  12. Q3 String Class (b) inti = 10; int j = i; String s = new String("string1"); • When a value (of primitive data types) is assigned to a variable (of primitive data types), the value is stored into (the memory slot allocated to) the variable. • When a variable (of primitive data types) is assigned to another variable (of primitive data types), the value of the former is stored into the latter. • When an object is assigned to a variable (for storing object references), the reference to this object is stored into the variable. [CS1020 Tutorial 1 AY2013/4 S2]

  13. Q3 String Class (b) String t = s; s = new String("string2"); • When a variable (for storing object references) is assigned to another variable (for storing objects references), the reference stored in the former is stored into the latter. • In the above example, the reference to "string1" is stored into t in the first statement and the reference to "string2" is stored into s in the second. [CS1020 Tutorial 1 AY2013/4 S2]

  14. Q3 String Class (c) String s = new String("string1"); String t = new String("string2"); • In the lower example, u gets the references to "string1" from s, s gets the references to "string2" from t, and t gets the reference to "string1" from u. • Note that the string objects are not affected at all, only the references in the variables change. • Since arrays are also objects in Java, for swapping involving arrays, please think carefully whether the references or the elements are swapped. String u = s; s = t; t = u; [CS1020 Tutorial 1 AY2013/4 S2]

  15. Q3 String Class (d) String s1 = new String("programming"); String s2 = new String("programming"); String s3 = s2; • In this example, the two string objects are distinct objects even though they have the same content. [CS1020 Tutorial 1 AY2013/4 S2]

  16. Q3 String Class (d) • For two variables (for storing object references) a and b, a == b is true if the references stored in a and b are the same. System.out.println("s1 == s2: " + (s1 == s2)); System.out.println("s2 == s3: " + (s2 == s3)); System.out.println("s1 == s3: " + (s1 == s3)); …false …true …false System.out.println("s1.equals(2): " + (s1.equals(s2))); System.out.println("s2.equals(3): " + (s2.equals(s3))); System.out.println("s1.equals(3): " + (s1.equals(s3))); …true …true …true • For two variables (for storing object references) a and b, whether a.equals(b) depends on the implementation of the method. • For two String variables a and b, a.equals(b) if both represent the same sequence of characters. [CS1020 Tutorial 1 AY2013/4 S2]

  17. Q3 String Class (e) String t1 = "CS1020"; String t2 = "CS1020"; String t3 = "CS" + "1020"; • As observed in part d), when string objects are created using constructors, even though the same string constant is passed into the constructor, the objects created are still different. System.out.println("t1 == t2: " + (t1 == t2)); System.out.println("t2 == t3: " + (t2 == t3)); System.out.println("t1 == t3: " + (t1 == t3)); …true …true …true • String constants which represent the same sequence of characters are considered the same by the compiler. Only one object will be created for these constants. Therefore, in this example, all variables contain the reference to the same object created for “CS1020”. [CS1020 Tutorial 1 AY2013/4 S2]

  18. Q3 String Class (f) String s1 = new String("programming"); s1 = s1.substring(3, 7); • s1 = s1.substring(3, 7) creates a new string "gram" and stores the reference to this string into s1. The original string is not changed. [CS1020 Tutorial 1 AY2013/4 S2]

  19. Q4 Overloading • For (c), Java does not automatically convert 3.5 to 3 since it would result in a loss of precision. As such, m(int a) would not be called. • For (e), Java converts 10 to 10.0 since it does not result in a loss of precision. As such, m(double a, double b) will be called. • The directly matched methods are called in (a), (b) and (d). public class C { public static intm() { return 123; } public static intm(int a) { return 3 * a; } public static intm(int a, int b) { returna + b; } public static intm(double a, double b) { return(int) (a * b); } } (a) System.out.println(C.m()); (b) System.out.println(C.m(7)); (c) System.out.println(C.m(3.5)); (d) System.out.println(C.m(10, 20)); (e) System.out.println(C.m(10, 20.0)); 123 21 Error 30 200 [CS1020 Tutorial 1 AY2013/4 S2]

More Related