lab 9 (S)

Report
CS110 Programming Language I
Lab 9: Methods II
Computer Science Department
By: TA. Nora Alaqeel
Spring 2014
( Variablesoutput
Scope )
Lab Exercise 1: program
Find Errors (If Any):
class ScopeEx {
public static void main(String args[]) {
int num = 1;
{
// creates a new scope
int num = 2;
}
} //end main
} //end class
By: TA. Nora Alaqeel
( Variablesoutput
Scope )
Lab Exercise 1: program
Find Errors (If Any):
class ScopeEx {
public static void main(String args[]) {
{
// creates a new scope
int num = 1;
}
{
// creates a new scope
int num = 2;
}
} //end main
} //end class
By: TA. Nora Alaqeel
( Variablesoutput
Scope )
Lab Exercise 1: program
Find Errors(If Any):
class ScopeEx {
public static void 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);
}
By: TA. Nora Alaqeel
}
( Method Overloading )
(T or F) Tell whether the following method overloading
is true or false:
Public static void myMethod (int firstArg, int secondArg)
{
…..
}
Public static void myMethod (char firstArg, int secondArg)
{
…..
}
By: TA. Nora Alaqeel
( Method Overloading )
(T or F) Tell whether the following method overloading
is true or false:
Public static void myMethod (int firstArg, int secondArg)
{
…..
}
Public static void myMethod (int firstArg)
{
…..
}
By: TA. Nora Alaqeel
( Method Overloading )
(T or F) Tell whether the following method overloading
is true or false:
Public static void myMethod (int firstArg, double secondArg)
{
…..
}
Public static void myMethod (double firstArg, int secondArg)
{
…..
}
By: TA. Nora Alaqeel
( Method Overloading )
(T or F) Tell whether the following method overloading
is true or false:
Public static void myMethod (int firstArg, int secondArg)
{
…..
}
Public static void myMethod (int first, int second)
{
…..
}
By: TA. Nora Alaqeel
( Method Overloading )
(T or F) Tell whether the following method overloading
is true or false:
Public static void myMethod (int firstArg, int secondArg)
{
…..
}
Public static String myMethod (int firstArg, int secondArg)
{
…..
}
By: TA. Nora Alaqeel
( Returning Values + Calling )
Problem Description
(Room Type) Write a program with a method named computeRoomArea
which takes two float arguments entered by the user, as the length and
width of 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
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
Code Skelton
Cont.
public static float computeRoomArea(float l , float w) {
return (l * w);
} //end computeRoomArea
} // end class
By: TA. Nora Alaqeel
Evaluation
(Print Grade) Write a program with a method named
printGrade which 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)
Grade
Score
A
Score >= 90.0
B
90.0 > Score >= 80.0
C
80.0 > Score >= 70.0
D
70.0 > Score >= 60.0
F
Score < 60.0
By: TA. Nora Alaqeel
Sample Output:
Please Enter Your Score: 87
Your Grade Is: B
By: TA. Nora Alaqeel
Assignment Problem(s)
Q1: Write a method named lastDigit that returns the last digit of an
integer. For example, lastDigit(3572) should return 2. It should work for
negative numbers as well. For example, lastDigit(-947) should return -7.
Write a program to test your method.
Assignment Problem(s)
Q2: Write a program containing a method named countChars that
receives a string read from the user, and count the number of characters
in this string. Print out the result on main method.
Assignment Problem(s)
Q3: Write a method named swapPairs that accepts a String as a
parameter and returns that String with each pair of adjacent letters
reversed. If the String has an odd number of letters, the last letter is
unchanged. For example, the call swapPairs("forget") should return
"ofgrte" and the call swapPairs("hello there") should return "ehll
ohtree". Write a program to test your method.

similar documents