viernes, 8 de agosto de 2014

Arguments

// The following is sample terminal output that
// displays the contents of the .java file
// we have written (uses the cat command),
// compiles and runs it with arguments.

$ cat Arguments.java
public class Arguments {
    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
    }
}
$ javac Arguments.java
$ java Arguments arg0 arg1 arg2
arg0
arg1
arg2
$

Objects


class Point {
    private double x;
    private double y;
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    public void print() {
        System.out.println("(" + x + "," + y + ")");
    }
    public void scale(){
        x=x/2;
        y=y/2;
    }
}

public class Main {
    public static void main(String[] args) {
        Point p = new Point(32, 32);
        for (int i = 0; i < 5; i++) {
            p.scale();
            p.print();
        }
    }
}

Functions

class Student {
    private String firstName;
    private String lastName;
    public Student(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    public void printFullName(){
            System.out.println(firstName + " " + lastName);
    }
       
     
}

public class Main {
    public static void main(String[] args) {
        Student[] students = new Student[] {
            new Student("Morgan", "Freeman"),
            new Student("Brad", "Pitt"),
            new Student("Kevin", "Spacey"),
        };
        for (Student s : students) {
            s.printFullName();
        }
    }
}

Loops


public class Main {
    public static void main(String[] args) {
        int[] numbers = {
            951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544, 
        615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941, 
        386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345, 
        399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217, 
        815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717, 
        958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470, 
        743, 527};
        
        int a;
        float flt;

        for (int el : numbers){
            a = el/2;
            flt = (float) el/2;
            if (a==flt){
                System.out.println(el);
            }
            if (el==237) {
                break;
            }
        }
    }
}