Top Banner
Intruduction to JAVA (Practical) Taner ERKAN 2011 By: Andrey Bogdanchikov Page 1 Task1 All task files, first lines, must have descriptive information about you in comments /************************ * Name: Vasya Pupkin * Group: EN1-A-04 * Task: Task1 subtask A * Date: 12.10.2011 ************************/ Deadline 19.10.2011 [Problem A] Write program that reads double numbers A, B and C then finds result of following equation ? Console Sample Enter A:50 Enter B:4 Enter C:14 Result is 100 [Problem B] Write program that asks from user his name and checks if his/her name has ODD or EVEN number of letters. Console Sample Enter your name: Andrey Your name consist of EVEN number of letters [Problem C] Print all numbers from A to B with step 0.5 by use of while statements. A and B is entered from console. Console Sample Enter A: 0 Enter B: 3 0.0 0.5 1.0 1.5 2.0 2.5 3.0 [Problem D] Write program that reads N. Prompts N float numbers and finds MAXIMUM, MINIMUM and MEDIAN value between them. Console Sample Enter N: 5 Enter 1 number: 4.6 Enter 2 number: 1.2 Enter 3 number: -8.2 Enter 4 number: 5.0 Enter 5 number: -3.2 MAXIMUM is 5.0 MINIMUM is -8.2 MEDIAN is -0.12 [Problem E] Create a program that prints and finds sum of all numbers between 1 and 50 that are divisible by 2 or 3 but not divisible by 6.(for cycle) Console Sample Numbers that divisible by 2 or 3 but not 6: 2 3 4 8 9 10 14 15 16 20 21 22 26 27 28 32 33 34 38 39 40 44 45 46 50 Sum is 1275
45

Taner Erkan Java 1 praktika Ders notları - Kopya

Apr 13, 2015

Download

Documents

Taner Erkan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 1

Task1

All task files, first lines, must have descriptive information about you in comments

/************************ * Name: Vasya Pupkin * Group: EN1-A-04 * Task: Task1 subtask A * Date: 12.10.2011 ************************/

Deadline 19.10.2011

[Problem A]

Write program that reads double numbers A, B and C then finds result of following equation

?

Console Sample

Enter A:50 Enter B:4 Enter C:14 Result is 100

[Problem B]

Write program that asks from user his name and checks if his/her name has ODD or EVEN number of letters.

Console Sample

Enter your name: Andrey Your name consist of EVEN number of letters

[Problem C]

Print all numbers from A to B with step 0.5 by use of while statements. A and B is entered from console.

Console Sample

Enter A: 0 Enter B: 3 0.0 0.5 1.0 1.5 2.0 2.5 3.0

[Problem D]

Write program that reads N. Prompts N float numbers and finds MAXIMUM, MINIMUM and MEDIAN value between them.

Console Sample

Enter N: 5 Enter 1 number: 4.6 Enter 2 number: 1.2 Enter 3 number: -8.2 Enter 4 number: 5.0 Enter 5 number: -3.2 MAXIMUM is 5.0 MINIMUM is -8.2 MEDIAN is -0.12

[Problem E]

Create a program that prints and finds sum of all numbers between 1 and 50 that are divisible by 2 or 3 but not divisible by 6.(for cycle)

Console Sample

Numbers that divisible by 2 or 3 but not 6: 2 3 4 8 9 10 14 15 16 20 21 22 26 27 28 32 33 34 38 39 40 44 45 46 50 Sum is 1275

Page 2: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 2

Task2

All task files, first lines, must have descriptive information about you in comments

/************************ * Name: Vasya Pupkin * Group: EN1-A-04 * Task: Task2 subtask A * Date: 20.10.2011 ************************/

Deadline 26.10.2011

[Problem A]

Write program that reads N, creates array of N integers with values of 2 to the power of INDEX. Where INDEX change for each element. Output this

array.

console:

Console Sample

Enter N: 4

Numbers: 1 2 4 8

[Problem B]

Read array of 10 double elements. Numbers located in even indexes multiply by 2, in odd indexes divide by 2. Output result in reversed order.

Console Sample

Enter 10 numbers: 2 2 2 2 2 2 2 2 2 2 1 4 1 4 1 4 1 4 1 4

[Problem C]

From file “numbers.txt” read all numbers. Find minimum and maximum values among them.

Console Sample numbers.txt

Minimum: 4 Maximum: 6

4 5 6

[Problem D]

Write program that prints following Rhombus for given N.

Console Sample

Enter N: 4 *

***

*****

*******

*****

***

*

[Problem E]

Read all text from file “bigtext.txt” and count number of each letter in this text, Capital and Small.

Console Sample bigtext.txt

H – 1 d – 1 e – 1 l – 3 o – 2 r -1 w - 1

Hello world

Page 3: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 3

Task3

All task files, first lines, must have descriptive information about you in comments

/************************ * Name: Vasya Pupkin * Group: EN1-A-04 * Task: Task3 subtask A * Date: 26.10.2011 ************************/

Deadline 2.11.2011

[Problem A]

Read N, read M, create matrix of size NxM and fill it by multiplication of its indices. Then output result.

Console Sample

Enter N: 3

Enter M:2

Result:

0 0

0 1

0 2

[Problem B]

Read array of 10 double elements. Create two dimensional array of size 4x10. Fill each row by numbers in array multiplied by 2 for 1st

row, by 3 for 2nd

row, and by 5 and 7 for others. Then output result in good designed manner (like following).

Console Sample

Enter 10 numbers: 2 1 2 1 2 1 4 3 4 3 Result: 4 2 4 2 4 2 8 6 8 6

6 3 6 3 6 3 12 9 12 9

10 5 10 5 10 5 20 15 20 15

14 7 14 7 14 7 28 21 28 21

[Problem C]

Read text from “shortstory.txt” file; change all occurrences of word “SDU” into “Suleyman Demirel University”; then output result to “longstory.txt”

file.

shortstory.txt longstory.txt

I am a student of SDU. I like to study in SDU. I love SDU atmosphere and SDU warmth.

I am a student of Suleyman Demirel University. I like to study in Suleyman Demirel University. I love Suleyman Demirel University atmosphere and Suleyman Demirel University warmth.

Page 4: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 4

[Problem D]

Create a program that generates 100 random numbers (0<= r <100). Select among them numbers divisible by 2.

Sort and output, first in increasing order then in decreasing order. (Random r = new Random(); int random = r.nextInt(100);)

Console Sample

Numbers generated: 8 3 18 8 7 23 5 1 0 54 32 2 4 6 3 6 2 4 1 8 Divisible by 2: 8 18 8 0 54 32 2 4 6 6 2 4 8 Increasing order: 0 2 2 4 4 6 6 8 8 8 18 32 54 Decreasing order: 54 32 18 8 8 8 6 6 4 4 2 2

[Problem E]

Read from file “matrix.txt” N then M. After that read N lines by M numbers. Calculate and output transpose matrix into “transpose.txt”.

Console Sample bigtext.txt

2 2 1 2 3 4

1 3 2 4

[BONUS Problem F]

Write program that prompts number N(2<=N<=10). Then draws following figure by use of switch statement. (no cycles!)

Console Sample

Enter N: 4

****

--

***

-

**

Enter N: 8

********

------

*******

-----

******

----

*****

---

****

--

***

-

**

Enter N: 2

**

Page 5: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 5

Task4

All task files, first lines, must have descriptive information about you in comments

/************************ * Name: Vasya Pupkin * Group: EN1-A-04 * Task: Task4 subtask A * Date: 09.11.2011 ************************/

Deadline 16.11.2011 [Problem A(20)]

Create Random N. in range between 1000 and 1000000. Output it. Print all divisors of this number.

Console Sample

Random number: 1013

Divisors: 1 1013

[Problem B(40)]

Fill randomtext.txt with random 1000 letters, CAPITAL or small. Write methods:

a) int numberOfVowels(String a)// that returns number of vowels.

b) boolean isThere SDU(String text)// that checks if in text there word ‘SDU’

c) String longestPalindrome(String line)// that outputs longest palindrome in line.

d) double averageASCII(String all)// finds average ASCII code of all symbols.

Read generated randomtext.txt and apply previous methods on them.

Console Sample

number of vowels: 324 гласная There is word SDU longest Palindrome: ajfgfja Average ASCII is 102.56

[Problem C(20)]

Create class Car that has two fields: type and color. One constructor which creates Object from two String parameters, color should be converted to

small letters and type to capitals. One method that prints text: “This car is #color #type”, where #color and #type are values of corresponding fields.

Create main Class which ask from user to enter parameters of cars, until user enter “no cars”. Output created objects.

Console

Enter car parameters: Blue BMW Enter car parameters: Yellow Zhiguli Enter car parameters: no cars This car is blue BMW This car is yellow ZHIGULI

[Problem D(20)]

Create a program that generates 100 random numbers (0<= r <100).

Create methods that finds maximum digit from any integer.

Sort this numbers by their digits. (i.e number having smallest maximum digit should be first, next after that, and so on) and output them. (if numbers have

same maximum digit then you can place them in any order)

Console Sample

Numbers generated: 8 3 18 8 7 23 5 1 0 54 32 2 4 6 3 6 2 4 1 8 sorted: 0 1 1 2 2 23 3 32 3 4 4 5 54 6 6 7 18 8 8 8

[Problem E(BONUS 20)]

Read from file “calc.txt” long expression that consist of numbers (can be very large) and ‘+’ and ‘-‘ signs. To “result.txt” output result of this

expression.(Question from Koralas Ilyasov)

calc.txt result.txt

321+541-23+1-0 840

Page 6: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 6

Task5

All task files, first lines, must have descriptive information about you in comments

/************************ * Name: Vasya Pupkin * Group: EN1-A-04 * Task: Task5 subtask A * Date: 31.10.2010 ************************/

Deadline 21.11.2010

[30pt Problem A]

Read multiple line text from file “mupdup.dat” get list of all words from that text.

word - is any set of symbols that is separated by spaces, new line symbols or tabulations.

For each word find (create specific method for each of them):

a) number of Capital letters

b) is Palindrome or not (palindrome is text that is same if you read in reverse order)

c) number of vowels

d) sum of digits if exist

Output result to “mopdop.dat” like it is written in example

mupdup.dat mopdop.dat

I like 11 wow things in university. Wow 1st emotion.

I – 1 Capitals, Palindrome, 1 vowels, 0 is sum like – 0 Capitals, not Palindrome, 2 vowels, 0 is sum 11 – 0 Capitals, Palindrome, 0 vowels, 2 is sum wow – 0 Capitals, Palindrome, 1 vowels, 0 is sum things – 0 Capitals, not Palindrome, 1 vowels, 0 is sum in – 0 Capitals, not Palindrome, 1 vowels, 0 is sum university. – 0 Capitals, not Palindrome, 4 vowels, 0 is sum Wow – 1 Capitals, Palindrome, 1 vowels, 0 is sum 1st – 0 Capitals, not Palindrome, 0 vowels, 1 is sum emotion. – 0 Capitals, not Palindrome, 4 vowels, 0 sum

Page 7: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 7

[30pt Problem B]

Army

The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the

highest rank.

One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previousi - 1 ranks is impossible.

Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he

can finally realize his dream.

Input

The first input line contains an integer n (2 ≤ n ≤ 100). The second line contains n - 1 integers di (1 ≤ di ≤ 100). The third input line contains two

integers a and b (1 ≤ a < b ≤ n). The numbers on the lines are space-separated.

Output

Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.

Console Sample

input

3

5 6

1 2

output

5

input

3

5 6

1 3

output

11

[ 40pt Problem C]

Create program that reads from console two numbers N,M. Creates two dimensional array NxM. Realize following methods:

public static void fillRandom(int a[][]); // fill each cell with random number maximum 10

public static int totalSum(int a[][]); // finds total sum of all elements

public static void printArray(int a[][]); // accurately prints two dimensional matrix to screen

public static int[][] sum(int a[][],int b[][]); // sum of two matrixes and return, accepts two identical size matrixes.

public static fillValue(int a[][],int value); // set for each cell given value

Write in main such that all of these methods will be executed (see example):

Page 8: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 8

Console Sample Enter N: 2

Enter M: 3

Matrix A filled by random values:

6 3 1

3 1 2

Total sum of Matrix A is 16

Sum of matrixes A+A is Matrix B:

12 6 2

6 2 4

Total sum of Matrix B is 32

Printing Matrix A:

6 3 1

3 1 2

Printing Matrix B:

12 6 2

6 2 4

Enter number to fill Matrix A: 3

Fill Matrix A by value 3:

3 3 3

3 3 3

Sum of matrixes A+B is Matrix C:

15 9 5

9 5 7

Total sum of Matrix C is 50

[30pt bonus Problem D]

Create class Complex that has:

Two fields, int real and int imaginary;

Write two constructors, default constructor and constructor with two int arguments;

Write four methods:

1. method print that prints Complex number;

2. method sum that adds two Complex numbers and return results;

3. method subtract that subtracts one Complex from other and return result;

4. method multiply that multiplies two Complex numbers and return Complex number result;

Write Main class that creates objects of Complex numbers and outputs some intermediate results:

Console Number A

Enter real part: 7

Enter imaginary part: 8

Complex A number is:

7 + 8i

Number B

Enter real part: -4

Enter imaginary part: 9

Complex B number is:

-4 + 9i

Sum of A and B is:

3 + 17i

A subtract B is:

11 – 1i

Multiplication of A and B is:

-100 + 31i

Page 9: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 9

Task6

All task files, first lines, must have descriptive information about you in comments

/************************ * Name: Vasya Pupkin * Group: EN1-A-04 * Task: Task6 subtask A * Date: 23.11.2010 ************************/

Deadline 30.11.2010

In task3 folder you have three *.jar files one for each subtask:

a) (50pt)GuessMyNumber.jar - game to guess number that computer holds.

b) (50pt)TicTacToe.jar - game played by two players (X) and (O).

c) (bonus 50pt) MonkeyAndBananas.jar - game to help monkey to collect bananas

Hint:

to execute jar file use following command

java -jar NameOfJarFile.jar

here you will see result of execution

GOOD LUCK, I WISH YOU WILL SUCCEED!

Extra Lesson H/W:

Page 10: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 10

Task7

All task files, first lines, must have descriptive information about you in comments

/************************ * Name: Vasya Pupkin * Group: EN1-A-04 * Task: Task7 subtask A * Date: 07.12.2011 ************************/

Deadline 14.12.2011

[30pt Problem A bonus]

Задача Сказки на ночь

Файл входного файла: skazki.in

Файл выходного файла: skazki.out

Ограничение по памяти: 64 MB

Ограничение по времени: 1 s

Описание

Однажды к искусному сказочнику дедушке Мефодию, приехал внук Вася. Вася уже слышал от своего папы, что дедушка очень хороший сказочник. И он попросил дудушку Мефодия каждый вечер, перед сном, рассказывать по М сказок, но так чтобы каждая следующая сказка была короче предыдущей. И чтобы М сказок рассказанных в один день отличалась от М сказок остальных дней.

Теперь дед Мефодий гадает сколько же дней у него проживет Вася если он знает всего К сказок.

Все сказки отличаются по длинне.

Формат входных данных

Во входном файле записано два числа K и M. (1 <= K,M <= 20)

Формат выходных данных

Выходной файл должен содержать одно единственное число - количество дней, на сколько хватит деда Мефодия.

Примеры:

Ввод Вывод

3 2 3

4 4 1

Added: german

Difficulty: 0,976280633902

Accepted: 167

Submitted: 458

Автор: Андрей Богданчиков

Page 11: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 11

[50pt Problem B]

Create class Polynomial that can store and manipulate polynomial equations. We assume that length of polynomial is less than 10 (i.e. maximum

powers are x9).

Example of polynomial equation: 4x3+3x

2+x+65

Class must have following constructors, methods and fields:

Default constructor, Copy constructor and constructor that accepts array of coefficients.

(hint: int [] x = {3,6,1,7}; Polynomial p = new Polynomial(x);// 7x3+x

2+6x+3 )

private field coeffs that is array of integers. (where coefficient index corresponds to x power ex: coeff[3]*x3)

Methods:

o Polynomial add(Polynomial p);// that sums two Polynomials and returns Polynomial

o Polynomial subtract(Polynomial p);// that subtracts two Polynomials and returns Polynomial

o Polynomial multiply(Polynomial p);// that multiplies two Polynomials and returns Polynomial

o double calculate(double x);// that finds result of this polynomial by use of this x

o String toString();// that converts to string representation

Create Main that will create multiple objects of Polynomial class and use all previous methods.

Console Polynomial A

Enter size: 7

Enter coefficients: 8 4 0 3 6 2 -3 //notice that coeffs are reversed

Polynomial A is:

8x^6 + 4x^5 + 3x^3 + 6x^2 + 2x - 3

Polynomial B

Enter size: 5

Enter coefficients: -1 9 0 -4 -2

Polynomial B is:

-x^4 + 9x^3 – 4x - 2

Sum of A and B is:

8x^6 + 4x^5 – x^4 + 12x^3 + 6x^2 - 2x - 5

A subtract B is:

8x^6 + 4x^5 + x^4 - 6x^3 + 6x^2 + 6x - 1

Multiplication of A and B is:

-8x^10 + 68x^9 + 36x^8 - 35x^7 - 11x^6 + 44x^5 + 9x^4 - 57x^3 - 20x^2 + 8x + 6

Enter number: 2

Equation B with x=2:

46

Page 12: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 12

[ 50pt Problem C]

Create hierarchy of classes, where class Person is a superclass:

Person has private fields name and surname. Support with needed constructors

Also Person has method whoAmI() that returns String with name and surname.

Student has protected field GPA(assisted by Abdikali) that stores average mark of student, support with corresponding constructors, and

override method whoAmI() that will return text with name, surname and GPA.

Teacher has protected field AdvisingGroup that stores String of group name he is advisor of.

Subclasses of Teacher have static field subjectName.

Subclasses of Student have static field courseNumber

Because all classes are subclasses of Person all of them should override method whoAmI() that will output all possible information about this

object.

Write Main that creates object of each class and executes all possible methods of that objects.

Person

Student

Freshman Sophomore Junior Senior

Teacher

JavaTeacher WebTeacher

Page 13: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 13

Task 1 A : Answer // Taner ERKAN

import java.util.Scanner; public class A { public static void main(String[]args) { Scanner in = new Scanner(System.in); int A,B,C; double Result; System.out.print("Enter A:"); A = in.nextInt(); System.out.print("Enter B:"); B = in.nextInt(); System.out.print("Enter C:"); C = in.nextInt(); double x=A*B*B; double y=C-6; Result = x/y; System.out.println("Result:"+Result); } }

Page 14: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 14

Task 1 B : Answer // Taner ERKAN

import java.util.Scanner;

public class B

{

public static void main(String[]args)

{

Scanner in = new Scanner(System.in);

System.out.print("Enter your name:");

String name = in.next();

int c = name.length();

if (c%2==0){

System.out.println("Even");}

else {

System.out.println("Odd");}

}

}

Page 15: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 15

Task 1 C : Answer // Taner ERKAN

import java.util.Scanner;

public class C

{

public static void main(String[]args)

{

Scanner in = new Scanner(System.in);

System.out.print("Enter A:");

double A = in.nextInt();

System.out.print("Enter B:");

double B = in.nextInt();

while (A<=B) {

System.out.print(A+" ");

A=A+0.5;

}

}

}

Page 16: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 16

Task 1 D : Answer // Taner ERKAN

import java.math.*;

import java.util.Scanner;

public class D {

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter N:");

int N = in.nextInt();

double sum = 0;

double max = Double.MIN_VALUE;

double min = Double.MAX_VALUE;

for (int x = 1; x<=N ; x++){

System.out.print("Enter "+x+" number:");

double num = in.nextDouble();

if (max <=num)

max = num;

if (min >= num)

min = num;

sum = sum+ num;

}

System.out.println(" MINIMUM is = " + min);

System.out.println(" MAXIMUM is = " + max);

System.out.print(" MEDIAN is = "+(sum/N));

}

}

Page 17: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 17

Task 1 E : Answer // Taner ERKAN

public class E

{

public static void main(String[]args)

{

int i,sum=0;

for(i=1;i<=50;i++)

{

if ((i%2==0 || i%3==0) && i%6!=0)

System.out.print(i+" ");

sum=sum+i;

}

System.out.println("\n"+"Sum is "+sum);

}

}

Page 18: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 18

Task 2 A : Answer // Taner ERKAN

import java.util.*;

public class A

{

public static void main(String[]args)

{

Scanner in=new Scanner(System.in);

System.out.print("Enter N:");

int N=in.nextInt();

int []a1=new int[N];

for (int i1=0;i1<a1.length;i1++){

a1[i1]=2;}

for (int i1=0;i1<a1.length;i1++){

double c=Math.pow(a1[i1],i1);

System.out.print((int)c+" ");}

System.out.println(Arrays.toString(a1));

}

}

Task 2 B : Answer // Taner ERKAN

import java.util.*;

public class B

{

public static void main(String[]args)

{

Scanner in=new Scanner(System.in);

int a1[]=new int[10];

System.out.print("Enter 10 numbers:");

for (int i1=0; i1<10;i1++)

{

int i2=in.nextInt();

a1[i1]=i2;

if (i1%2==0){

a1[i1]=a1[i1]/2;}

else

{a1[i1]=a1[i1]*2;}

System.out.print(a1[i1]+" ");

}

}

}

Page 19: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 19

Task 2 C : Answer // Taner ERKAN

import java.util.*;

import java.io.*;

public class C{

public static void main(String []args)throws Exception{

Scanner in = new Scanner (new File ("numbers.txt"));

int min= Integer.MAX_VALUE;

int max= Integer.MIN_VALUE;

while (in.hasNextLine())

{

int a=in.nextInt();

if (a<=min) min=a;

if (a>=max) max=a;

}

System.out.println("Min: "+min);

System.out.println("Max: "+max);

in.close();

}

}

//////

Number.txt

4 7 -2 -1 6

Page 20: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 20

Task 2 D : Answer // Taner ERKAN

import java.util.*;

public class D{

public static void main(String args[]){

Scanner in = new Scanner(System.in);

System.out.print("Enter N:");

int n = in.nextInt();

for (int i = 1; i <= n; i++){

for (int x = 1; x<=n-i; x++)

System.out.print(" ");

for (int y = 1; y<=i*2-1; y++)

System.out.print("*");

for (int x = 1; x<=n-i; x++)

System.out.print(" ");

System.out.println();

}

for (int i = 1; i < n; i++){

for (int x = 1; x<=i; x++)

System.out.print(" ");

for (int y = 1; y<=(n-i)*2-1; y++)

System.out.print("*");

for (int x = 1; x<=i; x++)

System.out.print(" ");

System.out.println();

}

}

}

Page 21: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 21

Task 2 E : Answer // Taner ERKAN

import java.util.*;

import java.io.*;

public class E{

public static void main (String args[])throws Exception{

Scanner in= new Scanner (new File ("bigtext.txt"));

int a[] = new int[100];

char k=' ';

while (in.hasNextLine())

{

String text=in.nextLine();

for(int i=0; i<text.length();i++)

{

k =text.charAt(i);

if (k>='A' && k<='Z')

a[(int)k-65]+=1;

if (k>='a' && k<='z')

a[(int)k-71]+=1;

}

}

for (int j=0;j<=25; j++)

if (a[j]!=0)

System.out.println((char)(j+65)+"-"+ a[j]);

for (int j=26;j<=51; j++)

if (a[j]!=0)

System.out.println((char)(j+71)+"-"+a[j]);

}

}

//

bigtext.txt

Hello world

Page 22: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 22

Task 3 A : Answer // Taner ERKAN

import java.util.Scanner;

public class A

{

public static void main(String[]args)

{

Scanner in = new Scanner(System.in);

System.out.print("Enter N:");

int N=in.nextInt();

System.out.print("Enter M:");

int M=in.nextInt();

int a[][]=new int[N][M];

for (int i=0;i<N;i++)

for (int j=0;j<M;j++)

a[i][j]=i*j;

for (int i=0;i<N;i++){

for (int j=0;j<M;j++)

System.out.print(a[i][j]+" ");

System.out.println();

}}

}

Page 23: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 23

Task 3 B : Answer // Taner ERKAN

import java.util.Scanner;

public class B

{

public static void main(String[]args)

{

Scanner in = new Scanner(System.in);

System.out.print("Enter 10 numbers:");

double a[]=new double[10];

int i,i2,j;

for (i=0;i<a.length;i++){

a[i]=in.nextInt();}

double a2[][]=new double[4][10];

for (j=0;j<10;j++){

a2[0][j]=a[j]*2;

a2[1][j]=a[j]*3;

a2[2][j]=a[j]*5;

a2[3][j]=a[j]*7;}

for (i2=0;i2<4;i2++){

for (j=0;j<10;j++)

System.out.printf("%4.0f",a2[i2][j]);

System.out.println();}

}

}

Page 24: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 24

Task 3 C : Answer // Taner ERKAN

import java.util.*;

import java.io.*;

public class C

{

public static void main(String[]args)throws Exception{

Scanner in=new Scanner(new File("shortstory.txt"));

PrintWriter out=new PrintWriter(new File("longstory.txt"));

while (in.hasNextLine()){

String s=in.nextLine();

out.println(s.replace("SDU","Suleyman Demirel University"));

}

in.close();out.close();

}

}

// longstory.txt

I am a student of Suleyman Demirel University. I like to study in Suleyman

Demirel University.

I love Suleyman Demirel University atmosphere and Suleyman Demirel University

warmth.

// shortstory.txt

I am a student of SDU. I like to study in SDU.

I love SDU atmosphere and SDU warmth.

Page 25: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 25

Task 3 D : Answer // Taner ERKAN

import java.util.*;

public class D

{

public static void main(String[]args)

{

int n=100;

int a[]=new int[n];

Random r = new Random();

System.out.print("Random numbers: ");

int b[]=new int[n];

for (int i=0;i<n;i++){

int random = r.nextInt(n);

a[i]=random;

System.out.print(random+" ");}

//-----------------------------------------

System.out.println();

System.out.println("----------------------");

//-----------------------------------------

System.out.print("Divisible by 2: ");

int k = 0;

for (int i=0;i<n;i++){

if (a[i]%2==0){

b[k]=a[i];

k++;

System.out.print(a[i]+" ");

}}

//-----------------------------------------

System.out.println();

System.out.println("-----------------------");

//-----------------------------------------

System.out.print("Increasing order: ");

for (int x=0;x<k-1;x++){

for (int y=x+1;y<k;y++){

if (b[x]>b[y]){

int swap=b[x];

b[x]=b[y];

b[y]=swap;}

}}

for (int x=0;x<k;x++){

System.out.print(b[x]+" ");}

//-----------------------------------------

System.out.println();

System.out.println("-----------------------");

//-----------------------------------------

System.out.print("Decreasing order: ");

for (int x=k-1;x>=0;x--){

System.out.print(b[x]+" ");}

}

}

Page 26: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 26

Task 3 E : Answer // Taner ERKAN import java.util.*;

import java.io.*;

public class E

{

public static void main(String[]args) throws Exception

{

Scanner in = new Scanner(new File("matrix.txt"));

PrintWriter out = new PrintWriter(new File("transpose.txt"));

int i,j;

int N=in.nextInt();

int M=in.nextInt();

int a[][]=new int[N][M];

for (i=0;i<N;i++)

for (j=0;j<M;j++)

a[i][j]=in.nextInt();

for (i=0;i<M;i++){

for (j=0;j<N;j++)

out.print(a[j][i]+" ");

out.println();}

in.close();out.close();

}

}

// matrix.txt

3 4

1 2 3 0

0 0 2 1

2 4 -5 2

// transpose.txt

1 0 2

2 0 4

3 2 -5

0 1 2

Page 27: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 27

Task 3 F : Answer // Taner ERKAN import java.util.Scanner;

public class F{

public static void main(String args[]){

Scanner in = new Scanner(System.in);

int n=in.nextInt();

switch(n){

case 10:

System.out.println("**********");

System.out.println("--------");

case 9:

System.out.println("*********");

System.out.println("-------");

case 8:

System.out.println("********");

System.out.println("------");

case 7:

System.out.println("*******");

System.out.println("-----");

case 6:

System.out.println("******");

System.out.println("----");

case 5:

System.out.println("*****");

System.out.println("---");

case 4:

System.out.println("****");

System.out.println("--");

case 3:

System.out.println("***");

System.out.println("-");

case 2:

System.out.println("**");

default:

break;

}

}

}

Page 28: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 28

Task 4 A : Answer // Taner ERKAN import java.util.*;

public class A{

public static void main(String args[]) {

Random r = new Random();

System.out.print("Enter N:");

int n = r.nextInt(999000)+1000;

System.out.println(n);

System.out.print("Divisors:");

for (int i=1;i<=n;i++)

if (n%i==0)

System.out.print(i+" ");

}

}

Page 29: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 29

Task 5 A : Answer // Taner ERKAN import java.util.*;

import java.io.*;

public class ProblemA {

/*a*/ public static int SumCapital(String s){

int count = 0;

for (int i = 0; i<s.length(); i++)

if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') count++;

return count;

}

/*b*/ public static String Swap(String s) {

String str = "";

for (int i = s.length()-1; i >= 0; i--) {

str += s.charAt(i);

}

return str;

}

public static boolean isPalindrome(String s) {

String s1, s2;

if (s.length()+1 % 2 == 0) {

s1 = s.substring(0, s.length() / 2);

s2 = Swap(s.substring(s.length() / 2, s.length()));

} else {

s1 = s.substring(0, s.length() / 2);

s2 = Swap(s.substring(s.length() / 2 + 1, s.length()));

}

if (s1.equalsIgnoreCase(s2)) {

return true;

} else {

return false;

}

}

/*c*/ public static int NumbVowels(String s)

{

char ch[] = {'A', 'E', 'I', 'O', 'Y', 'a', 'e', 'i', 'o', 'y'};

int count = 0;

for (int i = 0; i < s.length(); i++) {

for (int j = 0; j < ch.length; j++) {

if (s.charAt(i) == ch[j]) {

count++;}

}

}

return count;

}

/*d*/ public static int SumDigits(String s){

int count = 0;

for (int i = 0; i<s.length(); i++)

if (s.charAt(i) >= '1' && s.charAt(i) <= '9') count=count+Integer.parseInt("0"+ s.charAt(i));

return count;

}

public static void main (String args[]) throws Exception{

Scanner in = new Scanner (new File("mupdup.dat"));

PrintWriter out = new PrintWriter(new File("mopdop.dat"));

while (in.hasNext()){

String s = in.next();

out.println(s + " - " + SumCapital(s) + " Capitals," + (isPalindrome(s)?" Palindrome, ":" not

Palindrome, ") + NumbVowels(s) + " vowels, " + SumDigits(s) + " is sum");

}

in.close(); out.close();

}

}

Page 30: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 30

//mopdop.dat

I like 11 wow things in university.

Wow 1st emotion.

//mupdup.dat

I - 1 Capitals, Palindrome, 1 vowels, 0 is sum

like - 0 Capitals, not Palindrome, 2 vowels, 0 is sum

11 - 0 Capitals, not Palindrome, 0 vowels, 2 is sum

wow - 0 Capitals, Palindrome, 1 vowels, 0 is sum

things - 0 Capitals, not Palindrome, 1 vowels, 0 is sum

in - 0 Capitals, not Palindrome, 1 vowels, 0 is sum

university. - 0 Capitals, not Palindrome, 4 vowels, 0 is sum

Wow - 1 Capitals, Palindrome, 1 vowels, 0 is sum

1st - 0 Capitals, not Palindrome, 0 vowels, 1 is sum

emotion. - 0 Capitals, not Palindrome, 4 vowels, 0 is sum

Task 5 B : Answer // Taner ERKAN

import java.util.*;

public class ProblemB {

public static void main (String args[]){

Scanner in = new Scanner (System.in);

int n = in.nextInt();

int d[] = new int[n];

for (int i = 1; i<n; i++)

d[i] = in.nextInt();

int a = in.nextInt();

int b = in.nextInt();

int sum = 0;

for (int i = a; i<b; i++){

sum += d[i];

}

System.out.println(sum);

}

}

Page 31: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 31

Task 5 C : Answer // Taner ERKAN import java.util.*;

public class ProblemC {

public static void fillRandom(int a[][]){

for (int i = 0; i<a.length; i++)

for (int j = 0; j<a[i].length; j++){

Random rand = new Random();

a[i][j] = rand.nextInt(11);

}

}

public static int totalSum(int a[][]){

int sum = 0;

for (int i = 0; i<a.length; i++)

for (int j = 0; j<a[i].length; j++)

sum += a[i][j];

return sum;

}

public static void printArray(int a[][]){

for (int i = 0; i<a.length; i++){

for (int j = 0; j<a[i].length; j++)

System.out.print(a[i][j] + " ");

System.out.println();

}

}

public static int[][] sum(int a[][],int b[][]){

int [][]c = new int[a.length][a[0].length];

for (int i = 0; i<a.length; i++)

for (int j = 0; j<a[0].length; j++)

c[i][j] = a[i][j] + b[i][j];

return c;

}

public static void fillValue(int a[][],int value){

for (int i = 0; i<a.length; i++)

for (int j = 0; j<a[i].length; j++)

a[i][j] = value;

}

public static void main (String args[]){

Scanner in = new Scanner (System.in);

System.out.print("enter N: ");

int n = in.nextInt();

System.out.print("Enter M: ");

int m = in.nextInt();

System.out.println("Matrix A filled by random values:");

int a[][] = new int[n][m];

fillRandom(a);

printArray(a);

System.out.println("Total sum of Matrix A is " + totalSum(a));

System.out.println("Sum of matrixes A+A is Matrix B: ");

int [][]b = new int[n][m];

b = sum(a, a);

printArray(b);

System.out.println("Total sum of Matrix B is " + totalSum(b));

System.out.println("Printing Matrix A:");

printArray(a);

System.out.println("Printing Matrix B:");

printArray(b);

System.out.print("Enter number to fill Matrix A: ");

int value = in.nextInt();

System.out.println("Fill Matrix A by value "+ value + " :");

fillValue(a, value);

printArray(a);

System.out.println("Sum of matrixes A+B is Matrix C:");

int c[][] = new int[n][m];

c = sum(a, b);

printArray(c);

System.out.print("Total sum of Matrix C is " + totalSum(c));

}

}

Page 32: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 32

Task 5 D : Answer // Taner ERKAN import java.util.*;

public class Complex {

public int real;

public int imaginary;

public Complex(){}

public Complex(int a, int b){

real = a; imaginary = b;

}

public static void print(Complex a){

if (a.imaginary>=0)

System.out.println(a.real + " + " + a.imaginary + "i");

else System.out.println(a.real + " - " + (-1*a.imaginary) + "i");

}

public static Complex Sum(Complex a, Complex b){

Complex c = new Complex();

c.real = a.real + b.real;

c.imaginary = a.imaginary + b.imaginary;

return c;

}

public static Complex Subtract(Complex a, Complex b){

Complex c = new Complex();

c.real = a.real - b.real;

c.imaginary = a.imaginary - b.imaginary;

return c;

}

public static Complex Multiply(Complex a, Complex b){

Complex c = new Complex();

c.real = a.real*b.real;

c.imaginary = a.imaginary*b.imaginary;

return c;

}

public static void main (String args[]){

Scanner in = new Scanner (System.in);

System.out.printf("Number A\nEnter real part: ");

int n = in.nextInt();

System.out.printf("Enter imaginary part:");

int m = in.nextInt();

System.out.println("Complex A number is: ");

Complex a = new Complex(n,m);

print(a);

System.out.printf("Number B\nEnter real part: ");

n = in.nextInt();

System.out.printf("Enter imaginary part:");

m = in.nextInt();

System.out.println("Complex B number is: ");

Complex b = new Complex(n,m);

print(b);

System.out.println("Sum of A and B is:");

print(Sum(a,b));

System.out.println("A subtract B is:");

print(Subtract(a,b));

System.out.println("Multiplication of A and B is:");

print(Multiply(a, b));

}

}

Page 33: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 33

Task 6 A : Answer // Taner ERKAN

import java.util.*;

public class GuessMyNumber {

public static void main(String args[]){

Scanner in = new Scanner(System.in);

System.out.print("I hold in memory one number between 0 and 100 try to guess

it\nYou type one number I will say my number is greater or smaller\n");

Random r = new Random();

int random = r.nextInt(101);

int n = -1;

int count = 0;

while (n!=random){

System.out.println("Your number");

count++;

n = in.nextInt();

while(n>100 || n<0) { System.out.print("Type Correct Number!\nYour

number:\n");n = in.nextInt(); }

if (n>random) System.out.print("My number is smaller\n");

else if(n<random) System.out.print("My number is greater\n");

}

System.out .print("Correct Number\nNumber of tries is " + count + "\nGood

bye!");

}

}

Page 34: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 34

Task 6 B : Answer // Taner ERKAN import java.util.*;

public class MonkeyAndBananas {

public static int[][] random(int n){

int a[][] = new int[n][n];

for (int i = 0; i<n; i++)

{

for (int j = 0; j<i+1; j++)

{Random r = new Random();

int rand = r.nextInt(10);

a[i][j] = rand;

}

}

return a;

}

public static void print(int n, int l, int r, int[][]a, int x, int y){

int h = 1;

String s = "";

for (int g = 1; g<2*n-1; g++) s += " ";

for (int i =l; i<n; i++){

System.out.print(s);

for (int j = r; j<r+h; j++)

if (x==i && y==j) System.out.print("M");

else System.out.print(a[i][j] + " ");

System.out.println();

if (i!=n-1)

{

if (!s.equals("")) s = s.substring(0, s.length()-1);

System.out.print(s);

for (int j = r; j<r+h; j++)

System.out.print("/ \\ ");

System.out.println();

}

if (!s.equals("")) s = s.substring(0, s.length()-1);

h++;

}

}

public static void Valid(String s){

if (!s.equalsIgnoreCase("l")) if (!s.equalsIgnoreCase("r")) System.out.println("Type CORRECT

direction!"); }

public static void main(String agrs[])

{

Scanner in =new Scanner(System.in);

int n = in.nextInt();

in.nextLine();

int a[][] = new int[n+1][n+1];

a = random(n);

print(n, 0, 0, a, -1,-1);

System.out.print("This is banana tree!\nAt top of this tree we have Monkey\nMonkey collects bananas from

this tree, but it can go only down.\nHelp Monkey to collect bananas as much as possible!\nPress enter to

continue...\n");

String s = in.nextLine() ;

int Sum = 0;

int j = 0;

for (int i = 0; i<n; i++){

s = "";

Sum += a[i][j];

if (i!=n-1) {

print(n, i, j, a, i, j);

System.out.println("Now Monkey have " + Sum + " bananas");

while(!s.equalsIgnoreCase("l") && !s.equalsIgnoreCase("r")) {

System.out.println("Wherer to move Monkey (L)eft or (R)ight:");

s = in.nextLine();

Valid(s);

}

if (s.equalsIgnoreCase("r")) if (j<n) j++;

}}

System.out.println("As a result Monkey have " + Sum + " bananas");

Monkey.Max(a, n, 0, 0, a[0][0]);

System.out.println("Good bye");

}

}

Page 35: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 35

Task 6 C: Answer // Taner ERKAN import java.util.*;

public class Task1

{

public static void main(String args[])

{

Scanner in = new Scanner(System.in);

Random r = new Random();

int n = r.nextInt(101);

System.out.println("I hold in memory one number between 0 and 100 try to

guess it");

System.out.println("You type one number I will say my number is greater

or smaller");

int p;

int counter = 0;

do

{

counter++;

System.out.println("Enter your number:");

p = in.nextInt();

if(p < 0 || p > 100)

{

System.out.println("Type correct number!");

break;

}

else if (p<n)

{

System.out.println("My number is greater");

}

else if (p>n)

{

System.out.println("My number is smaller");

}

else if (p==n)

{

System.out.println("Number of tries is "+ counter );

System.out.println("Good bye");

break;

}

}while(p!=n);

}

}

Page 36: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 36

Task 6 D : Answer // Taner ERKAN import java.util.*;

public class TicTacToe {

public static boolean provV(char[][] a, int n, int x) {

boolean t = true;

for (int i = 1; i < n; i++) {

if (a[i][x] != a[i - 1][x] || a[i][x] == '.') {

t = false;

}

}

return t;

}

public static boolean provG(char[][] a, int n, int x) {

boolean t = true;

for (int i = 1; i < n; i++) {

if (a[x][i] != a[x][i - 1] || a[x][i] == '.') {

t = false;

}

}

return t;

}

public static boolean provD(char[][] a, int n) {

boolean t = true;

int i = 1;

int j = 1;

while (i < n) {

if (a[i][j] != a[i - 1][j - 1] || a[i][j] == '.') {

t = false;

}

i++;

j++;

}

return t;

}

public static boolean provR(char[][] a, int n) {

boolean t = true;

int i = n - 1;

int j = 0;

while (j < n - 1) {

i--;

j++;

if (a[i][j] != a[i + 1][j - 1] || a[i][j] == '.') {

t = false;

}

}

return t;

}

public static void print(char a[][], int n) {

System.out.print(" ");

for (int i = 0; i < n; i++) {

System.out.print(" " + (i + 1));

}

System.out.println();

System.out.print(" ");

for (int i = 0; i < n*2 + 1; i++) {

System.out.print("-");

}

System.out.println();

for (int i = 0; i < n; i++) {

System.out.print((i + 1) + " :");

for (int j = 0; j < n; j++) {

System.out.print( a[i][j] + ":");

}

System.out.println();

}

System.out.print(" ");

for (int i = 0; i<n*2 + 1; i++)

Page 37: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 37

System.out.print("-");

System.out.println();

}

public static char[][] fill(char a[][], int n) {

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

a[i][j] = '.';

}

}

return a;

}

public static boolean Valid(char[][] a, int x, int y, int n) {

boolean t = true;

if (x > n || x < 1 || y > n || y < 1 || a[x - 1][y - 1] != '.') {

System.out.println("TYPE VALID COORDINATES!!");

t = false;

}

return t;

}

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

int n;

if (args.length == 0) n = 3;

else n = Integer.parseInt(args[0]);//= Integer.parseInt(args[0]);

char a[][] = new char[n][n];

a = fill(a, n);

print(a, n);

System.out.print("This is TicTacToe arena\nGame is played by two players(X,O), each player types

coordinates\nof their moves and tries to hold one line vertical, horizantal or diagonal \n");

int h = 1;

boolean t = false;

int x;

int y;

while (h <= n * n && !t) {

do {

System.out.print("Player " + (h % 2 == 0 ? "2(O)" : "1(x)") + "moves\n");

System.out.print("Type two coordinates:\n");

x = in.nextInt();

y = in.nextInt();

} while (!Valid(a, x, y, n));

a[x - 1][y - 1] = (h % 2 == 0 ? 'O' : 'X');

print(a, n);

for (int i = 0; i < n; i++) {

if (provV(a, n, i) || provG(a, n, i) || provD(a, n) || provR(a, n)) {

System.out.println("Congratulation player " + (h % 2 == 0 ? "2(O)" : "1(X)") + " you win!");

t = true; break;

}

}

h++;

}

System.out.println("Good bye");

}

}

Page 38: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 38

Task 7 B: Answer // Taner ERKAN

package B;

import java.util.Scanner;

public class Main {

public static void main(String [] args){

Scanner in = new Scanner(System.in);

int aa[], alength, bb[], blength;

double calc;

Polynomial a, b, c, d, e;

System.out.println("Polynomial A");

System.out.print("Enter size: ");

alength = in.nextInt();

aa = new int[alength];

System.out.print("Enter coefficients: ");

for (int i = alength - 1; i >= 0; i--) {

aa[i] = in.nextInt();

}

System.out.println("Polynomial A is:");

a = new Polynomial(aa);

System.out.println(a);

System.out.println("Polynomial B");

System.out.print("Enter size: ");

blength = in.nextInt();

bb = new int[blength];

System.out.print("Enter coefficients: ");

for (int i = blength - 1; i >= 0; i--) {

bb[i] = in.nextInt();

}

System.out.println("Polynomial B is:");

b = new Polynomial(bb);

System.out.println(b);

System.out.println("Sum of A and B is:");

c = a.add(b);

System.out.println(c);

System.out.println("A subtract B is:");

d = a.subtract(b);

System.out.println(d);

System.out.println("Multiplication of A and B is:");

e = a.multiply(b);

System.out.println(e);

System.out.print("Enter number:");

calc = in.nextDouble();

System.out.println("Equation B with x=" + (int) calc);

System.out.println((int) b.calculate(calc));

}

Page 39: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 39

}

//polynomial.txt

package B;

public class Polynomial {

private int[] coeffs;

private int l = 0;

public Polynomial() {

coeffs = new int[l];

}

public Polynomial(int[] x){

coeffs = x;

for(int i = 0;i < x.length;i++){

coeffs[i] = x[i];

}

}

public Polynomial(Polynomial k){

coeffs = k.coeffs;

}

public static String pow(int n){

String s = "";

if(n > 1){

s = s + "x^" + n;

}

else if(n == 1){

s = s + "x";

}

return s;

}

@Override

public String toString(){

String s = "";

boolean k = true;

//int n = coeffs.length - 1;

/*if(coeffs[n] != 0){

if(n == 0){

s = s + coeffs[n];

}

else{

if(coeffs[n] == 1){

s = s + pow(n);

}

else if(coeffs[n] == -1){

s = s + "-" + pow(n);

}

else if(coeffs[n] > 0){

s = s + coeffs[n] + pow(n);

}

Page 40: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 40

else if(coeffs[n] < 0){

s = s + coeffs[n] + pow(n);

}

}

}

else

s = s + "0";*/

for(int i = coeffs.length - 1;i >= 0;i--){

if(coeffs[i] != 0){

if (k) {

if (i == 0) {

s += coeffs[i];

} else {

if (coeffs[i] == 1) {

s += pow(i);

} else if (coeffs[i] == -1) {

s += "-" + pow(i);

} else if (coeffs[i] > 0) {

s += coeffs[i] + pow(i);

} else {

s += coeffs[i] + pow(i);

}

}

k = false;

}

else if(i == 0){

if(coeffs[i] < 0){

s = s + " - " + (int)Math.abs(coeffs[i]);

}

else{

s = s + " + " + coeffs[i];

}

}

else{

if(coeffs[i] == 1){

s = s + " + " + pow(i);

}

else if(coeffs[i] == -1){

s = s + " - " + pow(i);

}

else if(coeffs[i] > 0){

s = s + " + " + coeffs[i] + pow(i);

}

else if(coeffs[i] < 0){

s = s + " - " + (int)Math.abs(coeffs[i]) + pow(i);

}

}

}

}

if(s.equals("")){

s = s + "0";

}

return s;

}

public Polynomial add(Polynomial p) {

Page 41: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 41

l = Math.max(p.coeffs.length, coeffs.length);

int a[] = new int[l];

Polynomial sum = new Polynomial(a);

//Polynomial sum = new Polynomial(p.coeffs.length >= coeffs.length ? p :

this);

for (int i = 0; i <= Math.min(p.coeffs.length, coeffs.length) - 1; i++) {

sum.coeffs[i] = coeffs[i] + p.coeffs[i];//(p.coeffs.length <

coeffs.length ? p.coeffs[i] : coeffs[i]);

}

for (int i = Math.min(p.coeffs.length, coeffs.length); i <

Math.max(p.coeffs.length, coeffs.length); i++) {

sum.coeffs[i] = (p.coeffs.length > coeffs.length ? p.coeffs[i] :

coeffs[i]);

}

return sum;

}

public Polynomial subtract(Polynomial p) {

l = Math.max(p.coeffs.length, coeffs.length);

int a[] = new int[l];

Polynomial sum = new Polynomial(a);

//Polynomial sum = new Polynomial(p.coeffs.length >= coeffs.length ? p :

this);

for (int i = 0; i <= Math.min(p.coeffs.length, coeffs.length) - 1; i++) {

sum.coeffs[i] = coeffs[i] - p.coeffs[i];

}

for (int i = Math.min(p.coeffs.length, coeffs.length); i <

Math.max(p.coeffs.length, coeffs.length); i++) {

sum.coeffs[i] = (p.coeffs.length > coeffs.length ? -p.coeffs[i] :

coeffs[i]);

}

/*for(int i = 0;i < sum.coeffs.length;i++)

sum.coeffs[i] *= -1;

if(p.coeffs.length > coeffs.length)

return sum;

else {

for(int i = 0;i < sum.coeffs.length;i++)

sum.coeffs[i] *= -1;

return sum;

}*/

return sum;

}

public Polynomial multiply(Polynomial p) {

l = p.coeffs.length + coeffs.length;

int a[] = new int[l];

Polynomial mul = new Polynomial(a);

for (int i = 0; i < p.coeffs.length; i++) {

for (int j = 0; j < coeffs.length; j++) {

mul.coeffs[i + j] += p.coeffs[i] * coeffs[j];

}

}

return mul;

}

Page 42: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 42

public double calculate(double x) {

double total = 0;

int power = 0;

for (int i = 0; i <= coeffs.length - 1; i++) {

total += (double) (coeffs[i] * Math.pow(x, power));

power++;

}

return total;

}

/*public static int[] newPoly(int leng) {

int[] a = new int[leng];

return a;

}*/

}

Task 7 B: Answer // Taner ERKAN

import java.io.*;

import java.util.*;

public class Main1 {

public static void main(String [] args)throws Exception{

Scanner in1 = new Scanner(new File("skazki.in"));

int K = in1.nextInt();

int M = in1.nextInt();

long answer = fac(K)/(fac(M)*fac(K - M));

PrintWriter out = new PrintWriter(new File("skazki.out"));

out.print(answer);

out.close();

in1.close();

}

public static int fac(int n){

int total = 1;

if(n > 0){

for(int i = 1;i <= n;i++){

total = total * i;

}

}

return total;

}

}

//skazi.in

3 2

// skazi.out

1

Page 43: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 43

Task 7 c: Answer // Taner ERKAN

public class WebTeacher extends Teacher{

private static String subjectName = "WEB";

public WebTeacher(String name,String surname,String AdvisingGroup){

super(name,surname,AdvisingGroup);

}

@Override

public void whoAmI(){

super.whoAmI();

System.out.print(" "+subjectName);

}

}

public class Freshman extends Student{

private static int courseNumber = 1;

public Freshman(String name,String surname,double GPA){

super(name,surname,GPA);

}

@Override

public void whoAmI(){

super.whoAmI();

System.out.print(" "+courseNumber);

}

}

public class JavaTeacher extends Teacher{

private static String subjectName = "JAVA";

public JavaTeacher(String name,String surname,String AdvisingGroup){

super(name,surname,AdvisingGroup);

}

@Override

public void whoAmI(){

super.whoAmI();

System.out.print(" "+subjectName);

}

}

public class Main {

public static void main(String [] args){

Person p = new Person("Name", "Surname");

Student s = new Student("Stu","Dent",4);

Student sq = new Student("a","b", 3);

Teacher t = new Teacher("Tea","Cher","Group");

Freshman f = new Freshman("Fresh","Student",3.0);

Sophomore so = new Sophomore("Soph","Student",3.5);

Junior j = new Junior("Junior","Student",3.9);

Senior sen = new Senior("Senior","Student",4.0);

JavaTeacher jt = new JavaTeacher("Java","Some","JavaGroup");

WebTeacher wt = new WebTeacher("Web","Some","WebGroup");

Page 44: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 44

System.out.print("Person ");p.whoAmI();

System.out.println();

System.out.print("Student ");s.whoAmI();

System.out.println();

System.out.print("Teacher ");t.whoAmI();

System.out.println();

System.out.print("Freshman ");f.whoAmI();

System.out.println();

System.out.print("Sophomore ");so.whoAmI();

System.out.println();

System.out.print("Junior ");j.whoAmI();

System.out.println();

System.out.print("Senior ");sen.whoAmI();

System.out.println();

System.out.print("JavaTeacher ");jt.whoAmI();

System.out.println();

System.out.print("WebTeacher ");wt.whoAmI();

System.out.println();

System.out.print("St 2");sq.whoAmI();

}

}

public class Student extends Person{

protected double GPA;

public Student(String name,String surname,double GPA){

super(name,surname);

this.GPA = GPA;

}

@Override

public void whoAmI(){

super.whoAmI();

System.out.print(" "+GPA);

}

}

public class Person {

private String name;

private String surname;

public Person(String n,String s){

name = n;

surname = s;

}

public void whoAmI(){

System.out.print(name + " " + surname);

}

}

Page 45: Taner Erkan Java 1 praktika  Ders notları - Kopya

Intruduction to JAVA (Practical) Taner ERKAN 2011

By: Andrey Bogdanchikov Page 45

public class Teacher extends Person{

protected String AdvisingGroup;

public Teacher(String name,String surname,String AdvisingGroup){

super(name,surname);

this.AdvisingGroup = AdvisingGroup;

}

@Override

public void whoAmI(){

super.whoAmI();

System.out.print(" "+AdvisingGroup);

}

}

public class Junior extends Student{

private static int courseNumber = 3;

public Junior(String name,String surname,double GPA){

super(name,surname,GPA);

}

@Override

public void whoAmI(){

super.whoAmI();

System.out.print(" "+courseNumber);

}

}

public class Sophomore extends Student{

private static int courseNumber = 2;

public Sophomore(String name,String surname,double GPA){

super(name,surname,GPA);

}

@Override

public void whoAmI(){

super.whoAmI();

System.out.print(" "+courseNumber);

}

}