Bilkent IEEE Computer Society. Java Tutorial Solutions

Bilkent IEEE Computer Society Java Tutorial Solutions Bilkent IEEE Computer Society – Java Tutorial Fall 2014 2 Bilkent IEEE Computer Society –...
Author: Lionel Jones
0 downloads 2 Views 301KB Size
Bilkent IEEE Computer Society

Java Tutorial Solutions

Bilkent IEEE Computer Society – Java Tutorial

Fall 2014

2

Bilkent IEEE Computer Society – Java Tutorial Warm Up 1) Write the outputs of given lines. If there is Error instead of output, write "ERROR". a)

int x = 10; System.out.println (!(x > 10)) ; true basar boolean ifadesi çünkü

b)

int a = 5; System.out.println(++a);

a yı arttırır sonra bastırır -> 6

System.out.println(a); a yı bastırır -> 6 System.out.println(a++); a yı bastırır sonra artırır -> 6

c)

double x = 5; System.out.println(3/4+""+x); 05.0 “” Bos String ifadesi ve “” ile baska bir ifadeyi toplayinca sonuc da String oluyor.

3(int) / 4(int) islemi integer bolmesi oldugundan sonuc 0. [sayilardan bir tanesi bile 3.0 gibi noktali sayi olsaydi islemin sonucu da noktali sayi olurdu]. X double olarak tanimlandigindan dolayi ciktisini aldigimizda noktali sayi ifadesini aliyoruz.

d)

double x = 4; System.out.println(3/4*x); 0.0

3

Bilkent IEEE Computer Society – Java Tutorial 3/4 integer bolmesi oldugundan dolayi 0’a esit. 0*x = 0. Ancak x double oldugundan dolayi carpmanin sonucu da double yani 0.0 olacak.

e)

int x = 4; if(x == 4) System.out.println(true); System.out.println(false); true false (if gibi kontrol yapilari bloklari olmadigi ve sonlarinda noktali virgulun

olmadigi durumlarda kendilerinden sonraki ilk komutu kontrol ederler.)

f)

int countA = 3; while(countA < 3) { System.out.println("x"); countA--; } int countB = 3; do { System.out.println("x"); countB--; } while(countB < 3); 3 2 1 0 -1 – 2 -3

..... infinite loop oluyor 4

Bilkent IEEE Computer Society – Java Tutorial Ancak integerlarin en kucuk sayisindan bir cikarinca en buyuk integer elde edilir ve loop uzun bir surenin ardindan durur.

g)

int n = 10; while(n > 0) { System.out.println(n); n = n - 3; }

10 7 4 1

5

Bilkent IEEE Computer Society – Java Tutorial 2)

Write a static method that takes an integer n as parameter and returns n!

public static int factorial(int n) { int result = 1; for( ; n > 1; n--) { result = result*n; } return result; }

3) Write a static method that takes an integer and returns whether the input is a square of an integer or not, as boolean.

public static boolean isSquare( int num) { for ( int i = 0; i*i 0; i++) { // add binary's last digit * 2^i to our decimal variable decimal += (int) ((binary % 10) * Math.pow(2, i));

// binary = binary / 10 binary /= 10; }

System.out.println("Decimal equivalent of " + binary + " is " + decimal);

8

Bilkent IEEE Computer Society – Java Tutorial 5) Take a number from user and convert it to binary.

Sample execution: Enter an integer:

122

Binary equivalent of 122 is 1111010

Scanner scan = new Scanner(System.in); int decimal; String binary; System.out.print(“Enter an decimal number: “); decimal = scan.nextInt();

binary = ""; while (decimal > 0) { // add remaining decimal number's mod 2 to binary binary = decimal % 2 + binary;

// divide decimal by two decimal /= 2; } System.out.println("Binary equivalent of " + decimal + " is " + binary);


9

Bilkent IEEE Computer Society – Java Tutorial 6) Write a function that calculates Greatest Common Divisor using Euclid’s algorithm.

public static int gcd(int p, int q) { while (q != 0) { int temp = q; q = p % q; p = temp; } return p; } 


10

Bilkent IEEE Computer Society – Java Tutorial Previous Midterm Questions 1) (Fall 2012)

Write a static method that takes a String as its parameter and return whether this String is a “Pangram” or not as boolean. A pangram includes every letter of the alphabet at least once. Example: “The quick brown fox jumps over a lazy dog.” public static boolean isPangram(String input)
 {     String alphabet = "abcdefghijklmnopqrstuvwxyz";
     for (int i = 0 ; i < alphabet.length() ; i++)
        if(input.indexOf(alphabet.charAt(i)) == -1)
            return false;
     return true;
 }

// Alternative solution public static boolean isPangram(String input)
 {     String alphabet = "abcdefghijklmnopqrstuvwxyz";
     for (int i = 0 ; i < input.length() ; i++)
 alphabet.remove(input.charAt(i)); 
      if(alphabet.length() > 0) return false; else return true; }

11

Bilkent IEEE Computer Society – Java Tutorial 2) (Spring 2013)

Write the equivalent following while loop with using only one for loop. int i = 0; while(i