1 / 16

CS110 Programming Language I Lab 9 : Methods II Computer Science Department Spring 2014

CS110 Programming Language I Lab 9 : Methods II Computer Science Department Spring 2014. ( V ariables Scope ). Find Errors ( If Any) : class ScopeEx { public static void main(String args []) { int num = 1; { // creates a new scope int num = 2; } } //end main

mateja
Download Presentation

CS110 Programming Language I Lab 9 : Methods II Computer Science Department Spring 2014

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. CS110 Programming Language I • Lab 9:Methods II Computer Science Department Spring 2014 By: TA. Nora Alaqeel

  2. ( Variables Scope ) FindErrors (If Any): classScopeEx { publicstaticvoid main(String args[]) { int num = 1; { // creates a new scope intnum = 2; } } //end main } //end class Compile-time error, num already defined. (nested blocks) By: TA. Nora Alaqeel

  3. ( Variables Scope ) FindErrors (If Any): classScopeEx { publicstaticvoid main(String args[]) { { // creates a new scope int num = 1; } { // creates a new scope int num = 2; } } //end main } //end class No errors here (non-nested blocks) By: TA. Nora Alaqeel

  4. ( Variables Scope ) FindErrors(If Any): classScopeEx { publicstaticvoid main(String args[]) { int n1; n1 = 10; if(n1 == 10) { // start new scope int n2 = 20; System.out.println("n1 and n2 : "+ n1 +" "+ n2); } System.out.println("n1 is " + n1); System.out.println(”n2 is " + n2); } } // n1 Visible in all main // n2 visible only to this block // n1 and n2 both visible here // n1 is still visible here. // n2 is not visible here!. By: TA. Nora Alaqeel

  5. ( Method Overloading ) (T or F) Tell whether the following method overloading is true or false: Public static void myMethod (intfirstArg, intsecondArg) { ….. } Public static void myMethod(char firstArg, intsecondArg) { ….. } True By: TA. Nora Alaqeel

  6. ( Method Overloading ) (T or F) Tell whether the following method overloading is true or false: Public static void myMethod (intfirstArg, intsecondArg) { ….. } Public static void myMethod(intfirstArg) { ….. } True By: TA. Nora Alaqeel

  7. ( Method Overloading ) (T or F) Tell whether the following method overloading is true or false: Public static void myMethod (intfirstArg, double secondArg) { ….. } Public static void myMethod(double firstArg, intsecondArg) { ….. } True By: TA. Nora Alaqeel

  8. ( Method Overloading ) (T or F) Tell whether the following method overloading is true or false: Public static void myMethod (intfirstArg, intsecondArg) { ….. } Public static void myMethod(int first, intsecond) { ….. } False By: TA. Nora Alaqeel

  9. ( Method Overloading ) (T or F) Tell whether the following method overloading is true or false: Public static void myMethod (intfirstArg, intsecondArg) { ….. } Public static String myMethod (intfirstArg, intsecondArg) { ….. } False By: TA. Nora Alaqeel

  10. ( Returning Values + Calling ) Problem Description (Room Type)Write a program with a method named computeRoomAreawhich takes two float arguments entered by the user, as the lengthand widthof a room. The method must calculate and returns the area of the room. In the main() method, you should display the result, and one of the following statements: This is a Huge Room. (If the room’s area larger than 100) This is a Tiny Room. (Otherwise – If smaller or equal to 100) Hint: Area=length * width Sample Outputs: Please Enter length and width of the room: 12 10 The Area of the Room= 120 This is a Huge Room. By: TA. Nora Alaqeel

  11. Code Skelton import java.util.Scanner; public class Room { public static void main(String[] args) { Scanner input = new Scanner(System.in); float length, width, area; System.out.print( "Please Enter length and width of the room: ") ; length = input.nextFloat(); width = input.nextFloat(); area = computeRoomArea(length, width); System.out.println( "The Area of the Room = " + area ); if (area > 100) System.out.println( "This is a Huge Room." ); else System.out.println( “This is a Tiny Room." ); } //end main By: TA. Nora Alaqeel

  12. Code SkeltonCont. public static float computeRoomArea(float l , float w) { return (l * w); } //end computeRoomArea } // end class By: TA. Nora Alaqeel

  13. Evaluation (Print Grade)Write a program with a method named printGradewhich takes one float argument entered by the user, as the user’s score. The method must returns the grade of the student. In the main() method, you should print the grade. The method header should be as the following: public static char printGrade(double score) By: TA. Nora Alaqeel

  14. Sample Output: Please Enter Your Score: 87 Your Grade Is: B By: TA. Nora Alaqeel

  15. package javaapplication26; import java.util.Scanner; public class JavaApplication26 { static Scanner input = new Scanner(System.in); public static void main(String[] args) { System.out.print("Please Enter Your Score: "); double s = input.nextDouble(); char g= printGrade(s); System.out.print("Your Grade Is: "); System.out.print(g); } //end main By: TA. Nora Alaqeel

  16. public static char printGrade(double score) { char grade; if (score >= 90.0) { grade ='A'; } else if (score >= 80.0) { grade ='B'; } else if (score >= 70.0) { grade ='C'; } else if (score >= 60.0) { grade ='D'; } else { grade ='F'; } return grade; }//end printGrade method } //end class By: TA. Nora Alaqeel

More Related