King Saud University

College of Computer and Information Sciences

Information Technology Department

First Semester 1430/1431

CSC 111: Introduction to Programming with Java

TUTORIAL # 6

Chap7: User-Defined Methods

Q1.  Trace the following program:

import java.util.*;
// input 2 4.5
public class M{
static Scanner console = new Scanner(System.in);
static int i = 0;
public static void main (String[] args)
{
int a;
double b;
i++;
System.out.println (i);
a = console.nextInt();
b = console.nextDouble();
double x = test(a, b);
System.out.println("a = " + a + ", b = "+
b + ",x= " + x +", test(x, b)= "+ test((int)x, b));
if (isEven(a) ) System.out.println(a+ " is even" );
i++; System.out.println(i);
}
public static double test(int x, double y)
{
i++;
System.out.println(i);
return y * 3 + 2 * x;
}
public static Boolean isEven(int m)
{
i++; System.out.println(i);
return (m%2 == 0 );
}
}

Q2.  Find the errors:

class err{
public static void main(String args[]){
int x , y;
String s = “test”;
m1(int x , s);
y = m2();
}
public void m1(int d ){
… }
public static m2( ) {
if ( true) return true; }
}

Q3.  What is the value of x after each of the following statements is executed

  1. x = Math.abs(3.77);
  2. x = Math.floor(-3.77);
  3. x = Math.abs(0.0);
  4. x = Math.ceil(0.0);
  5. x = Math.abs(-3.77);
  6. x = Math.ceil(-3.77);
  7. x = Math.ceil(-Math.abs(-8 + Math.floor(-4.5)));

Q4.  Write statements that assign to an integer variable n random integers in the following ranges:

  1. 1 <= n <= 2
  2. 1 <= n <= 100
  3. 0 <= n <= 9
  4. 1000 <= n <= 1112
  5. -1 <= n <= 1
  6. -4 <= n <= 55

Q5.  Write method headers for the following:

  1. A method that receives three strings and returns nothing.
  2. A method that receives an integer and a character and returns a string.
  3. A method that returns a Boolean and receives nothing.

Q6.  Indicate which method number would the following method calls match.

Q7.  Decide if each statements sets the instance variable sum or the local variable sum.

Top of Form

class YetOtherClass

{

int sum; // the instance variable

void aMethod( int data )

{

sum = data ; //

. . . .

}

void bMethod( int data )

{

int sum; // a local variable

sum = data ; //

. . . .

}

void cMethod( )

{

. . . .

sum = 23 ; //

int sum;

. . . .

}

}


Programming Questions:

Q8.  Write a method that receives a character and an integer and returns a string composed of the character repeated as many times as the integer.

Q9.  Write a method distanceToLightYears() that takes a distance in kilometers and returns the amount in light-years to travel that distance. The speed of light is approximately 2.997925*108 meters per second.

Q10.  Write a method stripVowels() that extracts and processes the current input line. The method returns, as a string, the consonant portion of the input.

Q11.  A company wants to transmit data over the telephone, but they are concerned that their phones may be tapped. All of their data is transmitted as four-digit integers. They have asked you to write a program that will encrypt their data so that it may be transmitted more securely.

Your program should read a four-digit integer and encrypt it as follows: Replace each digit by (the sum of that digit plus 7) modulus 10. Then, swap the first digit with the third and the second with the fourth. Then print the encrypted integer.

Write a program that inputs four-digit integer and prints an encrypted integer number and decrypts it to form the original number. (Use a method to decrypt and another to encrypt)

Bottom of Form

1