Download Lab8 Practice Methods - cse141introductiontoprogramming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to Programming CSE141 (3+1)
Lab Dated: 4th Nov 2010
Revision of Methods
Objective
This lab provides understanding of what is a method, how the control is transferred on function
call and how simple parameters are passed from main program to method. This would also give
them an idea regarding switch case statement also. Lastly we will have a number of examples for
our practice. In our last lab we discussed the following example: an example:
// Example of simple Method
public class AddExample {
public static int AddNumbers(int x, int y)
{
return x+y;
}
public static void main(String[] args) {
int a=5, b=8;
System.out.println(AddNumbers(a,b));
}
}
We practiced the following questions (Remember ??)
1) Write a method that would take two strings as input and check if both of them are equal
or not?
import java.util.Scanner;
public class AddExample {
public static int CompareStrings(String str)
{
if (str.equals("hello"))
//
if (str.equalsIgnoreCase("Hello"))
return 1;
else
return 0;
}
public static void main(String[] args) {
Scanner x= new Scanner(System.in);
System.out.println("Please enter the string”);
String str1= x.nextLine();
if (CompareStrings(str1)==1)
System.out.println("Match");
else
System.out.println("MisMatch");
}
}
2) Write a method that would take two strings and return their combination as output
import java.util.Scanner;
public class AddExample {
public static String ConcatStrings(String str1, String str2)
{
return str1+" "+str2;
}
public static void main(String[] args) {
Scanner x= new Scanner(System.in);
System.out.println("Please enter the first string");
String str1= x.nextLine();
System.out.println("Please enter the second string");
String str2= x.nextLine();
String str3=ConcatStrings(str1, str2);
System.out.println(str3);
}
}
3) Write a method that would take a 4 digit number as input and a divisor and returns the
remainder
If we want to make our code reusable (that is we want to call it again and again this can
be done by changing parameters for every call)
//Example of Code Reusability
import java.util.Scanner;
public class AddExample {
public static int BreakNumber(int num1, int divisor)
{
System.out.println(num1/divisor);
return num1%divisor;
}
public static void main(String[] args) {
Scanner x= new Scanner(System.in);
System.out.println("Please enter a four digit number");
int num= x.nextInt();
num= BreakNumber(num, 1000);
num= BreakNumber(num, 100);
num= BreakNumber(num, 10);
num= BreakNumber(num, 1);
}}
Today’s Lab
// Example of how to use switch-case statement
import java.util.*;
public class CaseExample{
public static void main(String[] args) {
int option;
Scanner in=new Scanner(System.in);
System.out.println("*******************
System.out.println("1. Square Root");
System.out.println("2. Cosine");
System.out.println("3. PI");
System.out.println("4. Exit");
MENU
*******************");
System.out.println("Enter your option");
option=in.nextInt();
switch(option)
{
case 1:
System.out.println("enter int value");
int value=in.nextInt();
System.out.println("The square root is:” +Math.sqrt(value));break;
case 2:
System.out.println("The Cosine is: "+Math.cos(23));break;
case 3:
System.out.println("The PI value is: "+Math.PI);break;
case 4:
System.exit(0); break;
default:
System.out.println("Option is not available");
}
}
}
Practice Questions for Methods:
1) Given three ints, a b c, return true if it is possible to add two of the ints to get the third.
For example
twoAsOne(1, 2, 3) → true
twoAsOne(3, 1, 2) → true
twoAsOne(3, 2, 2) → false
2) The number 6 is a truly great number. Given two int values, a and b, return true if either
one is 6. Or if their sum or difference is 6. Note: the function Math.abs(num) computes
the absolute value of a number.
Comp6(6, 4) → true
Comp6(4, 5) → false
Comp6(1, 5) → true
3) Write a method that would take two strings as input and check whether the second is the
substring of first or not. If yes then returns true else returns false
4) Write a method that would take three numbers as input and return the maximum number
as output
---------------------- The End ----------------------------------------
Related documents