String result getinfo mia 21 true private string

This preview shows page 2 - 4 out of 4 pages.

} String result = getInfo ("Mia", 21, true); private String getInfo( String name, int age, boolean isSingle ) { Complete the method header for the getInfo() method: 12 1 2 3 4 5 6 7 Returning values This allows a method to pass information back to the method call. private int getBlocks(int hrs, int mins) { int totalMins; int blocks; totalMins = hrs * 60 + mins; blocks = totalMins / 10; return blocks; } A method can only return a single value (a primitive or an object). The method header must ALWAYS state the type of value the method is returning.
Image of page 2

Subscribe to view the full document.

CS101 Lecture 7 3 13 1 2 3 4 5 6 7 8 9 The return statement Every method that returns a value must use the keyword "return" as the last statement in the method. private int getBlocks(int hrs, int mins) { int totalMins = hrs * 60 + mins; int blocks = totalMins / 10; return blocks; } private String getName() { String name = "Fred Fish"; return name; } The value returned must be the same type as declared in the method header, e.g., 14 1 2 3 4 5 6 7 8 9 10 11 12 Returning values When a method call is executed the returned value replaces the method call public class CallingMethods { public void start() { int numBlocks = getBlocks(1, 15); numBlocks = getBlocks(2, 5); } private int getBlocks(int hrs, int mins) { int totalMins, blocks; totalMins = hrs * 60 + mins; blocks = totalMins / 10; return blocks; } } 7 12 15 1 2 3 4 5 6 7 8 9 10 11 12 Returning values The variable on the left hand side of the call to a method must be of the same type as the return type of the method. public class CallingMethods { public void start() { int blocks1 = getBlocks(3, 5); int blocks2 = getBlocks(6, 35); } private int getBlocks(int hrs, int mins) { int totalMins, blocks; totalMins = hrs * 60 + mins; blocks = totalMins / 10; return blocks ; } } 16 1 2 3 4 5 6 7 8 9 10 11 12 13 Ex02 Which of the following calls to the calculate() method will compile? public class Test02 { public void start() { double a = calculate(5 , 4); (a) double d = calculate(4.0); (b) int c = calculate(2 , 4.2); (c) int x = calculate(3.5, 1); (d) boolean b = calculate(3, 1.1); (e) } } (a), (c) will compile private int calculate(int a, double b) { int amount = (int)(a + b * 2); return amount; } 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Ex03 Which of the following calls to the lots() method will compile?
Image of page 3
Image of page 4
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern