//******************************************************************* // Welcome to CompileJava! // If you experience any issues, please contact us ('More Info') --> // Also, sorry that the "Paste" feature no longer works! GitHub broke // this (so we'll switch to a new provider): https://blog.github.com\ // /2018-02-18-deprecation-notice-removing-anonymous-gist-creation/ //******************************************************************* import java.lang.Math; // headers MUST be above the first class import java.util.*; // one class needs to have a main() method public class HelloWorld { // arguments are passed using the text field below this editor public static void main(String[] args) { OtherClass myObject = new OtherClass("Hello World!"); Vector v = new Vector(); v.add(1); v.add(2); Vector w = v; System.out.println( v.equals(w) ); Vector t = ( ) v.clone(); Vector z = new Vector(); z.add(1); z.add(2); System.out.println( v.equals(z) ); A t = new A(1,"ciao",v); v.remove(0); System.out.println( t.v ); int [][] mat = new int[10][10]; mat[0][0]=43; } } public class A{ public int x; public String y; public Vector v = new Vector(); //public Vector w = v; A( int x, String y ,Vector v ){ this.x = x; this.y = y; this. v = v; } } // you can add other public classes to this editor in any order public class OtherClass { private String message; private boolean answer = false; public OtherClass(String input) { message = "Why, " + input + " Isn't this something?"; } public String toString() { return message; } } //******************************************************************* // Welcome to CompileJava! // If you experience any issues, please contact us ('More Info') --> // Also, sorry that the "Paste" feature no longer works! GitHub broke // this (so we'll switch to a new provider): https://blog.github.com\ // /2018-02-18-deprecation-notice-removing-anonymous-gist-creation/ //******************************************************************* import java.lang.Math; // headers MUST be above the first class // one class needs to have a main() method public class HelloWorld { // arguments are passed using the text field below this editor public static void main(String[] args) { OtherClass myObject = new OtherClass("Hello World!"); Animal a1 = new Animal(10,10); // se metti un solo 10 non compila perche' il costruttore richiede due parametri Animal a2 = new Gatto(); Gatto g1 = new Gatto(); a1.mkSound(); a2.mkSound(); g1.mkSound(); Integer t = 5; //System.out.println( a1.getPeso() ); System.out.println( a1.n_gambe ); System.out.println( a2.n_gambe ); System.out.println( g1.n_gambe ); System.out.println( g1.peso ); System.out.println( t.equals(3) );; Dog aDog = new Dog("Max",19); Dog bDog = aDog; bDog.setName("Andrea"); System.out.println( "--" + aDog.getName().equals("Andrea") ); System.out.println( "--" + bDog.getName().equals("Andrea") ); // we pass the object to foo foo(aDog); // aDog variable is still pointing to the "Max" dog when foo(...) returns System.out.println("-> " + aDog.getName().equals("Max")); // true System.out.println("-> " + aDog.getName().equals("Fifi")); // false } public static void foo(Dog d) { System.out.println(d.getName().equals("Max")); // true d.setName("Giorgio"); System.out.println( "alt : " + d.getAlt().equals(18) ); // change d inside of foo() to point to a new Dog instance "Fifi" d = new Dog("Fifi",20); System.out.println( d.getName().equals("Fifi")); // true } } public class Dog{ private String name; private Integer alt; Dog( String name, int alt ){ this.name = name; this.alt = alt; } public String getName(){ return this.name; } public Integer getAlt(){ return this.alt;} // se metti int hai problemi a fare d.getAlt().equals(18) perche' tu ritorni un int che non e' un oggetto, e te sai chiamare i metodi solo sugli oggetti public void setName(String name){ this.name = name; } } public class Animal{ public int peso; public int n_gambe; Animal(){ peso = 32; n_gambe = 6; } Animal( int peso, int n_gambe ){ this.peso = peso; this.n_gambe = n_gambe; } public void mkSound(){ System.out.println( "Boh" ); } public int getPeso(){ return peso; } } public class Gatto extends Animal{ public int n_gambe; Gatto(){ n_gambe = 4; } public void mkSound(){ System.out.println( " Miao " ); } } public class OtherClass { private String message; private boolean answer = false; public OtherClass(String input) { message = "Why, " + input + " Isn't this something?"; } public String toString() { return message; } } //******************************************************************* // Welcome to CompileJava! // If you experience any issues, please contact us ('More Info') --> // Also, sorry that the "Paste" feature no longer works! GitHub broke // this (so we'll switch to a new provider): https://blog.github.com\ // /2018-02-18-deprecation-notice-removing-anonymous-gist-creation/ //******************************************************************* import java.lang.Math; // headers MUST be above the first class import java.util.*; // RICORDARTELO // one class needs to have a main() method public class HelloWorld { // arguments are passed using the text field below this editor public static void main(String[] args) throws CloneNotSupportedException { OtherClass myObject = new OtherClass("Hello World!"); System.out.println(myObject); Persona a = new Persona(1,2,"ciao"); Persona b = a.clone(); Object o = new Object(); Object oo; oo = o.clone(); b.h=201; System.out.println( b.h + " " + a.h); System.out.println( b.peso ); System.out.println( b.nome ); System.out.println(b.v); b.v.remove(0); System.out.println(b.v); System.out.println( a.v ); System.out.println( a.equals(b) ); Vector p = new Vector(); p.add(a); p.add(b); for( Persona e : p ){ e.scrivi(); } @SuppressWarnings (value="unchecked") Vector q = (Vector) p.clone(); System.out.println("\n\n"); for( Persona e : q ){ e.scrivi(); } } } class Persona implements Cloneable{ int h; int peso; String nome; Vector v = new Vector(); Persona( int h, int peso, String nome ){ this.h = h; this.peso = peso; this.nome = nome; this.v.add(2); this.v.add(1); } public Persona clone() throws CloneNotSupportedException{ return (Persona) super.clone(); } public void scrivi(){ System.out.print( h + " "); System.out.print( peso + " " ); System.out.print( nome + " " ); System.out.println( v ); } } // you can add other public classes to this editor in any order public class OtherClass { private String message; private boolean answer = false; public OtherClass(String input) { message = "Why, " + input + " Isn't this something?"; } public String toString() { return message; } }