Пользователь Brian
Brian
Level 41

Old Level 07

Published in the Java Developer group
6411 members

Information technologies

Old Level 07 - 1Computer revolution, which began at the end of 20-th century resulted in creation of the internet (the web) in 90-s. And that was a beginning of even greater revolution. The effect of Internet creation is like industrialization. But the yummiest is that this process has just started.

New world

Site quantity is over 200 million. 3 billion internet users out there. Internet-auctions, web-sites, online shops, internet-services. Information Technology industry grows 20%-30% annually. It is monstrous rates. And it keeps growing. For the last 10 years in Silicon Valley (the main center of high-tech industry of the world), each 2 month a company is created, which afterwards costs the billions of dollars. Not to mention about such internet stars as Facebook ($220 billion), Amazon ($140 billion) and Google ($350 billion). All these companies would not appear, if there was no Internet. All of it results in high demand on IT-specialists. World high-tech industry requires: software developers, designers, QA testers, architects, managers, system administrators and other specialists.

It’s good to be an IT specialist

Old Level 07 - 2If you are an IT-specialist, it’s a golden time for you. You can work for a big company and live in a small town or work for foreign company. You may earn twice less than in EU, for example, but you’ll get 3-10 times more then on local labor market. The smaller the town the greater the difference. You’ll receive valuable experience, good money and bright future. Sometimes you’ll go on business trips to your employer office. And if you like it a lot, you may relocate there. A rising tide lifts all boats. Why would not be such boat that is at the right time and in the right place? This is also an art.

You have reached a new level

Level 7

Old Level 07 - 3

1 Elly, Arrays

- Hey, Amigo! - What’s up, Elly? - Today I’ll tell you about a new interesting entity - arrays. An array is a special data type that can store more than one value. Old Level 07 - 4- I’ll start with analogy. Let’s compare a usual house and a high-rise one. A house is occupied by a single family, whereas a high-rise building is divided into apartments. You need to specify the unique address to write a letter to a family that lives in the house. For a family that lives in an apartment, you must specify the apartment number in addition. - Yeah, that’s clear. - So, an array variable is like a high-rise building. You can store several values in it. Such a variable has some apartments (cells). Each of the apartments can be accessed by its number (index). To do this, after the variable name in square brackets you must specify the index of the cell accessed. It’s pretty simple. - I hope so. - An array variable (a high-rise building) may be of any type, so you just have to write TypeName[] variable_name instead of TypeName variable_name. - Here are examples what you can do with arrays: Old Level 07 - 5- How exciting! - An array variable needs additional initialization. - Why so? - An usual variable is ready for use just after its declaration. As for arrays, it’s a bit more complicated: - First, you need to create a container of N elements, and then you can place values into it. Old Level 07 - 6- Yep. It becomes clearer now. - The basics about arrays:
  1. The array consists of multiple cells.
  2. Every cell is accessed by indicating its number.
  3. All the cells are of the same type.
  4. In an array of n elements, the cells have indexes 0,1,2,...,n-1. There is no cell with index n in the array.
  5. The initial value for all the cells is null, for primitive types – 0, 0.0 for fractional ones, false - for boolean type, exactly the same as in simple uninitialized variables.
  6. String[] list is just a declaration of a variable. First you need to create an array (container) and put it in the variable, and then use it. See the example below.
  7. When you create an object array (container), it’s necessary to specify its length (how many cells it will have). To do this you need to write a command: new TypeName[n];
    TypeName is the type of values which will be stored in cells of the array.
Old Level 07 - 7- Examples: Old Level 07 - 8

2 Risha, Arrangement of arrays in memory

- Hey, Amigo! Let me show you how it all works in the memory: Old Level 07 - 9- And what is this 345? - Actually, I plucked it out of the air, but normally, it’s the address of the string containing the word «Stop». - As for arrays, it’s a bit more complicated:
Old Level 07 - 10
- Are 155,166,177 also numbers pulled out of a hat that represent the address of strings on the right? - Yep. It’s a good thing that you guessed it. Note that, in the second picture, there’s an additional object - an array of 10 cells. - Everything’s clear, thanks. A picture is worth a thousand words. Thank you, Risha.

3 Risha, Examples of arrays Array

- I want to give you a few examples of interesting things you can do with arrays: Example 1. Old Level 07 - 11Example 2. Old Level 07 - 12Example 3. Old Level 07 - 13Example 4. Old Level 07 - 14Example 5. Old Level 07 - 15

4 Diego, Tasks for creation and use of arrays

- Hey, Amigo! Here’s a couple of tasks for you, buddy:
Tasks
1 1. Maximum of an array of 20 numbers
1. In the method initializeArray():
1.1. Create an array of 20 numbers
1.2. Read from the keyboard 20 numbers and fill the array with them
2. The method max(int[] array) should find the maximum number of the elements of the array

2

2. Array of strings in reverse order
1. Create an array of 10 strings.
2. Read from the keyboard 8 strings and store them in the array.
3. Display to the screen the contents of the entire array (10 items) in reverse order. Each item should be on a new line.

3 3. 2 arrays
1. Create one array of 10 strings.
2. Create another array of 10 numbers.
3. Read from the keyboard 10 strings and fill the string array with them.
4. Write the length of a string from each cell of the string array in the cell of numbers array with the same index. Display to the screen the contents of the array of numbers. Each value should be on a new line.
4 4. Array of numbers in reverse order
1. Create an array of 10 numbers.
2. Read from the keyboard 10 numbers and store them in the array.
3. Arrange array elements in reverse order.
4. Display to the screen the result. Each value should be on a new line.
5 5. One large and two small arrays
1. Create an array of 20 numbers.
2. Read from keyboard 20 numbers and fill the array with them.
3. Create two arrays of 10 numbers each.
4. Copy numbers of the large array to two small arrays: the half of the numbers to the first array, the other half to the second one.
5. Display to the screen the second small array. Each value should be on a new line.

5 Elly, ArrayList vs. Array

- It’s me. - Hi, Elly! - Today we have a new exciting topic! Now I’ll tell you about an interesting new class, the ArrayList. - Oh, a new class? Wow! What does it do? - Let me begin with a little back story. The programmers didn’t like one property of an array: it is impossible to change its size. What to do if you want to store three entries more in the array, but there’s only one free space? - The only solution to the lack-of-space problem was to create a very large array that contains all the elements. But this often led to inefficient usage of memory. For example, if in 99% of array lifetime you needed to store only 3 elements in array, and just in 1% - 100 elements, you would have to create array of 100 elements. - So what did the programmers come up with? - They wrote the class ArrayList that did the same job as the Array, but could vary its size. - An interesting move. And how did they do that? - Every ArrayList object stores an ordinary array of elements inside. When you read elements from the ArrayList, it reads them from its internal array. When you write the elements, it writes them to the internal array. Compare: Old Level 07 - 16- So what is the advantage of the ArrayList? As for me, the code became longer. - First, ArrayList supports some additional features not available in array, which programmers use very often. For example, insert elements into and delete them from the middle of the array without leaving holes. - Second, ArrayList can change its size. When ArrayList needs to store one more element in its internal array, and there’s no free space, the following happens inside the ArrayList:
  1. another one array is created, twice as large.
  2. all the elements of the old array are copied to the new array.
  3. a new array is stored in an internal variable of the ArrayList object, the old array is declared garbage (we simply don’t store its reference any more).
Old Level 07 - 17And how to work with the ArrayList? - Actually, in the same manner as with an array. Look here. Let’s compare work with the ArrayList and work with an array. As an example, let’s solve the following task: «Program should read from keyboard 10 string and display them to the screen in reverse order». - Check this: Old Level 07 - 18- I painted equivalent actions in both columns with one color. - It seems different, but if you get a closer look everything is the same. - Yep. Now we have no square brackets when using ArrayList. Instead, we use the methods get, set and add. - I’ve noticed it. But it’s still very similar.

6 Diego, ArrayList tasks

- Are you doing nothing again? You’re a robot, aren’t you? Robots are always busy with something. Here are some tasks to keep you up. But let’s begin with a couple of tips. - Tip 1: A list most commonly means an ArrayList. - Tip 2: A string means the type String. - Tip 3: To create a list of strings most commonly means ArrayList<String> list = new ArrayList<String>();
Tasks
1 Task 1. 5 different strings in a list
1. Create a list of strings.
2. Add 5 different strings to the list.
3. Display to the screen its size.
4. Display to the screen contents of the list. Each value should be on a new line. Use a loop.
2 Task 2. 5 lines: «101», «102», «103», «104», «105»
1. Create a list of strings.
2. Add 5 strings to the list. «101», «102», «103», «104», «105».
3. Delete the first one, the middle one and the last one.
4. Display to the screen contents of the list. Each value should be on a new line. Use a loop.
5. Display to the screen its size. (After deleting one entry, the indexes of other entries change. For instance, if we delete first element, the second becomes the first one).
3 Task 3. 5 lines in reverse order
1. Create a list of strings.
2. Read 5 strings from keyboard, then add them to the list.
3. Arrange them in reverse order.
4. Display to the screen contents of the list. Each value should be on a new line. Use a loop.
4

Task 4. Add 5 strings to the beginning of the list
1. Create a list of strings.
2. Read from the keyboard 5 strings. Add these strings to the beginning of the list, not to the end.
3. Display to the screen contents of the list. Each value should be on a new line. Use a loop.

5 Task 5. Delete the last string and add it to the beginning of the list
1. Create a list of strings.
2. Read from keyboard 5 strings. Add these strings to the list.
3. Delete the last string and add it to the beginning of the list. Repeat this action 13 times.
4. Display to the screen contents of the list. Each value should be on a new line. Use a loop.

7 Risha, Generics

- Now, another awesome topic. - This day is full of surprises. It’s like a real birthday. - Today I’ll tell you what Generics are. Generics are types that have parameters. In Java, container classes enable you to specify types of their internal objects. - When we declare a generic variable, we specify two types instead of one: the type of the variable and the type of data stored in the variable. A good example of this is ArrayList. When we create a new object/variable of ArrayList type, it’s also good to specify type of values to be stored inside the list. Old Level 07 - 19- It sounds exciting. I especially loved what you said about any type. - It just seems to be good. In fact, if in one method strings are placed into an ArrayList, and in the other method you work with its contents and expect that there will be only numbers, the program will crash. - I see. - So far, we are not going to create our own classes with type-parameters, we’ll learn to use classes written by Java creators. - May I use any class as a type-parameter, even the one written by myself? - Yes, any type, except primitive ones. All class parameters have to be inherited from the class Object. - So I can’t write ArrayList<int>, can I? - You can’t. But Java developers have written for primitive types their non-primitive analogues - classes inherited from Object. Here’s how it looks like: Old Level 07 - 20- Primitive types and analogue classes (wrapper classes) can easily be assigned to each other: Old Level 07 - 21- Fine. Then I guess I’ll use the ArrayList more often.

8 Risha, Examples of ArrayList, Generics

Old Level 07 - 22- Now I’ll give you really useful things. Here are some handy examples to work with ArrayList and Generics: - Example 1. Old Level 07 - 23- Example 2. Old Level 07 - 24- Example 3. Old Level 07 - 25- Example 4. Old Level 07 - 26- Example 5. Old Level 07 - 27- Awesome! So, Diego is going to give me a bunch of similar tasks now, isn’t he? - Yep!

9 Diego, List<T> tasks

- Finally you are free. I’m tired of keeping in mind these tasks for you. Here’s a couple more to keep you up:
Additional tasks to do in Intellij Idea
1 1. Three arrays
1. Read from the keyboard 20 numbers, save them in a list, then sort them into three other lists:
List 1 stores numbers divisible by 3 (x%3==0)
List 2 stores numbers divisible by 2 (x%2==0)
List 3 stores the rest of numbers.
The numbers divisible by 3 and 2 at the same time (for example 6) belong to both lists – list 1 and list 2.
2. The method printList() should display each element of the list on a new line.
3. Use the method printList() to display to the screen these three lists. First the list 1 should be displayed, then list 2, after that list 3.
2 2. 5 words in reverse order
Read from the keyboard 5 words. Add them into a string list and display them to the screen in reverse order.
3 3. The word «here»
1. Create a list of the words «stop», «look», «listen».
2. Add a string containing «here» after each word.
3. Display the result. Each element of list should be on a new line. Use “for” loop.
4 4. Letters «r» and «l»
1. Create a list of words/strings, fill it with whatever you want.
2. The method fix() should:
2.1. delete all the words containing letter «r» from the list of strings
2.2. double all the words containing letter «l».
2.3. leave the word unchanged if it contains both letters «r» and «l».
2.4. don’t do anything with other words.

Example:
rose
willow
lyre
oak

Output data:
willow
willow
lyre
oak
5 5. Duplicate the words
1. Read from the keyboard 10 words/strings, add them into a string list.
2. The method doubleValues should duplicate words according to the pattern:
a,b,c → a,a,b,b,c,c.
3. Display the result. Each element of list should be on a new line. Use “for” loop.

10 Professor

Old Level 07 - 28- I found my wonderful lectures! So today you’ll gain the most valuable knowledge in arrays and lists. Here they are: Tips about ArrayList in Java

11 Julio

- Hey, Amigo! I’m glad you’ve coped with all this - you deserve a reward:

12 Captain Squirrels

- Hello, soldier! - Good morning, sir! - I have some awesome news for you. Here’s a quick check to reinforce your skills. Do it every day, and you’ll enhance your skills real quick. Tasks are specially designed to do in Intellij IDEA.
Additional tasks to do in Intellij Idea
1 1. Display numbers in reverse order
Read from the keyboard 10 numbers and fill the list with them.
Display to the screen the numbers in reverse order. Use the loop.
2 2. Move first M lines to the end of the list
Read from the keyboard 2 numbers: N and M.
Read from keyboard N strings and fill a list with them.
Move first M lines to the end of the list
Display to the screen the list. Each value should be on a new line.
3 3. Largest and smallest number in an array
Create an array of 20 numbers.
Fill it with numbers that are read from keyboard.
Find the largest and smallest number in an array.
Display to the screen the largest and smallest numbers separated by a space.
4 4. Read strings from the keyboard until user enters the string “end”
Create a list of strings.
Read from the keyboard strings (whatever you want), add them to the list.
Read strings from the keyboard until user enters the string “end”. “end” should be omitted.
Display to the screen the strings. Each string should be on a new line.
5 5. Bang!
Write a program that counts down from 30 to 0, and displays «Bang!» at the end. The program should reduce the number 10 times per second. Use the function below to insert a delay:
Thread.sleep(100); //delay of one tenth of a second.
Example:
30
29

1
0
Bang!
6 6. Family
Create a class Human with the fields: String name, boolean sex, int age, Human father, Human mother. Create 9 objects of Human class and fill them in such a way as to obtain two grandfathers, two grandmothers, a father, a mother, and three children. Display to the screen the objects to the screen.
Tip:
If you write your method String toString() in the class Human, then it’ll be used to display an object.
Example output:
Name: Anna, sex: female, age: 21, father: Paul, mother: Kate
Name: Kate, sex: female, age: 55
Name: Ian, sex: male, age: 2, father: Michael, mother: Anna
7 7. Move one modifier static
Move one static modifier so the program compile.
- Those tasks were for greens. I added bonus tasks of higher complexity. For top guns only.
Bonus tasks
1 1. Program doesn’t compile and run. Fix it.
Task: Read from the keyboard the cat data and display it to the screen.
Example:
Cat name is Jinx, age is 6, weight is 5, tail = 22
Cat name is Maisy, age is 8, weight is 7, tail = 20
2 2. Add new functionality to the program.
Old Task: The program reads strings until the user enters a blank line by pressing enter. Then it converts lower-case into upper-case (Mom turns into MOM) and displays them on the screen.
New task: The program should read strings until the user enters a blank line by pressing enter. The program then builds a new list. If the string has an even number of characters, the string is duplicated, if odd number, the string is tripled.
Example input:
Cat
Cats
Me
Example output:
Cat Cat Cat
Cats Cats
Me Me
3 3. Learning and practicing algorithm.
Read from the keyboard 20 numbers and display them in descending order.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULLL VERSION