1 / 26

F21SF Software Engineering Foundations

6 More Basics Including class within a class, repetition, division. Dept of Computer Science. F21SF Software Engineering Foundations. Monica Farrow EM G30 email : M.Farrow@hw.ac.uk Material available on Vision. Topics. A class as an instance variable CRC cards Repetition

Download Presentation

F21SF Software Engineering Foundations

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. SJF L6 6 More Basics Including class within a class, repetition, division Dept of Computer Science F21SFSoftware Engineering Foundations Monica Farrow EM G30 email : M.Farrow@hw.ac.uk Material available on Vision

  2. SJF L6 Topics • A class as an instance variable • CRC cards • Repetition • Integer division

  3. SJF L6 Classes as instance variables • Instance variables can be primitive integer types such as int and double, and an object of a java class such as String. • An instance variable can also be an object of one of our own classes • We’ll now include an owner, using the Name class, to the Car class

  4. SJF L6 Updated Car class diagram Car String model int tankSize double manfMPL Name owner String getModel() double estimateDistance() Name getOwner() MainCar main(..) Updated Car class to include an owner, using the Name class Name String firstName String middleName String lastName String getFirstAndLastName() Gets and sets Etc etc

  5. SJF L6 CRC cards • CRC stands for Classes, Responsibilities, Collaborations • They were invented by Kent Beck and Ward Cunningham in 1989 as a teaching aid, to show • the responsibilities of a class - what it knows or does • The collaborators of a class - any class which is used to get information for, or perform actions for the class at hand • They are also now often used as a design aid too. • CRC cards are not part of UML, but often used with UML

  6. SJF L6 The name of a class, at the top The collaborators of the class, which help to carry out each responsibility, on the RHS. The responsibilities of the class, on the LHS Creating a CRC card

  7. SJF L6 CRC cards for the Car program

  8. SJF L6 Updated Car class //other instance variables as before private Name ownerName; public Car(String model, int tank, double mpg, Name owner){ this.model = model; tankSize = tank; manfMPG = mpg; ownerName = owner; } public Name getOwnerName() { return ownerName; }

  9. SJF L6 Updated main method Name myName = new Name("John", "David", "Smith"); Car myCar = new Car("Ford Ka", 40, 33.6, myName); Name owner = myCar.getOwnerName(); System.out.println("The car belongs to " + owner.getFirstAndLastName() ); System.out.println(" It is " + owner.getFirstName() + "’s car.");

  10. F21SF SEF L2 UML sequence diagram main() myName:Name Create a name Standard output Create a car myCar:Car Get owner name Get first and last name Use standard output to display results Get first name Use standard output to display results

  11. SJF L6 Obtaining the Car owner’s details • In the main method, you can obtain the owner name in 2 ways Name owner = myCar.getOwner(); String name = owner.getFirstName(); OR String name = myCar.getOwner().getFirstName(); evaluate from the LHS – get the Name object, then the first name

  12. SJF L6 Chaining method calls • Warning! myCar.getOwner().getFirstName() • If you have omitted to create (instantiate) an object (i.e. not done = new ...() ), you could get a NullPointer exception. Then it is difficult to work out where the problem is. Is it with the myCar object or the object returned from myCar.getOwner? • For this reason, I recommend not chaining method calls. Complete change of subject coming up. . .

  13. SJF L6 REPETITION There are several different templates for repeating a block of code. repeat until some predetermined event occurs i.e. a variable holds a certain value repeat a fixed number of times (when the number of repetitions is known before the code is run).

  14. SJF L6 While loop You can do all repetition with a while loop While some condition is true, carry out the loop block int n = 1; //initialisation while (n <= 3) { System.out.println(n); n++; //add one to n (same as n = n+1) } // end of loop System.out.println(“The end”);

  15. F21SF1 Use case and Activity diagrams Activity diagram for the while loop n = 1 [n> 3]n== 1] print “the end” [n<= 3] print n add 1 to n [counter > n] [counter <=n]

  16. SJF L6 While loop walkthrough • Initially, n = 1 • Test (n <=3) is true, do so loop • Print n (1); increase by 1 (n = 2) • Back to start of while loop with n = 2 • Test (n <=3) is true, do so loop • Print n (2); increase by 1 (n = 3) • Back to start of while loop with n = 3 • Test (n <=3) is true, do so loop • Print n (3); increase by 1 (n = 4) • Back to start of while loop with n = 4 • Test (n <= 3) is false, so go to end of loop • Print “The End”

  17. SJF L6 while loops summary • The condition goes in parentheses (rounded brackets) • The loop block (what gets done) goes in curly brackets • The condition must be a boolean expression resulting in a boolean value, either true or false • The condition is evaluated at the start of every iteration. When the condition is false, the program carries on from the end of the block • i.e. after the curly brackets

  18. SJF L6 Infinite loops An infinite loop is one that never stops, because the boolean expression never becomes false. n = 3; while (n < 7) { System.out.println (n); } n will stay 3, and be less than 7, for ever Your program appears to do nothing, or the same thing over and over again, for ever Find out how to stop your program using your IDE! In my version of eclipse, there is a red square on the toolbar next to the Console window

  19. SJF L6 for loops A ‘for’ loop is a sort of structured ‘while’ loop. We use it when we know how many iterations we’re going to need. If we declare the counter within the loop It can only be used inside the loop Its scope is local to the loop block Boolean expression Stop when this is false Increase by one at end of each time through the block initialisation for (int count = 1; count <= limit; count++) { total += count; }

  20. SJF L6 Loop exercises • Do these in a main method • Write a for loop to • Print the even numbers from 2 to 10 • Print every number from the value in int num down to 1 • Print 20 asterisks • Create a String of 20 asterisks • Print a number of characters, when the number of characters is stored in num, and the character to be printed is stored in char ch • Count the number of letter ‘A’s in a String s • Choose one of these and write it as a while loop • Draw one of these as an activity diagram

  21. SJF L6 Division • There is a difference between integer division and real division • Both use the / operator • In java real division, where at least ONE side of the operator is real, the result is real and includes a decimal part. • 11/4 = 2.75 • In java integer division, where BOTH sides of the operator are integers, the result is an integer consisting of only the integer part of the answer • 11/4 = 2

  22. SJF L6 Remainder or modulus • When BOTH sides of the expression are integers, you can find • The integer section of the result by using the / operator • The remainder section of the result by using the % operator • E.g. 11/4 = 2 remainder 3 or 2.75 int x = 11; int y = 4; double z = 4; int intDiv = x/y; //2 int remainder = x%y; //3 double realDiv = x/z; //2.75

  23. SJF L6 Forcing double division • Sometimes we hae 2 integers but want a real result • There are several ways of handling this • Multiplying by 1.0 forces an integer to become a real number double ans = num1 * 1.0 / num2; • Convert one number to a double by casting. Insert the desired type in brackets in front of the variable. double ans = (double) num1 / num2;

  24. SJF L6 Division exercise 1 • Given int u; double v; int w = 0; int x = 3; int y = 7; double z = 8; • What is the result of: • u = y/x; • u = y%x; • v = y/x; • u = z/x; • u = x/w; • v = z/x;

  25. SJF L6 Division exercise 2 • Share out a number of items int items between a number of people int people • Find the number of items each person gets • Find the number of items left over • Divide the number int num by 2 into int part1 and int part2. The 2 parts should be as equal as possible e.g 4-> 2 & 2, 5 -> 3 & 2

  26. SJF L6 Summary / To Do • We have • Added a Name object to the Car attributes • Looked at repetition with while and for • Looked at integer and double division • Read through the lecture and study the code. • Do exercises • You should now start assignment 1

More Related