Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Bootcamp - Object-Oriented Programming and Data Structures | CS 2110, Study notes of Computer Science

Material Type: Notes; Class: Object-Oriented Programming and Data Structures; Subject: Computer Science; University: Cornell University; Term: Summer 2008;

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-5t0
koofers-user-5t0 🇺🇸

10 documents

1 / 76

Toggle sidebar

Related documents


Partial preview of the text

Download Java Bootcamp - Object-Oriented Programming and Data Structures | CS 2110 and more Study notes Computer Science in PDF only on Docsity! Java Bootcamp David I. Schwartz COMS/ENGRD 211 Read Step 1 first on Page 3! It will explain what you need to do!Page 1 Table of Contents Step 0 Understand the notation in this tutorial. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Step 1 Figure out how to do this tutorial.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Step 2 How to find Java at Cornell. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Step 3 What is a Java application?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Step 4 Java Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Step 5 Java Character Set. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Step 6 Java Comments and Whitespace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Step 7 Java Tokens. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Step 8 Java Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Step 9 Empty and Block Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Step 10 Declaration and Assignment Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Step 11 Introduction to Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Step 12 Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Step 13 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Step 14 Building A Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Step 15 Creating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Step 16 Storing and Accessing Objects (References) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Step 17 Special reference–null . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Step 18 Special method–toString. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Step 19 Accessing an object’s members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Step 20 What is static? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Step 21 Aliases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Step 22 Methods and objects (and aliases) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Step 23 Using Java’s this . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Step 24 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Step 25 Class Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Step 26 Java API and import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Step 27 Constants: Static Import and Enumerations (enum) . . . . . . . . . . . . . . . . . . . . . . . . . 69 Step 28 Wrapper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Step 29 Autoboxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Step 30 Vectors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Step 31 User I/O. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Step 32 Other things for the future…?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76Page 2 Java Bootcamp Step 1 Page 5/761.5 Problem 1 Where are the solutions posted for the tutorial? 1.6 Problem 2 What is autoboxing? Check out the Java 5 summary or release notes to find out. Java Bootcamp Step 2 Page 6/76Step 2: How to find Java at Cornell. 2.1 Background Cornell has public labs (CIT), Engineering labs (ACCEL), and departmental labs, most of which you won’t be able to access until you affiliate with a particular department. Review the links on the course website: http://www.cs.cornell.edu/courses/cs211 (you don’t need the current year). See Software under the Course Info link. Most likely you will work in a CIT lab if you don’t use your own computer. To access course-specific software, click on Windows’s Start and select All Programs → Class Files. 2.2 Java Environments Use any type of Java development environment that you wish. If you think you are using something completely off-the-wall, check your code with something more standard. 2.3 Examples and Help Review the CS211 website (see above). In CS211, we require that students understand how to compile and run their programs from the command line and, thus, use directly use the JDK (Java Development Kit). You can find help on these environments by following the links on the website. 2.4 Problem 1 Compile and run the following code (see class software in a public lab): public class Step2 { public static void main(String[] args) { System.out.println("Hello, world!"); } } 2.5 Problem 2 In the above example, replace "Hello, world!" with args[0]. Now use the command line in a DOS window to compile and run your code. If you have never done this kind of programming, skim applications.html, which is posted along with this tutorial (“Companion Document”). Java’s on-line tutorial also has a brief explanation of using command-line arguments. To test your program, enter the following command at the DOS prompt: > java Step2 Buh! Java Bootcamp Step 3 Page 7/76Step 3: What is a Java application? 3.1 Long Answer Refer to Applications.html, which Step 2 mentions. 3.2 The Gist • Except for import statements, all Java code must reside inside a class. • Unless you want your program to run within a webpage (an applet), you need to write an application. A Java application has a main method that starts the program. • Every class can have a main method. • You need to “tell” Java which main method you wish to run when executing a program. • When compiling from the command-line, each file must include only one public class. If you prefer to place multiple classes in one file, do not modify the classes with the word public–only the class containing method main should have public as part of the declaration. 3.3 Problem 1 Write a Java application that contains the following code in one file called MyProgram.java: public class MyProgram { public static void main(String[] args) { System.out.println( new Person(args[0],args[1]) ); } } class Person { private String first; private String last; public Person(String f, String l) { first = f; last = l; } public String toString() { return first + " " + last; } } From the command-line, compile and run the program with arguments Dimmu and Borgir. Java Bootcamp Step 6 Page 10/76Step 6: Java Comments and Whitespace 6.1 Java Comments • Single line comments: // stuff /* stuff */ • Multiline comments: /* stuff */ • You can nest comments. 6.2 Java Whitespace • Spacebar, return, new line, tab. • You can separate tokens and statements with as much whitespace as you want. • Do not split a token! 6.3 Problem 1 Correct the following program, which should print hello: public Class Test { public static void ma in (String[] Args) { // hello /* hello System.out.println(); } } Java Bootcamp Step 7 Page 11/76Step 7: Java Tokens 7.1 Punctuation • Statements end with semicolon! (;). • Classes, methods, constructors, statement blocks, inner classes, initialization blocks, and initializer lists use braces ({ }). Inner classes and initialization blocks aren’t covered in our CS100, so don’t worry about them for now. • Methods and expressions use parentheses (( )). • Arrays use square brackets ([ ]). • Parameter lists and variable declarations use commas (,). • String literals (strings created w/o using a constructor) use double quotes (" "). • Character values use single quotes (' '). • Escape characters use a backslash (\). • What about other symbols, like + and .? Those are operators–discussed soon! 7.2 Problem 1 The following program should print integers from 0 to 10. Unfortunately, we forgot to apply the punctuation! Please fix it. public class Punctuation public static void main String args int count = 0 final int STOP = 10 while count <= STOP System.out.println count count++ 7.3 Reserved Words Reserved words are tokens that are reserved for the language and cannot be used to represent any value. See textbook for comprehensive list: • values: true false null this • types: boolean int double char void • control: if else for do while return switch • modifiers: public private static final protected • classes: new class extends implements super interface enum • amusing: goto Java Bootcamp Step 7 Page 12/767.4 Values Also thought of constants or literals: tokens whose representation means exactly the values expressed in their name. Java has primitive (non-object) types and reference (object) types. Numbers: • integers. We focus on int: -2147483648 ↔ 2147483647 See also long. • doubles: decimal point: 0.1, .1, 1., 1.0 scientific notation: 1e-6, 1.23E2 Boolean: • false and true • no 0 and 1 to represent truth! Characters: • Unicode: '\uxxxx' • ASCII: use decimal 0–127. • Literal representation: single quote: 'a', '1' • No empty character ('')! • Escape characters: \n (new line), \t (tab), and others References: • Java has reference values to point to objects, but you cannot directly access those values. • Special reference value you can use–“no object”: null • You’ll get much more into references later in the tutorial. 7.5 Problem 1 If you mix characters and numerical values in an expression, Java promotes the expression to a value. Since double > int > char, you can actually mix all three together. System.out.println(' ' * 2.0); // what does this output? why? System.out.println(true + 29); // what does this output? why? System.out.println("Hi\nthere!") // what does this output? System.out.println((char)97); // (type) is cast operator; output? Why does the expression (String) 9 cause Java to vomit? How do you create a string containing a primitive value? Hint: Use +. Actually, see next section. Java Bootcamp Step 7 Page 15/767.8 Identifiers As long as you don’t use reserved words, you can name elements of code: • variables • methods • classes Java names: • Name contain alphanumeric characters, underscore, and currency symbols. • Must not start with number! • Case sensitive! Variables: • Store and refer to a value. • Can be in method (local variable), method header (formal parameter), or field (class variable or instance variable–see static later). • Must have declared type–Java is strongly typed! Discussed more in statements section. For now, type name to declare. Methods: • See above for naming rules. • Convention: start with lowercase character. Classes: • See above for naming rules. • Convention: start with uppercase letter. Scope: • How and when you can access variables, methods, and classes requires discussion of Java statements and OOP. • This issue will appear all over the tutorial. The discussion of scope has to evolve because there is simply too much Java to learn all at once. 7.9 Problem 1 Is the following code legal? If so, what does it do? Should you use names such as these? public class CLASS { public static void main(String[] Sgra) { int INT = 10; for (int Int=0;Int<INT;Int++) System.out.println(Int); } } Java Bootcamp Step 7 Page 16/767.10 Operators Review: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/opsummary.html Basics: • Arithmetic: +, -, *, /, % (mod), - (negation, subtraction) • Logic: & (and), && (short-circuit and), | (or), || (short-circuit or), ! (not), ^ (xor) • Comparison: ==, !=, <, <=, >, >= • Conditional: ?: • Assignment: =, +=, *=, -=, … • Increment: --, ++ • Cast: (type) • Member access: . (no pointer, dereferencing) • Array access: [index] • String concatenation: + • Object type comparison: instanceof Power? • Java does not use **; ^ means XOR (exclusive OR, not a power!). • For , use Math.pow(x,y). Precedence: • See http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html • Handy to remember that precedence of [] and . are highest. • Use parentheses ( ( ) ) to control the order of operation in an expression. Associativity: • Two main rules people run into: - Arithmetic works left to right. - Assignment works right to left. • Example: System.out.println("answer: "+1+2); // outputs answer: 12 x = y = 3; // • Use parentheses ( ( ) ) to control the order of operation in an expression. Promotion (revised): • Number types: double > int > char. • Cannot mix boolean with numbers. • Everything promotes to string with +. xy Java Bootcamp Step 7 Page 17/76Arithmetic: • Consequence of ints promoting to double? • In mixed operations, result becomes double. • Integer division results in integer result! Not rounded–result is floored! Casting: • (type) expr • arrays and inheritance: cast has lower precedence! So, use ((type) expr) Conditional operator: • expr ? expr1 : expr2 • Returns either expr1 or expr2 • Example: int x = 1; int y = 2; System.out.println( x==y? 'a' : 'b'); Increment operators (some examples): • prefix example: int x = 1; int y = ++x; System.out.println(x); // outputs 2 System.out.println(y); // outputs 2 • postfix example: int a = 1; int b = a++; System.out.println(a); // outputs 2 System.out.println(b); // outputs 1 • bonkers example (way beyond scope of CS211) int x = 1; x = x++; System.out.println(x); // might not be what you think :-) 7.11 Problem 2 Write a program that checks if 13 is even or odd. Why does System.out.println(3/4) output zero? Write a program that generates a random integer between 1 and 100, inclusive. Hint: Math.random() returns a double in the interval [0, 1). Write a program that converts all of the lowercase letters to uppercase without using an array or strings. Hint: for(int count=start;count<=stop;count++). Java Bootcamp Step 10 Page 20/76Step 10: Declaration and Assignment Statements 10.1 The Rules • all variables have types (strongly typed)! • variables may be declared only once in a block! • all variables must have values before used! • all variables do not have initial values in methods! • when variables declared but not assigned in class (fields), defaults are “zero” some tricky ones: references are null, booleans are false 10.2 Declaration Syntax type name; type name, …, name; 10.3 Assignment Syntax • general: name = expr; • combined syntax: type name = expr; final type name = expr; • more about scope? coming up! 10.4 Problem 1 What is wrong with the following code, besides the fact that I did not comment it? public void something( ) { int x = 1; boolean y; System.out.println(y); System.out.println(x==y); } Java Bootcamp Step 11 Page 21/76Step 11: Introduction to Scope 11.1 Blocks • Classes, methods, control structures, and blocks of statements. • Blocks can enclose other blocks. 11.2 Variables • Variables declared before an enclosed block are seen and used inside the enclosed block. • Changes to the variable are retained while the outer block is still running. { int x = 2; { System.out.println(x); x = 3; } // output is 2 System.out.println(x); } // output is 3 • Variable declared inside an inner block is not visible to the outer block. { int x = 3; { boolean y = true; } double y = 10; } 11.3 Methods • Methods form their own blocks. • So, variables declared inside method are local variables, meaning that their scope is strictly in the method. • Method formal parameters (header) also act as local variables. 11.4 Classes • Variables declared at class level (see braces) are visible to all methods and other variables declared at the class level. These variables are called fields. • Actually, there’s a lot more to discuss: this, private, public, protected, and static. Coming up! 11.5 Why nested blocks? You can reuse the same variable name for loops: void doStuff() { for (int i = 0 ; i < 10 ; i++ ) { /* stuff */ } for (char i = 'a'; i <= 'z'; i++ ) { /* stuff */ } } Note that reusing a variable with a different type is usually bad style. Java Bootcamp Step 11 Page 22/7611.6 Problem 1 What does the following code output? public class Scope { public static void main(String[] args) { int x = 1; System.out.println("S1 for x: "+x); { System.out.println("S2 for x (before changing): "+x); x = x + 1; System.out.println("S2 for x (after changing): "+x); int y = 3; { System.out.println("S3: "+(x + y)); } } String y = "yes, this is legal"; System.out.println("S1: "+y); } } 11.7 Problem 2 What does the following program output? Note that when creating an object (new Data()), Java first assigns the fields (x, y), then performs the constructor (Data()). Hint: The fields are in a block, which means the rules above still apply! class Data { int x; int y = x; Data() { System.out.println(x); x = 1; System.out.println(y); } } public class Test { public static void main(String[] args) { new Data(); } } Java Bootcamp Step 12 Page 25/7612.7 Problems Write a program that generates a random integer between 0 and 10, inclusive. Report whether the number is even or odd. Write a program that performs a logical and operation without actually using the and operators. So, assign two boolean variables and then test then using if statements. Suppose that a variable test might be assigned to one of these characters: 'a', 'b', 'c', or 'd'. Use a switch statement to do the following: • If test is 'a', the program tells the user "You did great!". • If test is 'b', the program tells the user "You did well!". • If test is 'c', the program tells the user "You passed!". • If test is 'd', the program tells the user "Well, you didn’t fail!". • Otherwise, the program tells the user "Mission Control, we’ve got problems". 12.8 Repetition Statements There are three general structures: while, do-while, for. while syntax: while (conditionalexpression) block do-while syntax: do block while(conditionalexpression); for-statement: for ( initializations ; checks ; increment_decrements ) block for-each statement–will show later in arrays (Step 24) and Vectors (Step 30): for ( object : collection ) block for ( int : array ) block Java Bootcamp Step 12 Page 26/7612.9 Repetition Examples int i = 0; while (i < 5) { System.out.println(i); i++; } for ( int i = 0; i < 5 ; i++ ) System.out.println(i); for ( int i = 10; i < 100 ; i=i+10 ) System.out.println(i); 12.10 Problem 1 Write a program that prints the following grid of characters without using an array: a n b o . . . . . . m z Write the program with for and then with while. 12.11 Blocks in Control Flow and Scope Reminders Note how I use block for the body of each statement. The block can be empty, one statement, or multiple statements: • The empty case can be an empty statement or empty block. • The single statement does not require braces. • The multiple statements do require braces. Because the bodies of the control statements are indeed blocks, you can indeed declare variables inside those blocks. • Since those blocks are inner blocks, those declared variables must be visible only in those inner blocks. • For the for statement, the initializations can also be newly declared variables. • For repetition statements, each time the body repeats, the variables declared inside the body are simply re-declared without any problem. If you intend for a variable to be seen from outside the control structure, you should declare that variable before the control statement. 12.12 Problem 2 int j = 0; for ( int i = 0 ; i + j < 10 ; i++, j++ ) ; System.out.println( ___ ) ; // can you print the value of i? j? Java Bootcamp Step 13 Page 27/76Step 13: Methods 13.1 Where do they go? • Inside a class. There are no “stand-alone” methods. • You can write methods in any order in a class without having to use “function prototypes.” • Each class can have one main method. When running a program, you indicate which class’s main method you wish to use. Refer to the beginning of the tutorial for more discussion. 13.2 Method Syntax Syntax: modifiers returntype name(params) stuff block modifiers: • privacy: private, public, protected, package (see OOP) • static or non-static returntype: • must be valid type: class or primitive • may be void (no return value) name: • must be valid Java identifier • usually start with lowercase letter params: • arguments to the method • must be declared • for methods with no parameters, use just (). For example, int test() {code}. • you may supply a variable amount of parameters with syntax type … var. The ellipsis (…) is indeed part of the syntax! See Step 13.13. stuff: • methods can throw exceptions, which is something you’ll learn in CS211. • there are other “things” you can put here, but we don’t usually discuss them in CS211. Java Bootcamp Step 13 Page 30/7613.8 Return Type and Values Return types: • Java methods must have a return type. • The types may be any valid type (primitive, object). • If you prefer not to return anything, you must give a return type of void. return statements: • Can written anywhere in a method body. • void methods are allowed to have a return statement (with no return expression) anywhere in the method. Use return;. • Java passes the value of the return expression back to the code that made method call. So, the code that makes the method call (e.g., Math.sqrt(4)), gets replaced by the returned value (2). 13.9 Problem 3 What does the following code output? public class Methods2 { public static void main (String[] args) { System.out.println( and(true,false) ); boolean t1 = nand(true,false); boolean t2 = xor(true,false); System.out.println( and(t1,t2) ); } // Return short-circuiting AND of t1,t2: public static boolean and(boolean t1, boolean t2) { return t1 && t2; } // Return exclusive OR of t1,t2: public static boolean xor(boolean t1, boolean t2) { return t1 != t2; } // Return NOT-AND of t1,t2: public static boolean nand(boolean t1, boolean t2) { return !(t1 & t2); } } Java Bootcamp Step 13 Page 31/7613.10 Problem 4 Trace the following code. What does the program output? Which method (search1 or search2) is more reusable (and thus, arguably better style)? public class LinearSearch { public static void main (String[] args) { int[] x = {1, 4, 5, -1}; // shortcut to create 1D array of values search1(x,-1); System.out.println(search2(x,-1)); System.out.println(search2(x,0)); } // linear search for target in data x, return early if found: public static void search1(int[] x, int target) { for (int i = 0; i < x.length; i++) if (x[i] == target) { System.out.println("Success!"); return; } System.out.println("Fail!"); } // linear search for target in data x, // return true if found, else false: public static boolean search2(int[] x, int target) { for (int i = 0; i < x.length; i++) if (x[i] == target) return true; return false; } } 13.11 Method Overloading Method overloading: • Write more than one method with the same name in same class. • Helps with creating too many silly names, like search1, search2, …. • Common example: System.out.println. See PrintStream in API. Rules: • You may change order of arguments, types, number of params and combinations of these changes. • These two changes do not constitute overloading: - changing just the return type - changing just the parameter names Java Bootcamp Step 13 Page 32/7613.12 Problem 5 Write two more myRand methods: • One to generate a random bit. • Another to generate a random character. Judicious use of myRand for integers can help reduce the amount of code you write. public class Overloading { public static void main(String[] args) { System.out.println("int: "+myRand(1,10)); System.out.println("bit: "+myRand()); System.out.println("char: "+myRand('a','z')); } // Return random int, low <= high: public static int myRand(int low, int high) { if (low > high) { System.out.println("myRand Failure!"); System.exit(0); } return (int) (Math.random()*(high-low+1)) + (int) low; } // Generate random bit: // Generate random character: } Java Bootcamp Step 14 Page 35/7614.4 Design By Abstraction Abstraction: • Try to create general structures that hide nitty-gritty details. • Helps a programmer deal with more English-like code, making it easier to program. • Use named constants: - Rather than using specific values inside the body of a block, define constants as fields or local variables that store those values. • Use methods calling other methods: - The “early” methods (first in the stack) can have really high-level sounding names. - The “later” methods (uppermost in the stack) can have very detailed, low-level code. • Use encapsulation (has-a relationships): - If a thing has a something, then that something belongs in the class (a member) - The class encapsulates states and behaviors. - Provides a mechanism to “bury” things away in another class. • Use information hiding: - Determine how an object should communicate with other code. - Make high-level, public methods to communicate with the code. - Make all other class members private. Java rules for privacy modifiers: • private member cannot be accessed by a class different from the class in which the private member is defined. • public members can be accessed by any class. • package (blank) visibility. Acts as public when not using packages. • protected visibility relates to inheritance, which means we’ll deal with it later. Style for privacy: • Make fields private when possible. For inheritance, private fields are irritating, so we generally make them protected. • Utility methods (methods used only by other methods inside a classes) should be private. No need for another class to see how you hacked together a weird solution. • Setters, getters, and other accessors should be public, so that you can use your objects. Static vs. Non-Static Members: • Make a member static if you wish to access a member without creating an object (because that member should influence or be shared by all objects created from a class). • Will discuss more later when you’ve reviewed how to access an object’s members. Java Bootcamp Step 14 Page 36/7614.5 Overall Structure In English: • classes contain members and constructors • constructor is essentially a method that makes an object and returns the object’s location in memory (reference) • members are fields and methods (and other things you’ll learn about in CS211) In code: modifiers class name stuff { fields constructors methods morestuff } 14.6 Problems What does the following program output? How would you create another Person with the name "Harlan Ellison"? How would you modify class Person to include a middle name (don’t bother inheritance for now–CS211 will cover that later)? public class Glance { public static void main(String[] args) { Person p = new Person("Dimmu"); p.setLastName("Borgir"); System.out.println(p); } } class Person { private String firstName; private String lastName; public Person(String fn) { firstName=fn; } public void setLastName(String ln) { lastName=ln; } public String toString() { return firstName+" "+lastName; } } Java Bootcamp Step 15 Page 37/76Step 15: Creating Objects 15.1 Where are we? To write program: • Form the ADTs: You need to determine the composite “things” you need to model, including their properties and actions/behavior. • Implement the ADTs by writing the code for the classes. • Write code that uses the classes: Make objects (specific instances of classes): So, we need a way to make objects. 15.2 Constructor Syntax modifiers Classname(params) block • Constructors resemble methods, but: - Constructors have same as class. (Actually, a method could have the same name as a class, but you end up with strange looking code.) - Constructors have no specified return type: they do in fact return the address of the object that they create. • Constructors are usually public so that another class can make objects. Examples: public Person(String n) { name = n; } Ways to call a constructor: new Person("Shagrath"); Person p; p = new Person("Horgh"); Person p = new Person("Dani"); 15.3 Problem 1 Write a class Complex that represents a complex number ADT. Include an add method. Java Bootcamp Step 16 Page 40/76Step 16: Storing and Accessing Objects (References) 16.1 Where are we? • You can write a class by modeling a thing’s data and actions. • The class provides the code from which you build and use unique objects. • You need to figure ways to build objects with constructors. • A constructor returns a reference to the newly created object. Remaining questions: • What’s a reference? • How to access an object with the reference? 16.2 What’s a reference? Short answer: • Reference (or reference value): the address of an object, which is indeed a value. • References are returned from constructor calls. Example: • Run the following code: public class RefTest { public static void main(String[] args) { System.out.println( new Person() ); } } class Person { } • Java will output a strange looking value, like Person@092abc. What happened? • new Person() creates an object of type Person. - class Person has the empty constructor because you didn’t provide one. • The return value of the constructor call is the address of the Person object that you created. • There is a “secret” method in Java that has a default behavior, which returns the reference value as a string. - The method is toString, which is inherited from class Object. All classes extend Object. - Refer to the API on toString’s default behavior. It’s actually not guaranteed to be the address, but for our JVM, it is. - You will usually override toString because of this weirdness. Java Bootcamp Step 16 Page 41/7616.3 Comparing Primitive and Reference Values Primitive types: • When you say int x; x = 1;, - Java allocates memory to an integer. - Java adds the association of x and its address in a table somewhere else in memory. - For the assignment, Java looks up the address of x and then puts the value of 1 there. • The idea is that all variables have a specific address, allocated memory, and possibly a value. • Refer to the stack from methods. Each method call allocates memory because of the variables inside the methods. Object types: • When you say Person p;, - Java allocates a small amount of space to hold an address value, but not the entire object. In fact, Java has not created an object yet! - The variable p has a location in memory. When you create an object and copy its address to store in p’s location, p then refers to the object. • When you say p = new Person();, - Java creates an object. - Java allocates enough memory to hold the object in another location called the heap. This memory holds instance variables and administrative information. - The constructor returns the address of the object, which is stored in p’s portion of memory. The object is not stored in p! • So, there are two portions of memory: - p, which has it’s own location (local variable, parameter, field), which holds an address of an object. - the object, which resides somewhere else in memory (the heap). • Java does not allow you to change a reference value. - You can still make and change objects, but while an object’s reference is “alive,” you cannot change the value itself. - C/C++ people might be disappointed–what Java is effectively disallowing is pointer arithmetic. Java Bootcamp Step 16 Page 42/7616.4 Picture Suppose you have a method with the following code: int x = 1; Person p = new Person(); Refer to the figure below. Both x and p are local variables that are allocated on the stack. The object created from class Person has space allocated on the heap. 16.5 Problem 1 Review this code and answer the questions that follow: public void m1() { Person p = new Person(); Person q = new Person(); // before q = p; // after } At the point where the code says // before, do p and q refer to the same object? Why or why not? At the point where the code says // after, do p and q refer to the same object? Why or why not? You’ll see me repeat similar code in the upcoming section on aliases. The Stack 1 The Heap x p abc123 abc123 Java Bootcamp Step 19 Page 45/76Step 19: Accessing an object’s members 19.1 Where are we? So far, • You can write a class. - A class has fields, constructors, and methods. - There’s other stuff you will eventually see in CS211. • You can create objects using constructors in a classes. - Constructor returns reference to newly created object. - You can store addresses of newly created objects in reference variables. - You cannot store entire object in a variable! Well, how you access the members of an object? 19.2 Member Access Encapsulation reminder: • has-a relationship. • State (fields): An object might have a name, an age, …. • Behavior (methods): An object might have a way to report its age, might tell you who its parents are, …, So, we need a way to say “object’s something” or “object has-a something.” • Java uses the dot (.) operator to get to the “something.” • Syntax: reference_variable.member constructor_call.member • You can shorten these two to just ref.member. • There’s a 3rd syntax (Classname.member), which requires static. And yet again, I want to delay formal discussion. But it’s coming up really soon! The member must be public (or accessible within the scope) for the dot to work! 19.3 Examples Practice accessing an object’s field. Below, I’m breaking choosing clarity over style: class Person { public int age; } public class TestDot { public static void main(String[] args) { Person p1 = new Person(); System.out.println(p1.age); // outputs 0 System.out.println( new Person().age ); // outputs 0 } } Java Bootcamp Step 19 Page 46/76Accessing a method is very similar because of the ref.member syntax: class Person { private int age; public Person(int a) { age = a; } public int getAge() { return age; } } public class TestDot { public static void main(String[] args) { Person p = new Person(50); System.out.println( p.getAge() ); // outputs 50 } } 19.4 Objects are unique (reminder) Refer to the example, above. If you say , Person p = new Person(10); Person q = new Person(20); the objects the p and q refer are different. There are two entirely different addresses. However, the code used is identical. Why? • Objects are created from a class. • The class provides the blueprint (the code) used to “stamp out” an object. • When calling an object’s members, - Java uses the code from the class to see how to process the member. - But, the specific values Java uses come from the specific object. So, both p and q have getAge methods, but each call to getAge will access different values of the field age: System.out.println(p.getAge()); System.out.println(p.getAge()); If you want to make things more interesting, Person p = new Person(20); Person q = new Person(20); Both objects are still different. You just happen to have p’s age be 20 and q’s age be 20. After all, there certainly many people in the world who happen to have the same age. And they are not all the same person! Java Bootcamp Step 19 Page 47/7619.5 Changing an object’s fields Generally, fields should be private to prevent access, which is a consequence of maintain information hiding. However, you should at least see a small example: class Data { public int x; public int y=20; private int z; } public class TestPublic { public static void main(String[] args) { Data d = new Data(); System.out.println(d.x); d.x = 10; System.out.println(d.x); } } You should get 0 and 10 as output. Why 0 on the first output? Recall the fields always have a default of “zero” unless you initialize their values directly in the class. 19.6 Privacy: A Nitpicking Detail Now and then you’ll see code that looks as follows…I’ve extracted a bit of Complex: private double r, i; public Complex add(Complex c) { return new Complex ( r+c.r , i+c.i ); } How could I say c.r and c.i, though r and i are private? Well, c is type Complex, so as far as Java is concerned, c and the current object are allowed to see each other’s members. Yes, it’s kind of kinky. 19.7 Problem 1 In the above example, try to access and change d’s y and z fields. Follow the same order of output statements: • Why does d.y output as 20 before you change it? • Why does your program not allow you to access d.z? Rewrite Data such that it has no public fields, but you can still access and change its fields. Expand class Person to include a last name, first name, and methods to access those names. Write a program that adds two complex numbers together. Report the output. Java Bootcamp Step 20 Page 50/7620.6 Problem 2 Special geeky problem: why did Java pick the name “static” for static? Why is the main method modified as static? 20.7 Problem 3 What’s wrong with the following code? public class TestMain { public int x; public static void main(String[] args) { System.out.println(x); } } 20.8 Problem 4 Trace the following code. What is special about the Student constructor? (There is an interesting “trick” that I am using.) What is the output? class Student { private String name; private static int count; public static int currentYear; public static final int GRADYEAR = 2005; public Student(String name) { this.name=name; count++; } public static int getCount() { return count; } } public class StaticTest2 { public static void main(String[] args) { System.out.println(Student.GRADYEAR); Student s1 = new Student("Dani"); Student s2 = new Student("Shagrath"); Student.currentYear = 2001; System.out.println(s2.currentYear); System.out.println(Student.getCount()); } } Java Bootcamp Step 21 Page 51/76Step 21: Aliases 21.1 Demonstration What happens when you do run this code? class Person { public int x; public Person(int n) { x=n; } public String toString() { return "#"+x; } } public class TestPerson { public static void main(String[] args) { Person p = new Person(1); Person q = new Person(2); System.out.println(p); // output: #1 System.out.println(q); // output: #2 p = q; System.out.println(p); // output: #2 System.out.println(q); // output: #2 } } Why does Java use q’s toString and not p’s toString after you say p=q? The statement p = q forges an alias between p and q. An alias is another name for the same thing. So, p becomes another name for q. See the next section for an explanation of how the alias is formed. 21.2 Relevant rules There are some interesting consequences of the following rules: • Java variables must obey scope. • Java is strongly typed. • Java allows to copy variable values to other variables. • You store address of object in a variable, not the entire object. Java Bootcamp Step 21 Page 52/76So, to say p=q, • Both variables must be visible to each other, which they are, because they are local variables in the same method block. • Both references must refer to the same type of object. Actually, p must be supertype or same type as q. We’ll be reviewing inheritance later in CS211. • p is the left side of an assignment, which means it will get the value of the expression on the right side of the assignment. • q stores an address value, which is copied into p. Since p gets the value of q, p must now point to the same object that q does! See below: Consequences: • If you say p.x = 10, q.x is now 10, because both variables refer to the same object. • The object that p originally referred to is now toast. Why? - That object was created in the current method and associated only with p. Since p has been reassigned, there is no connection (via a reference) to that original object. - To retain a connection to p’s original object, you would need to insert a statement before p=q, like Person tmp = p. Person object in Heap q p abc123 abc123 The Stack Java Bootcamp Step 22 Page 55/7622.3 “Return an object” (actually, return a reference to an object) Because a constructor creates an object and then returns a reference to it, you can think of the constructor call as an expression. For instance, just as Math.sqrt(4) returns 2, the expression new Person("Shagrath") returns a value, which is a reference. So, instead of saying something like this: public Something method() { Something s = new Something(); return s; } try this: public Something method() { return new Something(); } 22.4 Example In your Complex class, you might have an add method that looks something like this: public Complex add(Complex other) { return new Complex( r + other.r , i + other.i ); } By relying on references and aliases, you can add two Complex numbers as follows: Complex c = new Complex(1,2).add( new Complex(3,4) ); 22.5 Problem 2 If you can demonstrate that the following code outputs the value: 28.0, you’ve pretty much nailed the rules about scope: class blah { public int x1 = 10; public int x2 = 17; public String method1(double x1) { if ( x1 > 0 ) { int x2 = 1; } { boolean x2 = true; } return method2( x1 + x2 ); } public String method2(double x2) { return "the value: " + ( x2 + x1 ); } } public class TestScope { public static void main(String[] args) { System.out.println(new blah().method1(1)); } } Java Bootcamp Step 23 Page 56/76Step 23: Using Java’s this 23.1 “The Current Object” Recall that the code in a class provides a blueprint to create an object, which provides the following design for Java: • When you call a constructor, you create one object at a time. • When you access that object’s members via the dot operator, you access just that object’s members, because each object is unique. These reminders might seem obvious, but there is a subtle concept–the notion of the current object. Refer to this example that I’ve been using: class Complex { private double re, im; // components of current object public Complex(double r, double i) { re = r; im = i; } public Complex add(Complex other) { return new Complex( re+other.re, im+other.im ); } } Note how the add method knows that re and im refer to the current object’s fields? In case you’re not sure who that current object is, I’ll show how the rules about the current object apply. The following code uses Complex from above: public class TestCurrent { public static void main(String[] args) { Complex c1 = new Complex(1,2); Complex c2 = new Complex(3,4); c1.add(c2); // c1 is current, c2 is supplied c2.add(c1); // c2 is current, c1 is supplied } } When I call c1.add(c2), Java works with c1’s members (re, im, add–of course, only add can be accessed from outside the class). The trick from a few sections ago is that the dot resembles as “has-a” or “pointer.” Inside the add method, other becomes an alias for the object that c2 refers to. So, when Java calls other.re, Java thinks of other as the current object, which has its own re. If you’re wondering how I circumvented the privates, look back a few sections on a nitpicking rule (Step 19.6) that explains how objects from the same class can access private members. Normally you really should use a getter method. Java Bootcamp Step 23 Page 57/7623.2 Representing the current object Now and then you actually need to express the current object inside a class. There are three general situations: • You want to call another constructor of the same class as part of another constructor. • You want to distinguish between a field and local variable that have identical names. • You need to assign the current object to a supplied object. Java’s keyword for the current object is this, which I think it the funniest keyword in the history of programming. The Whitespace programming language is really funny, too. 23.3 Constructor Chaining Suppose you don’t want an “empty” Person to be created. You could chain one constructor to another by calling a constructor of the current class, which means calling this(args). For example, below, Person() ends up executing Person(String f, String l), because the arguments of this("John","Doe") match it: class Person { private String first, last; public Person() { this("John","Doe"); } public Person(String f, String l) { first = f; last = l; } } Remember that the first statement of a constructor can be this(args) or super(args). Eventually, the last constructor in the chain of calls in the current class must have just super(args). Java Bootcamp Step 24 Page 60/76Step 24: Arrays 24.1 Java Arrays General principles: • Java arrays are objects. • Java arrays are 1-D. To make a multidimensional array, you need to make an array of arrays. • Java arrays have a special kind of constructor. • Java arrays are strongly typed–all elements must have the same type. • You cannot change the size of an array after you create it. Arrays are not dynamic! 24.2 Creating 1-D Arrays Declaration: type[] name; type name[]; • The type can be any valid type: primitive and reference. • You can declare multiple names in the same statement, but we advice against it. Assignment: type[] name; name = new type[size]; type[] name = new type[size]; • Think of new type[size] as a constructor. • size must be an integer (0 or greater) or expression that evaluates to an integer. • All values inside the array are “zero.” Think of each element as a “field” of the object. Refer to the rules for default values of fields in classes. Example: int[] x = new int[10]; // default values? 0 Person[] p = new Person[2]; // default values? null! Java Bootcamp Step 24 Page 61/7624.3 Indexing/Accessing/Storing Expression syntax: name[index] name[index].member Statement syntax: name[index] = expression; name[index].method(…); More rules: • Labeling of indices starts at zero! • index must be an integer or integer expression that corresponds to the position of an element in the array. • If you attempt to access an index that does not exist, Java complains with an with out-of-bounds exception. • To access an array’s length, use the “secret field” length, e.g., x.length. Note that Java strings use the length() method. • The []s have higher precedence than dot (.). - So, if I want to access a Person object’s name, for example, I can say p[2].getName();. - Of course, p[2] must first store a Person object! 24.4 Array of Objects People often forget to fill an array with objects! Suppose that you want to print an array of Complex numbers: final int SIZE = 4; Complex[] c = new Complex[SIZE]; for (int i = 0 ; i < c.length; i++) System.out.println(c[i]); When you run the above code, all you get is four nulls. Why? The default value of all objects is zero for fields, as discussed in the previous section. So, you need first to fill the array! final int SIZE = 2; Complex[] c = new Complex[SIZE]; c[0] = new Complex(0,1); c[1] = new Complex(1,2); for (int i = 0 ; i < c.length; i++) System.out.println(c[i]); 24.5 Problem 1 Write a program that creates an array of 10 random Complex numbers. Java Bootcamp Step 24 Page 62/7624.6 Problem 2 Write a method rotate that returns a character array that contains the rotation of the lowercase English alphabet for an input of shift. A rotation is a simple form of encryption in which a character is shifted a given number of characters to the left. Assume that shift is a legal integer between 0 and 26. For instance, rotate(3) produces a character array in the order xyzabcdefghijklmnopqrstuvw. Test your program with a few strings. If you need to refresher on characters, refer to the earlier sections on the Java language. Note that there is nothing special about a method that returns an array other than the fact that it really returns a reference to an array. Your method header for rotate might be as follows: public static char[] rotate(int shift) The last statement will return an expression of type char[]. 24.7 Initializer Lists If you have a small array and known values, you can use an initializer list to declare, assign, and store in one statement. type[] name = { e1 , … , en } ; There are some interesting rules that seem to depart from standard Java convention: • The entire statement must be in on one line. • You need to place a semicolon after the last brace. Examples: int[] x = { 1 , 2 , 3 } ; Person[] p = { new Person("A"), new Person("B") } ; 24.8 Anonymous Arrays Sometimes someone decides to return to return an initializer list, as in return {1,2,3} ; Unfortunately, Java does not permit this syntax. Instead you have to use the more general form of the initializer list, which is called an anonymous array: new type[] { e1 , … , en } CS211 will cover anonymous classes later on in the semester, so don’t worry too much about this particular topic for now. Java Bootcamp Step 24 Page 65/7624.11 Simulating Dynamic Arrays Java arrays must have a size, which cannot be changed once the array is created. But there are a couple of useful tricks. Objects: class IntArray { private int[] x; public IntArray(int n) { x = new int[n]; } } • You can call new IntArray(size) with an any valid size. • Of course, once you create that object, its internal array x is now fixed. Methods: • Actually, we’ve already been using the trick of copying reference values. • When you call a method, like something(int[] x), the parameter x gets the address of the array that you supply. There is a formal “dynamic array” in Java, which is called a Vector. See next section. 24.12 Iterating through an array with for-each Java 5 introduced the for-each control structure, which I show briefly in Step 12.8. When using arrays, the index for iteration can be a nuisance. So, to condense the following common pattern: Type[] a = Expression; for (int i = 0; i < a.length; i++) { Type e = a[i]; statements using e } you could use the following pattern: Type[] a = Expression; for (Type e : Expression) { statements using e } The pattern above means “for each e in expression, do something.” (continued on next page) Java Bootcamp Step 24 Page 66/76The following example demonstrates the for-each by printing a collection of strings: String[] s = {"A", "B", "C"}; for (String e : s) System.out.print(e); System.out.println(); The above example will output ABC. You can also iterate over general collections of data, which we will explore when reaching Vectors in Step 30. For more information, refer to Java’s general explanation of syntax and semantics that I used to write this section at this website: http://jcp.org/aboutJava/communityprocess/jsr/tiger/enhanced-for.html. 24.13 Problem 4 Write a program that does the following two tasks: • Adds and outputs the sum of all elements in an array of integers. Use this array in your main method:int[] a = {1 , 2 , 3 }. • Searches the same array a for the number 2 in a method called find2. Upon finding 2, method find2 returns true without inspecting any further elements. Java Bootcamp Step 25 Page 67/76Step 25: Class Object 25.1 Background I have tried to avoid too much conversation about inheritance, because it is a subject we go over again in CS211. But, class Object is important–all classes in Java extend Object. So, Object is a supertype of all reference types. What makes this notion extremely useful for data structures is that a supertype reference can store a subtype reference with no hassle. You can then write general data structures that have contents of type Object, which allows you to store every kind of object. Pretty handy! 25.2 Problem How many methods do all objects in Java automatically inherit? 25.3 Example: equals revisited You can define equality for any class using Object’s equals method. In the example, below, I define equality for class A as equivalence of A’s instance variable k: public class Equals { public static void main(String[] args) { A a1 = new A(1); A a2 = new A(1); System.out.println(a1.equals(a2)); } } class A { public int k; public A(int k) { this.k = k; } public boolean equals(Object other) { return k == ((A) other).k; } } 25.4 Problems What is the output of Equals? Would you get the same output if you print out a1==a2? Why or why not? If you define a class B that extends A, class B extends A { public B(int k) { super(k); } } will an object of B equal an object A and if they have the same value of k? Demonstrate your answer with new A(1).equals(new B(1)) and new B(2).equals(new A(2)). Java Bootcamp Step 27 Page 70/76enum Color {BLUE, RED} enum Coin { penny(1), dime(10); private final int value; Coin (int value) { this.value=value;} public int value() {return value;} public String toString() { return this==penny? "p" : "d"; } } public class Enums { public static void main(String[] args) { Color marble = Color.BLUE; Coin c = Coin.penny; System.out.println(Coin.dime); System.out.println(c.value()+Coin.dime.value()); } } 27.5 Problem 2 Review the above link concerning enums. Write a program that uses enums to model playing cards. Your program randomly shuffle a random deck of cards and then print out each card in order, from top to bottom. Java Bootcamp Step 28 Page 71/76Step 28: Wrapper Classes 28.1 Java Types For better or for worse, Java has two kinds of types: • primitive: int, double, boolean, char, … • reference: all classes, interfaces, enums, null To make generic data structures, you will generally use class Object, which means that you might need a way to convert primitives to objects. Java provides wrapper classes for all primitive types, which you can find in the java.lang package. You never need to import any classes from this package, since Java automatically uses them. For example, to create an Integer object from the primitive value 4, you would do this: Integer i = new Integer(4); You can now store i in an Object variable: Object o = i; To get a handle on how Integer works, it has this approximate structure: class Integer { private int value; public Integer(int value) { this.value = value; } // more methods--see java.lang.Integer in API } 28.2 Do we still need wrapper classes? Java 5 has autoboxing of primitive types, which automatically converts them to their equivalent wrapper types. See Step 29 for more detail. However, you may still need to wrap object types within others, depending on the application. For example, consider the “dynamic array” of Step 24.11–we wrap an array, which is a class, with another kind of class. 28.3 Problem 1 Without relying on autoboxing, create an array of integers int[] a = { 1, 2, 3, 4 }; that can be passed into a method int addInts(Object[] x) that returns the integer sum of all elements in x. Java Bootcamp Step 29 Page 72/76Step 29: Autoboxing 29.1 Common Irritation In older versions of Java, you could not mix reference and primitive types. For example, given a data structure of Object types, if a user tried to enter an integer, you had to convert the value to its wrapper class Integer. For example, you might see this sort of code in older Java: Object[] o = { new Integer(1), new Integer(2), new Integer(3) }; 29.2 Autobox/Unbox To ease your frustration, Java 5 will automatically “upgrade” (autoboxing) or “downgrade” (unboxing) a primitive type to its wrapper equivalent. For example, the following code demonstrates autoboxing of the integer 1: Object o = 1 + new Integer(2); When unboxing, an reference type can be converted to its primitive type, as follows: int x = 1 + new Integer(2); 29.3 Some issues Do not rely on autoboxing/unboxing if you have several numerical computations, as these features involve a performance hit. For more information, review http://java.sun.com/j2se/ 1.5.0/docs/guide/language/autoboxing.html. 29.4 Problem 1 If you output x and o in the above example, you get the same answer. Why? 29.5 Problem 2 An Integer object can be null. So, can you unbox null? What happens if you do? Java Bootcamp Step 31 Page 75/76Step 31: User I/O 31.1 Class Scanner If you have learned Java with version 1.4.2 or earlier, you were likely using a non- standard class to assist with user and/or file input. Java 5 introduced a great new class called Scanner that simplifies the process of obtaining input. 31.2 Example import java.util.*; public class UserIO { public static void main(String[] args) { // "plain" user I/O (no type checking): Scanner in = new Scanner(System.in); System.out.print("Please enter a legal integer: "); in.nextInt(); // fancier user I/O: boolean stop = false; System.out.print("Please enter an integer: "); while(!stop) { Scanner s = new Scanner(System.in); try { s.nextInt(); stop = true; } catch (Exception e) { System.out.print("Not legal! Re-enter: "); } } } } 31.3 Problem 1 Run the above program. What happens if you enter non-integer input at the first prompt? 31.4 Problem 2 Although you might not have seen a try-catch structure beforehand, explain what the second input structure does when you run the program. Java Bootcamp Step 32 Page 76/76Step 32: Other things for the future…? 32.1 Output and Formatting 32.2 File I/0 32.3 Move Applications.html and Java links to this tutorial 32.4 Javadoc 32.5 Define and use packages 32.6 Exceptions 32.7 Generics
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved