Chapter 01.
The Strength of the Fours: Variants, Statements, Conditions and Cycles
I n the beginning there were only four elements. Kingdoms rule each of them.
The Variants, the Gods of Eternal Changing. They claim the power of fact, that every code, written
anywhere, by anyone needs to be changed, depending on conditions or on information passed to the
program. Without them, a code would only work based on predetermined values, and therefore the
Program could only be plunged into darkness. Because, what cannot be changed, determined to stop
working in a world, where everything changes. It’s only a matter of time.
There are many variants, their difference is defined by what and how they can store a Value.
for ciklus
hogyan módosítanád az utolsó megírt programot (for ciklus; peti választotta szám) úgy, hogy csak
addig a számig írja ki a számokat, amíg a peti által választott számhoz ér
feladat 4
Write a program that takes a temperature in Celsius and converts it into Fahrenheit.
input: 24,5 (Celsius)
feladat 5
Write a method that describes everybody's favorite, Leo (no, not Messi)
- If oscar was (int) 88, print the String "Leo finally won the oscar! Leo is happy.”
- If oscar was 86, you have to print "Not even for The Wolf of Wall Street?!”
- If it was not 88 or 86 (and below 88), print "When will you give Leo an Oscar?”
- If it was over 88, you should print "Leo got one already!”
Input format: containing a single int value
try with oscar = 90,88,87 and 86
feladat 8
Given a String and a non-negative int n, print a String that consists of n copies of the original string.
input
n = 5; String = "ha"
expected output: "hahahahaha"
feladat 6
Given an integer, n, perform the following conditional actions:
If n is odd, print 'Weird'.
If n is even and in the inclusive range of 2 to 5, print 'Not Weird'.
If n is even and in the inclusive range of 6 to 20, print 'Weird'.
If n is even and greater than 20, print 'Not Weird'.
Input Format
a positive integer, n.
Output Format
Print 'Weird' or 'Not Weird' based on the logic written above.
feladat 7
Given an integer, n , print its first 10 multiples.
Input Format
A single integer, n.
Constraints
0 <= n <= 50
Output Format
Print 10 lines of output; each line i (where 1 <= i <= 10) contains the result of n x i in the form: 'n x i
= result'.
feladat 18:
Write a program that takes three numbers as input to calculate and print the average of the numbers.
feladat 19:
Write a program to display the following pattern.
feladat 28: (ezt együtt fogjuk csinálni, ne kezdj hozzá)
Write a program to convert a decimal number to a binary number.
TÖMBBEL megcsinálni
feladat 27:
Write a program to compare two numbers. (like below)
feladat 26:
Write a program to print the odd numbers from 1 to 99. Print one number per line.
feladat 25:
Write a program to accept a number and check the number is even or not. Print true if the number is
even or false if the number is odd.
feladat 23:
Write a program to print all numbers from 1 to 100. If the number is even, print true next to it, or if it
odd print false next to it.
Sample:
1 false
2 true
3 false
…
100 true
feladat 22:
Given a number. Write a program to print all numbers from 1 to 100, which are divisible by that
number.
Sample input: 5
Output:
5
10
15
…
100
feladat 21:
Write a program to calculate the sum of two integers and return true if the sum is equal to a third
integer.
Sample:
Input the first number : 5
Input the second number: 10
Input the third number : 15
The result is: true
feladat 19:
Write a program that makes the lazy person feel guilty. 🫣 Means:
Given an hour as an input.
Calculate how many seconds of the day have passed.
Sample:
input the hour of the day: 17,5 (which means 17:30)
Output: “63000 seconds of the day have passed. It's about time to do something…”
feladat 20:
Write a program to print the first 100 prime numbers. This task doesn't make sense, but it's good to
show off with it.
How many times have you heard that “Hey, can you list the first 100 prime numbers?”
You don't have to be embarrassed in front of your friends anymore!
// Utolsó feladat
→ befejezni rendesen!
feladat 28:
Diák adatos program:
- kor/nem/index alapján keresés (pl. ha több diák is van adott néven)
- bizonyos érdemjegy és a felettiek
- mindig minden adatot kiírni
- hely alapján keresés
- student count átírás
Feladat: Fibonacci (Könyv az abakuszról)
Hány pár nyúl lesz n hónap múlva, ha feltételezzük hogy:
* Az első hónapban csak egyetlen újszülött nyúlpár van;
* Az újszülött nyúlpárok két hónap alatt válnak termékennyé;
* mindent termékeny nyúlpár minden hónapban egy újabb párt szül;
* És a nyulak örökké élnek?
N értékét a felhasználótól kérjük be.
feladat 29:
Given a string, print a string made of the chars at indexes 0,1, 4,5, 8,9 ... (it continues on)
So "kittens" yields "kien".
feladat 30:
Print the sum of the numbers in a given array (print 0 for an empty array).
Except the number 13 is very unlucky, so it does not count and neither does the number that comes
immediately after a 13.
Input format:
* The first line contains a single integer, denoting the size of the array.
* Each of the subsequent n lines contains a single integer denoting the value of the array at the
next position.
feladat 31:
Count the number of \"xx\" in the given string. Overlapping is allowed, so \"xxx\" contains 2 \"xx\"
Input format:
* One line of input containing a String
countXX("abcxx") → 1
countXX("xxx") → 2
countXX("xxxx") → 3
feladat 32:
Step through my green glass door.
You can take the moon, but not the sun.
You can take your slippers, but not your sandals.
You can go through yelling, but not shouting.
You can't run through fast, but you can run with speed.
You can take a sheet, but not your blanket.
You can wear your glasses, but not your contacts.
Have you figured it out? Good! Then write a program that can figure it out as well.
Input format:
* One line of input containing a String.
Input:
“moon” → true
“sun” → false
“slippers” → true
“sandals” → false
“yelling” → true
… so goes on
decimal-binary
first 100 prime numb
diákos program (midnig minden adat, hely alapján keresés)
public class Main {
static final int studentCount = 5;
static String[] name = new String[studentCount];
static String[] sex = new String[studentCount];
static int[] age = new int[studentCount];
static int[][] grade = new int[studentCount][3];
Feladat: Osztályok!
1. Készíts új projektet (file -> new -> project), nevezd el ‘Homework’-nek
2. A Main osztály main metódusa legyen üres
3. Készíts új osztályokat: ProblemGenerator, Plumber, Electrician, Carpenter
4. Az osztályokat az alábbi leírások szerint készítsd el:
ProblemGenerator:
1. Osztályváltozója: String problem;
2. Metódusai: newProblem()
newProblem():
// Generates random integers 0 to 4
int randomNum = random.nextInt(5);
ha randomNum = 0 akkor problem = “Csőtörés”
ha randomNum = 1 akkor problem = “Rövidzárlat”
ha randomNum = 2 akkor problem = “Dugulás”
ha randomNum = 3 akkor problem = “Sérült bútor”
ha randomNum = 4 akkor problem = “Áramkimaradás”
ha randomNum = 5 akkor problem = “Bútor cseréje”
Plumber:
1. Osztály szintű változója: String name, int age
2. Metódusai: pipeFix(), problemSolving(), antiClogging()
3. Példányosításnál kötelező legyen értéket adni name és age változóinak
4. Példányosításnál írja ki: name vagyok, age éves vízvezeték szerelő, miben segíthetek?
pipeFix():
● írja ki: name vagyok, age éves, a csőtörést megjavítottam.
antiClogging():
● írja ki: name vagyok, age éves, a dugulást elhárítottam.
problemSolving():
● egy bemeneti paramétere van: String problem
● ha problem = “Csőtörés” akkor pipeFix()
● ha problem = “Dugulás” akkor antiClogging()
Electrician:
5. Osztály szintű változója: String name, int age
6. Metódusai: shortCircuitFix(), problemSolving(), blackoutFix()
7. Példányosításnál kötelező legyen értéket adni name és age változóinak
8. Példányosításnál írja ki: name vagyok, age éves villanyszerelő, miben segíthetek?
shortCircuitFix():
● írja ki: name vagyok, age éves, a rövidzárlatot megjavítottam.
blackoutFix():
● írja ki: name vagyok, age éves, az áramkimaradást elhárítottam.
problemSolving():
● egy bemeneti paramétere van: String problem
● ha problem = “Áramkimaradás” akkor blackoutFix()
● ha problem = “Rövidzárlat” akkor shortCircuitFix()
Carpenter:
9. Osztály szintű változója: String name, int age
10. Metódusai: furnitureFix(), problemSolving(), furnitureCreate()
11. Példányosításnál kötelező legyen értéket adni name és age változóinak
12. Példányosításnál írja ki: name vagyok, age éves bútoros, miben segíthetek?
furnitureFix():
● írja ki: name vagyok, age éves, a bútort megjavítottam.
furnitureCreate():
● írja ki: name vagyok, age éves, az új bútort elkészítettem.
problemSolving():
● egy bemeneti paramétere van: String problem
● ha problem = “Bútor cseréje” akkor furnitureCreate()
● ha problem = “Sérült bútor” akkor furnitureFix()
5. A main osztály main metódusában példányosít egyet-egyet mindegyik osztályodból.
6. Hívd meg newProblem() metódust.
7. Ha a problemGenerator.problem vízvezetékszerelőt kíván, add a problémát neki.
Ha a problem megoldásához asztalos kell, add neki.
Ha pedig vízvezetékszerelőre van szükség, akkor add át neki a problemet.